## ~pmikkelsen/aoc2022

45e8a454635fb19e6a41544873445f88962c7e91 — Peter Mikkelsen 1 year, 7 months ago
```day5
```
```2 files changed, 582 insertions(+), 1 deletions(-)

M aoc.pl
A input5.txt
```
`M aoc.pl => aoc.pl +70 -1`
```@@ 24,6 24,7 @@ solve(1, Part, R) --> parse_day1(P), { solve_day1(Part, P, R) }.
solve(2, Part, R) --> parse_day2(Part, P), { solve_day2(P, R) }.
solve(3, Part, R) --> parse_day3(Part, R).
solve(4, Part, R) --> parse_day4(Part, R).
+solve(5, Part, R) --> parse_day5(Part, R).

% Time the stuff and test that I got the correct answer
% It will print the time for each solution, then a 0 for the first fail,

@@ 37,7 38,9 @@ test(results) :-
solution(3, 1, 8240),
solution(3, 2, 2587),
solution(4, 1, 466),
-	solution(4, 2, 865).
+	solution(4, 2, 865),
+	solution(5, 1, "GFTNRBZPF"),
+	solution(5, 2, "VRQWPDSGP").

%% Parsers for each day

@@ 101,7 104,57 @@ parse_day4(_, 0) --> "".
day4_range(Part, R) -->
integer(A), "-", integer(B), ",", integer(C), "-", integer(D), nl,
{ day4_contains(Part,A,B,C,D) -> R = 1 ; R = 0 }.
+
% day5
+parse_day5(Part, R) -->
+	day5_crates(Crates),
+	nl,
+	day5_moves(Part, Crates, Crates1),
+	{ maplist(list_head, Crates1, R) }.
+day5_crates(Crates) -->
+	{
+	length(Crates, N),
+	length(Start, N),
+	maplist(=([]), Start)
+	},
+	day5_crates(Start, Crates).
+
+day5_crates(Crates0, Crates) -->
+	day5_crate_line(Crates0, Crates1, 0, 1), nl,
+	{ maplist(append, Crates0, Crates1, Crates2) },
+	day5_crates(Crates2, Crates).
+day5_crates(Crates, Crates) --> % the redundant number line below the crates
+	day5_crate_numbers(Crates), nl.
+
+day5_crate_line([O|Os], [C|Cs], F0, F) -->
+	day5_crate(O, C, F0, F1), " ", day5_crate_line(Os, Cs,F1,F).
+day5_crate_line([O], [C], F0, 1) --> day5_crate(O, C, F0, 1).
+
+day5_crate([], [], F, F) --> "   ".
+day5_crate(_, [C], _, 1) --> "[", [C], "]".
+
+day5_crate_numbers(Cs) -->
+	{ length(Cs, N), ! },
+	day5_crate_numbers(1, N).
+day5_crate_numbers(N, Rest) -->
+	{ N1 is N + 1, N =< Rest },
+	" ", integer(N), " ",
+	day5_numbers_space(N, Rest),
+	day5_crate_numbers(N1, Rest).
+day5_crate_numbers(N, Rest) --> { N #= Rest + 1 }, "".
+
+day5_numbers_space(X,X) --> !, "".
+day5_numbers_space(_,_) --> " ".
+
+day5_moves(Part, Crates0, Crates) -->
+	"move ", integer(Count),
+	" from ", integer(From),
+	" to ", integer(To),
+	nl, !,
+	{ day5_perform_move(Part, Crates0, Crates1, Count, From, To) },
+	day5_moves(Part, Crates1, Crates).
+day5_moves(_, Crates, Crates) --> "".
+
% day6
% day7
% day8

@@ 188,6 241,22 @@ day4_contains(2,A,B,C,_) :- C     in  A..B.
day4_contains(2,A,_,C,D) :- A     in  C..D.

% day5
+day5_perform_move(_, Crates, Crates, 0, _, _).
+day5_perform_move(Part, Crates0, Crates, Count, From, To) :-
+	Count #> 0,
+	nth1(Part, [1, Count], LiftCount),
+	Count1 #= Count - LiftCount,
+	day5_perform_move_step(Crates0, Crates1, LiftCount, From, To),
+	day5_perform_move(Part, Crates1, Crates, Count1, From, To).
+day5_perform_move_step(Crates0, Crates2, LiftCount, From, To) :-
+	day5_lift(Crates0, Crates1, LiftCount, S, From),
+	day5_lift(Crates2, Crates1, LiftCount, S, To).
+day5_lift([C|Cs], [C1|Cs], LiftCount, S, 1) :- length(S, LiftCount), append(S, C1, C).
+day5_lift([C|Cs], [C|Cs1], LiftCount, S, N) :-
+	N #> 1,
+	N1 #= N - 1,
+	day5_lift(Cs, Cs1, LiftCount, S, N1).
% day6
% day7
% day8

```
`A input5.txt => input5.txt +512 -0`
```@@ 0,0 1,512 @@
+[J]             [F] [M]
+[Z] [F]     [G] [Q] [F]
+[G] [P]     [H] [Z] [S] [Q]
+[V] [W] [Z] [P] [D] [G] [P]
+[T] [D] [S] [Z] [N] [W] [B] [N]
+[D] [M] [R] [J] [J] [P] [V] [P] [J]
+[B] [R] [C] [T] [C] [V] [C] [B] [P]
+[N] [S] [V] [R] [T] [N] [G] [Z] [W]
+ 1   2   3   4   5   6   7   8   9
+
+move 2 from 4 to 6
+move 1 from 9 to 5
+move 3 from 2 to 4
+move 8 from 4 to 7
+move 2 from 9 to 7
+move 3 from 8 to 3
+move 2 from 1 to 2
+move 5 from 7 to 9
+move 1 from 9 to 4
+move 1 from 8 to 3
+move 1 from 3 to 4
+move 2 from 4 to 9
+move 7 from 3 to 5
+move 6 from 1 to 8
+move 11 from 7 to 9
+move 12 from 5 to 3
+move 6 from 6 to 9
+move 3 from 3 to 8
+move 4 from 2 to 7
+move 3 from 5 to 7
+move 1 from 5 to 7
+move 2 from 2 to 5
+move 1 from 5 to 2
+move 5 from 8 to 9
+move 7 from 7 to 2
+move 3 from 8 to 7
+move 1 from 8 to 9
+move 4 from 3 to 6
+move 1 from 5 to 1
+move 9 from 9 to 6
+move 7 from 9 to 6
+move 20 from 6 to 5
+move 12 from 9 to 8
+move 5 from 5 to 1
+move 3 from 7 to 4
+move 6 from 2 to 7
+move 2 from 3 to 1
+move 4 from 3 to 8
+move 1 from 4 to 1
+move 7 from 7 to 5
+move 4 from 8 to 2
+move 3 from 6 to 2
+move 3 from 2 to 9
+move 4 from 1 to 7
+move 2 from 1 to 2
+move 3 from 9 to 5
+move 11 from 8 to 5
+move 1 from 6 to 9
+move 1 from 8 to 5
+move 1 from 1 to 2
+move 24 from 5 to 4
+move 2 from 1 to 6
+move 11 from 5 to 4
+move 2 from 7 to 9
+move 1 from 6 to 2
+move 4 from 2 to 1
+move 28 from 4 to 2
+move 1 from 7 to 8
+move 9 from 2 to 5
+move 2 from 9 to 6
+move 4 from 4 to 2
+move 1 from 7 to 4
+move 3 from 4 to 7
+move 1 from 6 to 9
+move 21 from 2 to 3
+move 3 from 1 to 6
+move 5 from 6 to 2
+move 7 from 2 to 3
+move 1 from 9 to 3
+move 1 from 8 to 4
+move 1 from 7 to 8
+move 3 from 5 to 8
+move 1 from 1 to 7
+move 2 from 7 to 9
+move 2 from 8 to 4
+move 1 from 9 to 2
+move 1 from 8 to 6
+move 11 from 3 to 4
+move 1 from 7 to 8
+move 6 from 5 to 9
+move 2 from 8 to 7
+move 1 from 6 to 5
+move 7 from 3 to 8
+move 9 from 3 to 6
+move 1 from 8 to 3
+move 1 from 7 to 4
+move 2 from 3 to 5
+move 4 from 5 to 7
+move 4 from 6 to 8
+move 2 from 7 to 9
+move 11 from 4 to 2
+move 1 from 4 to 2
+move 6 from 8 to 9
+move 1 from 7 to 1
+move 1 from 3 to 7
+move 3 from 7 to 8
+move 6 from 8 to 9
+move 6 from 4 to 8
+move 18 from 9 to 3
+move 1 from 5 to 8
+move 5 from 6 to 5
+move 6 from 8 to 1
+move 3 from 5 to 4
+move 1 from 9 to 8
+move 3 from 4 to 8
+move 15 from 3 to 6
+move 2 from 5 to 9
+move 3 from 3 to 1
+move 9 from 6 to 4
+move 2 from 1 to 5
+move 2 from 5 to 8
+move 6 from 4 to 2
+move 6 from 1 to 6
+move 3 from 4 to 6
+move 6 from 9 to 1
+move 4 from 2 to 1
+move 7 from 8 to 1
+move 1 from 6 to 7
+move 17 from 1 to 5
+move 1 from 7 to 1
+move 5 from 2 to 1
+move 1 from 8 to 6
+move 11 from 6 to 4
+move 2 from 2 to 3
+move 3 from 1 to 8
+move 7 from 2 to 5
+move 4 from 6 to 7
+move 4 from 1 to 5
+move 15 from 5 to 9
+move 2 from 3 to 7
+move 2 from 8 to 2
+move 1 from 1 to 9
+move 6 from 2 to 6
+move 7 from 5 to 6
+move 5 from 7 to 3
+move 1 from 6 to 1
+move 2 from 3 to 4
+move 1 from 3 to 4
+move 5 from 6 to 4
+move 14 from 9 to 2
+move 1 from 8 to 9
+move 1 from 7 to 8
+move 1 from 9 to 6
+move 2 from 9 to 5
+move 1 from 1 to 2
+move 7 from 6 to 9
+move 1 from 3 to 4
+move 8 from 5 to 2
+move 1 from 6 to 7
+move 1 from 7 to 4
+move 1 from 8 to 4
+move 1 from 3 to 9
+move 7 from 9 to 5
+move 1 from 9 to 1
+move 6 from 5 to 1
+move 8 from 2 to 4
+move 1 from 5 to 6
+move 1 from 6 to 7
+move 1 from 7 to 9
+move 7 from 2 to 9
+move 1 from 9 to 4
+move 3 from 9 to 1
+move 1 from 9 to 6
+move 11 from 2 to 8
+move 9 from 1 to 8
+move 1 from 6 to 4
+move 1 from 1 to 9
+move 12 from 4 to 2
+move 4 from 9 to 3
+move 3 from 4 to 6
+move 9 from 8 to 6
+move 12 from 4 to 9
+move 8 from 6 to 3
+move 8 from 2 to 7
+move 11 from 3 to 4
+move 2 from 2 to 7
+move 2 from 6 to 1
+move 1 from 2 to 3
+move 2 from 6 to 2
+move 3 from 2 to 6
+move 2 from 1 to 6
+move 1 from 6 to 1
+move 1 from 6 to 4
+move 2 from 6 to 3
+move 1 from 6 to 5
+move 4 from 3 to 8
+move 12 from 4 to 5
+move 5 from 9 to 7
+move 3 from 8 to 7
+move 1 from 9 to 1
+move 3 from 8 to 2
+move 13 from 5 to 6
+move 1 from 2 to 9
+move 13 from 6 to 7
+move 7 from 9 to 6
+move 2 from 4 to 6
+move 1 from 8 to 6
+move 1 from 1 to 6
+move 1 from 2 to 9
+move 1 from 2 to 3
+move 12 from 7 to 9
+move 7 from 8 to 4
+move 1 from 1 to 3
+move 2 from 7 to 9
+move 15 from 7 to 4
+move 8 from 6 to 3
+move 1 from 8 to 9
+move 1 from 7 to 2
+move 10 from 3 to 5
+move 6 from 5 to 9
+move 1 from 2 to 8
+move 1 from 5 to 8
+move 2 from 8 to 9
+move 10 from 4 to 9
+move 20 from 9 to 6
+move 1 from 7 to 6
+move 4 from 9 to 3
+move 1 from 5 to 9
+move 4 from 4 to 9
+move 8 from 9 to 7
+move 2 from 5 to 1
+move 7 from 4 to 3
+move 8 from 3 to 2
+move 6 from 9 to 8
+move 1 from 3 to 7
+move 1 from 3 to 1
+move 7 from 7 to 8
+move 13 from 8 to 3
+move 2 from 2 to 8
+move 1 from 8 to 2
+move 1 from 4 to 1
+move 1 from 1 to 8
+move 2 from 8 to 2
+move 24 from 6 to 2
+move 2 from 7 to 8
+move 5 from 3 to 4
+move 25 from 2 to 6
+move 5 from 4 to 9
+move 2 from 8 to 7
+move 2 from 7 to 3
+move 4 from 6 to 2
+move 2 from 6 to 4
+move 9 from 2 to 3
+move 11 from 3 to 7
+move 10 from 7 to 8
+move 1 from 7 to 9
+move 3 from 2 to 4
+move 8 from 8 to 2
+move 1 from 2 to 6
+move 2 from 4 to 1
+move 1 from 8 to 2
+move 1 from 6 to 9
+move 1 from 8 to 3
+move 6 from 9 to 7
+move 2 from 9 to 1
+move 9 from 6 to 8
+move 7 from 2 to 3
+move 7 from 8 to 2
+move 10 from 6 to 8
+move 7 from 1 to 2
+move 9 from 3 to 2
+move 5 from 3 to 8
+move 4 from 7 to 2
+move 2 from 3 to 2
+move 12 from 2 to 3
+move 6 from 4 to 2
+move 1 from 7 to 6
+move 5 from 3 to 5
+move 16 from 8 to 4
+move 12 from 2 to 7
+move 5 from 5 to 7
+move 1 from 8 to 3
+move 1 from 6 to 4
+move 17 from 7 to 4
+move 1 from 7 to 1
+move 1 from 1 to 9
+move 1 from 9 to 5
+move 11 from 4 to 9
+move 10 from 2 to 3
+move 1 from 5 to 4
+move 1 from 9 to 2
+move 2 from 2 to 1
+move 1 from 2 to 3
+move 23 from 4 to 5
+move 7 from 9 to 7
+move 3 from 9 to 1
+move 20 from 5 to 6
+move 3 from 5 to 8
+move 1 from 4 to 1
+move 2 from 8 to 3
+move 4 from 6 to 4
+move 7 from 7 to 2
+move 1 from 8 to 4
+move 19 from 3 to 9
+move 5 from 1 to 7
+move 7 from 2 to 6
+move 3 from 7 to 5
+move 2 from 3 to 4
+move 1 from 5 to 4
+move 1 from 1 to 4
+move 1 from 7 to 6
+move 13 from 6 to 7
+move 6 from 9 to 3
+move 1 from 3 to 5
+move 2 from 3 to 4
+move 2 from 6 to 2
+move 3 from 4 to 3
+move 8 from 9 to 1
+move 2 from 2 to 1
+move 8 from 6 to 7
+move 2 from 9 to 4
+move 20 from 7 to 1
+move 2 from 7 to 5
+move 2 from 5 to 1
+move 8 from 1 to 8
+move 8 from 8 to 6
+move 1 from 6 to 9
+move 8 from 6 to 1
+move 1 from 5 to 3
+move 7 from 3 to 2
+move 1 from 5 to 2
+move 2 from 9 to 7
+move 1 from 5 to 8
+move 18 from 1 to 4
+move 1 from 8 to 9
+move 3 from 2 to 3
+move 2 from 7 to 4
+move 5 from 2 to 4
+move 3 from 3 to 8
+move 8 from 1 to 7
+move 2 from 9 to 2
+move 32 from 4 to 5
+move 1 from 9 to 7
+move 1 from 2 to 1
+move 6 from 1 to 6
+move 1 from 2 to 4
+move 3 from 8 to 1
+move 3 from 6 to 5
+move 1 from 3 to 6
+move 2 from 1 to 9
+move 4 from 4 to 7
+move 31 from 5 to 4
+move 4 from 5 to 6
+move 1 from 6 to 1
+move 7 from 6 to 5
+move 1 from 9 to 4
+move 19 from 4 to 2
+move 1 from 5 to 9
+move 5 from 5 to 6
+move 3 from 4 to 2
+move 2 from 7 to 1
+move 4 from 7 to 8
+move 3 from 8 to 6
+move 2 from 6 to 7
+move 6 from 7 to 8
+move 3 from 1 to 5
+move 4 from 5 to 9
+move 15 from 2 to 1
+move 4 from 6 to 4
+move 2 from 6 to 3
+move 1 from 3 to 7
+move 4 from 1 to 2
+move 1 from 3 to 4
+move 2 from 7 to 4
+move 5 from 9 to 3
+move 2 from 7 to 3
+move 16 from 4 to 8
+move 8 from 8 to 5
+move 2 from 1 to 5
+move 1 from 9 to 6
+move 1 from 6 to 5
+move 7 from 5 to 9
+move 3 from 1 to 8
+move 1 from 8 to 4
+move 8 from 2 to 7
+move 3 from 1 to 3
+move 1 from 3 to 9
+move 2 from 4 to 2
+move 7 from 8 to 5
+move 7 from 9 to 1
+move 6 from 3 to 5
+move 6 from 7 to 4
+move 3 from 4 to 1
+move 3 from 2 to 5
+move 1 from 7 to 8
+move 1 from 7 to 5
+move 1 from 9 to 8
+move 2 from 2 to 4
+move 15 from 1 to 6
+move 8 from 5 to 9
+move 3 from 3 to 4
+move 4 from 4 to 3
+move 1 from 9 to 7
+move 6 from 9 to 4
+move 1 from 9 to 2
+move 6 from 4 to 9
+move 2 from 4 to 6
+move 5 from 6 to 9
+move 1 from 3 to 1
+move 8 from 6 to 8
+move 12 from 5 to 3
+move 1 from 5 to 3
+move 1 from 3 to 8
+move 4 from 6 to 1
+move 11 from 3 to 8
+move 1 from 2 to 1
+move 23 from 8 to 2
+move 3 from 1 to 2
+move 1 from 1 to 9
+move 2 from 2 to 3
+move 6 from 3 to 6
+move 1 from 7 to 6
+move 1 from 4 to 7
+move 1 from 4 to 3
+move 1 from 7 to 3
+move 4 from 8 to 4
+move 2 from 1 to 8
+move 3 from 8 to 1
+move 4 from 6 to 2
+move 7 from 9 to 1
+move 1 from 9 to 6
+move 2 from 2 to 3
+move 3 from 9 to 4
+move 1 from 9 to 3
+move 10 from 2 to 8
+move 16 from 2 to 5
+move 2 from 3 to 6
+move 6 from 1 to 8
+move 1 from 1 to 5
+move 8 from 8 to 5
+move 11 from 5 to 9
+move 2 from 1 to 8
+move 1 from 1 to 8
+move 4 from 4 to 6
+move 3 from 3 to 9
+move 14 from 9 to 3
+move 15 from 8 to 5
+move 9 from 5 to 4
+move 7 from 6 to 1
+move 1 from 6 to 3
+move 4 from 4 to 7
+move 2 from 6 to 2
+move 4 from 7 to 4
+move 4 from 1 to 4
+move 10 from 4 to 3
+move 14 from 3 to 6
+move 5 from 4 to 1
+move 6 from 5 to 7
+move 1 from 2 to 6
+move 3 from 7 to 2
+move 2 from 2 to 3
+move 3 from 7 to 8
+move 2 from 8 to 2
+move 2 from 2 to 7
+move 6 from 6 to 2
+move 1 from 8 to 7
+move 8 from 2 to 7
+move 1 from 4 to 1
+move 5 from 5 to 3
+move 3 from 3 to 2
+move 5 from 1 to 3
+move 7 from 5 to 8
+move 6 from 6 to 3
+move 1 from 5 to 9
+move 10 from 7 to 9
+move 26 from 3 to 4
+move 1 from 5 to 1
+move 6 from 8 to 2
+move 9 from 2 to 9
+move 1 from 7 to 5
+move 1 from 8 to 5
+move 2 from 6 to 2
+move 20 from 9 to 6
+move 1 from 1 to 6
+move 1 from 4 to 2
+move 1 from 5 to 8
+move 1 from 5 to 7
+move 3 from 1 to 3
+move 1 from 3 to 6
+move 12 from 4 to 8
+move 11 from 4 to 5
+move 1 from 7 to 5
+move 1 from 2 to 8
+move 1 from 1 to 8
+move 2 from 2 to 5
+move 8 from 6 to 2
+move 5 from 6 to 4
+move 2 from 5 to 3
+move 12 from 8 to 4
+move 5 from 2 to 6
+move 3 from 8 to 1
+move 11 from 6 to 8
+move 10 from 4 to 6
+move 5 from 4 to 6
+move 12 from 6 to 5
+move 22 from 5 to 6
+move 3 from 6 to 5
+move 3 from 8 to 5
+move 1 from 3 to 8
+move 4 from 8 to 1
+move 6 from 1 to 7
+move 5 from 6 to 9

```