~pmikkelsen/aoc2022

bdc741b7cc44211d109c843aba6b09969e86feac — Peter Mikkelsen 1 year, 6 months ago 9c3cf32
day9
2 files changed, 2095 insertions(+), 1 deletions(-)

M aoc.pl
A input9.txt
M aoc.pl => aoc.pl +95 -1
@@ 28,6 28,10 @@ solve(5, Part, R) --> parse_day5(Part, R).
solve(6, Part, R) --> parse_day6(Part, R).
solve(7, Part, R) --> parse_day7(Part, R).
solve(8, Part, R) --> parse_day8(Part, R).
solve(9, Part, R) --> parse_day9(Part, R).
solve(10, Part, R) --> parse_day10(Part, R).
solve(11, Part, R) --> parse_day11(Part, R).
solve(12, Part, R) --> parse_day12(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,


@@ 49,7 53,15 @@ test(results) :-
	solution(7, 1, 1447046),
	solution(7, 2, 578710),
	solution(8, 1, 1736),
	solution(8, 2, 268800).
	solution(8, 2, 268800),
	solution(9, 1, 6011),
	solution(9, 2, 2419).
%	solution(10, 1, _),
%	solution(10, 2, _),
%	solution(11, 1, _),
%	solution(11, 2, _),
%	solution(12, 1, _),
%	solution(12, 2, _).

%% Parsers for each day



@@ 259,7 271,89 @@ product_list([], R, R).
product_list([X|Xs], R0, R) :-
	R1 is R0 * X,
	product_list(Xs, R1, R).

% day9
parse_day9(Part, R) -->
	{
	nth1(Part, [2, 10], NKnots),
	length(Knots, NKnots),
	Start = loc(0,0),
	member(Start, Locations),
	maplist(=(Start), Knots)
	},
	day9_steps(Locations, Knots),
	{
	length(Locations, _), !,
	sort(Locations, UniqueLocations),
	length(UniqueLocations, R)
	}.

day9_steps(Ls, Knots) -->
	day9_step(Ls, Knots, Knots1), !,
	day9_steps(Ls, Knots1).
day9_steps(_, _) --> "".

day9_step(Ls, Knots, Knots1) -->
	day9_direction(Dir), " ", integer(Count), nl,
	{
	!,
	day9_move(Ls, Dir, Count, Knots, Knots1)
	}.
day9_direction(left) --> "L".
day9_direction(right) --> "R".
day9_direction(up) --> "U".
day9_direction(down) --> "D".

day9_move(Ls, Dir, Count, [loc(Hx, Hy)|Knots], Knots2) :-
	Count > 0,
	Count1 is Count - 1,
	nth1(Ix, [left, right, up, down], Dir),
	nth1(Ix, [-1, 1, 0, 0], X),
	nth1(Ix, [0, 0, 1, -1], Y),
	Hx1 is Hx + X,
	Hy1 is Hy + Y,
	day9_follow_chain(loc(Hx1, Hy1), Knots, Knots1),
	reverse(Knots1, [EndLoc|_]),
	member(EndLoc, Ls),
	day9_move(Ls, Dir, Count1, [loc(Hx1, Hy1)|Knots1], Knots2).
day9_move(_, _, 0, Knots, Knots).

day9_follow_chain(_, [], []).
day9_follow_chain(loc(Hx, Hy), [loc(Tx, Ty)|Knots], [loc(Tx1, Ty1)|Knots1]) :-
	day9_follow(Hx, Hy, Tx, Ty, Tx1, Ty1),
	day9_follow_chain(loc(Tx1, Ty1), Knots, Knots1).

day9_follow(Hx, Hy, Tx, Ty, Tx, Ty) :- % H and T close enough 
	Dx is abs(Hx - Tx),
	Dy is abs(Hy - Ty),
	Dx =< 1,
	Dy =< 1,
	!.
day9_follow(Hx, Hy, Hx, Ty, Hx, Ty1) :- % Same x coordinate for H and T
	Ty > Hy + 1,
	Ty1 is Hy + 1,
	!.
day9_follow(Hx, Hy, Hx, Ty, Hx, Ty1) :- % Same x coordinate for H and T
	Ty < Hy - 1,
	Ty1 is Hy - 1,
	!.
day9_follow(Hx, Hy, Tx, Hy, Tx1, Hy) :- % Same y coordinate for H and T
	Tx < Hx - 1,
	Tx1 is Hx - 1,
	!.
day9_follow(Hx, Hy, Tx, Hy, Tx1, Hy) :- % Same y coordinate for H and T
	Tx > Hx + 1,
	Tx1 is Hx + 1,
	!.
day9_follow(Hx, Hy, Tx, Ty, Tx1, Ty1) :- % Diagonal
	member(o(X,Y), [o(1,1), o(1,-1), o(-1,1), o(-1,-1)]),
	Tx1 is Tx + X,
	Ty1 is Ty + Y,
	Dx is abs(Hx - Tx1),
	Dy is abs(Hy - Ty1),
	Dx =< 1,
	Dy =< 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14
R 2
U 7
L 5
U 15
L 15
D 9
L 1
D 13
U 3
R 15
L 6
D 10
L 4
U 10
R 14
L 8
U 7
D 10
U 1
R 7
L 14
R 5
L 9
D 15
L 14
R 14
L 7
R 6
L 3
D 13
L 5
D 10
R 9
L 2
D 16
U 9
L 14
R 2
D 2
R 1
L 1
U 7
D 12
R 4
U 15
R 7
U 14
R 2
D 3
R 6
L 9
U 8
R 7
L 8
D 6
R 13
D 10
L 9
D 1
U 14
L 8
R 3
L 14
R 2
D 13
L 1
R 15
U 5
D 4
R 9
D 9
L 3
D 12
R 4
U 8
L 6
D 10
L 6
R 12
D 14
U 5
D 13
R 13
U 11
D 10
R 16
L 3
U 10
D 8
L 1
U 8
D 4
L 6
R 12
D 7
L 5
U 11
R 5
D 7
L 2
U 9
L 8
R 14
D 5
R 6
U 10
D 16
U 4
R 11
U 3
R 4
U 5
L 11
R 12
U 9
R 9
D 12
R 8
D 9
L 5
D 5
U 11
L 16
U 2
L 5
R 10
U 13
R 10
L 3
D 3
U 2
L 14
D 15
L 8
D 1
L 10
R 6
L 5
U 6
R 14
U 11
R 16
L 1
R 16
L 10
U 9
D 2
L 2
U 15
L 3
D 11
L 16
U 9
D 17
U 4
D 14
U 4
L 5
R 14
L 2
U 13
D 1
U 6
R 3
U 7
D 12
L 11
U 6
L 5
D 16
R 11
L 2
R 15
D 3
U 11
R 2
D 6
R 16
L 9
R 4
L 8
D 4
L 3
D 15
U 10
D 4
U 8
R 2
U 16
L 9
D 5
R 8
D 11
R 13
U 11
D 2
R 16
D 10
L 11
U 6
L 13
D 10
L 10
R 13
U 5
R 10
D 2
R 2
D 14
L 15
R 15
D 1
L 2
U 17
L 12
R 9
D 10
L 2
D 12
R 8
U 1
L 6
U 14
L 15
U 13
L 15
D 3
U 8
R 14
U 3
L 12
R 5
L 12
R 12
U 6
D 10
U 4
D 11
U 1
D 17
U 5
D 8
U 9
R 9
L 6
R 10
U 1
D 3
R 1
D 4
L 16
D 16
U 3
L 17
D 8
R 15
D 9
R 7
D 8
R 17
L 13
U 16
D 2
L 1
R 3
D 11
R 15
L 10
R 9
U 1
R 11
L 15
U 2
R 12
U 18
R 18
D 4
U 7
D 13
U 18
D 14
U 7
D 2
L 1
R 1
U 4
L 13
R 4
U 13
L 16
D 12
R 4
U 9
D 11
R 4
L 16
D 4
L 8
D 3
R 9
U 15
R 3
L 9
U 2
R 15
L 5
R 3
D 17
U 17
R 11
L 13
U 8
D 11
L 12
U 1
D 5
U 12
D 2
R 13
D 6
R 5
D 10
U 15
R 7
L 4
R 8
D 10
U 17
L 12
U 14
L 9
D 18
R 11
U 13
D 9
L 1
D 9
U 6
R 15
L 1
U 4
D 13
R 18
U 3
L 2
U 18
R 17
L 14
U 12
R 7
L 11
D 3
U 17
D 9
R 13
L 18
D 15
R 16
U 16
L 12
D 16
R 18
L 13
D 8
U 16
D 2
U 17
D 4
L 12
U 3
D 2
R 3
L 14
D 7
L 6
U 5
L 19
R 19
L 7
R 1
D 2
U 11
L 7
U 8
L 4
R 4
L 12
U 7
R 3
U 3
L 2
R 3
D 9
R 16
U 4
R 12
L 10
D 3
U 17
D 6
U 14
D 2
L 19
U 3
R 19
L 11
U 13
R 11
D 14
L 4
D 11
U 9
L 8
U 18
L 17
D 7
U 7
R 19
L 8
D 6
R 7
U 19
D 7
R 7
L 16
R 11
L 2
R 10
U 6
R 2
U 8
L 8
D 3
L 12
D 16
U 1
D 12
L 4
D 16
R 13
L 12
R 11
U 8
L 6
D 6
L 1
U 5
L 3
R 6
U 8
R 5
U 4
D 14
R 7
U 2
L 9
U 8
R 13
U 1
D 15
R 18
D 15
R 14
D 15
R 8
D 16
U 6
L 7
D 10
R 9
L 12
R 11
L 3
R 12
D 8
U 14
L 18