~pmikkelsen/aoc2022

c18c16a8b6971c711c5869df3f9b8e0252a27495 — Peter Mikkelsen 1 year, 6 months ago bdc741b
day10
2 files changed, 183 insertions(+), 4 deletions(-)

M aoc.pl
A input10.txt
M aoc.pl => aoc.pl +47 -4
@@ 55,9 55,9 @@ test(results) :-
	solution(8, 1, 1736),
	solution(8, 2, 268800),
	solution(9, 1, 6011),
	solution(9, 2, 2419).
%	solution(10, 1, _),
%	solution(10, 2, _),
	solution(9, 2, 2419),
	solution(10, 1, 16880),
	solution(10, 2, "\n###..#..#..##..####..##....##.###..###..\n#..#.#.#..#..#....#.#..#....#.#..#.#..#.\n#..#.##...#..#...#..#..#....#.###..#..#.\n###..#.#..####..#...####....#.#..#.###..\n#.#..#.#..#..#.#....#..#.#..#.#..#.#.#..\n#..#.#..#.#..#.####.#..#..##..###..#..#.\n").
%	solution(11, 1, _),
%	solution(11, 2, _),
%	solution(12, 1, _),


@@ 354,7 354,46 @@ day9_follow(Hx, Hy, Tx, Ty, Tx1, Ty1) :- % Diagonal
	Dx =< 1,
	Dy =< 1,
	!.

% day10
parse_day10(Part, R) -->
	day10_instructions([1], Xs),
	{
	( Part = 1
	-> findall(V, (nth1(Ix, Xs, X), 20 is Ix mod 40, V is X * Ix), Vs), 
	   sum_list(Vs, R)
	; phrase(day10_crt(Xs, 1), R0),
	  R = ['\n'|R0]
	)
	}.
day10_instructions(Xs0, Xs) -->
	day10_instruction(Xs0, Xs1), nl, !,
	day10_instructions(Xs1, Xs).
day10_instructions(XsRev, Xs) --> "", { reverse(XsRev, Xs) }.

day10_instruction([X|Xs], [X,X|Xs]) --> "noop".
day10_instruction([X|Xs], [X1,X,X|Xs]) -->
	"addx ",
	integer(I),
	{ X1 is X + I }.

day10_crt([_], _) --> "".
day10_crt([X|Xs], Cycle) -->
	{
	Pos is (Cycle - 1) mod 40,
	( 1 >= abs(X - Pos)
	-> Ch = "#"
	;  Ch = "."
	),
	( Pos = 39
	-> End = "\n"
	;  End = ""
	),
	Cycle1 is Cycle + 1
	},
	Ch, End,
	day10_crt(Xs, Cycle1).

% day11
% day12
% day13


@@ 383,8 422,12 @@ integer(N) -->
	{ integer(N), number_chars(N, Ds), ! },
	Ds.
integer(N) -->
	sign(S),
	digits(Ds),
	{ catch(number_chars(N, Ds), _, fail), ! }.
	{ catch(number_chars(N0, Ds), _, fail), !, N is S * N0 }.

sign(-1) --> "-", !.
sign(1) --> "".

digits([D|Ds]) --> digit(D), !, digits(Ds).
digits([]) --> "".

A input10.txt => input10.txt +136 -0
@@ 0,0 1,136 @@
noop
noop
addx 5
addx 3
noop
addx 14
addx -12
noop
addx 5
addx 1
noop
addx 19
addx -15
noop
noop
noop
addx 7
addx -1
addx 4
noop
noop
addx 5
addx 1
addx -38
noop
addx 21
addx -18
addx 2
addx 2
noop
addx 3
addx 5
addx -6
addx 11
noop
addx 2
addx 19
addx -18
noop
addx 8
addx -3
addx 2
addx 5
addx 2
addx 3
addx -2
addx -38
noop
addx 3
addx 4
addx 5
noop
addx -2
addx 5
addx -8
addx 12
addx 3
addx -2
addx 5
addx 11
addx -31
addx 23
addx 4
noop
noop
addx 5
addx 3
addx -2
addx -37
addx 1
addx 5
addx 2
addx 12
addx -10
addx 3
addx 4
addx -2
noop
addx 6
addx 1
noop
noop
noop
addx -2
addx 7
addx 2
noop
addx 3
addx 3
addx 1
noop
addx -37
addx 2
addx 5
addx 2
addx 32
addx -31
addx 5
addx 2
addx 9
addx 9
addx -15
noop
addx 3
addx 2
addx 5
addx 2
addx 3
addx -2
addx 2
addx 2
addx -37
addx 5
addx -2
addx 2
addx 5
addx 2
addx 16
addx -15
addx 4
noop
addx 1
addx 2
noop
addx 3
addx 5
addx -1
addx 5
noop
noop
noop
noop
addx 3
addx 5
addx -16
noop