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