## ~pmikkelsen/aoc2022

c18c16a8b6971c711c5869df3f9b8e0252a27495 — Peter Mikkelsen 1 year, 6 months ago
```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]) -->
+	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
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop
+noop