DEV Community

loading...

Discussion on: Daily Challenge #31 - Count IPv4 Addresses

Collapse
jasman7799 profile image
Jarod Smith
// sectionSum :: (Number -> Number) -> Number
const sectionSum = value => section => value * 256 ** section;
// ipSum :: [Number] -> Number
const ipSum = ipArray => ipArray.reduceRight((sum, value, section) => sum += sectionSum(value)(3-section));
// ipDifference :: Number -> Number -> Number
const ipDifference = ipSum1 => ipSum2 => Math.abs(ipSum1 - ipSum2);
// ipSections :: String -> [Number]
const ipSections = address => address.split('.').map(section => parseInt(section));
// ipVal :: String -> Number 
const ipVal = ip => ipSum(ipSections(ip))

// ipsBetween :: (String -> String) -> Number 
const ipsBetweenCount = startIp => endIp => ipDifference(ipVal(startIp))(ipVal(endIp))