From 775ac4bbe75e24896ecf0c85b04ff8a15a8e2803 Mon Sep 17 00:00:00 2001 From: zuckerberg <5-zuckerberg@users.noreply.git.neet.dev> Date: Mon, 5 Dec 2022 13:19:00 -0700 Subject: [PATCH] Cleanup code --- 5/solution.nix | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/5/solution.nix b/5/solution.nix index 257fd16..16acabf 100644 --- a/5/solution.nix +++ b/5/solution.nix @@ -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)); } \ No newline at end of file