Cleanup code

This commit is contained in:
zuckerberg 2022-12-05 13:19:00 -07:00
parent 6b3d803456
commit 775ac4bbe7

View File

@ -19,28 +19,32 @@ let
(line: map toInt (subtractLists ["move" "from" "to"] (splitString " " line))) (line: map toInt (subtractLists ["move" "from" "to"] (splitString " " line)))
(splitString "\n" (last input)); (splitString "\n" (last input));
# funcs for how many boxes can be moved # funcs for how many boxes to be moved
moveTopOnly = cnt: 1; # crane can only move one bpx at a time moveTopOnly = cnt: 1; # crane can only move one box at a time
moveAll = cnt: cnt; # crane can move any number of boxes at a time moveAll = cnt: cnt; # crane can move any number of boxes at a time
moveThree = min 3; # crane can only move 3 boxes at a time
# performs a single move # performs a single move
performMove = moveCntFunc: stacks: mv: performMove = moveCntFunc: stacks: mv:
if head mv == 0 then stacks # base case; no more boxes to move if head mv == 0 then stacks # base case; no more boxes to move
else let else
cnt = elemAt mv 0; let
cnt' = moveCntFunc cnt; cnt = elemAt mv 0; # boxes that need to be moved
src = elemAt mv 1; cnt' = moveCntFunc cnt; # boxes that can be moved
dest = elemAt mv 2; src = elemAt mv 1;
boxes = take cnt' (elemAt stacks (src -1)); dest = elemAt mv 2;
stacks' = imap1 (i: s: boxes = take cnt' (elemAt stacks (src -1)); # box(es) being moved
if i == src then drop cnt' s stacks' = imap1 (i: s:
else if i == dest then boxes ++ s if i == src then drop cnt' s
else s) stacks; else if i == dest then boxes ++ s
in performMove moveCntFunc stacks' [(cnt - cnt') src dest]; else s) stacks;
in
performMove moveCntFunc stacks' [(cnt - cnt') src dest];
# performs all moves in sequence # performs all moves in sequence
performMoves = moveCntFunc: foldl (performMove moveCntFunc) stacks moves; performMoves = moveCntFunc: foldl (performMove moveCntFunc) stacks moves;
in { in {
part1 = concatStrings (map head (performMoves moveTopOnly)); part1 = concatStrings (map head (performMoves moveTopOnly));
part2 = concatStrings (map head (performMoves moveAll)); part2 = concatStrings (map head (performMoves moveAll));
# moveThree = concatStrings (map head (performMoves moveThree));
} }