Cleanup code

This commit is contained in:
zuckerberg 2022-12-04 00:04:27 -07:00
parent ded272b88a
commit 224b88d839

View File

@ -4,20 +4,26 @@ let
# read input # read input
input = readFile ./input; input = readFile ./input;
pairs = let pairs = let
readRange = s: let readRange = s:
l = splitString "-" s; let l = splitString "-" s;
in [ (toInt (head l)) (toInt (last l)) ]; in [ (toInt (head l)) (toInt (last l)) ];
readPair = a: b: [ (readRange a) (readRange b) ]; readPair = a: b: [ (readRange a) (readRange b) ];
in map (line: split readPair (splitString "," line)) (splitString "\n" input); in map
(line: split readPair (splitString "," line))
(splitString "\n" input);
partiallyContainsOther = a1: a2: b1: b2: # overlap functions
partialOverlap = a1: a2: b1: b2:
a1 <= b1 && b1 <= a2 || a1 <= b2 && b2 <= a2; a1 <= b1 && b1 <= a2 || a1 <= b2 && b2 <= a2;
containsOther = a1: a2: b1: b2: a1 <= b1 && a2 >= b2; completeOverlap = a1: a2: b1: b2:
hasOverlap = overlapFunc: a: b: let a1 <= b1 && a2 >= b2;
a1 = head a; b1 = head b; a2 = last a; b2 = last b;
in overlapFunc a1 a2 b1 b2 || overlapFunc b1 b2 a1 a2; # calc overlaps
hasOverlap = overlapFunc: a: b:
let a1 = head a; b1 = head b; a2 = last a; b2 = last b;
in overlapFunc a1 a2 b1 b2 || overlapFunc b1 b2 a1 a2;
overlaps = overlapFunc: map boolToInt (map (split (hasOverlap overlapFunc)) pairs); overlaps = overlapFunc: map boolToInt (map (split (hasOverlap overlapFunc)) pairs);
in { in {
part1 = sum (overlaps containsOther); part1 = sum (overlaps completeOverlap);
part2 = sum (overlaps partiallyContainsOther); part2 = sum (overlaps partialOverlap);
} }