mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Day 5 part 1
This commit is contained in:
parent
49f1283cf6
commit
086ee442fa
2 changed files with 647 additions and 0 deletions
513
2022/input/day_5.txt
Normal file
513
2022/input/day_5.txt
Normal file
|
@ -0,0 +1,513 @@
|
|||
[H] [Z] [J]
|
||||
[L] [W] [B] [G] [R]
|
||||
[R] [G] [S] [J] [H] [Q]
|
||||
[F] [N] [T] [J] [P] [R] [F]
|
||||
[B] [C] [M] [R] [Q] [F] [G] [P]
|
||||
[C] [D] [F] [D] [D] [D] [T] [M] [G]
|
||||
[J] [C] [J] [J] [C] [L] [Z] [V] [B]
|
||||
[M] [Z] [H] [P] [N] [W] [P] [L] [C]
|
||||
1 2 3 4 5 6 7 8 9
|
||||
|
||||
move 3 from 2 to 1
|
||||
move 8 from 6 to 4
|
||||
move 4 from 8 to 2
|
||||
move 3 from 1 to 9
|
||||
move 1 from 2 to 4
|
||||
move 3 from 7 to 5
|
||||
move 3 from 9 to 2
|
||||
move 3 from 3 to 5
|
||||
move 1 from 5 to 1
|
||||
move 5 from 1 to 8
|
||||
move 2 from 1 to 8
|
||||
move 3 from 7 to 3
|
||||
move 1 from 8 to 9
|
||||
move 6 from 9 to 8
|
||||
move 3 from 8 to 7
|
||||
move 7 from 8 to 9
|
||||
move 2 from 5 to 9
|
||||
move 2 from 2 to 9
|
||||
move 3 from 3 to 7
|
||||
move 2 from 8 to 3
|
||||
move 7 from 4 to 8
|
||||
move 3 from 4 to 1
|
||||
move 4 from 8 to 6
|
||||
move 4 from 6 to 1
|
||||
move 8 from 1 to 2
|
||||
move 1 from 1 to 4
|
||||
move 3 from 5 to 1
|
||||
move 8 from 9 to 8
|
||||
move 4 from 3 to 1
|
||||
move 5 from 5 to 3
|
||||
move 2 from 7 to 1
|
||||
move 1 from 7 to 4
|
||||
move 1 from 7 to 2
|
||||
move 3 from 3 to 5
|
||||
move 3 from 9 to 1
|
||||
move 9 from 8 to 1
|
||||
move 2 from 9 to 7
|
||||
move 1 from 8 to 5
|
||||
move 4 from 5 to 3
|
||||
move 1 from 3 to 4
|
||||
move 1 from 9 to 6
|
||||
move 1 from 6 to 9
|
||||
move 7 from 4 to 9
|
||||
move 1 from 7 to 3
|
||||
move 1 from 8 to 2
|
||||
move 8 from 2 to 1
|
||||
move 4 from 3 to 5
|
||||
move 2 from 9 to 6
|
||||
move 2 from 6 to 2
|
||||
move 2 from 4 to 9
|
||||
move 8 from 9 to 2
|
||||
move 3 from 7 to 9
|
||||
move 1 from 3 to 5
|
||||
move 2 from 3 to 8
|
||||
move 9 from 2 to 1
|
||||
move 1 from 8 to 7
|
||||
move 4 from 2 to 9
|
||||
move 4 from 5 to 6
|
||||
move 1 from 8 to 9
|
||||
move 27 from 1 to 2
|
||||
move 1 from 6 to 4
|
||||
move 3 from 6 to 4
|
||||
move 7 from 9 to 8
|
||||
move 4 from 4 to 1
|
||||
move 9 from 2 to 6
|
||||
move 2 from 1 to 9
|
||||
move 6 from 1 to 3
|
||||
move 1 from 5 to 3
|
||||
move 3 from 3 to 5
|
||||
move 3 from 5 to 3
|
||||
move 3 from 3 to 1
|
||||
move 4 from 6 to 7
|
||||
move 3 from 9 to 2
|
||||
move 1 from 6 to 4
|
||||
move 4 from 3 to 5
|
||||
move 3 from 6 to 5
|
||||
move 1 from 6 to 2
|
||||
move 15 from 2 to 3
|
||||
move 5 from 5 to 9
|
||||
move 13 from 3 to 9
|
||||
move 2 from 5 to 7
|
||||
move 1 from 4 to 2
|
||||
move 3 from 3 to 7
|
||||
move 11 from 2 to 7
|
||||
move 7 from 9 to 5
|
||||
move 3 from 5 to 7
|
||||
move 6 from 8 to 9
|
||||
move 4 from 1 to 2
|
||||
move 6 from 1 to 6
|
||||
move 3 from 5 to 1
|
||||
move 1 from 8 to 2
|
||||
move 4 from 2 to 9
|
||||
move 1 from 5 to 7
|
||||
move 6 from 7 to 6
|
||||
move 18 from 7 to 5
|
||||
move 1 from 7 to 1
|
||||
move 8 from 9 to 5
|
||||
move 1 from 2 to 6
|
||||
move 15 from 5 to 6
|
||||
move 6 from 5 to 3
|
||||
move 4 from 3 to 6
|
||||
move 26 from 6 to 5
|
||||
move 2 from 1 to 7
|
||||
move 4 from 5 to 9
|
||||
move 8 from 5 to 7
|
||||
move 3 from 7 to 9
|
||||
move 14 from 9 to 8
|
||||
move 7 from 5 to 2
|
||||
move 4 from 2 to 1
|
||||
move 5 from 1 to 9
|
||||
move 12 from 5 to 3
|
||||
move 5 from 8 to 5
|
||||
move 14 from 3 to 2
|
||||
move 1 from 5 to 2
|
||||
move 10 from 2 to 6
|
||||
move 7 from 9 to 6
|
||||
move 6 from 8 to 6
|
||||
move 1 from 2 to 7
|
||||
move 2 from 9 to 7
|
||||
move 2 from 8 to 6
|
||||
move 6 from 2 to 7
|
||||
move 1 from 1 to 8
|
||||
move 15 from 6 to 2
|
||||
move 1 from 6 to 9
|
||||
move 1 from 5 to 9
|
||||
move 1 from 9 to 6
|
||||
move 2 from 2 to 4
|
||||
move 3 from 9 to 5
|
||||
move 5 from 5 to 3
|
||||
move 3 from 3 to 6
|
||||
move 6 from 2 to 7
|
||||
move 1 from 5 to 9
|
||||
move 8 from 6 to 9
|
||||
move 2 from 6 to 7
|
||||
move 3 from 2 to 4
|
||||
move 9 from 6 to 7
|
||||
move 17 from 7 to 5
|
||||
move 1 from 8 to 4
|
||||
move 7 from 9 to 3
|
||||
move 12 from 5 to 8
|
||||
move 3 from 5 to 2
|
||||
move 4 from 7 to 8
|
||||
move 2 from 5 to 7
|
||||
move 1 from 7 to 9
|
||||
move 8 from 3 to 7
|
||||
move 17 from 7 to 5
|
||||
move 3 from 2 to 5
|
||||
move 1 from 3 to 6
|
||||
move 10 from 5 to 4
|
||||
move 5 from 2 to 7
|
||||
move 1 from 4 to 2
|
||||
move 3 from 9 to 8
|
||||
move 7 from 7 to 2
|
||||
move 5 from 5 to 1
|
||||
move 14 from 4 to 9
|
||||
move 3 from 9 to 8
|
||||
move 1 from 6 to 9
|
||||
move 2 from 1 to 4
|
||||
move 2 from 8 to 5
|
||||
move 16 from 8 to 6
|
||||
move 1 from 6 to 2
|
||||
move 11 from 9 to 2
|
||||
move 2 from 7 to 5
|
||||
move 1 from 1 to 6
|
||||
move 11 from 2 to 9
|
||||
move 4 from 2 to 8
|
||||
move 9 from 5 to 3
|
||||
move 1 from 4 to 2
|
||||
move 2 from 1 to 8
|
||||
move 1 from 2 to 9
|
||||
move 2 from 4 to 3
|
||||
move 8 from 6 to 9
|
||||
move 16 from 9 to 3
|
||||
move 16 from 3 to 2
|
||||
move 17 from 2 to 6
|
||||
move 1 from 9 to 3
|
||||
move 1 from 2 to 5
|
||||
move 1 from 9 to 4
|
||||
move 3 from 2 to 8
|
||||
move 1 from 9 to 1
|
||||
move 1 from 9 to 6
|
||||
move 7 from 3 to 1
|
||||
move 5 from 3 to 5
|
||||
move 3 from 8 to 3
|
||||
move 2 from 3 to 4
|
||||
move 6 from 8 to 4
|
||||
move 7 from 6 to 4
|
||||
move 3 from 6 to 7
|
||||
move 3 from 8 to 9
|
||||
move 3 from 5 to 2
|
||||
move 3 from 1 to 3
|
||||
move 1 from 4 to 8
|
||||
move 3 from 5 to 1
|
||||
move 13 from 4 to 7
|
||||
move 14 from 6 to 7
|
||||
move 6 from 1 to 9
|
||||
move 3 from 9 to 6
|
||||
move 1 from 8 to 7
|
||||
move 1 from 8 to 7
|
||||
move 20 from 7 to 3
|
||||
move 1 from 8 to 9
|
||||
move 1 from 1 to 9
|
||||
move 1 from 1 to 5
|
||||
move 1 from 4 to 6
|
||||
move 14 from 3 to 9
|
||||
move 1 from 2 to 6
|
||||
move 3 from 7 to 6
|
||||
move 6 from 3 to 2
|
||||
move 1 from 3 to 8
|
||||
move 2 from 7 to 3
|
||||
move 7 from 6 to 3
|
||||
move 12 from 3 to 1
|
||||
move 1 from 8 to 2
|
||||
move 1 from 4 to 9
|
||||
move 1 from 5 to 6
|
||||
move 1 from 6 to 4
|
||||
move 1 from 4 to 2
|
||||
move 2 from 2 to 3
|
||||
move 16 from 9 to 7
|
||||
move 3 from 6 to 7
|
||||
move 6 from 9 to 4
|
||||
move 4 from 4 to 7
|
||||
move 6 from 1 to 8
|
||||
move 2 from 3 to 6
|
||||
move 3 from 1 to 9
|
||||
move 3 from 2 to 3
|
||||
move 3 from 3 to 8
|
||||
move 5 from 2 to 8
|
||||
move 2 from 7 to 8
|
||||
move 3 from 1 to 5
|
||||
move 1 from 4 to 3
|
||||
move 2 from 9 to 8
|
||||
move 1 from 6 to 8
|
||||
move 2 from 9 to 1
|
||||
move 15 from 7 to 1
|
||||
move 1 from 6 to 5
|
||||
move 10 from 1 to 5
|
||||
move 1 from 4 to 1
|
||||
move 2 from 1 to 6
|
||||
move 9 from 7 to 8
|
||||
move 27 from 8 to 3
|
||||
move 1 from 6 to 1
|
||||
move 1 from 8 to 5
|
||||
move 5 from 5 to 6
|
||||
move 12 from 3 to 1
|
||||
move 3 from 7 to 1
|
||||
move 7 from 5 to 1
|
||||
move 1 from 6 to 4
|
||||
move 3 from 6 to 9
|
||||
move 1 from 4 to 2
|
||||
move 2 from 6 to 5
|
||||
move 1 from 7 to 6
|
||||
move 1 from 9 to 2
|
||||
move 2 from 5 to 6
|
||||
move 2 from 6 to 5
|
||||
move 3 from 1 to 3
|
||||
move 19 from 3 to 1
|
||||
move 2 from 2 to 9
|
||||
move 42 from 1 to 7
|
||||
move 4 from 9 to 7
|
||||
move 1 from 6 to 8
|
||||
move 1 from 8 to 5
|
||||
move 2 from 1 to 9
|
||||
move 3 from 5 to 7
|
||||
move 27 from 7 to 4
|
||||
move 1 from 1 to 4
|
||||
move 3 from 9 to 2
|
||||
move 18 from 4 to 9
|
||||
move 2 from 5 to 3
|
||||
move 1 from 7 to 1
|
||||
move 2 from 3 to 4
|
||||
move 8 from 7 to 5
|
||||
move 15 from 9 to 3
|
||||
move 1 from 9 to 7
|
||||
move 3 from 7 to 2
|
||||
move 2 from 7 to 2
|
||||
move 2 from 5 to 3
|
||||
move 1 from 1 to 5
|
||||
move 1 from 9 to 1
|
||||
move 1 from 3 to 1
|
||||
move 1 from 4 to 3
|
||||
move 8 from 7 to 3
|
||||
move 8 from 2 to 4
|
||||
move 1 from 9 to 6
|
||||
move 23 from 3 to 9
|
||||
move 1 from 9 to 6
|
||||
move 2 from 6 to 8
|
||||
move 1 from 8 to 6
|
||||
move 1 from 5 to 3
|
||||
move 7 from 4 to 8
|
||||
move 7 from 5 to 7
|
||||
move 2 from 8 to 3
|
||||
move 1 from 1 to 8
|
||||
move 3 from 7 to 4
|
||||
move 5 from 4 to 3
|
||||
move 1 from 1 to 8
|
||||
move 3 from 3 to 1
|
||||
move 8 from 9 to 7
|
||||
move 3 from 8 to 4
|
||||
move 1 from 6 to 2
|
||||
move 5 from 8 to 7
|
||||
move 6 from 3 to 1
|
||||
move 1 from 2 to 9
|
||||
move 7 from 7 to 9
|
||||
move 4 from 1 to 9
|
||||
move 2 from 4 to 2
|
||||
move 1 from 4 to 9
|
||||
move 1 from 1 to 6
|
||||
move 8 from 4 to 8
|
||||
move 4 from 1 to 5
|
||||
move 3 from 5 to 2
|
||||
move 2 from 2 to 5
|
||||
move 2 from 5 to 6
|
||||
move 1 from 3 to 7
|
||||
move 2 from 6 to 4
|
||||
move 1 from 5 to 7
|
||||
move 1 from 6 to 9
|
||||
move 1 from 4 to 1
|
||||
move 6 from 9 to 2
|
||||
move 8 from 9 to 7
|
||||
move 4 from 7 to 3
|
||||
move 4 from 8 to 3
|
||||
move 3 from 8 to 3
|
||||
move 8 from 3 to 5
|
||||
move 1 from 1 to 7
|
||||
move 11 from 9 to 7
|
||||
move 5 from 2 to 7
|
||||
move 1 from 8 to 1
|
||||
move 3 from 2 to 3
|
||||
move 1 from 1 to 4
|
||||
move 1 from 2 to 5
|
||||
move 20 from 7 to 8
|
||||
move 7 from 7 to 9
|
||||
move 4 from 4 to 7
|
||||
move 3 from 9 to 4
|
||||
move 5 from 7 to 4
|
||||
move 7 from 4 to 7
|
||||
move 4 from 9 to 2
|
||||
move 1 from 4 to 3
|
||||
move 4 from 3 to 5
|
||||
move 2 from 5 to 8
|
||||
move 4 from 5 to 2
|
||||
move 5 from 2 to 6
|
||||
move 2 from 6 to 3
|
||||
move 22 from 8 to 5
|
||||
move 13 from 7 to 9
|
||||
move 11 from 9 to 3
|
||||
move 2 from 6 to 8
|
||||
move 7 from 3 to 1
|
||||
move 18 from 5 to 2
|
||||
move 1 from 6 to 4
|
||||
move 1 from 4 to 9
|
||||
move 2 from 8 to 5
|
||||
move 2 from 9 to 1
|
||||
move 9 from 3 to 1
|
||||
move 4 from 5 to 6
|
||||
move 2 from 6 to 7
|
||||
move 3 from 9 to 5
|
||||
move 10 from 5 to 8
|
||||
move 6 from 8 to 7
|
||||
move 3 from 8 to 1
|
||||
move 6 from 2 to 3
|
||||
move 1 from 9 to 6
|
||||
move 5 from 3 to 4
|
||||
move 4 from 1 to 4
|
||||
move 17 from 1 to 5
|
||||
move 12 from 2 to 7
|
||||
move 1 from 3 to 6
|
||||
move 16 from 5 to 8
|
||||
move 3 from 5 to 6
|
||||
move 9 from 8 to 3
|
||||
move 8 from 8 to 4
|
||||
move 7 from 4 to 1
|
||||
move 5 from 1 to 4
|
||||
move 4 from 3 to 7
|
||||
move 14 from 7 to 3
|
||||
move 6 from 4 to 8
|
||||
move 9 from 7 to 4
|
||||
move 5 from 6 to 1
|
||||
move 1 from 7 to 1
|
||||
move 1 from 6 to 7
|
||||
move 16 from 4 to 5
|
||||
move 1 from 4 to 2
|
||||
move 1 from 7 to 5
|
||||
move 2 from 1 to 7
|
||||
move 2 from 7 to 4
|
||||
move 4 from 1 to 6
|
||||
move 13 from 5 to 6
|
||||
move 5 from 6 to 3
|
||||
move 22 from 3 to 2
|
||||
move 1 from 4 to 7
|
||||
move 4 from 5 to 4
|
||||
move 1 from 7 to 6
|
||||
move 5 from 8 to 5
|
||||
move 2 from 3 to 1
|
||||
move 13 from 6 to 1
|
||||
move 6 from 1 to 4
|
||||
move 1 from 8 to 1
|
||||
move 6 from 1 to 4
|
||||
move 1 from 5 to 4
|
||||
move 7 from 4 to 7
|
||||
move 3 from 1 to 5
|
||||
move 2 from 5 to 7
|
||||
move 5 from 5 to 1
|
||||
move 8 from 7 to 4
|
||||
move 1 from 6 to 4
|
||||
move 1 from 7 to 4
|
||||
move 9 from 2 to 7
|
||||
move 8 from 7 to 6
|
||||
move 5 from 6 to 4
|
||||
move 1 from 7 to 4
|
||||
move 2 from 4 to 9
|
||||
move 2 from 6 to 1
|
||||
move 8 from 2 to 6
|
||||
move 9 from 1 to 8
|
||||
move 9 from 6 to 2
|
||||
move 1 from 1 to 8
|
||||
move 6 from 8 to 4
|
||||
move 2 from 9 to 7
|
||||
move 2 from 7 to 9
|
||||
move 15 from 2 to 8
|
||||
move 18 from 4 to 2
|
||||
move 14 from 4 to 5
|
||||
move 10 from 2 to 4
|
||||
move 9 from 2 to 6
|
||||
move 1 from 9 to 3
|
||||
move 1 from 3 to 1
|
||||
move 6 from 5 to 8
|
||||
move 3 from 4 to 9
|
||||
move 2 from 2 to 1
|
||||
move 1 from 1 to 6
|
||||
move 3 from 9 to 7
|
||||
move 22 from 8 to 6
|
||||
move 1 from 8 to 9
|
||||
move 2 from 1 to 5
|
||||
move 5 from 5 to 4
|
||||
move 2 from 5 to 8
|
||||
move 2 from 8 to 7
|
||||
move 1 from 9 to 7
|
||||
move 1 from 5 to 8
|
||||
move 1 from 9 to 8
|
||||
move 15 from 6 to 4
|
||||
move 2 from 5 to 2
|
||||
move 11 from 4 to 6
|
||||
move 5 from 4 to 1
|
||||
move 5 from 4 to 2
|
||||
move 2 from 1 to 5
|
||||
move 6 from 2 to 8
|
||||
move 11 from 6 to 3
|
||||
move 12 from 6 to 8
|
||||
move 1 from 3 to 9
|
||||
move 3 from 3 to 2
|
||||
move 6 from 4 to 2
|
||||
move 2 from 5 to 8
|
||||
move 5 from 7 to 2
|
||||
move 11 from 8 to 4
|
||||
move 1 from 7 to 4
|
||||
move 1 from 9 to 6
|
||||
move 7 from 2 to 1
|
||||
move 3 from 6 to 5
|
||||
move 2 from 5 to 3
|
||||
move 1 from 5 to 9
|
||||
move 3 from 4 to 9
|
||||
move 4 from 9 to 1
|
||||
move 4 from 3 to 6
|
||||
move 3 from 4 to 8
|
||||
move 3 from 8 to 9
|
||||
move 2 from 8 to 2
|
||||
move 9 from 8 to 7
|
||||
move 2 from 3 to 1
|
||||
move 2 from 3 to 2
|
||||
move 1 from 3 to 6
|
||||
move 2 from 9 to 1
|
||||
move 8 from 7 to 5
|
||||
move 7 from 2 to 7
|
||||
move 2 from 8 to 9
|
||||
move 4 from 6 to 5
|
||||
move 13 from 1 to 5
|
||||
move 4 from 1 to 8
|
||||
move 3 from 9 to 3
|
||||
move 12 from 5 to 9
|
||||
move 3 from 8 to 9
|
||||
move 1 from 8 to 4
|
||||
move 3 from 2 to 7
|
||||
move 3 from 3 to 7
|
||||
move 1 from 9 to 2
|
||||
move 4 from 6 to 4
|
||||
move 6 from 5 to 6
|
||||
move 2 from 7 to 3
|
||||
move 2 from 2 to 1
|
||||
move 5 from 6 to 5
|
||||
move 1 from 1 to 7
|
||||
move 9 from 5 to 4
|
||||
move 10 from 9 to 6
|
||||
move 1 from 2 to 6
|
||||
move 12 from 7 to 6
|
||||
move 1 from 7 to 4
|
||||
move 23 from 6 to 1
|
||||
move 10 from 4 to 3
|
||||
move 16 from 1 to 5
|
||||
move 5 from 1 to 2
|
||||
move 6 from 3 to 7
|
||||
move 5 from 4 to 8
|
134
2022/src/days/day_5.gleam
Normal file
134
2022/src/days/day_5.gleam
Normal file
|
@ -0,0 +1,134 @@
|
|||
import gleam/io.{debug}
|
||||
import gleam/int
|
||||
import gleam/list.{map}
|
||||
import gleam/map.{Map}
|
||||
import gleam/iterator
|
||||
import gleam/string
|
||||
import gleam/erlang/process
|
||||
|
||||
type Movement {
|
||||
Movement(quantity: Int, from: Int, to: Int)
|
||||
}
|
||||
|
||||
pub fn pt_1(input: String) -> Int {
|
||||
let lines = string.split(input, on: "\n")
|
||||
assert Ok(first_line) = list.first(lines)
|
||||
let stack_count = { string.length(first_line) + 1 } / 4
|
||||
let stacks =
|
||||
lines
|
||||
|> iterator.from_list
|
||||
|> iterator.take_while(string.starts_with(_, "["))
|
||||
|> iterator.map(string.to_graphemes)
|
||||
|> iterator.to_list
|
||||
|
||||
let parsed = parse_stacks(stacks, stack_count - 1, map.new())
|
||||
|
||||
let movements =
|
||||
lines
|
||||
|> iterator.from_list
|
||||
|> iterator.drop_while(fn(line) { !string.starts_with(line, "move") })
|
||||
|> iterator.take_while(string.starts_with(_, "move"))
|
||||
|> iterator.map(parse_movement)
|
||||
|> iterator.to_list
|
||||
|
||||
execute_movements(parsed, movements)
|
||||
|> top_of_stacks(stack_count - 1, [])
|
||||
|> string.concat
|
||||
|> io.println
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
pub fn pt_2(input: String) -> Int {
|
||||
todo
|
||||
}
|
||||
|
||||
fn parse_stacks(
|
||||
stacks: List(List(String)),
|
||||
n: Int,
|
||||
parsed: Map(Int, List(String)),
|
||||
) -> Map(Int, List(String)) {
|
||||
case n < 0 {
|
||||
True -> parsed
|
||||
False -> {
|
||||
// Want to parse stack n
|
||||
let col =
|
||||
map(stacks, row_pluck(_, n))
|
||||
|> list.drop_while(fn(x) { x == " " })
|
||||
parse_stacks(stacks, n - 1, map.insert(parsed, n, col))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn row_pluck(row: List(String), n: Int) -> String {
|
||||
assert Ok(char) = list.at(row, n * 4 + 1)
|
||||
char
|
||||
}
|
||||
|
||||
fn parse_movement(line: String) -> Movement {
|
||||
case string.split(line, " ") {
|
||||
["move", quantity, "from", from, "to", to] -> {
|
||||
assert Ok(quantity) = int.parse(quantity)
|
||||
assert Ok(from) = int.parse(from)
|
||||
assert Ok(to) = int.parse(to)
|
||||
Movement(quantity, from - 1, to - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_movements(
|
||||
stacks: Map(Int, List(String)),
|
||||
movements: List(Movement),
|
||||
) -> Map(Int, List(String)) {
|
||||
case movements {
|
||||
[] -> stacks
|
||||
[m, ..ms] -> execute_movements(execute_movement(stacks, m), ms)
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_movement(
|
||||
stacks: Map(Int, List(String)),
|
||||
movement: Movement,
|
||||
) -> Map(Int, List(String)) {
|
||||
assert Ok(from) = map.get(stacks, movement.from)
|
||||
assert Ok(to) = map.get(stacks, movement.to)
|
||||
let #(to_move, remaining) = list.split(from, movement.quantity)
|
||||
|
||||
// update the stacks
|
||||
stacks
|
||||
|> map.insert(movement.from, remaining)
|
||||
|> map.insert(movement.to, list.append(list.reverse(to_move), to))
|
||||
}
|
||||
|
||||
fn top_of_stacks(
|
||||
stacks: Map(Int, List(String)),
|
||||
i: Int,
|
||||
out: List(String),
|
||||
) -> List(String) {
|
||||
case i < 0 {
|
||||
True -> out
|
||||
False -> {
|
||||
assert Ok(stack) = map.get(stacks, i)
|
||||
case list.first(stack) {
|
||||
Ok(top) -> top_of_stacks(stacks, i - 1, [top, ..out])
|
||||
Error(_) -> top_of_stacks(stacks, i - 1, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn abort(msg: String) -> Nil {
|
||||
io.println(msg)
|
||||
process.kill(process.self())
|
||||
}
|
||||
|
||||
fn dump(stacks: Map(Int, List(String)), i: Int) -> Map(Int, List(String)) {
|
||||
case i < map.size(stacks) {
|
||||
True -> {
|
||||
assert Ok(stack) = map.get(stacks, i)
|
||||
io.println(int.to_string(i + 1) <> ": " <> string.join(stack, " "))
|
||||
dump(stacks, i + 1)
|
||||
}
|
||||
False -> stacks
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue