This commit is contained in:
rhiannon morris 2022-12-10 07:05:22 +01:00
parent b0b6df2d40
commit 66e6052e5e
13 changed files with 102 additions and 36 deletions

View file

@ -1,4 +1,5 @@
MMCFLAGS := -s asm_fast.par.gc.stseg # this seems to break day7 for some absolutely bizarre reason
MMCFLAGS := # -s asm_fast.par.gc.stseg
all: aoc all: aoc

8
aoc.m
View file

@ -23,11 +23,11 @@ main2(!IO) :-
then then
read_lines_need(File, Lines, !IO), read_lines_need(File, Lines, !IO),
run_day(Day, Part, Lines, Out), run_day(Day, Part, Lines, Out),
write_line(univ_value(Out), !IO) output(Out, !IO)
else else
die("expected day, part, filename")). die("expected day, part, filename")).
:- pred run_day(int, part, lines, univ). :- pred run_day(int, part, lines, answer).
:- mode run_day(in, in, in, out) is cc_multi. :- mode run_day(in, in, in, out) is cc_multi.
run_day(Day, Part, Lines, Out) :- run_day(Day, Part, Lines, Out) :-
if solution(Day, P) then if solution(Day, P) then
@ -35,7 +35,7 @@ run_day(Day, Part, Lines, Out) :-
else else
die("no solution for day %d", [i(Day)]). die("no solution for day %d", [i(Day)]).
:- type sol == (pred(part, lines, univ)). :- type sol == (pred(part, lines, answer)).
:- inst sol == (pred(in, in, out) is cc_multi). :- inst sol == (pred(in, in, out) is cc_multi).
:- import_module day1. :- import_module day1.
@ -47,6 +47,7 @@ run_day(Day, Part, Lines, Out) :-
:- import_module day7. :- import_module day7.
:- import_module day8. :- import_module day8.
:- import_module day9. :- import_module day9.
:- import_module day10.
:- pred solution(int::in, sol::out(sol)) is semidet. :- pred solution(int::in, sol::out(sol)) is semidet.
solution(1, day1.run). solution(1, day1.run).
@ -58,3 +59,4 @@ solution(6, day6.run).
solution(7, day7.run). solution(7, day7.run).
solution(8, day8.run). solution(8, day8.run).
solution(9, day9.run). solution(9, day9.run).
solution(10, day10.run).

View file

@ -1,5 +1,6 @@
:- module basics. :- module basics.
:- interface. :- interface.
:- import_module io.
:- type part ---> one; two. :- type part ---> one; two.
:- pred part(string, part). :- pred part(string, part).
@ -8,8 +9,10 @@
:- func part(part, T, T) = T. :- func part(part, T, T) = T.
:- type answer ---> int(int); string(string); lines(list(string)).
:- pred output(answer::in, io::di, io::uo) is det.
:- import_module io.
:- import_module list. :- import_module list.
:- import_module string. :- import_module string.
@ -52,6 +55,10 @@ part(one, X, _) = X.
part(two, _, Y) = Y. part(two, _, Y) = Y.
output(int(I), !IO) :- write_int(I, !IO), nl(!IO).
output(string(S), !IO) :- write_string(S, !IO), nl(!IO).
output(lines(Ss), !IO) :- write_list(Ss, "\n", write_string, !IO), nl(!IO).
:- import_module exception. :- import_module exception.
:- type die ---> death(string). :- type die ---> death(string).

7
day1.m
View file

@ -1,9 +1,8 @@
:- module day1. :- module day1.
:- interface. :- interface.
:- import_module basics. :- import_module basics.
:- import_module univ.
:- pred run(part::in, lines::in, univ::out) is cc_multi. :- pred run(part::in, lines::in, answer::out) is cc_multi.
:- implementation. :- implementation.
:- import_module list. :- import_module list.
@ -13,8 +12,8 @@
:- import_module int. :- import_module int.
:- pragma no_determinism_warning(run/3). :- pragma no_determinism_warning(run/3).
run(one, Lines, univ(det_head(go(Lines)))). run(one, Lines, int(det_head(go(Lines)))).
run(two, Lines, univ(sum(take_upto(3, go(Lines))))). run(two, Lines, int(sum(take_upto(3, go(Lines))))).
:- func go(lines) = list(int). :- func go(lines) = list(int).
go(Lines) = go(Lines) =

66
day10.m Normal file
View file

@ -0,0 +1,66 @@
:- module day10.
:- interface.
:- import_module basics.
:- pred run(part::in, lines::in, answer::out) is cc_multi.
:- implementation.
:- import_module int.
:- import_module string.
:- import_module list.
:- import_module char.
:- type instr ---> noop; addx(int).
:- pred line(instr::out, list(string)::in, list(string)::out) is semidet.
line(noop) --> ["noop"].
line(addx(N)) --> ["addx", X], {to_int(X, N)}.
:- pred line(string::in, instr::out) is semidet.
line(Str, I) :- line(I, words(Str), []).
:- type reg == int.
:- type cycle == int.
:- type signal == int.
:- pred regs(reg::in, list(instr)::in, list(reg)::out) is det.
regs(_, [], []).
regs(R, [noop | Is], [R | Ss]) :-
regs(R, Is, Ss).
regs(R, [addx(N) | Is], [R, R | Ss]) :-
regs(R + N, Is, Ss).
:- pred regs(lines::in, list(signal)::out) is semidet.
regs(Lines, Signals) :-
map(line, Lines, Instrs),
regs(1, Instrs, Signals).
:- func signals(cycle, list(reg)) = list(reg).
signals(_, []) = [].
signals(C, [R | Rs]) = [C * R | signals(C + 1, Rs)].
:- func signals(list(reg)) = list(reg).
signals(Rs) = signals(1, Rs).
:- func render1(cycle, list(reg)) = list(char).
render1(_, []) = [].
render1(C, [R | Rs]) = [X | Xs] :-
(if abs(R - C) =< 1 then X = '' else X = ' '),
Xs = render1((C + 1) mod 40, Rs).
:- func render(list(reg)) = lines.
render(Rs) = map(from_char_list, chunk(render1(0, Rs), 40)).
:- pragma no_determinism_warning(run/3).
run(Part, Lines, Out) :-
if regs(Lines, Regs)
then
( Part = one,
Indices = [20, 60, 100, 140, 180, 220],
Out = int(sum(map(det_index1(signals(Regs)), Indices)))
; Part = two,
Out = lines(render(Regs))
)
else die("bad input").

7
day2.m
View file

@ -6,9 +6,8 @@
:- interface. :- interface.
:- import_module basics. :- import_module basics.
:- import_module list. :- import_module list.
:- import_module univ.
:- pred run(part::in, list(string)::in, univ::out) is cc_multi. :- pred run(part::in, list(string)::in, answer::out) is cc_multi.
:- implementation. :- implementation.
:- import_module int. :- import_module int.
@ -97,5 +96,5 @@ run(Lines, P, F, Out) :-
die("invalid input"). % lol die("invalid input"). % lol
:- pragma no_determinism_warning(run/3). :- pragma no_determinism_warning(run/3).
run(one, Lines, univ(Out)) :- run(Lines, line1, score1, Out). run(one, Lines, int(Out)) :- run(Lines, line1, score1, Out).
run(two, Lines, univ(Out)) :- run(Lines, line2, score2, Out). run(two, Lines, int(Out)) :- run(Lines, line2, score2, Out).

8
day3.m
View file

@ -2,9 +2,8 @@
:- interface. :- interface.
:- import_module basics. :- import_module basics.
:- import_module list. :- import_module list.
:- import_module univ.
:- pred run(part::in, list(string)::in, univ::out) is cc_multi. :- pred run(part::in, list(string)::in, answer::out) is cc_multi.
:- implementation. :- implementation.
:- import_module int. :- import_module int.
@ -60,9 +59,8 @@ go2(t(X, Y, Z), Prio) :-
die("no item in common in:\n - %s\n - %s\n - %s", [s(X), s(Y), s(Z)]). die("no item in common in:\n - %s\n - %s\n - %s", [s(X), s(Y), s(Z)]).
run(one, Lines, univ(sum(Prios))) :- run(one, Lines, int(sum(Prios))) :- map(go1, Lines, Prios).
map(go1, Lines, Prios). run(two, Lines, int(sum(Prios))) :-
run(two, Lines, univ(sum(Prios))) :-
if threes(Lines, Groups) then if threes(Lines, Groups) then
map(go2, Groups, Prios) map(go2, Groups, Prios)
else else

7
day4.m
View file

@ -1,9 +1,8 @@
:- module day4. :- module day4.
:- interface. :- interface.
:- import_module basics. :- import_module basics.
:- import_module univ.
:- pred run(part::in, lines::in, univ::out) is cc_multi. :- pred run(part::in, lines::in, answer::out) is cc_multi.
:- pred number(int::out, chars::in, chars::out) is semidet. :- pred number(int::out, chars::in, chars::out) is semidet.
@ -50,5 +49,5 @@ go(P, Lines, Out) :-
:- pragma no_determinism_warning(run/3). :- pragma no_determinism_warning(run/3).
run(one, Lines, univ(Out)) :- go(all_overlap, Lines, Out). run(one, Lines, int(Out)) :- go(all_overlap, Lines, Out).
run(two, Lines, univ(Out)) :- go(any_overlap, Lines, Out). run(two, Lines, int(Out)) :- go(any_overlap, Lines, Out).

5
day5.m
View file

@ -1,9 +1,8 @@
:- module day5. :- module day5.
:- interface. :- interface.
:- import_module basics. :- import_module basics.
:- import_module univ.
:- pred run(part::in, lines::in, univ::out) is cc_multi. :- pred run(part::in, lines::in, answer::out) is cc_multi.
:- implementation. :- implementation.
:- import_module int. :- import_module int.
@ -96,7 +95,7 @@ interp(Part, {Count, From, To}) -->
take_n(From, Count, Res), put_n(Part, To, Res). take_n(From, Count, Res), put_n(Part, To, Res).
:- pragma no_determinism_warning(run/3). :- pragma no_determinism_warning(run/3).
run(Part, Lines, univ(Out)) :- run(Part, Lines, string(Out)) :-
if input(Towers, Insts, Lines, []) then if input(Towers, Insts, Lines, []) then
foldl(interp(Part), Insts, Towers, Res), foldl(interp(Part), Insts, Towers, Res),
from_char_list(map(det_head, Res), Out) from_char_list(map(det_head, Res), Out)

5
day6.m
View file

@ -1,9 +1,8 @@
:- module day6. :- module day6.
:- interface. :- interface.
:- import_module basics. :- import_module basics.
:- import_module univ.
:- pred run(part::in, lines::in, univ::out) is cc_multi. :- pred run(part::in, lines::in, answer::out) is cc_multi.
:- implementation. :- implementation.
:- import_module int. :- import_module int.
@ -26,7 +25,7 @@ length(one) = 4.
length(two) = 14. length(two) = 14.
:- pragma no_determinism_warning(run/3). :- pragma no_determinism_warning(run/3).
run(Part, Lines, univ(Out)) :- run(Part, Lines, int(Out)) :-
if Lines = [Line] then if Lines = [Line] then
Len = length(Part), Len = length(Part),
(if start_index(Len, to_char_list(Line), Out0) (if start_index(Len, to_char_list(Line), Out0)

5
day7.m
View file

@ -1,9 +1,8 @@
:- module day7. :- module day7.
:- interface. :- interface.
:- import_module basics. :- import_module basics.
:- import_module univ.
:- pred run(part::in, lines::in, univ::out) is cc_multi. :- pred run(part::in, lines::in, answer::out) is cc_multi.
:- implementation. :- implementation.
:- import_module int. :- import_module int.
@ -53,5 +52,5 @@ go(one, Top, sum(map(size, Found))) :-
go(two, Top, det_head(sort(map(size, Found)))) :- go(two, Top, det_head(sort(map(size, Found)))) :-
find(size_gte(size(Top) - 40_000_000), Top, Found). find(size_gte(size(Top) - 40_000_000), Top, Found).
run(Part, Lines, univ(Out)) :- run(Part, Lines, int(Out)) :-
if dir(Top, Lines, []) then go(Part, Top, Out) else die("bad input"). if dir(Top, Lines, []) then go(Part, Top, Out) else die("bad input").

3
day8.m
View file

@ -1,9 +1,8 @@
:- module day8. :- module day8.
:- interface. :- interface.
:- import_module basics. :- import_module basics.
:- import_module univ.
:- pred run(part::in, lines::in, univ::out) is cc_multi. :- pred run(part::in, lines::in, answer::out) is cc_multi.
:- implementation. :- implementation.

5
day9.m
View file

@ -1,9 +1,8 @@
:- module day9. :- module day9.
:- interface. :- interface.
:- import_module basics. :- import_module basics.
:- import_module univ.
:- pred run(part::in, lines::in, univ::out) is cc_multi. :- pred run(part::in, lines::in, answer::out) is cc_multi.
:- implementation. :- implementation.
:- import_module int. :- import_module int.
@ -95,7 +94,7 @@ run0(Part, Lines, Out) :-
foldl2(next, condense(Dirs), Start, _, init, End), foldl2(next, condense(Dirs), Start, _, init, End),
count(End, Out). count(End, Out).
run(Part, Lines, univ(Out)) :- run(Part, Lines, int(Out)) :-
disable_warning [no_solution_disjunct] ( disable_warning [no_solution_disjunct] (
not run0(Part, Lines, Out) => die("bad input") not run0(Part, Lines, Out) => die("bad input")
). ).