-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.pl
73 lines (58 loc) · 1.74 KB
/
day2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
:- use_module(library(charsio)).
:- use_module(library(dcgs)).
:- use_module(library(format)).
:- use_module(library(pio)).
:- use_module(library(lists)).
:- use_module(library(pairs)).
games([G|Gs]) --> game(G), "\n", games(Gs).
games([]) --> [].
game(Id-Rs) --> "Game ", integer(Id), ": ", reveals(Rs).
reveals([R|Rs]) --> counts(R), "; ", reveals(Rs).
reveals([R]) --> counts(R).
counts([C|Cs]) --> count(C), ", ", counts(Cs).
counts([C]) --> count(C).
count(C-N) --> integer(N), " ", color(C).
color(red) --> "red".
color(green) --> "green".
color(blue) --> "blue".
integer(N) --> digits(Ds), { number_chars(N, Ds) }.
digits([D|Ds]) --> digit(D), digits(Ds).
digits([D]) --> digit(D).
digit(D) --> [D], { char_type(D, decimal_digit) }.
% Part 1
possible_count(red-N) :- N =< 12.
possible_count(green-N) :- N =< 13.
possible_count(blue-N) :- N =< 14.
possible_counts(Cs) :- maplist(possible_count, Cs).
possible_reveals(Rs) :- maplist(possible_counts, Rs).
games_possible([], []).
games_possible([Id-Rs|Gs], PGs) :-
possible_reveals(Rs)
-> ( PGs = [Id-Rs|PGs0], games_possible(Gs, PGs0) )
; games_possible(Gs, PGs).
part1(Gs) :-
games_possible(Gs, PGs),
maplist(portray_clause, PGs),
pairs_keys(PGs, Ids),
sum_list(Ids, S),
portray_clause(S).
% Part 2
product(X, Y, Z) :- Z is X * Y.
power(_-Rs, P) :-
append(Rs, Cs), % flatten the reveals
keysort(Cs, SCs),
group_pairs_by_key(SCs, GCs), % group by color
portray_clause(GCs),
pairs_values(GCs, Css),
maplist(list_max, Css, Ms),
foldl(product, Ms, 1, P).
part2(Gs) :-
maplist(power, Gs, Ps),
sum_list(Ps, S),
portray_clause(S).
run :-
argv([I|_]),
phrase_from_file(games(Gs), I),
part1(Gs),
part2(Gs),
halt.