Compare commits

...

35 Commits

Author SHA1 Message Date
03c8e7d0ec omg I never committed d24p2 2024-12-12 22:17:04 -08:00
fd48bea516 d25p1 2023-12-25 16:45:25 -08:00
95261557ea d24p2 2023-12-25 13:20:40 -08:00
b8c59949e9 d23p2 2023-12-25 02:26:26 -08:00
69e2bc3b20 d23p2 naive 2023-12-24 23:40:09 -08:00
e0a1757e9e d24p1 2023-12-24 23:39:29 -08:00
5204bf0d60 d23p1 2023-12-24 15:40:59 -08:00
0064376377 d22p2 2023-12-24 00:18:26 -08:00
6d19325d59 d22p1 2023-12-23 23:53:53 -08:00
6efe1f874e d21p2 2023-12-22 18:21:40 -08:00
9189483484 d21p1 2023-12-20 23:28:45 -08:00
63921cea50 d20p2 2023-12-20 03:58:35 -08:00
3475d23962 cleanup d20p1 2023-12-20 02:49:53 -08:00
a4856cf9ef d20p1 2023-12-20 02:48:21 -08:00
43a83d487b d19p2 2023-12-19 23:15:01 -08:00
cbe8fbde87 d19p2 half done still need to calculate volume of the polyhedroid or find a better way 2023-12-19 02:49:11 -08:00
7e0fd280cb d19p1 2023-12-19 01:10:33 -08:00
63a054505d d18p2 2023-12-18 00:40:31 -08:00
89b14c0db9 d18p1 2023-12-18 00:31:11 -08:00
c4713f768a don't look at this commit it's horrible visualization oh but maybe it has a better heuristics 2023-12-17 17:05:43 -08:00
ce7568e5fc d17p2 2023-12-17 11:30:10 -08:00
edcc8f4dba d17p1 2023-12-17 11:14:35 -08:00
2202ebf4a8 better d16p2 2023-12-15 23:47:00 -08:00
793bb420ea d16p2 2023-12-15 23:39:35 -08:00
e50db48a2c d16p1 2023-12-15 23:23:19 -08:00
227aa6713d Make d15p2 a little more readable and remove unneeded imports. 2023-12-15 00:42:27 -08:00
8683c9c8b3 d15 2023-12-15 00:17:21 -08:00
0f5dcc20ce d14p2 for real 2023-12-14 22:24:55 -08:00
a6c506874c first half of part2 seems to work I hate swipl tabling 2023-12-14 13:50:21 -08:00
db08023b9e d14p1 2023-12-14 02:01:31 -08:00
7a66d289ea d13p1 and d13p2 2023-12-13 00:32:49 -08:00
25ad602e9a d12 classic dynamic programming, removing special python features 2023-12-12 16:33:12 -08:00
3169d08eb3 d12 classic dynamic programming explainer in py 2023-12-12 16:26:49 -08:00
da1707b365 d12p2. It was actually good; swipl's memoization table just didn't work. 2023-12-12 11:50:35 -08:00
b2affa1b5c add bruteforce d12p2 2023-12-12 01:15:01 -08:00
59 changed files with 9437 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
% Usage: swipl part1.pl input.txt
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).

65
12/part2.pl Normal file
View File

@@ -0,0 +1,65 @@
% Usage: swipl part2.pl input.txt
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
test :- main(['test.txt']).
main([FileName | _]) :-
input(FileName, Lines),
concurrent_maplist(resolve_repeated, Lines, Ns),
sum_list(Ns, Count),
writef('Res=%w\n', [Count]).
resolve_repeated(Line-Bads, N) :-
append([Line, [?], Line, [?], Line, [?], Line, [?], Line], L5),
append([Bads, Bads, Bads, Bads, Bads], B5),
abolish_private_tables, table(resolve/2),
resolve(L5-B5, N).
% Bads is list of contiguous blocks of bad items in Xs; w/unks resolved in Y.
resolve([]-[], 1).
resolve([o|Xs]-Bads, Y) :- resolve(Xs-Bads, Y).
resolve([?|Xs]-Bads, Y) :-
findall(N, (resolve([o|Xs]-Bads, N); resolve([#|Xs]-Bads, N)), Ys),
sum_list(Ys, Y).
resolve([#|Xs]-[N|Bads], Y) :-
length(BadsUnks, N),
( append(BadsUnks, [], [#|Xs]),
RemainingXs = []
; append(BadsUnks, [Next|RemainingXs], [#|Xs]), (Next = o; Next = ?)
),
bads_or_unks(BadsUnks),
resolve(RemainingXs-Bads, Y).
% List contains all bads or unks
bads_or_unks([]).
bads_or_unks([#|List]) :- bads_or_unks(List).
bads_or_unks([?|List]) :- bads_or_unks(List).
% List contains exactly N bad items.
nbads([], 0).
nbads([#|List], N) :- N > 0, Remain is N - 1, nbads(List, Remain).
processed_line(1, Line-Bads, NewLine-NewBads) :-
append(Line, [o], NewLine),
NewBads = Bads.
% read input file into [(o;#;?)]-[list of bad runs]
input(FileName, Lines) :- phrase_from_file(lines(Lines), FileName).
lines([]) --> eos.
lines([Status-Parity|Lines]) --> status(Status), parity(Parity), lines(Lines).
status([]) --> " ".
status([o]) --> all_dots, " ".
status([o, #|Cdr]) --> all_dots, "#", status(Cdr).
status([o, ?|Cdr]) --> all_dots, "?", status(Cdr).
status([#|Cdr]) --> "#", status(Cdr).
status([?|Cdr]) --> "?", status(Cdr).
all_dots --> ".".
all_dots --> ".", all_dots.
parity([N]) --> number(N), ("\n"; eos).
parity([N|Cdr]) --> number(N), ",", parity(Cdr).

86
12/part2.py Normal file
View File

@@ -0,0 +1,86 @@
# This demonstrates classic dynamic programming and prioritizes explainability.
# Of course all of this would have been cleaner and would perform the same if
# we were to use recursion with memoization (e.g. @memoize.cache)
import sys
import dataclasses
@dataclasses.dataclass(frozen=True)
class Entry:
working: int
broken: int
def total(self):
return self.working + self.broken
def no_dot_between(l, start, end):
return not any(c for c in l[start+1 : end] if c == ".")
def process(springs, counts):
# entries[(i,j)] = how many ways if we have only first i springs and j nums,
# given if that one was chosen to be working or no.
entries = {}
# boundary conditions: when there's no spring and no count left, 1 choice
entries[(-1, -1)] = Entry(working=1, broken=0)
# boundary conditions: when there's no count left but still springs left
for i in range(len(springs)):
entries[(i, -1)] = Entry(
working=(entries[(i - 1, -1)].working
if springs[i] in (".", "?")
else 0),
broken=0)
# boundary conditions: when there's no springs left but there's still count
for j in range(len(counts)):
entries[(-1, j)] = Entry(working=0, broken=0)
# building the rest of the table
for i in range(len(springs)):
for j in range(len(counts)):
prev_entry_if_working = entries[(i - 1, j)]
prev_working_entry_if_broken = (
entries[(i-counts[j], j-1)].working
if i-counts[j] >= -1 and no_dot_between(springs, i-counts[j], i)
else 0)
if springs[i] == ".":
entries[(i, j)] = Entry(
working=(prev_entry_if_working.working +
prev_entry_if_working.broken),
broken=0)
elif springs[i] == "#":
entries[(i, j)] = Entry(
working=0,
broken=prev_working_entry_if_broken)
elif springs[i] == '?':
entries[(i, j)] = Entry(
working=(prev_entry_if_working.working +
prev_entry_if_working.broken),
broken=prev_working_entry_if_broken)
# import code; code.interact(
# local=locals(),
# banner=(f"springs[{i}]={springs[i]}, counts[{j}]={counts[j]} "
# f".={prev_entry_if_working}, "
# f"#={prev_working_entry_if_broken}\n"
# f"--> ({i}, {j}) = {entries[(i, j)]}"),
# exitmsg='')
return entries[(len(springs) - 1, len(counts) - 1)].total()
if __name__ == "__main__":
with open(sys.argv[1], "rt") as f:
lines = f.readlines()
total = 0
for line in lines:
springs, counts = line.strip().split(" ")
counts = [int(c) for c in counts.split(",")]
# delete these next 2 lines to have part 1 back
springs = "?".join([springs]*5)
counts = counts*5
n = process(springs, counts)
# print(f"{n} <-- {springs}-{counts}")
total += n
print(f"total = {total}")

1339
13/input.txt Normal file

File diff suppressed because it is too large Load Diff

44
13/part1.pl Normal file
View File

@@ -0,0 +1,44 @@
:- use_module(library(pio)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Blocks),
convlist(vert_reflect, Blocks, Vs),
convlist(horz_reflect, Blocks, Hs),
sum_list(Vs, V),
sum_list(Hs, H),
Answer is 100*V + H,
writef('Answer=%t\n', [Answer]).
vert_reflect(Block, N) :- reflect(Block, N).
horz_reflect(Block, N) :- maplist({N}/[Row]>>(reflect(Row, N)), Block).
reflect(Items, N) :- perfect_reflect(Items, N).
reflect(Items, N) :-
append(SubItems, [_|_], Items),
perfect_reflect(SubItems, N).
reflect(Items, N) :-
append(Prefix, SubItems, Items),
perfect_reflect(SubItems, SubN),
length(Prefix, Len), N is SubN + Len.
% reflect right down the middle. N is half list length.
perfect_reflect([A, A], 1).
perfect_reflect(Items, N) :-
append([[Item], OtherItems, [Item]], Items),
perfect_reflect(OtherItems, N_sub1),
N is N_sub1 + 1.
input(FileName, Blocks) :- phrase_from_file(blocks(Blocks), FileName).
blocks([]) --> eos, !.
blocks([Block|Blocks]) --> block(Block), blocks(Blocks).
block([Line]) --> line(Line), ("\n"; eos), !.
block([Line|Lines]) --> line(Line), block(Lines).
line([]) --> ("\n"; eos), !.
line([#|Chars]) --> "#", line(Chars).
line([o|Chars]) --> ".", line(Chars).
eos([], []).

55
13/part2.pl Normal file
View File

@@ -0,0 +1,55 @@
:- use_module(library(pio)).
:- use_module(library(clpfd)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Blocks),
convlist(almost_reflect, Blocks, Vs), sum_list(Vs, V),
maplist(transpose, Blocks, TBlocks),
convlist(almost_reflect, TBlocks, Hs), sum_list(Hs, H),
Answer is 100*V + H,
writef('Answer=%t\n', [Answer]).
almost_reflect(Items, N) :- almost_perfect_reflect(Items, N).
almost_reflect(Items, N) :-
append(SubItems, [_|_], Items),
almost_perfect_reflect(SubItems, N).
almost_reflect(Items, N) :-
append(Prefix, SubItems, Items),
almost_perfect_reflect(SubItems, SubN),
length(Prefix, Len), N is SubN + Len.
almost_perfect_reflect([A, B], 1) :- diff(A, B, 1).
almost_perfect_reflect(Items, N) :-
append([[A], OtherItems, [A]], Items),
almost_perfect_reflect(OtherItems, N_sub1),
N is N_sub1 + 1.
almost_perfect_reflect(Items, N) :-
append([[A], OtherItems, [B]], Items),
diff(A, B, 1),
perfect_reflect(OtherItems, N_sub1),
N is N_sub1 + 1.
% reflect right down the middle. N is half list length.
perfect_reflect([A, A], 1).
perfect_reflect(Items, N) :-
append([[Item], OtherItems, [Item]], Items),
perfect_reflect(OtherItems, N_sub1),
N is N_sub1 + 1.
diff(L1, L2, N) :-
foldl([A, B, Prev, Curr]>>(Curr is Prev + abs(A - B)), L1, L2, 0, N).
input(FileName, Blocks) :- phrase_from_file(blocks(Blocks), FileName).
blocks([]) --> eos, !.
blocks([Block|Blocks]) --> block(Block), blocks(Blocks).
block([Line]) --> line(Line), ("\n"; eos), !.
block([Line|Lines]) --> line(Line), block(Lines).
line([]) --> ("\n"; eos), !.
line([1|Chars]) --> "#", line(Chars).
line([0|Chars]) --> ".", line(Chars).
eos([], []).

15
13/test.txt Normal file
View File

@@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#

100
14/input.txt Normal file
View File

@@ -0,0 +1,100 @@
#.O.O......##.......O.#O....O....O.O...O..O..##O#..O..#...##...........#..O..OO.O#O...OO.O..#...#..#
......O.#...#..O.O.....#O...#..OO.O.........O..O.#..O.#.O...O.##......O..O.#....#......O....O....O#O
...#...O..O..#.O...O##.O....##.....OOO#....#.O..O#O.OOO.........O....#.O...O.O...O...#.#..O..#.O.#.#
#...##...O.OO....OO.#..O#...O..#O.O...O.......#.##.###....O..O.O.#.#O........OO..##.................
...#O....#.O.....O.O..O.#.......#.#.#O....O....OOO.#.....O.#...#...O..##..##O.#O....O.#O.#O..#..O.OO
...O....O..#.....O..O..O#.O...O..#..OO###.O.....#.OO....O#......O#..........#....#.O#.O.O.#.....O.O.
....O...#..O.........O...OO#O......OOO..#..O....#..#...O...O..#...O.O..#O...#O.O...O..###..#.O.#....
...O.#O.#..##O.O.#...O...#....O..#.####....OO.#.#...........O.O...#..........O..O#.O..O....#.......#
........O.........O....OO...........##...O.#.OO.....#....#.O###.......O#O..#.#.O#..#....O..#......O.
O..#....O#O.#..O...............#..#..........OO#.#.#.#...#..#..O.#...#......O..#...O...O..O.......O.
OO#.O##OO......O.#..#O....O#......O..O...O...OO...#...........O...#......O....O.OOO#..#.....O.OO..O.
O.#.OO...#..#..O...#....#..O.O....O.OOO..O....#...O....O.O....O.O#........#.##.......O..O#.O....#...
......O..O..O..###.#..O..#...#..#.##O.#O.OO...O.O..#OO...O...O.#O..O..#....#O....OO#.#...O...O..#.#O
.#.#O..#O....O.##.#O.#O.O.O....#.....OO#O...OO....#O...O##..OO.OOO......#.....#..#O.OO...#..#....O..
..OO.#.#..#....#..#.......OO..OOO.#...#..O##.O..##O..O....OO#.OO...#O.##.#......#O#.O#..O##...#.....
O.#.....#.O.OO.O......##.........O.O#.O...#......O#.....O.....O#..O.#O..O.#......#..O..O.O..........
...O..O..O.#.##.O..#.........#...#O..#....O#...#..O###.O...#.......O...#.#.#...O.....#O...O....##O.#
.#OO..O........O.O...#.#.....O..O..OO.O....O..##....OO....O...O..O#..O.OO.#...O.O.#.O.#O....O.......
....O.O.O.O..O#..##..O.O.O..O.#.OO.O..#.OOO.O.#.OO.O.#O........#O..O.......O..O..#..O..#..#.O.#.....
.#.O..#.O#O.O.......O.#.#O.OO..O..OO..O#.O.O#O....##OO.......O..#O.O.O#.....#..#..#..O#O.......#O.O.
....O..O#....O#.#O..#.OOOO..#.......OO.......##...#O....O...#..#..#.O..OO..O.#..#.O....#.#.##OO.....
.#O..###..#O#.O#O....O.##..........O.#..O..O.OO.O##...#O.#.O.O...#..#O.........#...#..#.........#O..
#.#.......#O..#..##..O.#...#O#...#.#.O..O.OO##.O....#.....O..OOO.............O..##..#..#..O....#...#
#.#..O..........#.O.#O..O.#...#.#O#..O.#....#.#....#O.......#O.#.O.O.##..OO....O.##..O#....O.....O.O
.O...##...O..#..O#O...O#.....O.#O....OO.O.#O..#....OO.#.O.........#......#O.........O...#.O...#O.O#.
...O.....#........O.#O...#..#..##.......O.O#....#.O...OO##OOO#.#..O#OO..OO..##O....O.#.O...O....##..
............#.O.O#O..#..#..O.........#O..O....OO..#..#..O.#.O.#..O..O..O..#OO...O...OO#O.OO....#..O.
...O.O.....O..O.##.....OO..#O...O...O..O.#...#......O..O.......OO..OO....#OO.O..#........##.O.O....O
#O#......O.O.O#..O.O..OO..#O#..#OO.#...O...O.O.O....#..O#O..O###O.##OO#.O.O.......O.O#..#O.O.O.#...#
..O........OOO.O....OOOO......#.O...#.....O.O#......O#O.O.#O.....#..O....O#.O....O..#..O..O....O...#
...O......O..#....O...#O..OO..#.#OO.O...OOO......#.........#OOO.#.#........O...#OO.O.OOO....O.O#....
......#.O...OO#...#O.#..##.O.#..O..O..O.O.#.O.O.##.#O#O...#OOO...#..O....O..O...OOO..O..#.##....O..#
...O....O.#O.OO..#O...#.#....#O.##OO..#..OO..#....OO.O#....O.O..#.#..O...#.O.#.O.#..O..O..#..O#O.OO.
..O..O.O......O#..#..O..O..O#..#.......#.....#O..O...#O..O..OO#.#.##...OO#..##.O.O.O.O........####.#
#.O#......OO...#.###...OO......#..#.....#.#.##.O..#.#..OO..#...OO#.O#.#..O##....O.....#..#OOO.....#.
.#......O....O.#...O..##.O..#..#O...#.O#....#.O#....O.O#.O#.#.OO.O..O#....O....#...O.O#......O#OO...
....#O#...#.O#.....O.O..OO##O....#.O..O...O..O.OO#...O#.#...O.O.O.....O#..O...#O.....#...O#O...O#.#.
#..O..O..OO..#.O#.......O......#...O.#..#O#..#.O.#.O#...#O.....O.OO..#...O...O.##.....O....OO..#....
..#......O#O...#.........#...O#.#..O.#..O.O.......#O.#...OOO....O#....OO..OOO.......#O....OO........
O.O.#..#..#O........##OO...O.O#....#...#O.......#.O#O...O..O.O##.O..#..OOOO#...O.#O..O#...O.O#O..#O.
.....#O#.#O......OO..O.....#....#.#.O.O.##.O##.....O..O......#O#OOO.....#.....#OO..###.#...O.O.#.#..
#...##.OO.....O.....OO......#.O.O..O......#....##O##..OOO..#.....#...O.......O.......O....##........
.###..#...O..O.#.....O#........O...##O....#.O.#.O.O.#O.......O#..#...OO...O#.OO...O.....O.O.O..OO#.O
...O..#...#..##..#.O##.##O.....O.#..#O.......OO#..O#.OO...##O......OOO...O.O..O#...#.##..O...O..#..O
....O.......#..O#..#O..O....O.O....#...O....O.##.#OOOO....O.#...#O...O#.#.......#.......OO#.#...#.#.
#.OO.O##...O...##.O#..O#......#.#..OO.OO.#.O.O##.#....O.O.##O.....#O..#........#O#...#...#.....O#.#.
O.O.#..##.....##O.......O..OO..#.O......#.......#O....#.......O.#OO...O.O....#.##.....#.....OO......
O#..##..O........#.#..O.#.....#O....O#.....O...#O.......O.O.........O....O.#..O#.O.....#O.#.#.O.#.O.
...O.OO.OO.O..O...#.#.#.O.O.OOO......O.#.#.O.#..O..#.OO.O..##O.......O.........OOO#.O.O...#O.O....O.
O.##...#.#..O#.#..#..O...#...#...O..O..#..O...#O....O..#.##O....O..O..O.##O...O....O..##O..OO.#.....
...OOO.O....O.O.#....O#.OO..#.#.O.#.O#.O...O#O#......##..........#...O......O...#...O....O.#...#....
...#..##.O.#O#.O.#...#..O...O#O....##..OO##..#OO.........#...O#..O#.OOO...OO...#.O.#.OO##..#O#.#...#
O##...#......#O.O#...O....O...#O#.O...O.O.OOO....O.....##....O..#O.O....#.#...O........##OO#.O##...#
OO..#.....O..O#.O...O..OO....#.....##...#..O...#O..#.#O.#O.#O##...#....O....#.O.......#......O.....O
O.O....O##.OO...O.....#......#....O.#O.#...OO#.OO......OO...O#....OO#O...O....##....#...........O.O.
.....O..O.OO....O#..#....#.O....#....#O..##..O..#..........O.OO#.OOO...........O#O.O.......#......OO
.O.#.#.O..O.#....OOO.O..O....O...O......O.O.O..#O.#.#.....OOO.........#.O....#O.O..#O....O#........O
.##.OO#.O.....O..O..#.O..O.O.....O.O.#.O.....OOO.........O.O.O...#..O#..O..O..#O.O.#.#..O.#..#..O#.#
O...#..#..O...O...#..O..O.O.O...........#...#..#........#..OO.#O....#...#....O.........O.#.......#..
O..O###.##.#............#...OO#......#.O..O..#...O.O....O##...#.O.......#.#O..O........##...#.O....O
..#O#....##.O.....###..O.#O.O........#.......O.#...#O..#..OO...#.O...O.OO........###....##OO...O.O..
...O.....O..##....O.O##.OO...O.#.#.#.#.#......O.........O.O#....#.#..O.#.O#OO#.....O.OOO...O....OO..
#..#O...O....................O.#.......O.O...........#O.O....OO.O......O.O##O#.#O.O.O.O...O.O.#.O#O.
.O..O..#...OO.##.#...#.O#.O.###...O......O.......#.O##.O.....##.O......#.O...OO....O#.#..#..O.O.....
....O#.O.O...OO#.O.O#.O...#..........OO....#......O.O#.O.#......O...OOOO..O...##.#...O..#..O..##O.OO
...OO..#O.##O..O...#O.#..O..#.#.#..O#.O..#..#.....O...#.O#..........O....##.O...#O......OO.O..O.....
.#O##.O..#.OO#..O..##O..#.#...........O##.#O.OO#O.....O#..O.....O......O.....O..O...O.#..#..O...#O##
...........#.O#O.#O..O#.....##O....O.#....OO...#..OO..#.O.O#.#OOO....O...#O..#.O#......O.....#.##O..
..##.O.O...O.O..#.......#.O.OO..O....#....O.O.OO..#...O#O##.OO.O.#..#.#O.....##....O.O.O.....#..##.O
#.O.O...#....#..##..O.O.#O.....OO.#O...O......#.O.....OO..O.O.O.O...O..#....#OO.......#......O.#.O..
....#....OO..O##.O..O..OO.OOO.O.O.OO.O...##....O#O.....O....#.#.O...OO.O..#...#.OOO#O..#...OO......#
.O#.OO.O......##....#.#..O#OO..#....#....##O#.O.#...#O..#...OO......O...O#.#.O.O.O..O....O.#O.......
.##OO....##..O...##....O.#.#OOOO.O.#..O..O...#...O.O..O.OO..##..#O.O.......O#O....OO.OO.OO..........
.........O.#..#.....O..OO...#..O.OO...#.O.O.....#....O#..#.OOO.#...#..O.#....#O.......O.O...OO#..O..
#..OO#O......O.O...O#.#..#.O...##O#..O.....OO.O.##.O#O#...O#...#O.O.OO....#O#OO.O.#...O....##.......
OO..OO.....#OO.....O........O.......O#.O......O#.#O...#...O.##.#...O.....O.....O....#O...##O.......#
.#.OO..OOOO..O#O..O......O.#.OO.#.O.........O#OO..#.O.#..#O...O.........O#.#.O#O.OOO.O..#...#.O....#
O#...O#.#..O#.......O....#..#.O#.O.#....#....O..#......O...O...O..##.#O#O......O...#..#..OO..#.OOO..
#.....OO..O..#.OO..#...#.....O#.................##.......O.O..#...#O.O...OO....#OO#....O..O.O..O.O..
..#.......O#O..O.OOO.#........#..OO..O#.#.#..................O..OOO..#.O.....##O....O#..O.OO.O..#O.O
OO...OOO.....O..#O.##.O...#O...O....#......O.O..#OO#O..###O.......O...O..O..............O#....OO.O..
.O..O.#..O........#...##.....O...O##O.O..#.....O.#..#..OO#......O....#..OO.#......O...#O...##......O
...#.##O#.....O..O...O##O...#.O.#.O..O.O....#..O.O#....OO#OO..##.....#...O..##OO.O.O...O#O.....#O...
...#O.OO....O#...O...O....#O..................#.O.O..O.O...O..##O...O#.OO.#..#.#......O.#...O.......
..O...#O.##...O...O...O#O.O..#..O.#O#O....#.#O..#O....O...#.#......#.......#..OO.O.........###O##...
....#O...O.........O............#.OO##.O.O.OO.........O.#.O.#OO.....O..O..##...O#..........#.O..#.O.
.##.O#.O...O#O..#.O...#OO......O..OOOO.......O.O..#....#OO....O.O#O.......#O.....#OO.#.............#
..##..#OO#.......O....................O.........OO..#...O...O.###O..O.......#..O.#.....O.O...O.....O
....#......#..O..#.O.O..O..O...#O.O....#.....O..O.#..#O.#.O#.O#..#.....O.##.O..O#............#.#....
##..#...#..O.O.#O.......O#O..O..O...#O.O............#OO.OO.O...O..#..O..O.O#.O#.....O.O....#.####.#.
O...O#..O#..#.O...OO....#.....OO##.........O.O...O..#..OO#..##.O....O.#..O..O......O....O....O#....O
....#OO.O...OO.....O..##O.#.#..##.#.......O....#O..#......O.#....O.#...#O...O.OO..#OO....#....O.O#O.
...####..O.O....#.#O...#O........OO.O.#O.O.#....#.O...........#....#..O##.O.#OO..#..OO.....OO#.#.##.
.O.O..#....#..#O...#O.....O.#O..O.O#....#...#......#..O.O..#O.O.......#..#..OO..#...#....###O.#...OO
....O#.##...#...........#O.O.O...O.#.O#...#.#.OO#O.O.#OO#.....#...O.O#O.#....O.O.....O.O...#...#.OO.
O..O...OO.#...O.##....#.......##.O..#.##.OOO...#.......O..O.#...#..#....#O.O..O.....O.....O.O.##....
.#OO.##...O.#.............O.#...#....##...O.O.#..O..#O..#.###..OO.#OO#.O.........#...O.......#..#O.O
...O..O...OO.#...O#.#...O..#...O..OO....O.OO.OOO.O...O..O.##O.O.##...O.O.##...#O#....O..OO...#.#.#..
.#...#..O.O.##...#.#..O...OO.##.O....O.O......O.#..O#.#.#..O....#....#O.#..OO...O.OO#.O#...#.O..##..
#..O..#..........O.O#....#OO..#.#...#....#.....#....O....OO...O#....#.#.O##..##O#...#.......#.#O.O#O

42
14/part1.pl Normal file
View File

@@ -0,0 +1,42 @@
:- use_module(library(pio)).
:- use_module(library(clpfd)).
:- initialization(main, main).
:- table rocks/3.
main([FileName|_]) :-
input(FileName, Input),
transpose(Input, Rotated),
concurrent_maplist([Row, [#|Row]]>>(true), Rotated, Rocks),
/*concurrent_*/maplist(row_weight, Rocks, Weights),
sum_list(Weights, W),
writef('%t\n', [W]).
row_weight(Row, Weight) :-
phrase(rocks(Rocks), Row),
convlist([N-'#', N]>>(true), Rocks, RockCounts),
sum_list(RockCounts, Weight),
writef('>> %t <-- %t\n', [Weight, Row]).
rocks([0-0]) --> eos.
rocks([LastN-I, LastN-LastI|Rocks]) -->
[0], rocks([LastN-LastI|Rocks]),
{I is LastI + 1}.
rocks([N-I, LastN-LastI|Rocks]) -->
[1], rocks([LastN-LastI|Rocks]),
{I is LastI + 1, N is LastN + 1}.
rocks([0-I, N-'#', LastN-LastI|Rocks]) -->
[#], rocks([LastN-LastI|Rocks]),
{I is LastI + 1, N is LastN*(2*LastI - LastN + 1) / 2}.
% things below are for reading from input file
input(FileName, Input) :- phrase_from_file(lines(Input), FileName).
lines([]) --> eos, !.
lines([Line|Lines]) --> line(Line), lines(Lines).
line([]) --> "\n"; eos.
line([1|Chars]) --> "O", line(Chars).
line([0|Chars]) --> ".", line(Chars).
line(['#'|Chars]) --> "#", line(Chars).
eos([], []).

117
14/part2.pl Normal file
View File

@@ -0,0 +1,117 @@
:- use_module(library(pio)).
:- use_module(library(clpfd)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Input),
rolls(Input, 0, [], First-Cycle-Hist),
reverse(Hist, [_|TrueHist]),
BillionOffset is ((999999999 - First) mod Cycle) + First,
nth0(BillionOffset, TrueHist, BillionMap),
% maplist({TrueHist}/[M]>>(
% nth0(Nth, TrueHist, M),
% map_weight(M, W),
% writef('Weight(%t) = %t\n', [Nth, W]),
% print(M)), TrueHist),
write('======================\n'),
print(BillionMap),
map_weight(BillionMap, FinalWeight),
writef('First = %t, Cycle = %t, FinalOffset = %t, Answer = %t\n',
[First, Cycle, BillionOffset, FinalWeight]).
map_weight(Map, W) :- rotate(Map, Rotated), map_weight(Rotated, _, W).
map_weight([_], 0, 0).
map_weight([Row|Map], RowI, W) :-
map_weight(Map, PrevRowI, PrevW),
RowI is PrevRowI + 1,
include(['O']>>(true), Row, Rocks),
length(Rocks, NRocks),
W is PrevW + RowI*NRocks.
rolls(Map, N, Hist, First-Cycle-Hist) :-
match(Map, Hist, Cycle),
First is N - Cycle - 1,
!.
rolls(Map, N, Hist, X) :-
roll(Map, NewMap),
NextN is N + 1,
NewHist = [Map|Hist],
rolls(NewMap, NextN, NewHist, X).
match(Map, [Map|_], 1) :- !.
match(Map, [_|Entries], N) :-
match(Map, Entries, NextN),
N is NextN + 1.
% North is to the left
roll(Map, NewMap) :-
concurrent_maplist(collapse, Map, NorthCollapsed),
rotate(NorthCollapsed, West),
concurrent_maplist(collapse, West, WestCollapsed),
rotate(WestCollapsed, South),
concurrent_maplist(collapse, South, SouthCollapsed),
rotate(SouthCollapsed, East),
concurrent_maplist(collapse, East, EastCollapsed),
rotate(EastCollapsed, NewMap).
rotate(Map, NewMap) :-
transpose(Map, X),
concurrent_maplist(reverse, X, NewMap).
print(Map) :-
rotate(Map, RotMap),
append([[_|RealMap], [_]], RotMap),
maplist([X]>>(
append([_|Y], [_], X),
atomics_to_string(Y, S),
writef('%t\n', [S])), RealMap),
write('\n').
collapse(Row, NewRow) :-
phrase(rock_counts(Counts), Row),
phrase(condense(Zs), Counts),
phrase(reexpand(Zs), NewRow),
true.
reexpand([]) --> eos.
reexpand([N-Next|Rocks]) --> ['#'], stack(N, Next), reexpand(Rocks).
stack(0, 0) --> [], !.
stack(0, Z) --> {NextZ is Z - 1}, ['.'], stack(0, NextZ), !.
stack(N, Z) --> {NextZ is Z - 1, NextN is N - 1}, ['O'], stack(NextN, NextZ).
condense([]) --> [0-0-0].
condense([N-Next|Rocks]) --> [0-I-I, N-z-Next], condense(Rocks), !.
condense(Rocks) --> [_-_-_], condense(Rocks).
rock_counts([0-0-0]) --> [].
rock_counts([LastN-I-LastZ, LastN-LastI-LastZ|Rocks]) -->
['.'], rock_counts([LastN-LastI-LastZ|Rocks]),
{I is LastI + 1}.
rock_counts([N-I-LastZ, LastN-LastI-LastZ|Rocks]) -->
['O'], rock_counts([LastN-LastI-LastZ|Rocks]),
{I is LastI + 1, N is LastN + 1}.
rock_counts([0-I-I, LastN-z-Len, LastN-LastI-LastZ|Rocks]) -->
['#'], rock_counts([LastN-LastI-LastZ|Rocks]),
{I is LastI + 1, Len is I - LastZ - 1}.
% North is to the left
input(FileName, Input) :-
phrase_from_file(lines(Raw), FileName),
concurrent_maplist(pad, Raw, PaddedInput),
concurrent_maplist(reverse, PaddedInput, Reversed),
transpose(Reversed, Rotated),
concurrent_maplist(pad, Rotated, Input).
pad(Row, Rowx) :- append(['#'|Row], ['#'], Rowx).
lines([]) --> eos, !.
lines([Line|Lines]) --> line(Line), lines(Lines).
line([]) --> "\n"; eos.
line(['O'|Chars]) --> "O", line(Chars).
line(['.'|Chars]) --> ".", line(Chars).
line(['#'|Chars]) --> "#", line(Chars).
eos([], []).

10
14/test.txt Normal file
View File

@@ -0,0 +1,10 @@
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....

1
15/input.txt Normal file

File diff suppressed because one or more lines are too long

26
15/part1.pl Normal file
View File

@@ -0,0 +1,26 @@
:- use_module(library(pio)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Input),
hashes(Input, Hash),
writef('Answer=%t\n', [Hash]).
hashes(Strs, Hash) :-
concurrent_maplist(reverse, Strs, RStrs),
concurrent_maplist(hash, RStrs, Hashes),
sum_list(Hashes, Hash).
hash([], 0).
hash([C|Str], Hash) :- hash(Str, Prev), Hash is (Prev + C)*17 mod 256.
% Input stuff
input(FileName, Input) :- phrase_from_file(string(Input), FileName).
string([]) --> (eos; "\n"), !.
string([Item|Items]) --> item(Item), string(Items).
item([]) --> (","; "\n"; eos), !.
item([C|Chars]) --> [C], item(Chars).
eos([], []).

47
15/part2.pl Normal file
View File

@@ -0,0 +1,47 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Actions),
perform_all(Actions, Boxes),
findall(Power, power_of_some_lens(Boxes, Power), Powers),
sum_list(Powers, Answer),
writef('Answer=%t\n', [Answer]).
power_of_some_lens(Boxes, Power) :-
nth1(BoxI, Boxes, _-Box),
nth1(LensI, Box, _-Focal),
Power is BoxI * LensI * Focal.
perform_all(Actions, FinalBoxes) :-
length(EmptyBoxes, 256),
foldl([N-[], N, NextN]>>(NextN is N + 1), EmptyBoxes, 0, _),
foldl(perform, Actions, EmptyBoxes, FinalBoxes).
perform(BoxN-Action-Label, Before, After) :-
nth0(BoxN, Before, BoxN-Box),
call(Action, Label, Box, NewBox),
select(BoxN-Box, Before, BoxN-NewBox, After).
remove(_, [], []).
remove(Label, [Label-_|Box], Box) :- !.
remove(Label, [Lens|Box], [Lens|NewBox]) :- remove(Label, Box, NewBox).
add(Focal, Label, Box, NewBox) :- select(Label-_, Box, Label-Focal, NewBox), !.
add(Focal, Label, Box, NewBox) :- append(Box, [Label-Focal], NewBox).
% Input stuff. [0-add(5)-"rn", 3-remove-"cm", ...]
input(Name, Actions) :- phrase_from_file(insts(Actions), Name).
insts([]) --> (eos; "\n"), !.
insts([Box-Action-LabelS|Items]) -->
item(Label-Action), insts(Items),
{reverse(Label, Rev), hash(Rev, Box), string_codes(LabelS, Label)}.
item([]-remove) --> "-", (","; "\n"; eos), !.
item([]-add(N)) --> "=", number(N), (","; "\n"; eos), !.
item([C|Chars]-X) --> [C], item(Chars-X).
hash([], 0).
hash([C|Str], Hash) :- hash(Str, Prev), Hash is (Prev + C)*17 mod 256.

1
15/test.txt Normal file
View File

@@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7

110
16/input.txt Normal file
View File

@@ -0,0 +1,110 @@
\..../...................-......|..........|...-|.-.............|...................\/......-.................
../|....|.......-...............\....\...........-...............|.......|...........-........................
............................\........../|........./.......................................\.........|....|./||
..................\.//.............-.......\..-.|.......\.......\.............................-..-....|.......
../....\......|.|......|.....|./.-...|......-................//......|.../..-........\............\...........
...|...-.....\...............-.../.................|...............\-................/|...............\.......
|............................./...\.........\.............-....../-..|......|.|.../................\..........
.............-.|.../................|....................../................../..............................-
.......\.\....../.....|...........................-............/..-........-........................\......-..
.....-.\................-.....|\....-....|..../....|.....|.\............./.../.....\...............|....|.....
....|............................/............-|..|.........................-....................-....\.......
|....../......../../............................../....|......\..\....\\./.|................/......\......-./\
........-........./|............................./....\....-................../...|........|........-.........
..........\......./..................|..../.........../.........../........../...................|.-.\.......\
................-..........\....................................../.........\\........-...--..........|..\....
.-./...\......................\...../...\.\../.......|....................-...............\.-....\......./\..\
.......................-.......\..............................\............................\.|...-............
.......|...-..........-........-.......\.........././.-..|..../.....................\.........................
.....................\..|...../........\.....\\\..........\........................-............\..\...\......
....|.................|.........|.................................../.............-...........................
......................|................../....\..|...................-.\.......-\.........................|...
........-.........|.......\.................|................-..|../|\............../......................../
............|.\.................\..-...\.../...-....\...\...........|../..\........\.-........................
......./..............-..//................-.............../................/...-.......-......-............|.
........\|...\......-....|...........\....\./..........-......\......../..-...-...|........|/................\
....\....-../......-..................................|..\/../....-.....|...............\.-.....\............|
...................|..................|...........\............./.......-.......................\..\-../../...
....-..............-..\/.|.......................\.................../......|..-..\......|.........-/.........
........................./..\-..-.|..........-.....-............................\...................../.......
...../../..../....|...|..|...|.......\..\...\........-..../.../......................-....../..\..............
..........\.......\.....|.../.........../....|./.../......-..../.....|-...||......./..........|...\...........
...............................................-.........\.....|..................\|......................\...
.....................................\..................--......-........................-.//-....../-.-.....|
........../..../.....|....\........................|.................................../............./...-/...
........................-............./....-....-...-...........-..............|..............................
......|.....|........-./...\........|......................|........|.......|...-.....|.......................
..-.......-....../.............-..\...................................|.........|..........|............./....
.............................|.....\...........................././|......|................-....-.............
........../..................-..........\....\..../.....\.................-...........|.......................
................../..-......................|...........-|..............-......|......................-.......
-...........-..........|........-.......|\.././.|............................-/.|...........-/...............\
.../..-..........................--.........|...|.|...................................-|...\........|.........
........|.......-............|.........--.........-......./..../........-........\....../..../...........-....
..............-.............../...........................-................./......--..\..../.................
.......................--...........\..............\.......-.......-..|/.....\....-/......../........-........
.............|......|...............-...................\...............\..-.....................|.\.........\
.....\.-....\............\........|........|....\..............\............................................./
............................../...............././.......\.....\..........\|......-......|....|...........|...
................|............/.....|.........|.......\...../...|....-........|................................
./...\............\............|..-....\...\........................\..........\......\.-........../.....\....
.......-...|../..|..-.......-............................................-.....\.........-........../../......
...../.......-......../|..............|...................\......|............/|..........|..|...../..-...|..-
....-.........../../....../\.................................\./.....................-...........-............
.....|....................|.....|.......-..-.........\........|..............|...............-|.|....-/..-....
.\|............-../..\.\.....|....../...|.............\.............................................\.........
........|........../...\....-.......\..\.|-...........|./|./..........\|........--...\........\../............
\..-..-......./.\....../....\..|.........\-...-||/...-...\....\.......-.\.......-................\...\.....\..
................................\............../..-.......................-............./.....................
\............................/...........-......\..-.......-.........../............|...............|.........
......|..|........\.........................../.......-.............-.................../.....................
..........................|......|.../..........................\..................................-......-...
../...................-.........|...\...........-.|/..........|........-.-.................|.......|.........\
........................../......../..................\...........|......\.......................-............
...../..../\.../|....\....................................|....|......................|.........\...........-.
.|...../.....-.....................................|........||........|...........................//..........
........................\............|.............-..........................-......../..............|.......
...................\.........-.........\..........|......................../\...-......................\|.....
........././...........|.|..................../.....................-.............................-.|.|...-...
.-................................................-................-..\........-................../.\....|...|
.................../.|......\..\.\./.\.............|.......................................|..................
...............|............\\.....-..-.........\.....\-|..../............|\.......|.....\.\...../......\.....
...-.............../..........\.....-..|....../......................-.....\.................../.\...........\
...................-.....................................\....\....../....|..\...-................\......-....
.|..-............\........|.....|.\....\......-............\.|....|.\..............\|.|/...\.........|....../.
........../...........|.-/...\......./../.............../.\/......-/........................./......\..|......
........|......................................\..................|............................/........-.-...
...\....../................/...................\../...\\...................\.......|.................-........
.....................................|.........../.\..--.........|\..........|.....-..................|...\...
.../...........\-..-....................\.-..-..........................-........|..../.......................
.........../-.../.............../.\................-.-..\.......\/............\......../...\\\......../.......
.......-..\...\.....\..\...........................|..\.......//..................../../.........|.........-..
......./............../...../.............../............/...../.............\......-...........\...-|.......-
..|......\....................../..................\..................|.....................-....\............
../........................\............/.../...............|............-......................./............
.-...../......\............\............|..........................\../.....\.....................-|.../......
...........|.......\....\.............................\.....................--.....|......................../.
...|......./-.........................../.\......................|................|......|....|../...../......
...-............./..../................-......-....\\..|\...........|...................../....\|.\.-.....-...
............./..//...........|...\|........-/..../.............|......................../.../.................
......\........|........\...\.\........../..../..-.....-......./..|............-......................-.......
....-./............|......|........................./.........\......................................\........
...................................-.........-..../....|../...........\..-....-.|..................../...././.
-...........|.-.......|.../.................|......./.....\..\......|.................................\....\..
......\.....................-....................-......................................|.....................
................................\.......-...........|\......................./..........\-..-................|
....../......|..................\.....|....\...........\...........................................\.......|..
..............-..\..|\.../............\..\.....-.....|.|\......\...................|.........\...........-....
.-.-..\....././.........\.|...-....\....|....|............|..................../.............\................
...|..................................................-.............\../......................................
\................-....................-./.........../.......\./..\|..|....../..-\.-...\..../.....-....|.......
................|.................\|./................-..././\......../...................|....|....|.........
........-...............................................\........................\.........................../
....../..............................\....\...|........\.-........\...............|..../.........../...|......
../....................................|...|...............................\.\..|.............................
...|/|............|............/..........................\.......\.......-.../..--../...........\|...........
.......-..........\...........\/.....-./../................|.|........\\.................................-....
..|..\............\...|........../................................../...................|.\.............-.....
....-.......\/................../....|................-.././..|.........-..........\.|.........../...../..|...
/../.|..............||...\..........\....-.../.....\-..........-.......-........................-.../...\.....
../.|....|......../........-...........................\\......................\...................-..........

70
16/part1.pl Normal file
View File

@@ -0,0 +1,70 @@
:- use_module(library(pio)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Map),
propagate(Map, [0-0-3], FinalMap),
count(FinalMap, Answer),
write(Answer), nl.
count(Map, X) :-
findall(1, (nth0(_, Map, Row), nth0(_, Row, _-D), \+ D = [0,0,0,0]), Ls),
length(Ls, X).
% propagate(Map, Queue, NewMap). 'Dir' is 0/1/2/3 == light coming from N/E/S/W.
propagate(Map, [], Map).
propagate(Map, [X-Y-Dir|Queue], FinalMap) :-
( (nth0(X, Map, Row), nth0(Y, Row, Cell-Done), nth0(Dir, Done, 0))
-> replace(Dir, Done, 1, NewDone),
replace(Y, Row, Cell-NewDone, NewRow),
replace(X, Map, NewRow, NewMap),
call(Cell, X, Y, Dir, NewCells),
append(Queue, NewCells, NewQueue)
; NewQueue = Queue,
NewMap = Map
),
propagate(NewMap, NewQueue, FinalMap).
'.'(X, Y, 0, [NewX-Y-0]) :- NewX is X + 1.
'.'(X, Y, 1, [X-NewY-1]) :- NewY is Y - 1.
'.'(X, Y, 2, [NewX-Y-2]) :- NewX is X - 1.
'.'(X, Y, 3, [X-NewY-3]) :- NewY is Y + 1.
'/'(X, Y, 0, [X-NewY-1]) :- NewY is Y - 1.
'/'(X, Y, 1, [NewX-Y-0]) :- NewX is X + 1.
'/'(X, Y, 2, [X-NewY-3]) :- NewY is Y + 1.
'/'(X, Y, 3, [NewX-Y-2]) :- NewX is X - 1.
'\\'(X, Y, 0, [X-NewY-3]) :- NewY is Y + 1.
'\\'(X, Y, 1, [NewX-Y-2]) :- NewX is X - 1.
'\\'(X, Y, 2, [X-NewY-1]) :- NewY is Y - 1.
'\\'(X, Y, 3, [NewX-Y-0]) :- NewX is X + 1.
'|'(X, Y, 0, NewCells) :- '.'(X, Y, 0, NewCells).
'|'(X, Y, 2, NewCells) :- '.'(X, Y, 2, NewCells).
'|'(X, Y, 3, NewCells) :- '|'(X, Y, 1, NewCells).
'|'(X, Y, 1, [X1-Y-0, X2-Y-2]) :- X1 is X + 1, X2 is X - 1.
'-'(X, Y, 1, NewCells) :- '.'(X, Y, 1, NewCells).
'-'(X, Y, 3, NewCells) :- '.'(X, Y, 3, NewCells).
'-'(X, Y, 2, NewCells) :- '-'(X, Y, 0, NewCells).
'-'(X, Y, 0, [X-Y1-1, X-Y2-3]) :- Y1 is Y - 1, Y2 is Y + 1.
replace(I, List, Elem, NewList) :-
nth0(I, List, _, Rest),
nth0(I, NewList, Elem, Rest).
% Input stuff. Cell-(N,E,S,W) indicating which direction was already handled
input(Name, Map) :- phrase_from_file(lines(Map), Name).
lines([]) --> eos, !.
lines([Line|Lines]) --> line(Line), lines(Lines).
line([]) --> ("\n"; eos), !.
line([Char-[0,0,0,0]|Chars]) --> [C], line(Chars), {char_code(Char, C)}.
eos([], []).
% Debug stuff
print(Map) :- maplist([X]>>(write(X), nl), Map), nl.

78
16/part2.pl Normal file
View File

@@ -0,0 +1,78 @@
:- use_module(library(pio)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Map),
length(Map, Height), MaxX is Height - 1,
Map = [Row|_], length(Row, Width), MaxY is Width - 1,
findall([X-0-3], between(0, MaxX, X), S1),
findall([X-MaxY-1], between(0, MaxX, X), S2),
findall([0-Y-0], between(0, MaxY, Y), S3),
findall([MaxX-Y-2], between(0, MaxY, Y), S4),
append([S1, S2, S3, S4], Starts),
concurrent_maplist(
{Map}/[S, N]>>(propagate(Map, S, End), count(End, N)), Starts, Ns),
max_list(Ns, Answer),
nl, write(Answer), nl.
count(Map, X) :-
findall(1, (nth0(_, Map, Row), nth0(_, Row, _-D), \+ D = [0,0,0,0]), Ls),
length(Ls, X).
% propagate(Map, Queue, NewMap). 'Dir' is 0/1/2/3 == light coming from N/E/S/W.
propagate(Map, [], Map) :- write("."), flush_output.
propagate(Map, [X-Y-Dir|Queue], FinalMap) :-
( (nth0(X, Map, Row), nth0(Y, Row, Cell-Done), nth0(Dir, Done, 0))
-> replace(Dir, Done, 1, NewDone),
replace(Y, Row, Cell-NewDone, NewRow),
replace(X, Map, NewRow, NewMap),
call(Cell, X, Y, Dir, NewCells),
append(Queue, NewCells, NewQueue)
; NewQueue = Queue,
NewMap = Map
),
propagate(NewMap, NewQueue, FinalMap).
'.'(X, Y, 0, [NewX-Y-0]) :- NewX is X + 1.
'.'(X, Y, 1, [X-NewY-1]) :- NewY is Y - 1.
'.'(X, Y, 2, [NewX-Y-2]) :- NewX is X - 1.
'.'(X, Y, 3, [X-NewY-3]) :- NewY is Y + 1.
'/'(X, Y, 0, [X-NewY-1]) :- NewY is Y - 1.
'/'(X, Y, 1, [NewX-Y-0]) :- NewX is X + 1.
'/'(X, Y, 2, [X-NewY-3]) :- NewY is Y + 1.
'/'(X, Y, 3, [NewX-Y-2]) :- NewX is X - 1.
'\\'(X, Y, 0, [X-NewY-3]) :- NewY is Y + 1.
'\\'(X, Y, 1, [NewX-Y-2]) :- NewX is X - 1.
'\\'(X, Y, 2, [X-NewY-1]) :- NewY is Y - 1.
'\\'(X, Y, 3, [NewX-Y-0]) :- NewX is X + 1.
'|'(X, Y, 0, NewCells) :- '.'(X, Y, 0, NewCells).
'|'(X, Y, 2, NewCells) :- '.'(X, Y, 2, NewCells).
'|'(X, Y, 3, NewCells) :- '|'(X, Y, 1, NewCells).
'|'(X, Y, 1, [X1-Y-0, X2-Y-2]) :- X1 is X + 1, X2 is X - 1.
'-'(X, Y, 1, NewCells) :- '.'(X, Y, 1, NewCells).
'-'(X, Y, 3, NewCells) :- '.'(X, Y, 3, NewCells).
'-'(X, Y, 2, NewCells) :- '-'(X, Y, 0, NewCells).
'-'(X, Y, 0, [X-Y1-1, X-Y2-3]) :- Y1 is Y - 1, Y2 is Y + 1.
replace(I, List, Elem, NewList) :-
nth0(I, List, _, Rest),
nth0(I, NewList, Elem, Rest).
% Input stuff. Cell-(N,E,S,W) indicating which direction was already handled
input(Name, Map) :- phrase_from_file(lines(Map), Name).
lines([]) --> eos, !.
lines([Line|Lines]) --> line(Line), lines(Lines).
line([]) --> ("\n"; eos), !.
line([Char-[0,0,0,0]|Chars]) --> [C], line(Chars), {char_code(Char, C)}.
eos([], []).
% Debug stuff
print(Map) :- maplist([X]>>(write(X), nl), Map), nl.

10
16/test.txt Normal file
View File

@@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

141
17/input.txt Normal file
View File

@@ -0,0 +1,141 @@
332114151454212113454214512653223255653145556624424154456133366132437365465536664121621126441346245234461242215311265554234431413125424232423
424122453313324434224115136213521634564424342426236222533671444755341166437735256274463642146521245445525564224535224235252421321153331414212
413135332143231235543411242346121246426526353322253563266165247116731545675123735113725356164644235243456435165362223324541111145554434255422
342414155321154351141122633651163361322521331431171711732775134466255552673716661246145621631741136461146333345323514452212522243423331243533
233122133123434345314435544645431134453126541174227715665316662211115137412561563227455475361474445363563152111331443163553513522255241544413
311155314135534244231531535261532514433556673217725631772737642444271655435143741351644364131521776726224131145423362141264421135213424444312
125223544215111353343424155256123365154351463763766674147324253115775277444764475616636627512777314123411653155251461135612231241532431443541
154322542255554251655461341623214513115361722221264336433413737164732151366715314571755667734463471347653566243234254154662144543222435244125
252325122542331561151545613663525144424744652635577332743277337361111426356754123713326313135767754125231545141233212414322652635242142211243
454455312142515231552161246536164222446676131324746654761237342662724443133775752352417235142551513416366647642554343645133342433313112223215
211321313544336425166555444645646314661414271372414365632613313465526437351145461333343753254644114475234636422562331151621222261343242132553
333323312524514633125345124531441435312467416662235315752745412217643724364731152373347747421417475252352125265161445435134163426142233353211
143415113132116416143626162231614424513725134766666676741241522865823673657635526716525717332247166312254667317151665462144364126645223552411
123334222542663461264564446667515346467312356331546413675457588884644474545872866632331424544647524462746756437435461546521636115321425325224
535345333562155134222145436426256733221166524375121367788836878623525623827363242436426285231274677542316742253627612345624461262345554521442
513513214555346252356533625326334371762344355253554747668642823328845258872476637478333343444741446277731137761242516643141361443236463232215
314525145346116555121632636161213754311442177643444555582648638323545372736247584882637437532357337727176314174617322351326335323111115424155
142332123435235625634363646646665357435316213755777836867244734747572768533477723538834633673282641347553626573276175764552564465454441435441
115211236413132455511236635577446251412725323837876686385477828546337857552852634472863357843565343753573547723431623543642234642413143151542
344114335565255663416177255247156565162253555737563755723446262582337484754524764236238872364836885463427235157556711172742232315152424214355
322351245444122231555251374126727321177145723432388575878262785645433733687626542747657328463363325266561117334673213464515334316451252323323
114145243212231145266145633211575313262368663374456836668225838276887736247478522334345632464286642377734773245555273715255363521255411436125
455425264555563656334547167127221744428428767672557672528634562733434744234767632378343637854563773843778372227556167732575433414361653661645
523515321446441335631614642677743527678465286472244872478426436773365258245445623486253654722483532674422763615327372421556232261315262663663
243531332212553542512231776744326572857225588676323528368528574588687496986977437666235744834528878678885244665767573623253644364166116254113
444444112522431332452677416112527782362277632366487625237276747897354943947465889953376837548526737743346528781622556466746677544446126242133
551224351455343155254473223427545326426283637545435543486459335566488585644794556748393635283844433672572582847247363644233223765612263324266
624512252531155611465764114412153772872664238382223677844796388487978588349347899373747553577834454374823787527861133444621766341566356141365
122513321515254337477661465512352573543734686234338559996944463795463356755575964934753954988266763385387234286423462633175673633153162556456
536631433532656576652771312522342646633782573233279989744885775639388386544597879884345946975725737282827242738352524521432253763215654523115
322242133611462672545617461568524454733756842479444543488449547487568897468683955886935535694896858528355443457358667243642313225326142635412
433165134443115323655412565336574774363263823339637755748738478844753443366543756864599667497478638535364437368324232432351147613376615461324
255546365535224352467626565444446263832366733855339686844454779545954833997374664863737346696795866578234664656443247566163775227133522126211
332543346224456342457145637872588878872574934465846438696585998768959393446858386444585949749989395845243437522874257474667424163321432143662
562336531135536464571531445325786432525654973537497993963876555849853689467557948538984566573893374699522258566835263827361447616765121531241
134431331273426633773265462863548226445269397663747657533667545839948559886569784664389446348545683389848688445244623781735357464754775332441
455514441477666641215728225436682865472376638684673865686537967585959697777945778763958683986999446946735832744256768434443442331647672323162
642551661752757431115132444368754488566587583899343636597956846969766697666696549958595334986455854649598564858883536552667547462457766361136
523135446552147326745727437763658824294779583474959943837486894449889786848545858986984945998848759695488574225628248774221513366313255152244
662142534574774471476733285584773353469368338384693894458894844555764768998647478459784635737579344766346566445772776385244757261641646262231
426164675347223367356542678766487676449383835573944385678679999588547686484865465688679958436853663379986545474734678688476311743671535264562
636221515617411216247337338427225735479885464746489395486445598697885986665448699866884656794474879996386956934725254526364324475256241275663
631655547723135622724356638578268487796375786548339985946895559755594954857979446575876457888866863986588685442687887862434325557242465434334
526561675234753571754873872743282697865877868743798687568577798587569899574484554484486596454938697634387879836757377864647232634674626657136
224367751471353647528352332475353678438596388378986467455448887646465576695666985755458794657464949967358875375525855268833747673643621712113
555155247624263644685247526382749435467889975896454465589895886678995785898565848957859844445874878643778658535937624565377735216144455146652
326237275165115664286248564635235597494984939548556988958465866659979766684556496568777489668689659466945846453722248242466764614544772336545
252431512527441277652682654526537378589387973677795869974677788748496486946677468975449899885555473979374748343648488332525562346344443772553
334445255752755383853263265588439634448596738474455889458598988975979996868859879877898797766766846958879864596965328638267846232745551326455
331776676677775667763658323247548647473346994464947655845774859797686965997669898985478769844957676563749678677496544457376882437561732771415
524166432536425482552537234634638489648635668489984866757588557998759988879759786984579576979588678858478864969749575527756262527122755635643
621727645151575887576234428334879934367558864978956787976575789687695598659778579995684454959976779569644787946544773527367553271175577267671
146536267557111863546535476546756933855578657694848587666677867759855877966869976666895795977754986464958576833534434856257276637677672374622
627162456571435862223277266645437845769458457477859464885659565779885987687868765787589649788985577888968356376393555423832745685215266467517
356365425561522443354482823779863564644867657899958444967567857769895796968885877556985858769679467875486466963448536675724876383637637347332
662617714433245736446788383839477478955657876855994484679589576895957757596689789878757698854677478674545986936783367487553578825131414257161
332752445446138652863525378663559896379746684495687998757655759676957899686977876787997889474577748675663489587853377638443448848132472112131
453742717451167763344725476499465974694977569678544576559869769779787858777859878659995587547699787885775974835467735257544662734254664167234
637433522225172664387548695933565599774766649475487879985879567866859778897575697897755575969866878866559366585534438655344868386256513661571
546147626464562275655733897739395889457894577877799798775969558887665695975589685557965565588684599794655433543949864872227536744356275446534
466362637642348545833267889637543575568589757869745856956895658665988768997999655989667957999546697465848679934687374434356755244437463163527
432241752456735627523255338945585453647447657554545955769795789789997877986787567985578897759678944769468657795794576536268322838744716172531
362335524521283767267868233894954334695664994778485687755666859567896666877777856977986957755786769546887535444947538736628443346847462743523
242277366552744663233252489644883664499486849778665576798778978997766697987689796855857957659765978885748744987558355833587345887644235437312
561476674157528347537548599693576474885768577976886585698957697776687879979969876766765577555667674874858777897543556687327834848556466137735
266443423617535645753445779387753884547464964579665696566776797688777799889668699855885667595966647668866856763675976465687353888681336245316
572245756261228737774356774434644334465597956478659678566557786678987687767797677855856669659856465546998943449649767856528226362754511645257
765454637722232528732727987873694678786897686957758658776958776876669697776896797985686878577656659767548457559453559953286436834721254612345
715574444332523722776764939533778537899944495848987975789655786679777697866668686698596879975965745866774945968874839792274752686441541427615
345426643746658264878244879834877886854849486559776688568989799999769977869688867896566869679855957757568478643556858683764734437851357452275
155264155457638423874255465346979399784764447766558558566789767877968796988868867775556888798564684589499477496386865936552684743662561437775
556553777462428352772346536564948445645848965477587585797665668668998889899669897695759889687557667557584743537744477835564286662643223525755
641634624577764727436225779978964774555745645897796877559888677666767998968896788888689868597664985685858554356376665498623746875723662152372
273464717234862288588878479738499968679778769844967858766886688999898688976778797877666987697667878994996657354955977482446733867376656676652
551452257437387582588858645358387357746754779495965859595599878879877987668898767897997855656868855556757747486357487665847763562466422346611
563771242132683432222557436749937783648584568946588665665565698788787787998889676985866968796874695585557699853673538872377582853855131713461
621754271237543666875247946859748999855959977745986585788867866697696868789869768995576776679784564654855987787897358797454368778485716675345
164557236272472546822232846668399389759849987667659785676559867986867888878979688978688695876854496969468763957695738688223564746777531171265
242346713471432688725345548968585493977887799777998877589665998798978979687788866756996567759999475865446943768859868732555538748467434116221
724542325744422353827368477965435394957587656594657956589558869678779879978997989779557567777576965957979596983484379373273827325462765413315
134467152455573564745453678895499973399455464974855888996756898968898666769686667565559598686586858797879648838954569323385654838463315425344
677644725227283525233274374448443847867587946496756687655559798668967889678966568857686688687474797656795664978587359585475743444537567171372
336331514454173343536876798555466989966569868897467756888959675759587897868888779865575958968754759769695636436637498444722662454277255276665
245372163442533628423276455934688944899667958898697795685589868988659597757695978985568755897588876768955385979973858688872756554467423674135
251472373172465268687323637558467886966997454586679595656555799786788685987977899567657589878679656758649535738838795357634278627632741221151
165461424121142643385468264846788537647578965477849879999585588788675566556988859776968656866748767857679947367996538366526226283326214137262
552347532473355256876326459866563676749894796886745598856867798578859675999778699689958599985994868557485944545796355254658552388764762637577
411635777264744624762842767953597944883658696686489566856859985666787779576968666779658795444747587477754964887759644474728635835454465116123
525225774221277357825768273779533699948575746956759694897959969678678956879656896656877599855484649666456453586543836385766273838732235171565
413446335411722332567467583938598777437966488478684886785797667899976899669555696776775778568978567578544555855646936463366758257267556526367
232225126611437885288888224674796768654898896446868958777899786966689959778798898557578584894674968778348499974537656632752837285262221612562
312524227137512563356783624765555493696544874684948745994767775568987755855875779575947858745547754676685368397837548474384853754524257634625
164576266612155222885335524447458943676659664764845487449595699877699967897599959997454474895848957865889443963557382768878244863267411634354
343423173533275137862288876573755898763474747556889894445589898998965697755557969845649449784567454999596856689395535336738467324251433443313
324642722572242663573535885523747435878468838854477747966756946868979567855589567654976484995966698365838477364437873477732253364252715313424
633536551523473523243244283326385679995773658874569756469784895788454454466967676647864944477548686957835935734344472886742338655365237743545
126254446714213213436657334784236857866388445465965986588698754599946596568588549887968594867664769467935498667776266236524482164321464331744
632122261674245423458774627724266986994837445876996574698444995879887876557659858469744965457586679894948948458686783436673327143364537263243
465244311333114323725266442638884838436463333596776867596749549964879559765764848754756989656759354743483867679752528468865454711412437437215
261415356334624227658332247423732949399345845987468987484698858676956555788758758658656776848894849855333685652263222425756836554766646272443
521646365566326414152826643636235965753596433593485485995795654765798865954954774954854696879635646973347786334722533446758821117733417454123
252365462474552424777626563728632557857897556978697654768788568859684765969498588674456548639599966588966387644635645878744172627175772722511
322111677544534553154787663277355446646868855664534468569956798568597597797985774677475959647634668749666469868462232582736411761171527151611
612342244172321553733876453434327274768778644399663358375677775959999689866565698576678735898955879849469468533635274885381342774377452254623
651234151461371123426165832588863365476947993573539948648464444454868478568755779686456634875633856784657866623534635827356542713775643236154
426422164365351275264187473582686768433977556986656756547883959898696745877867456753466554348833684967659438745566626826373666115341437512356
251224616536545656545722482674755228883867595576787346349774537744798879665975566954395436673954998843688762228354424347472522235531376331326
335541164543114144761463764542372366287647948934896437773435546559497746964557357657989773374388363578827574482885664583611615165136533323225
261331415374732264655453647768254758542843666899665464693864936867468988485548354636569486458753866966873563534677586636515154572216163145356
313232345646561617413414322476366687653335889569688478593764445835478384478864579889578586636387865692637456447655326361775525421537351443252
354422624313221353436553153273228525734583687565383399578579988847365867834335367495554595854533343462877332872884662534165677211161214245532
542143423513624235434762263462784556783874775748777548864766786566666483439848384735573868946946858786326267558486871376536352777136111223641
563426221453513247534322123144884253486238658666956647949649676497797648994455393587476533436988875438823554657527344561756544414561541432165
142324116651521772664345223478653548388638657544743867958975353393743498698444676463749436355523368338437534752654673372352117774565612455142
521252364664151234171376543732684442243322874775475946689463578554843486635378489996373438934348323722334326883767125435113461472456453544434
366456342146455776343221412213427758323428237676455365546898955778897855479458369598384867528536647227825337245572327613615476534655616364436
456262313325465615634626471552134825254874668258282367564793649864648953456359464348433437556572444858443732283547165236722572136426516552255
215214555215121531363153365156745726838285476672588458468636756485794378474346453448633724336222848523788322461363427143622643764542154516631
445435243132162412737127746252357518384582434632727743323776457988789744747556744546232245626842323687453554237454472415624653454464312353566
413512155566552553344114742623626673426573822753222774627337845846626888678573728525426326742682452368847826463175721614615753615514521215131
121335422224363443354722134157661571538463676346636753673773667735542264247284835668253673727235386467366265255634467667311151641335353324222
124223214542653663566665314176475124517877488374766545574687862278742556456474375457627645727273436443464373242226321257155151623466354226535
224342321546661552253517421437534637164144462465386448554748656436426565223525648357863358822784435247243523427371615216772115551454466464444
152151341446215313532274524113276662527567472536387628785437566568247675623344644462557726773438776443134255637564367716346225361466131111152
441131264554242642424433222772464422143526743636387232724335464526838833628275276362774824878543373332675711255316334646112165166145436225355
222223346255612511131365546422132224534414716137367462626727462537235434658525224753354265433542656353255435456465556334646626646616114541252
122254524154564644355156315711257331373433572663242435353634872647446868386788243424223288834522715673434774222724734714143326426614126132222
512124425315656654512332445376341167415722616144652756353244273445678654828624424884532254424714514364625764637237362662611534611466615252341
144321153255241633164514444655277754717616621362745215268637475632553485326364864874882263216537664313267663452313553535362633636242154324325
215522151121146641113121366265573756214663745551174365226877835824533746627332877664426625513336257243467577673364646532514644421421452544444
335313335414162252122432222633336424453112573546364534773326574784656537542222587263555442637622533151434567664221616224322511431465543243151
554515254153443453616316365142667412367456471655714423572426445365634244714461517246763535321157222422452237455335224412113513116341541551245
242342552224414663454122443156612324424462742634773552556733543127575614737735454524176471342351442215257671354234543465566226442453421214332
423332221231152525435143325351441332245166165545746441225161776522365226425741716113757265346324464231517554455663426161163531133151125441514
424112342444153352442235323533255342273475361517511641775633616543162217257766644611574212127347362451473365646541415632262325644422215452323
535123215132355441452363223224326111142177116662677154564514756736116151566423375123522315721736346462721611643433515643226113553235433255245
224211552121124334331251616112654554353552524371574216334214517671732613731631733667464726623341735667366531311523356541525225553551524513511
133135253512342521415314125243161342522122244673535175332435771431335676512526131661651773762677772541211631242452526663421451534253432541324
332331532553424153415425566412652656345152461311365447431514137764335267535631223555617762772366325564623566653643641356522511155115525413323
124122513322542451532212635122623231342565341643435327641416177253251451471617514752111736553524423266211413554453514154155451335251224234321
114322223311332542521324344432242254215112631121532433351616521421133465333644152516113467545556154614445514451441162115222213523543445151233

132
17/part1.pl Normal file
View File

@@ -0,0 +1,132 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
% :- table cost/3.
% :- table to_key/2.
main([FileName|_]) :-
input(FileName, Map),
solve(Map, N, Gs, Width, Height),
dict_pairs(Gs, _, GList), transpose_pairs(GList, Gx), max_member(Max-_, Gx),
visualize(Width, Height, Gs, Max, OutMap),
print(OutMap),
write(N), write(" - "), write(Max), nl.
solve(Map, N, EndGs, Width, Height) :-
length(Map, Height), EndX is Height - 1,
Start1 = 0-0-horz, Start2 = 0-0-vert,
to_key(Start1, K1), to_key(Start2, K2), Gs = cost{}.put(K1, 0).put(K2, 0),
Map = [Row0|_], length(Row0, Width), EndY is Width - 1,
heur(0-0, EndX-EndY, Heur0),
list_to_heap([Heur0-Start1, Heur0-Start2], Queue),
astar(Map, Gs, Queue, EndX-EndY, N, EndGs).
from_key(Key, X-Y-Dir) :-
X is Key div 10000,
Y is (Key mod 10000) div 10,
(Key mod 2 =:= 0 -> Dir = horz; Dir = vert).
replace(I, List, E, NewList) :- nth0(I, List, _, R), nth0(I, NewList, E, R).
display(0, " ", _).
display(G, "▗ ", Max) :- 0 < G, G =< (Max div 4).
display(G, "▝▖", Max) :- (Max div 4) < G, G =< 2*(Max div 4).
display(G, "▞▖", Max) :- 2*(Max div 4) < G, G =< 3*(Max div 4).
display(G, "▞▞", Max) :- 3*(Max div 4) < G, G =< 4*(Max div 4).
display(G, "▟▞", Max) :- 4*(Max div 4) < G, G =< 5*(Max div 4).
display(G, "▟▛", Max) :- 5*(Max div 4) < G, G =< 6*(Max div 4).
display(G, "█▛", Max) :- 6*(Max div 4) < G, G =< 7*(Max div 4).
display(G, "██", Max) :- 7*(Max div 4) < G.
put_g(Key-G, OldMap, NewMap) :-
from_key(Key, X-Y-_),
nth0(X, OldMap, OldRow), nth0(Y, OldRow, OldG), NewG is G + OldG,
replace(Y, OldRow, NewG, NewRow),
replace(X, OldMap, NewRow, NewMap).
visualize(Width, Height, Gs, Max, OutMap) :-
length(Row, Width), maplist(=(0), Row),
length(Map, Height), maplist(=(Row), Map),
dict_pairs(Gs, _, GList),
foldl(put_g, GList, Map, GMap),
maplist(
{Max}/[OldRow, NewRow]>>(
maplist(
{Max}/[G, Unicode]>>(
display(G, Unicode, Max)), OldRow, NewRow)),
GMap, OutMap).
astar(_Map, Gs, PQueue, DestX-DestY, N, Gs) :-
get_from_heap(PQueue, _, X-Y-From, _), X = DestX, Y = DestY,
to_key(DestX-DestY-From, FromKey), N = Gs.FromKey, !.
astar(Map, Gs, PQueue, DestX-DestY, N, EndGs) :-
get_from_heap(PQueue, _, X-Y-From, PQueueAfterPop),
to_key(X-Y-From, CurrentKey), CurrentG = Gs.CurrentKey,
findall(To-Cost, next(Map, X-Y-From, To, Cost), Neighbors),
foldl(add_neighbor(CurrentG, DestX-DestY),
Neighbors, PQueueAfterPop-Gs, NewPQueue-NewGs),
astar(Map, NewGs, NewPQueue, DestX-DestY, N, EndGs).
add_neighbor(CurrentG, DestX-DestY, (X-Y-Dir)-Cost, HeapIn-GsIn, HeapOut-GsOut) :-
NewGCandidate is CurrentG + Cost,
to_key(X-Y-Dir, Key), ExistingG = GsIn.get(Key, 9999999),
( NewGCandidate < ExistingG
-> NewG = NewGCandidate, GsOut = GsIn.put(Key, NewG),
heur(X-Y, DestX-DestY, Heur), Weight is NewG + Heur,
( get_from_heap(HeapIn, _, X-Y-Dir, _), HeapOut = HeapIn
; add_to_heap(HeapIn, Weight, X-Y-Dir, HeapOut)
)
; GsOut = GsIn, HeapOut = HeapIn
).
to_key(X-Y-horz, Key) :- Key is 0 + Y*10 + X* 10000.
to_key(X-Y-vert, Key) :- Key is 1 + Y*10 + X* 10000.
next(Map, X-Y-horz, NextX-Y-vert, Cost) :-
LowX1 is X - 3, HighX1 is X - 1,
LowX2 is X + 1, HighX2 is X + 3,
(between(LowX1, HighX1, NextX); between(LowX2, HighX2, NextX)),
vert(Map, X-Y, NextX, Cost).
next(Map, X-Y-vert, X-NextY-horz, Cost) :-
LowY1 is Y - 3, HighY1 is Y - 1,
LowY2 is Y + 1, HighY2 is Y + 3,
(between(LowY1, HighY1, NextY); between(LowY2, HighY2, NextY)),
horz(Map, X-Y, NextY, Cost).
vert(Map, X-Y, NextX, Cost) :-
cost(Map, NextX-Y, _),
findall(
Ci,
( (X < NextX -> between(X, NextX, Xi); between(NextX, X, Xi)),
Xi =\= X,
cost(Map, Xi-Y, Ci)),
Costs),
sum_list(Costs, Cost).
% Cost = Costs.
horz(Map, X-Y, NextY, Cost) :-
cost(Map, X-NextY, _),
findall(
Ci,
( (Y < NextY -> between(Y, NextY, Yi); between(NextY, Y, Yi)),
Yi =\= Y,
cost(Map, X-Yi, Ci)),
Costs),
sum_list(Costs, Cost).
% Cost = Costs.
heur(X1-Y1, X2-Y2, Dist) :- Dist is abs(X1 - X2) + abs(Y1 - Y2).
cost(Map, X-Y, Cost) :- nth0(X, Map, Row), nth0(Y, Row, Cost).
print(Map) :- maplist([X]>>(atomic_list_concat(X, N), write(N), nl), Map).
input(Name, Map) :- phrase_from_file(lines(Map), Name).
lines([]) --> eos, !.
lines([Nums|Lines]) -->
line(Line), lines(Lines),
{maplist([C, N]>>(N is C - 48), Line, Nums)}.
line(Nums) --> digits(Nums), "\n".

132
17/part2.pl Normal file
View File

@@ -0,0 +1,132 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
% :- table cost/3.
% :- table to_key/2.
main([FileName|_]) :-
input(FileName, Map),
solve(Map, N, Gs, Width, Height),
dict_pairs(Gs, _, GList), transpose_pairs(GList, Gx), max_member(Max-_, Gx),
visualize(Width, Height, Gs, Max, OutMap),
print(OutMap),
write(N), write(" - "), write(Max), nl.
solve(Map, N, EndGs, Width, Height) :-
length(Map, Height), EndX is Height - 1,
Start1 = 0-0-horz, Start2 = 0-0-vert,
to_key(Start1, K1), to_key(Start2, K2), Gs = cost{}.put(K1, 0).put(K2, 0),
Map = [Row0|_], length(Row0, Width), EndY is Width - 1,
heur(0-0, EndX-EndY, Heur0),
list_to_heap([Heur0-Start1, Heur0-Start2], Queue),
astar(Map, Gs, Queue, EndX-EndY, N, EndGs).
from_key(Key, X-Y-Dir) :-
X is Key div 10000,
Y is (Key mod 10000) div 10,
(Key mod 2 =:= 0 -> Dir = horz; Dir = vert).
replace(I, List, E, NewList) :- nth0(I, List, _, R), nth0(I, NewList, E, R).
display(0, " ", _).
display(G, "▗ ", Max) :- 0 < G, G =< (Max div 4).
display(G, "▝▖", Max) :- (Max div 4) < G, G =< 2*(Max div 4).
display(G, "▞▖", Max) :- 2*(Max div 4) < G, G =< 3*(Max div 4).
display(G, "▞▞", Max) :- 3*(Max div 4) < G, G =< 4*(Max div 4).
display(G, "▟▞", Max) :- 4*(Max div 4) < G, G =< 5*(Max div 4).
display(G, "▟▛", Max) :- 5*(Max div 4) < G, G =< 6*(Max div 4).
display(G, "█▛", Max) :- 6*(Max div 4) < G, G =< 7*(Max div 4).
display(G, "██", Max) :- 7*(Max div 4) < G.
put_g(Key-G, OldMap, NewMap) :-
from_key(Key, X-Y-_),
nth0(X, OldMap, OldRow), nth0(Y, OldRow, OldG), NewG is G + OldG,
replace(Y, OldRow, NewG, NewRow),
replace(X, OldMap, NewRow, NewMap).
visualize(Width, Height, Gs, Max, OutMap) :-
length(Row, Width), maplist(=(0), Row),
length(Map, Height), maplist(=(Row), Map),
dict_pairs(Gs, _, GList),
foldl(put_g, GList, Map, GMap),
maplist(
{Max}/[OldRow, NewRow]>>(
maplist(
{Max}/[G, Unicode]>>(
display(G, Unicode, Max)), OldRow, NewRow)),
GMap, OutMap).
astar(_Map, Gs, PQueue, DestX-DestY, N, Gs) :-
get_from_heap(PQueue, _, X-Y-From, _), X = DestX, Y = DestY,
to_key(DestX-DestY-From, FromKey), N = Gs.FromKey, !.
astar(Map, Gs, PQueue, DestX-DestY, N, EndGs) :-
get_from_heap(PQueue, _, X-Y-From, PQueueAfterPop),
to_key(X-Y-From, CurrentKey), CurrentG = Gs.CurrentKey,
findall(To-Cost, next(Map, X-Y-From, To, Cost), Neighbors),
foldl(add_neighbor(CurrentG, DestX-DestY),
Neighbors, PQueueAfterPop-Gs, NewPQueue-NewGs),
astar(Map, NewGs, NewPQueue, DestX-DestY, N, EndGs).
add_neighbor(CurrentG, DestX-DestY, (X-Y-Dir)-Cost, HeapIn-GsIn, HeapOut-GsOut) :-
NewGCandidate is CurrentG + Cost,
to_key(X-Y-Dir, Key), ExistingG = GsIn.get(Key, 9999999),
( NewGCandidate < ExistingG
-> NewG = NewGCandidate, GsOut = GsIn.put(Key, NewG),
heur(X-Y, DestX-DestY, Heur), Weight is NewG + Heur,
( get_from_heap(HeapIn, _, X-Y-Dir, _), HeapOut = HeapIn
; add_to_heap(HeapIn, Weight, X-Y-Dir, HeapOut)
)
; GsOut = GsIn, HeapOut = HeapIn
).
to_key(X-Y-horz, Key) :- Key is 0 + Y*10 + X* 10000.
to_key(X-Y-vert, Key) :- Key is 1 + Y*10 + X* 10000.
next(Map, X-Y-horz, NextX-Y-vert, Cost) :-
LowX1 is X - 10, HighX1 is X - 4,
LowX2 is X + 4, HighX2 is X + 10,
(between(LowX1, HighX1, NextX); between(LowX2, HighX2, NextX)),
vert(Map, X-Y, NextX, Cost).
next(Map, X-Y-vert, X-NextY-horz, Cost) :-
LowY1 is Y - 10, HighY1 is Y - 4,
LowY2 is Y + 4, HighY2 is Y + 10,
(between(LowY1, HighY1, NextY); between(LowY2, HighY2, NextY)),
horz(Map, X-Y, NextY, Cost).
vert(Map, X-Y, NextX, Cost) :-
cost(Map, NextX-Y, _),
findall(
Ci,
( (X < NextX -> between(X, NextX, Xi); between(NextX, X, Xi)),
Xi =\= X,
cost(Map, Xi-Y, Ci)),
Costs),
sum_list(Costs, Cost).
% Cost = Costs.
horz(Map, X-Y, NextY, Cost) :-
cost(Map, X-NextY, _),
findall(
Ci,
( (Y < NextY -> between(Y, NextY, Yi); between(NextY, Y, Yi)),
Yi =\= Y,
cost(Map, X-Yi, Ci)),
Costs),
sum_list(Costs, Cost).
% Cost = Costs.
heur(X1-Y1, X2-Y2, Dist) :- Dist is abs(X1 - X2) + abs(Y1 - Y2).
cost(Map, X-Y, Cost) :- nth0(X, Map, Row), nth0(Y, Row, Cost).
print(Map) :- maplist([X]>>(atomic_list_concat(X, N), write(N), nl), Map).
input(Name, Map) :- phrase_from_file(lines(Map), Name).
lines([]) --> eos, !.
lines([Nums|Lines]) -->
line(Line), lines(Lines),
{maplist([C, N]>>(N is C - 48), Line, Nums)}.
line(Nums) --> digits(Nums), "\n".

13
17/test.txt Normal file
View File

@@ -0,0 +1,13 @@
2413432311323
3215453535623
3255245654254
3446585845452
4546657867536
1438598798454
4457876987766
3637877979653
4654967986887
4564679986453
1224686865563
2546548887735
4322674655533

604
18/input.txt Normal file
View File

@@ -0,0 +1,604 @@
R 2 (#3e6b80)
U 6 (#2b1de3)
R 4 (#1465b0)
U 2 (#549d23)
R 6 (#aa9992)
U 6 (#27e7f3)
R 7 (#aa9990)
U 6 (#5a3933)
R 5 (#1465b2)
U 5 (#23ade3)
R 4 (#49b5b2)
U 6 (#39ad53)
R 7 (#54c4f2)
U 5 (#491513)
R 9 (#4f5960)
U 5 (#357be3)
R 3 (#4f2140)
U 6 (#258b43)
R 4 (#4f9570)
U 3 (#1ea371)
R 8 (#7be6c0)
U 5 (#67bd61)
R 5 (#a31240)
U 6 (#6bf7d1)
R 5 (#a31242)
U 4 (#3464a1)
R 3 (#7be6c2)
U 4 (#33f091)
R 5 (#5c0530)
U 6 (#4c4e11)
R 2 (#611e90)
U 3 (#128231)
R 6 (#5ecef2)
D 9 (#6eb251)
R 5 (#5e54d2)
U 9 (#1e8081)
R 3 (#73a8b0)
U 10 (#6dc731)
R 3 (#84d470)
U 2 (#6b91a1)
R 7 (#64edf0)
U 5 (#4da881)
R 10 (#710072)
D 5 (#4aa4f1)
R 7 (#4dd192)
D 6 (#4aa4f3)
R 3 (#2af062)
D 6 (#6fabe1)
R 5 (#177cc0)
U 5 (#26ba61)
R 5 (#5a3aa0)
U 9 (#3b5401)
R 5 (#012590)
U 2 (#747f11)
R 4 (#753b32)
U 7 (#965a31)
R 4 (#753b30)
U 3 (#20c281)
R 5 (#225830)
U 4 (#7a4da1)
R 4 (#716560)
U 6 (#036ca1)
R 5 (#55fd00)
D 4 (#064ad1)
R 4 (#93a480)
D 6 (#411d21)
R 3 (#990d00)
D 6 (#4af4a1)
R 3 (#0f8880)
D 4 (#0f56c3)
R 3 (#7f2590)
U 7 (#0f56c1)
R 7 (#374e40)
U 6 (#4af4a3)
R 2 (#161a20)
U 3 (#5277d1)
R 5 (#7c6760)
U 4 (#528313)
R 4 (#525840)
U 4 (#267643)
L 10 (#525842)
U 5 (#501273)
L 4 (#447950)
D 10 (#69cdc3)
L 3 (#530a00)
U 10 (#590643)
L 5 (#243450)
D 5 (#74a853)
L 6 (#7c2a42)
U 5 (#1d9673)
L 3 (#3f8d62)
U 8 (#606b43)
L 5 (#576480)
D 8 (#63d243)
L 5 (#86ad60)
U 4 (#63d241)
L 7 (#0be9f0)
U 8 (#4b0801)
L 2 (#64f9c0)
U 4 (#478081)
R 9 (#20b940)
U 4 (#a60cd1)
L 9 (#02c6a0)
U 5 (#0d8be1)
L 4 (#8226e0)
D 6 (#013a81)
L 3 (#5cf860)
D 4 (#985951)
L 5 (#162d72)
D 2 (#1c2c31)
L 2 (#654f82)
D 4 (#51b4e1)
R 10 (#401a32)
D 5 (#32c291)
L 7 (#84ff82)
D 2 (#25cf41)
L 3 (#43b090)
D 4 (#6208d1)
L 3 (#a428a0)
U 9 (#6208d3)
L 3 (#07d9c0)
U 9 (#6e8a01)
L 2 (#3ab640)
U 3 (#187711)
L 4 (#162d70)
U 6 (#8e5a81)
L 6 (#049580)
D 6 (#a81333)
L 2 (#342030)
D 3 (#233073)
L 7 (#510730)
D 4 (#15cbf3)
R 3 (#507520)
D 5 (#3be881)
R 6 (#52f2a0)
D 3 (#a52711)
L 3 (#0d7680)
D 5 (#1db6c3)
L 5 (#5f0450)
U 5 (#6c6d73)
L 2 (#47abc0)
D 5 (#707733)
L 5 (#771be2)
D 2 (#151c53)
L 3 (#413da2)
D 3 (#768703)
R 11 (#640b02)
D 2 (#37b343)
R 4 (#4ac8d0)
D 5 (#016421)
L 2 (#907990)
D 5 (#016423)
L 3 (#412220)
U 5 (#0f5603)
L 9 (#5e11d0)
D 5 (#24abb1)
L 7 (#1ab552)
U 2 (#06e641)
L 2 (#9e07e2)
U 4 (#06e643)
L 6 (#51f692)
U 4 (#763581)
L 8 (#3716a0)
U 4 (#1360d1)
R 5 (#567bc0)
U 2 (#222831)
R 9 (#7d2160)
U 6 (#72b991)
L 6 (#4a6840)
U 3 (#3ef773)
L 4 (#626a10)
U 4 (#4c3d93)
L 4 (#6fa5e2)
D 4 (#6e0b33)
L 4 (#6fa5e0)
U 3 (#38aaf3)
L 5 (#223552)
U 5 (#025c33)
L 4 (#4205c2)
D 4 (#49ddd3)
L 3 (#3aa782)
D 4 (#028fd3)
L 5 (#76e842)
D 3 (#028fd1)
L 3 (#9bc962)
U 4 (#6bde73)
L 9 (#9d6be0)
U 6 (#2db831)
L 5 (#1192c0)
U 3 (#6b5c33)
L 4 (#152940)
U 4 (#746923)
L 5 (#8648a0)
U 3 (#4cb8d1)
L 3 (#877170)
U 5 (#24ca21)
L 4 (#877172)
D 3 (#6e4261)
L 2 (#691310)
D 6 (#655001)
R 6 (#7e7740)
D 3 (#1562b1)
L 6 (#261a00)
D 4 (#700e91)
L 4 (#6ade10)
U 9 (#4a1251)
L 3 (#5a7c00)
U 7 (#5a30e1)
L 6 (#15a832)
U 3 (#152e01)
L 3 (#643622)
U 5 (#977db1)
L 7 (#4b7bc2)
U 2 (#546691)
L 7 (#8cd5a2)
U 7 (#5b7a31)
R 3 (#6c85f2)
U 6 (#6996a1)
R 8 (#458692)
U 3 (#370e51)
R 6 (#7bc6d2)
U 4 (#07aa01)
R 4 (#389dc2)
U 6 (#3de6d3)
R 6 (#54ece2)
U 5 (#625703)
R 10 (#0e9512)
D 5 (#1e8793)
R 3 (#873d92)
U 4 (#2eb4b3)
R 5 (#2d3432)
U 5 (#26b2f1)
R 2 (#2c8c52)
U 3 (#2ff4c1)
R 7 (#0e68f2)
U 3 (#32b371)
R 8 (#0e68f0)
U 4 (#5d6fb1)
R 3 (#128622)
U 5 (#06af41)
R 6 (#a95192)
U 4 (#274af1)
R 3 (#3af302)
U 6 (#50fff3)
R 6 (#3f9cf2)
U 2 (#50fff1)
R 3 (#4734a2)
U 3 (#1a5051)
L 12 (#986ce0)
U 3 (#224b61)
R 9 (#2d66c0)
U 4 (#6f2a31)
R 6 (#0aba80)
U 7 (#0ecb81)
R 4 (#606870)
U 3 (#0ecb83)
L 3 (#4e78e0)
U 3 (#4563f1)
L 11 (#8bed00)
U 3 (#3298c1)
L 5 (#5a39d0)
U 2 (#51d641)
L 8 (#8e6ee0)
U 4 (#233851)
R 4 (#31c7a0)
U 9 (#728831)
R 3 (#670cd0)
U 4 (#1f3621)
L 7 (#8df230)
U 4 (#44a361)
R 7 (#2aed30)
U 7 (#2a0161)
R 4 (#1db2c0)
U 3 (#6387c1)
R 3 (#1886b0)
D 3 (#128c21)
R 3 (#acb330)
D 3 (#3a9d01)
L 4 (#3658a0)
D 6 (#00ec51)
R 4 (#682712)
D 6 (#2440e1)
R 5 (#45f812)
D 9 (#6298d1)
R 3 (#778f22)
U 4 (#6298d3)
R 2 (#271462)
U 10 (#0d5ff1)
L 4 (#6a69e2)
U 5 (#0d5ff3)
R 6 (#24b5e2)
U 3 (#2440e3)
R 5 (#5f3192)
U 4 (#41c313)
R 3 (#236852)
U 7 (#6e15c1)
R 8 (#5187b2)
D 5 (#6e15c3)
R 2 (#65a3d2)
D 4 (#41c311)
R 11 (#3dfa02)
D 4 (#26ca51)
R 5 (#4e3912)
D 6 (#797803)
R 5 (#2f8ee2)
U 5 (#2a9b83)
R 8 (#3d6702)
D 5 (#4646e3)
R 3 (#3d6700)
D 2 (#43a1c3)
R 4 (#592f22)
D 3 (#9d61c1)
R 4 (#1f90f2)
D 5 (#172261)
R 7 (#5a7ba2)
D 8 (#797801)
R 6 (#4e4ad2)
D 8 (#28fda1)
R 5 (#55a862)
D 4 (#28fda3)
R 5 (#1665a2)
D 6 (#3fca41)
R 9 (#850700)
D 3 (#43f8f1)
R 3 (#3cbd90)
D 6 (#620311)
R 8 (#70b2b2)
D 6 (#25cb11)
R 3 (#336c52)
D 4 (#8da331)
R 4 (#336c50)
D 5 (#0594c1)
R 3 (#4a2b92)
U 8 (#31cac1)
R 5 (#392742)
D 8 (#233743)
R 6 (#798b32)
D 4 (#233741)
R 6 (#4fcde2)
D 4 (#1ffd91)
L 9 (#207ed2)
U 5 (#9314d1)
L 9 (#056d22)
D 5 (#56c131)
L 9 (#431340)
D 6 (#94bbb1)
R 8 (#357120)
D 2 (#19ca71)
R 8 (#8bebb0)
D 6 (#4257c1)
R 8 (#0c4fc0)
D 3 (#380131)
R 3 (#07a260)
D 3 (#02a841)
L 11 (#100a10)
D 2 (#07ad11)
L 4 (#4a2b90)
D 3 (#10d091)
R 6 (#609d22)
D 2 (#69a281)
R 9 (#320ed2)
D 5 (#0782c1)
R 2 (#67c382)
D 2 (#19a311)
R 7 (#14e152)
D 3 (#4ee351)
R 5 (#14e150)
D 8 (#54dae1)
R 3 (#428ba2)
D 5 (#0a5431)
R 3 (#1880e2)
D 2 (#2fc091)
R 9 (#440bc0)
D 6 (#758b11)
R 6 (#6a54b0)
D 3 (#31f8d1)
L 6 (#37e190)
D 5 (#0c4af1)
L 4 (#6acc62)
D 2 (#161df1)
L 4 (#72f272)
D 4 (#161df3)
L 7 (#088332)
D 4 (#182831)
L 8 (#82d720)
D 3 (#371df1)
L 7 (#7207b0)
D 6 (#206d61)
R 4 (#0c1082)
D 2 (#796721)
R 6 (#220252)
D 7 (#2948a1)
R 7 (#5f2c02)
U 7 (#137d21)
R 3 (#0fe252)
D 2 (#0164d1)
R 6 (#1ce912)
D 6 (#4e0983)
R 2 (#7d7c72)
D 11 (#598c93)
R 4 (#581202)
D 8 (#0ffba3)
L 4 (#500e32)
D 2 (#42bcf3)
R 4 (#31e852)
D 8 (#2d8913)
R 4 (#1d67d2)
D 5 (#5a03c3)
R 9 (#45c0b0)
D 4 (#3a1e53)
L 9 (#687430)
D 4 (#3a1e51)
L 4 (#25b1b0)
D 8 (#613e03)
L 7 (#4ea210)
D 8 (#366193)
L 3 (#4cb7c2)
D 8 (#452333)
L 6 (#675322)
D 6 (#6ae223)
L 3 (#0d88b2)
D 2 (#23a4a3)
L 8 (#60f512)
D 5 (#568a53)
L 3 (#0cbd02)
D 3 (#4bad03)
L 4 (#588672)
D 4 (#72dfa3)
L 2 (#960472)
D 9 (#394403)
L 5 (#28bcc2)
D 6 (#4eb073)
L 6 (#04e992)
D 3 (#22c803)
L 3 (#a76142)
D 5 (#22c801)
R 10 (#171512)
D 3 (#25f2f3)
R 6 (#581600)
D 7 (#3d0f33)
L 8 (#142a40)
D 4 (#6eb893)
L 5 (#142a42)
U 4 (#02a243)
L 9 (#522a40)
U 2 (#4d2a01)
L 4 (#081760)
D 7 (#17c601)
R 4 (#081762)
D 6 (#497a01)
R 5 (#2fbb50)
D 3 (#4be663)
R 4 (#3b3140)
D 2 (#48f803)
R 3 (#69ec82)
U 3 (#80c993)
R 5 (#3f05f2)
U 8 (#5d5463)
R 4 (#50d1c2)
D 3 (#472973)
R 3 (#1b68a2)
D 5 (#5ebab3)
R 7 (#464fd2)
D 3 (#64c0a1)
L 7 (#4062d2)
D 6 (#8e33b3)
L 5 (#1c3382)
D 6 (#8e33b1)
L 4 (#50b882)
D 4 (#64c0a3)
L 2 (#2bcfb2)
D 7 (#84fc61)
L 3 (#413452)
U 10 (#84fc63)
L 3 (#47ad42)
U 2 (#03ba03)
L 3 (#7197b2)
U 5 (#81aa33)
L 8 (#5c0552)
D 5 (#235433)
L 5 (#4236f2)
D 5 (#4e9bc3)
L 5 (#3740a0)
D 2 (#1e7f23)
L 10 (#3740a2)
U 3 (#5f66a3)
R 4 (#4236f0)
U 7 (#164f83)
R 4 (#5d1a30)
U 3 (#1192e1)
R 4 (#4c67a0)
U 5 (#954a91)
L 8 (#3348c0)
U 3 (#306c01)
L 4 (#694200)
U 8 (#52c163)
L 3 (#6abee0)
U 3 (#26c123)
L 4 (#6abee2)
U 4 (#4c3413)
L 9 (#4e8800)
U 5 (#1192e3)
L 3 (#2bb700)
U 4 (#21ceb1)
L 12 (#4ec360)
U 3 (#21ceb3)
L 4 (#32f940)
U 8 (#5d5f73)
L 4 (#1dcf10)
U 9 (#3cf413)
L 3 (#7cc870)
U 8 (#5f0953)
L 4 (#376c70)
U 6 (#01d623)
L 5 (#697e50)
U 5 (#99e693)
L 5 (#6f63b0)
U 3 (#576c53)
R 10 (#7fa030)
U 3 (#1fd933)
L 5 (#4eb2d2)
U 6 (#39a1f3)
L 4 (#3f0452)
D 4 (#42ffd3)
L 3 (#5b9d52)
U 7 (#25c2d3)
L 8 (#5ba250)
D 7 (#40d763)
L 5 (#5ba252)
D 3 (#5f2a73)
L 7 (#477ce2)
D 3 (#2652d1)
L 5 (#453770)
D 5 (#758781)
L 11 (#453772)
D 3 (#937381)
L 2 (#119e62)
D 5 (#131891)
R 5 (#3285e2)
U 3 (#211fd3)
R 11 (#8349d2)
U 4 (#211fd1)
R 4 (#3246c2)
U 3 (#2515f3)
R 9 (#5a5bd0)
D 7 (#6fbf83)
R 4 (#6a5680)
D 3 (#6fbf81)
R 9 (#329240)
D 7 (#750063)
R 5 (#5e71f2)
D 9 (#3358d3)
L 6 (#1d2652)
D 4 (#6a8ee3)
L 3 (#4e9cd2)
U 6 (#3e8623)
L 2 (#3607d2)
U 5 (#379d43)
L 6 (#3cf342)
U 3 (#853b63)
L 3 (#737102)
D 3 (#1de1b3)
L 2 (#66ff82)
D 10 (#634163)
L 3 (#2eff02)
U 6 (#2ac343)
L 2 (#528a42)
U 7 (#4d81d3)
L 3 (#2ea1e2)
D 5 (#19e063)
L 6 (#8b5a82)
D 7 (#4b8e33)
L 4 (#5e41d0)
D 2 (#159903)
L 5 (#31f3c0)
D 9 (#537013)
L 2 (#31f3c2)
D 7 (#4b7b43)
L 4 (#5bd890)
D 10 (#1be163)
L 5 (#0c3340)
D 3 (#540d83)
L 9 (#2449f0)
U 6 (#9411c1)
L 7 (#766db0)
U 7 (#9411c3)
L 4 (#37ee30)
D 3 (#516ad3)
L 3 (#0991b0)
D 2 (#98a733)
L 5 (#0ea042)
D 3 (#513f83)
L 2 (#a0c682)
D 8 (#616323)
L 4 (#a0c680)
D 3 (#4555b3)
L 3 (#79e032)
U 9 (#673de3)
L 4 (#5904b0)
U 7 (#495bd3)
L 3 (#5904b2)
U 2 (#49a633)
L 5 (#76a9c2)
U 10 (#10de23)

27
18/part1.pl Normal file
View File

@@ -0,0 +1,27 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Moves),
area(Moves, Area),
write(Area), nl.
area(Moves, Area) :-
area(Moves, 0, A1), MainArea is abs(A1),
convlist([r-N, N]>>(true), Moves, Horzs), sum_list(Horzs, Horz),
convlist([u-N, N]>>(true), Moves, Verts), sum_list(Verts, Vert),
Area is MainArea + Horz + Vert + 1.
area([], _, 0).
area([u-Len|Moves], H, Area) :- NewH is H + Len, area(Moves, NewH, Area).
area([d-Len|Moves], H, Area) :- NewH is H - Len, area(Moves, NewH, Area).
area([r-Len|Moves], H, Area) :- area(Moves, H, Area2), Area is Area2 + H*Len.
area([l-Len|Moves], H, Area) :- area(Moves, H, Area2), Area is Area2 - H*Len.
input(FileName, Moves) :- phrase_from_file(moves(Moves), FileName).
moves([]) --> eos, !.
moves([Move|Moves]) --> move(Move), moves(Moves).
move(DirAtom-Len) -->
alpha_to_lower(Dir), " ", number(Len), " (#", xdigits(_Color), ")", ("\n"; eos),
{char_code(DirAtom, Dir)}.

31
18/part2.pl Normal file
View File

@@ -0,0 +1,31 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Moves),
area(Moves, Area),
write(Area), nl.
area(Moves, Area) :-
area(Moves, 0, A1), MainArea is abs(A1),
convlist([r-N, N]>>(true), Moves, Horzs), sum_list(Horzs, Horz),
convlist([u-N, N]>>(true), Moves, Verts), sum_list(Verts, Vert),
Area is MainArea + Horz + Vert + 1.
area([], _, 0).
area([u-Len|Moves], H, Area) :- NewH is H + Len, area(Moves, NewH, Area).
area([d-Len|Moves], H, Area) :- NewH is H - Len, area(Moves, NewH, Area).
area([r-Len|Moves], H, Area) :- area(Moves, H, Area2), Area is Area2 + H*Len.
area([l-Len|Moves], H, Area) :- area(Moves, H, Area2), Area is Area2 - H*Len.
input(FileName, Moves) :- phrase_from_file(moves(Moves), FileName).
moves([]) --> eos, !.
moves([Move|Moves]) --> move(Move), moves(Moves).
move(DirAtom-Len) -->
alpha_to_lower(_), " ", number(_), " (#", xinteger(Hex), ")", ("\n"; eos),
{DirNum is Hex /\ 15, dir(DirNum, DirAtom), Len is Hex >> 4}.
dir(0, r).
dir(1, d).
dir(2, l).
dir(3, u).

14
18/test.txt Normal file
View File

@@ -0,0 +1,14 @@
R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)

733
19/input.txt Normal file
View File

@@ -0,0 +1,733 @@
vvr{a>520:jq,s<3691:R,a>325:A,R}
dmg{s<1730:A,x<3350:R,R}
ctv{s<3757:R,R}
xhh{a<3542:A,A}
cpx{m<1847:A,R}
qqg{a<911:nf,lpf}
mnr{a>1914:A,x>752:A,A}
ktx{m<1153:R,A}
mkz{a>2959:A,a<2378:rlr,R}
br{x>2184:A,s>3693:A,s>3530:R,A}
fpk{a<2465:A,a>2524:A,A}
ffs{a>536:A,a>328:A,s>3058:R,R}
zcl{m>2554:pz,a>2167:gr,qs}
lpf{a>1643:hc,x>3626:xxp,mth}
vl{x>2958:R,R}
nrs{m>2481:R,x>3117:A,m<1978:R,R}
sv{m<1749:R,rl}
tv{x<219:R,a<1429:A,m<2884:R,A}
nl{m>1827:A,m>1345:A,x>2928:A,R}
vj{x>439:A,s<2318:A,a<806:A,R}
kd{s>767:jhv,x<1090:bk,A}
ts{s<3148:rxp,a>1365:lr,bkt}
bl{m<2736:A,s<571:R,dv}
brp{a<923:R,a<1279:R,a<1574:A,R}
bgt{a<1575:R,s<3923:fbv,s<3953:A,pdj}
djn{s>322:A,m<1056:A,a<1988:A,A}
rqq{s<3130:A,m<2434:A,A}
lz{x>3622:A,x>3296:qt,s>3010:R,A}
pts{x>1771:R,R}
kpp{s>1217:A,s<1204:R,R}
bnq{s>3121:A,x>490:A,a>2141:R,A}
hxf{s>2885:R,a>1520:A,s>2880:A,A}
khd{s>3265:bc,a<1211:ffs,R}
gv{m>3417:A,a>2938:R,m<3354:A,R}
lk{m<2377:xk,s<3132:R,A}
cs{s<960:A,x>3655:R,R}
mh{a>1468:A,s>2418:R,x<508:vj,vgr}
cnt{x<766:R,x>1060:R,A}
zt{a>3624:R,x<1504:R,s>271:A,A}
fpt{m<1199:bnq,x<958:R,x<1153:A,mrg}
jkq{a>1783:qnp,a>1626:A,R}
fnc{x<3093:fzx,x>3571:cp,m<3233:zpz,xxl}
kmg{m<1640:lvj,x>2199:xv,R}
lkm{m>722:xbm,R}
pgd{s<2128:brp,jj}
shm{m>919:R,s<3189:R,R}
xfr{m>761:R,s>2748:A,m>323:A,R}
rgl{a<1202:R,R}
qgd{a<746:hzp,rgl}
vkj{m<2086:R,x>850:A,a>3710:R,R}
cm{s<1657:R,R}
lvj{s>2173:R,a>747:R,A}
vz{a<2716:A,R}
nsm{m<2126:A,a>1310:A,x>2771:A,A}
pk{x>2292:A,A}
jm{s<363:A,A}
hp{a>2641:trf,mh}
nlr{s>2969:A,x>1304:A,A}
hx{m<3078:R,R}
pjv{x>253:grc,x<97:R,x<193:A,hz}
xrb{x>3443:kn,A}
tx{a>1279:A,dcf}
qjb{m>1551:qfg,qrq}
ftr{s>2830:nmq,td}
jvn{s>3843:R,A}
gsz{s<2923:rfs,ts}
xhx{x<2340:lj,A}
gbn{m<882:A,A}
qvl{a>1697:hr,a<1466:A,a<1601:A,ll}
jh{m>173:R,s<564:A,s<797:A,R}
vgf{x>497:A,x>320:A,R}
lgk{a>3109:R,A}
lbx{x>1134:A,R}
nxp{s>840:A,R}
gkh{m<545:A,m>610:A,m>569:A,R}
zpz{x<3332:jk,m>2774:A,R}
sqt{x<399:R,m<3088:A,s<477:A,A}
mpf{a>3316:A,A}
ckv{m<3213:A,a<842:A,s<2396:A,A}
lpq{s<2685:A,A}
fg{m>3358:R,x>3763:A,R}
cf{a>1252:A,x>346:A,x>212:A,R}
bcv{s>3187:cxd,x>2785:R,x>2752:nsm,zn}
pq{x>2944:A,R}
vc{x>2389:cqz,m>1620:bd,bdr}
jgb{x<2002:A,R}
nfb{s<2319:A,a>3483:hj,m>1700:R,A}
zh{m<664:A,s<2311:A,A}
hrs{a<340:R,x>1096:A,R}
stl{a<1757:cfp,qrl}
lg{x<2142:R,m>1798:bx,x>3110:rg,A}
nrd{x<2026:qh,m>1735:mrk,pn}
cmk{s>292:A,A}
szf{a<996:A,x>1757:bqq,s<1243:R,A}
tg{a>989:A,s<936:R,s<1091:R,R}
ktl{x<2556:R,x<2639:A,R}
nb{m<3571:A,s>1051:R,R}
vx{a<2247:A,x<2460:A,a<2331:R,A}
in{s<2569:qj,csb}
rvc{m<1417:A,s>2822:A,m>3074:R,A}
hcv{a<3271:A,A}
blq{m<1523:mz,rq}
dvv{m<1558:rx,rn}
pdj{m>1764:R,s>3971:R,R}
tzf{s>3567:R,m<631:A,a<3273:R,R}
vvp{a<2271:nrc,m>2025:ls,a>3004:znl,ftr}
dx{s>3049:qdv,m>1362:hpq,m<759:A,xrl}
fgb{a>3186:pq,s>3045:A,rz}
zfr{s<3704:pg,a<1503:kkc,a>1606:rj,mcm}
hk{s<3670:A,a<2667:R,R}
vbs{m>2005:jgb,pts}
mmr{m>2322:R,m>1991:A,s>1195:A,R}
jsj{a<846:R,A}
gr{x>1637:A,m>1437:A,tzf}
smr{x>553:xkj,a>3828:pjv,s<2877:rvn,lk}
kkh{a<979:A,a<1290:R,m<3262:R,R}
rfs{s<2782:zfq,s>2869:rlj,vvp}
hld{m<2626:R,R}
pdr{s>3716:R,x<1984:A,R}
bpl{m>2642:R,m<2208:A,R}
rsb{s>2186:A,klj}
svh{m>1897:A,a<3228:A,s<2449:trr,A}
nvc{a>1611:sfr,A}
kx{x>637:A,x>614:A,A}
qh{x>1954:pdr,sl}
mr{x>2233:R,x>2163:A,R}
ndm{a<2304:R,A}
fb{m>2610:A,m>1344:A,A}
qm{m>2943:R,a>2109:R,R}
dv{a<678:A,R}
nbv{a<1299:R,m>2259:R,s>2404:A,R}
ls{a>2854:mpf,zqx}
xc{m>2101:R,s<2882:R,a>1165:A,R}
lct{m<2076:bs,s>2785:mkf,m>2855:A,bks}
dk{a<3506:A,A}
qmn{x>325:qf,a>449:kl,a<295:A,vm}
ncg{a<1018:lct,a<1371:rnd,x<2673:jkq,nvc}
zmq{m<2525:R,A}
thd{a>2757:gj,a<2405:qrn,m>2056:bmz,kr}
tts{m<3228:R,s>640:R,a>3171:A,A}
trr{a>3548:R,s<2437:R,R}
clc{s<2527:R,s>2549:A,A}
jtv{m<2950:A,s<210:R,R}
ghv{m>1136:xkd,s<1218:ql,m<502:gvn,szf}
jq{s<3727:A,m<1398:R,A}
ql{x<2601:lbx,dz}
rvn{x<189:vvb,hxn}
xx{x>2241:A,m>1897:A,R}
ccd{s<3621:A,R}
lts{s<3872:A,m>835:A,s<3931:R,A}
rn{s>925:R,R}
ngb{s<2880:A,R}
xxv{a<2094:A,s<2881:R,R}
kr{x>2351:A,s>258:R,a>2619:bdp,A}
mz{x<3454:fgb,zzp}
hd{a>2529:A,dmg}
hz{x>224:R,m<2084:R,A}
gh{m<971:A,m>1211:R,A}
jr{a>2700:R,m>1234:A,x<355:A,A}
jc{x<739:A,a<917:R,s<2457:ck,mnr}
bpj{x>330:rqq,R}
xfj{s<1260:ht,m<2092:jd,xhx}
bz{s<3823:A,R}
cxd{a<874:A,R}
bs{s>2688:A,m>1007:A,x<2651:A,R}
htn{m>977:R,a<2308:A,x<2327:A,A}
jhv{a>996:R,s<967:A,s<1075:R,A}
bcr{a<3147:ndm,tp}
pkj{m<2510:gbn,a>981:jjx,A}
zzp{s>2986:shm,x>3786:rpv,mb}
bc{s>3370:A,a<923:R,x>2579:R,A}
tj{x<2848:rb,s>1693:A,A}
fvl{s>1450:css,s>1274:fr,a>1541:ngh,rmz}
frc{m<2984:A,m<3157:sqt,tts}
lb{m>1612:A,A}
bp{m>2003:A,x>3015:cs,tg}
nnl{a<1000:R,s<3854:A,s>3945:A,R}
rl{m<3088:A,x<463:A,a>1631:A,R}
gvn{s>1250:A,s>1235:fhj,fqn}
rnf{m<733:R,x<1676:R,A}
cp{a<3134:fg,m>3460:A,kmc}
pd{s>637:vb,s<564:pt,R}
rz{x<3098:R,m>826:R,m<293:A,A}
zc{a>689:vmd,a<339:A,llk}
zl{s<3450:R,A}
rj{s<3855:A,m<1825:lcq,x>558:qn,R}
tp{a<3484:R,x>2024:R,a>3761:A,R}
jbz{m<3184:cnt,R}
fs{s<1772:A,m>908:nl,a>2631:A,A}
sfr{a>1871:A,a<1714:A,R}
lv{a>3338:A,s<314:R,x>2326:R,R}
hjj{m<1335:R,m>2001:R,a<2651:R,A}
sm{x>2614:R,m<621:A,A}
qdv{x<2058:R,R}
xsg{m>2409:qb,x<1022:bg,mgj}
vvb{x<121:A,m<1386:R,R}
szz{x>1786:xs,s<2260:stl,x<596:qg,zk}
sr{m>1474:A,gbk}
xxp{s<2328:A,x<3808:A,A}
fxk{s>3196:A,btj}
mgg{m>2309:R,a<2169:R,x<1003:jr,R}
rc{x>853:R,x>554:R,x<304:tk,A}
vzk{a>830:A,m<1128:kh,x>558:R,npn}
dd{x<2364:R,hk}
jvf{s<1220:kkh,m<3274:qgd,a>869:tx,dxb}
xn{a<1700:rjg,A}
vf{a>1341:A,s>3229:R,A}
dxb{m>3699:R,s<1248:R,m<3525:sml,A}
zd{s>1372:cbd,zc}
qb{x>1140:R,a>3588:R,a<3369:A,R}
ck{s>2389:R,x>753:A,m>2076:A,R}
bk{m<1112:A,x>422:A,x<254:R,A}
bdp{s>119:R,m<885:A,R}
xfq{m>1857:km,a<2650:rvk,m<1229:svp,fnm}
jjx{a<1105:R,A}
hh{x>2723:A,x>2324:R,R}
vxv{s<3855:A,a<1221:R,A}
hj{m<1924:A,A}
zj{a>2161:A,a<1948:mmr,a>2043:A,R}
lcq{m<760:A,R}
zmm{a>1990:vx,m>3237:nq,m<2768:R,zrk}
qrn{m>1714:qm,a>2144:htn,djn}
zk{a<2467:lkf,txv}
gsm{a>2592:sm,m<700:A,a>2099:R,R}
vhm{m>3185:R,m>2612:A,x<2721:R,R}
lss{a>1698:dd,ldr}
gf{a<589:ggp,a>940:xc,A}
qrl{a<2989:mgg,s>2068:rsb,x>881:xjq,cnz}
nz{x>640:zl,m<654:A,gh}
ljc{m<2348:xqn,m<3307:hvc,zpt}
ptz{a<1956:kmk,x>332:ks,dxr}
lj{s>1269:A,A}
rjr{a>1905:A,s<3610:A,R}
bkt{s<3288:xm,gkz}
jjq{s<2376:R,a<3384:R,m>3288:A,A}
fzx{x<2811:A,s<786:R,sjr}
hcz{a<1441:A,A}
ng{x>2025:A,a<1962:R,m>2393:A,A}
kg{x<1581:R,x<1788:R,R}
hc{a<1965:A,m<2176:R,R}
zrk{x>2229:A,R}
cgp{x>715:A,m>3614:A,R}
vmd{s>1327:R,A}
jsk{x>2685:bcv,m>1553:qgx,m>700:khd,mx}
kkc{m<1471:hcz,a>1447:A,x>594:A,tv}
ph{s<2709:R,a>615:A,R}
bqq{m>918:A,s<1255:A,x>2567:R,R}
dn{a>3209:zt,fc}
vm{x<119:R,a<356:R,A}
bvz{s<3732:zcl,fvj}
vgr{x>557:A,s>2325:R,A}
kl{m>1917:R,a>623:R,s<3697:R,A}
css{x<2079:flp,a>2235:vp,ttl}
qsf{s>458:rfh,s<225:A,x<1109:jm,A}
jjl{a<1765:qv,x>2506:tf,x>1208:zv,ljc}
xr{s>1663:A,R}
zqx{m<3112:A,R}
mm{x<2642:A,A}
bmm{a>1287:vf,m>2723:cf,tpd}
bx{s>1379:R,m<3154:A,a>2029:A,R}
hb{s>3578:A,a>1601:A,R}
npn{a>418:R,R}
gn{a<1162:dxm,a<1473:lz,xrb}
pj{x>3259:bgb,m<2105:rqc,hn}
cfp{m<1402:pgd,nn}
rx{a>2279:R,m<809:R,s<927:A,A}
dj{s<2516:R,R}
bj{s<3719:fls,pj}
ttl{a<753:djc,s>1593:tj,qr}
zp{m<862:A,R}
hzp{a<266:R,A}
hnb{m<729:A,m>1432:A,m>1081:A,R}
dfv{s<899:R,m>1776:bss,A}
gx{x<420:R,s<739:A,x>875:A,R}
lp{m>1603:A,x<2185:A,R}
khp{m<1240:R,s<3695:R,x>2040:R,A}
dxr{s<2453:A,x>300:A,dj}
rqc{m>859:mrq,a>1818:jvn,m<375:A,A}
cbf{a>1801:A,x<2146:R,A}
dxm{a<741:A,x<3647:lvx,s>3125:zm,cpx}
zdf{x>918:hrs,s<3499:bpj,x<510:qmn,vvr}
rk{a<1622:kjn,x>2035:lx,R}
mkf{s<2843:R,m>3082:R,R}
tpd{s>3176:R,a>1225:R,x>394:R,A}
tzq{m>2428:ngb,vl}
qj{s<1179:jjl,s>1877:szz,fvl}
xm{s<3237:R,a<738:A,A}
mf{a>1552:A,s<2401:R,R}
rlr{x>3280:A,x>2759:R,R}
hxn{a>3691:R,s<2707:R,s<2802:R,A}
lt{a>3090:R,s<71:A,A}
xk{a>3660:R,A}
zqm{a>928:A,A}
qs{m>1136:R,rnf}
jl{m<3203:A,m>3526:R,s>3891:A,A}
lx{a<1729:A,x>2216:A,m>1412:A,R}
gc{m<3522:R,a<2626:A,R}
pzc{a<3024:vtg,m>1682:R,m<1377:A,rrf}
gbk{m<654:R,s<1217:A,s<1223:R,A}
lvx{a<927:R,m>2149:R,x>3327:A,A}
fhj{s<1242:R,A}
xqn{m<896:pv,x<621:hq,pzc}
hq{s<661:cr,a>3031:R,s<1000:R,A}
xtz{s>2914:A,s>2909:R,R}
mpv{s>3459:bj,a>2014:blq,tgr}
rmz{m<2196:ghv,jvf}
fc{a<2548:kg,s>166:R,a<2930:A,lt}
rpv{x>3861:R,a<3150:R,s<2725:R,R}
nq{a>1505:R,m<3509:R,R}
qz{a<1558:R,m<3088:A,m>3640:R,R}
pg{a<1524:cdc,s>3406:A,m<1803:R,R}
nn{m<2341:fm,s>2091:nnd,x>667:R,A}
qnp{a<1882:A,s<2689:R,A}
kb{x<1708:A,A}
km{m>3254:R,x>2185:R,s>3646:cbf,R}
qhp{x<1902:bvz,ksc}
tf{m>2474:fnc,lh}
mrk{m>2540:gth,nh}
btj{a<3349:R,x>877:A,R}
cv{s>1227:ss,sr}
dlr{m<1789:gsm,m>2852:rtx,a>2963:bxj,zj}
xbm{s<3216:R,A}
cx{a>1657:R,x>1871:R,A}
cb{a<2526:R,A}
llk{m>2126:A,A}
hjb{m>3625:R,m>3514:R,A}
bq{a<1187:pkj,m<1393:lkm,x<695:bmm,svf}
fn{x>151:R,m<2679:A,A}
vp{a>2870:ff,m>2322:hd,s<1637:vjv,fs}
rlj{s>2896:qd,a>2284:zdx,a<1323:gf,jg}
ggp{m<1794:R,s<2883:A,x<1856:R,A}
znl{m>856:A,a<3666:hcv,sf}
svp{x>2155:xhh,a<3541:ctv,s<3726:ccd,bz}
mgj{x<1387:A,x<1600:R,R}
lkf{x>1081:qk,x>777:jf,x<686:mrc,jc}
rjg{a<1443:R,x>1229:R,s<1556:R,A}
mbg{a<622:A,x<834:A,x<867:R,R}
bss{a<3054:A,x>2187:A,a<3492:R,A}
xkd{a<562:kpp,R}
qr{s<1530:jdd,x<3305:R,R}
qfg{a<2318:R,fn}
jf{x>919:A,a>935:A,mbg}
bg{s>2516:R,A}
cbd{m>2449:A,a>1041:cn,a<450:R,xp}
xl{s<2925:hx,a>3207:R,R}
vrg{a>1289:hb,cj}
mtb{s>2981:hrh,A}
gfm{s<3070:R,m>3645:R,m>3357:R,R}
zfq{a<1584:js,lpq}
fbv{a<2481:A,x>1644:A,R}
rxp{a<1460:dx,bcr}
dlj{a>3193:A,x>2205:A,A}
nrc{a<941:rvc,s>2824:R,cx}
vjv{a<2589:R,s<1539:A,s<1591:A,R}
prg{x>943:R,x>469:R,a>1828:A,R}
cn{s>1408:A,m>1058:R,A}
hvc{x<696:frc,x<972:tt,qsf}
flp{a>2225:fq,a<1241:fj,s<1733:xn,prg}
bkf{m<1041:A,A}
dl{a>1330:R,s<2427:R,x<1467:A,A}
sn{m>3136:gfm,grr}
rfh{x>1129:R,A}
td{m>1204:R,m>773:A,a<2619:R,A}
jg{x<1834:A,a<1719:hxf,x<2218:ng,xxv}
jrj{a<386:A,R}
nv{m>210:R,R}
pjx{s<2086:mm,a<3090:fb,x>2720:R,sfp}
ll{s<2174:A,s<2409:R,R}
pn{x>2063:rjr,m>970:khp,a>1997:A,fhq}
gj{a>3526:R,a<3207:mr,lv}
nj{m<2883:A,s<3049:R,s>3494:R,cgp}
xq{a<3632:R,s>2980:R,R}
mrq{m<1543:R,m>1844:A,R}
rm{a>3252:hv,R}
fxx{a<1183:kmg,m<2218:qvl,zmm}
grr{a<2906:R,s<2997:R,m>2839:R,R}
kjn{x>2064:R,m>2516:R,x>1756:R,A}
bvd{s>3355:A,a>1525:R,R}
nf{x>3448:jrj,R}
hs{a<3135:R,s>399:R,A}
dln{s<561:mv,x>2863:nxp,s<800:A,A}
kmc{s>639:A,m>2835:R,R}
sfp{a>3626:A,x<2148:A,R}
ff{a<3401:R,a>3684:A,m>1555:hh,zp}
jz{a<3162:xt,a<3530:vq,s<3365:smr,svn}
tgr{x>3121:gn,x>2865:tzq,s>2899:jsk,ncg}
gl{s<739:pd,x>1894:dfv,a>2666:zmq,dvv}
grc{x<435:A,A}
sml{s<1264:R,m>3363:A,x<2284:A,A}
mkc{x<760:A,a<631:A,a>1386:R,kq}
xp{s>1423:A,x<2579:R,R}
vq{m>1343:fxk,nz}
mrg{m<1518:R,m<1658:R,A}
js{a>944:xg,x>2026:ph,s<2666:R,R}
kh{a<318:R,x<621:R,a<659:A,R}
rg{x>3435:R,R}
fhq{x<2040:A,a>704:R,m>551:A,R}
kq{m<2577:R,s<894:A,x<1039:A,A}
qd{m>2322:R,s<2906:A,x>1957:A,xtz}
ksc{x<2099:nrd,x>2265:lss,xfq}
kzv{a>2414:xj,lg}
kk{a<617:R,R}
jk{s>538:A,m<2863:A,R}
xg{a>1210:R,x>1982:R,x>1800:R,A}
cl{a>2733:R,clc}
bd{m<2716:A,a<3313:R,m<3413:A,fbm}
xjq{s<1984:A,m<1511:A,bpl}
rvk{a>1155:bkf,m<813:br,m<1432:A,zkh}
nd{s>452:kd,x>1163:kk,vzk}
fvj{s>3841:bgt,m<2521:bb,tz}
ks{m>2382:R,ntz}
tt{a<2850:R,A}
qgx{s>3258:zqm,s<3027:R,ktl}
ss{m>2041:A,ft}
rq{x<3120:xl,m>2642:sn,rtf}
jdd{s>1486:R,m>2101:A,m>762:A,A}
qx{a>3696:qrp,s>3657:A,R}
bks{a<607:A,R}
xs{a>2441:fh,x>2963:qqg,fxx}
mrc{m>2461:ckv,m>1428:kx,m>724:tq,A}
rb{m<2468:R,x>2419:R,x<2271:R,A}
ht{s<1252:R,A}
qk{x<1328:mf,x<1549:dl,nbv}
djc{m<1600:cm,a>283:nrs,R}
fh{s<2220:pjx,lgk}
bdr{a>3328:jfn,m>707:R,R}
rtx{x<1386:R,m>3269:gc,gb}
xkj{a>3749:A,a<3675:dtp,s<3004:pht,vkj}
pv{m>323:R,x>502:jh,s>514:nv,vz}
vv{a>3320:R,s<636:R,s>948:A,R}
sg{s>601:bp,cmk}
cr{a<2514:R,R}
gb{a<2813:A,R}
bmz{s<196:A,a<2556:fpk,s>307:pk,R}
bb{m>1091:A,R}
zdx{s>2879:R,a<3102:vk,A}
jfn{a<3606:A,A}
nbp{m>818:A,A}
kn{s>2923:R,A}
fr{a<1594:zd,a>2929:vc,kzv}
xj{m<2544:hjj,R}
hv{m>1991:R,A}
qrp{x>874:A,x>389:R,A}
cnc{m<985:A,R}
cnz{m<2037:hnb,A}
vmb{a>1700:jz,nm}
mv{m<1362:R,A}
gkz{s>3342:jsj,x>1892:xx,m<1670:R,kb}
xt{m>1817:nj,a<2551:fpt,ktx}
fj{a>463:A,A}
mx{m>253:gkh,s<3140:R,m>137:R,R}
jd{m>842:lb,m>451:kkv,s<1266:A,A}
cqz{s<1368:A,m<1575:A,R}
svf{s<3263:R,R}
sl{m<2246:A,R}
zrq{x>2077:lsm,x<1840:A,a>1618:R,bvd}
pz{m>3317:hjb,s>3590:A,x<1658:R,jvc}
csb{x<1439:vmb,x>2420:mpv,s<3414:gsz,qhp}
kmk{x>328:kpg,R}
qrq{a>1614:A,s<2403:zh,x>114:R,nbp}
fq{x>909:xr,a>2931:A,A}
lr{a>2309:rm,a>1791:vbs,s>3276:zrq,rk}
lsm{x>2277:R,a>1618:R,R}
klj{x>1054:R,m>2202:A,x>399:A,R}
mb{a<3099:R,s<2735:R,R}
fqn{m<210:R,A}
jj{s<2200:A,A}
pht{a>3714:R,A}
tz{x<1673:R,a<1378:A,A}
rrf{a>3595:A,a>3246:A,R}
nmq{a>2748:R,a<2542:R,x<2016:R,A}
fls{a<2084:vrg,mkz}
fbm{a>3544:R,A}
fnm{s>3733:dlj,lp}
zpt{s>665:xb,m>3543:hs,gv}
bxj{m>2343:R,A}
zkh{a<571:A,s>3679:A,a>921:R,A}
cdc{m>1919:A,R}
ngh{s>1238:xfj,s<1208:dlr,cv}
cj{a>700:A,R}
bgb{a>2604:dk,m>1677:jl,x>3687:lts,R}
dz{m<718:R,x<3415:R,R}
nnd{x>1061:R,m>2920:A,a>798:R,R}
hn{m<2984:A,a>2031:A,vxv}
dtp{x<945:A,a<3591:A,R}
qv{x>2355:sg,m<1752:nd,nxg}
vtg{a>2512:R,m<1657:A,x>981:R,A}
hr{x>2303:A,m<1178:A,A}
txv{s<2416:nfb,s<2487:svh,a>3134:xsg,cl}
mth{a>1241:R,A}
sf{s<2826:A,a<3808:A,a<3934:R,A}
hrh{m>2319:A,x>461:A,m>839:R,A}
tk{s>3674:A,s>3485:A,A}
ldr{m<1530:R,s<3738:hld,x<2358:rrr,nnl}
kpg{a>779:R,m>2426:R,R}
mcm{m>1928:qz,a>1571:R,a>1536:A,vgf}
kkv{s<1268:R,R}
ntz{x>354:R,R}
qg{x>390:hp,x<245:qjb,ptz}
rrr{s<3885:A,m<2398:R,R}
rtf{a>3255:xq,x>3511:A,s<3159:A,R}
ft{x>2278:R,m<1043:R,a<2670:R,R}
dcf{x>2425:A,x<1158:A,A}
vb{s<704:R,x<1816:R,R}
zv{s>491:gl,x<2068:dn,thd}
xb{s>882:nb,gx}
gth{s>3795:R,m<3296:A,A}
nxg{x>1262:bl,s>410:mkc,a>704:jtv,jbz}
rnd{x<2587:R,m<2247:xfr,vhm}
sjr{a>2727:R,A}
pt{s>532:R,R}
fm{x>631:A,x>335:A,s<2100:R,A}
gq{s>681:R,a<2732:R,m>1215:A,R}
svn{m<1967:qx,rc}
xv{s<2249:A,A}
zn{s<3051:A,x>2727:A,A}
hpq{s<2983:R,m<3115:R,A}
nh{a<1716:A,R}
qn{m>2908:R,s<3927:A,R}
lh{x<3047:dln,gq}
zch{x>1081:nlr,A}
np{x>876:zch,a<1580:mtb,sv}
tq{a>1139:A,a<709:R,R}
vk{m>1771:R,a>2624:A,R}
qt{a<1334:R,x>3407:R,R}
qf{s<3748:R,A}
zm{s<3247:R,x<3878:A,R}
xrl{a<807:A,a<1157:A,s>2999:A,R}
jvc{s<3505:R,s>3546:A,R}
trf{m>2082:jjq,a<3517:cnc,R}
nm{a<835:zdf,a<1402:bq,s>3203:zfr,np}
xxl{a<3140:cb,a<3432:vv,A}
{x=3161,m=1141,a=51,s=441}
{x=240,m=174,a=555,s=1269}
{x=639,m=25,a=2025,s=345}
{x=97,m=1410,a=1572,s=1133}
{x=763,m=1918,a=625,s=2037}
{x=83,m=1395,a=433,s=2841}
{x=359,m=652,a=831,s=129}
{x=979,m=204,a=1976,s=1629}
{x=3112,m=122,a=816,s=305}
{x=523,m=2334,a=108,s=1286}
{x=267,m=152,a=480,s=389}
{x=278,m=917,a=1419,s=2422}
{x=207,m=1337,a=600,s=282}
{x=128,m=913,a=3232,s=189}
{x=1359,m=15,a=1792,s=2028}
{x=87,m=2807,a=3202,s=104}
{x=4,m=240,a=686,s=1790}
{x=2332,m=537,a=1657,s=289}
{x=1739,m=75,a=2757,s=1320}
{x=2127,m=44,a=1389,s=224}
{x=564,m=3263,a=177,s=447}
{x=424,m=915,a=1856,s=18}
{x=632,m=1638,a=665,s=315}
{x=950,m=993,a=136,s=11}
{x=154,m=611,a=405,s=1090}
{x=1784,m=1239,a=506,s=878}
{x=379,m=332,a=89,s=1091}
{x=2234,m=315,a=1158,s=2140}
{x=971,m=283,a=56,s=1089}
{x=39,m=101,a=256,s=995}
{x=259,m=25,a=829,s=2383}
{x=2681,m=876,a=676,s=109}
{x=1783,m=2119,a=299,s=25}
{x=2735,m=1444,a=94,s=572}
{x=710,m=2673,a=372,s=1524}
{x=171,m=221,a=1344,s=234}
{x=250,m=547,a=40,s=2485}
{x=1282,m=1141,a=823,s=1862}
{x=3,m=557,a=1924,s=1310}
{x=23,m=1217,a=133,s=2587}
{x=2073,m=75,a=246,s=250}
{x=591,m=646,a=510,s=610}
{x=1057,m=1782,a=1460,s=360}
{x=715,m=2817,a=1441,s=1769}
{x=132,m=478,a=1510,s=1669}
{x=3098,m=1549,a=915,s=2968}
{x=1913,m=570,a=542,s=381}
{x=311,m=935,a=277,s=290}
{x=486,m=148,a=240,s=61}
{x=428,m=1469,a=26,s=1175}
{x=520,m=979,a=84,s=413}
{x=244,m=1671,a=2482,s=6}
{x=53,m=1426,a=3,s=202}
{x=787,m=1504,a=1281,s=1676}
{x=27,m=2265,a=1142,s=202}
{x=1876,m=170,a=2306,s=2130}
{x=699,m=48,a=228,s=634}
{x=684,m=1694,a=287,s=1244}
{x=1140,m=919,a=845,s=1543}
{x=2313,m=183,a=948,s=1856}
{x=345,m=325,a=399,s=673}
{x=758,m=79,a=523,s=1105}
{x=271,m=1196,a=611,s=1465}
{x=495,m=2296,a=543,s=873}
{x=1054,m=273,a=1040,s=975}
{x=2372,m=642,a=415,s=303}
{x=2865,m=216,a=208,s=1772}
{x=219,m=29,a=689,s=561}
{x=885,m=150,a=816,s=1035}
{x=2293,m=1052,a=546,s=527}
{x=1384,m=461,a=264,s=911}
{x=1125,m=652,a=3475,s=1492}
{x=193,m=823,a=511,s=719}
{x=3393,m=26,a=15,s=111}
{x=611,m=1012,a=114,s=839}
{x=527,m=159,a=3961,s=465}
{x=214,m=416,a=1332,s=348}
{x=21,m=1247,a=1356,s=112}
{x=3308,m=387,a=587,s=16}
{x=171,m=1571,a=2145,s=1685}
{x=1334,m=970,a=133,s=102}
{x=567,m=348,a=318,s=624}
{x=344,m=911,a=593,s=326}
{x=976,m=584,a=270,s=339}
{x=879,m=796,a=565,s=1348}
{x=1427,m=1060,a=751,s=1071}
{x=2412,m=582,a=407,s=1668}
{x=21,m=147,a=1452,s=681}
{x=457,m=404,a=1617,s=338}
{x=1810,m=1236,a=19,s=174}
{x=55,m=674,a=530,s=327}
{x=423,m=348,a=1067,s=424}
{x=2836,m=2279,a=3116,s=73}
{x=792,m=2863,a=1457,s=1643}
{x=537,m=910,a=2132,s=2005}
{x=895,m=1383,a=86,s=632}
{x=302,m=891,a=61,s=303}
{x=24,m=28,a=438,s=458}
{x=1134,m=104,a=219,s=623}
{x=208,m=1184,a=280,s=2224}
{x=1884,m=762,a=2518,s=588}
{x=1712,m=2692,a=580,s=1996}
{x=597,m=1108,a=1198,s=889}
{x=29,m=1692,a=456,s=2358}
{x=604,m=1656,a=149,s=374}
{x=995,m=1575,a=1300,s=1043}
{x=513,m=64,a=348,s=670}
{x=2016,m=148,a=288,s=240}
{x=2208,m=388,a=726,s=69}
{x=521,m=1808,a=2767,s=856}
{x=512,m=103,a=439,s=1943}
{x=148,m=935,a=787,s=477}
{x=1153,m=2337,a=238,s=3068}
{x=692,m=2215,a=1089,s=134}
{x=2260,m=1675,a=2775,s=1847}
{x=625,m=3316,a=1996,s=2999}
{x=2263,m=2373,a=2421,s=349}
{x=13,m=2202,a=316,s=132}
{x=1414,m=2836,a=2224,s=206}
{x=2597,m=203,a=741,s=1575}
{x=3420,m=1012,a=637,s=768}
{x=278,m=1222,a=1586,s=463}
{x=511,m=816,a=71,s=381}
{x=538,m=320,a=395,s=2388}
{x=522,m=741,a=2172,s=1418}
{x=112,m=619,a=1875,s=1458}
{x=462,m=2079,a=252,s=54}
{x=2828,m=121,a=297,s=19}
{x=444,m=1444,a=85,s=88}
{x=1328,m=43,a=1168,s=241}
{x=1963,m=162,a=135,s=915}
{x=350,m=994,a=223,s=3064}
{x=130,m=790,a=895,s=52}
{x=317,m=83,a=265,s=478}
{x=215,m=2935,a=703,s=2731}
{x=1274,m=1313,a=455,s=2270}
{x=18,m=1971,a=1987,s=1992}
{x=2444,m=646,a=1462,s=232}
{x=1054,m=44,a=241,s=1037}
{x=212,m=1389,a=1490,s=574}
{x=301,m=3887,a=1190,s=300}
{x=3405,m=2067,a=2279,s=976}
{x=503,m=1631,a=933,s=303}
{x=1877,m=2443,a=2986,s=2055}
{x=559,m=1073,a=2167,s=1286}
{x=2473,m=1614,a=1850,s=503}
{x=357,m=430,a=1105,s=15}
{x=601,m=271,a=891,s=290}
{x=1958,m=6,a=250,s=796}
{x=635,m=507,a=262,s=853}
{x=337,m=949,a=2590,s=53}
{x=3419,m=3441,a=1089,s=592}
{x=132,m=992,a=482,s=306}
{x=979,m=958,a=2428,s=572}
{x=20,m=60,a=1124,s=3558}
{x=189,m=406,a=1382,s=546}
{x=1264,m=12,a=716,s=239}
{x=916,m=2214,a=69,s=146}
{x=631,m=2307,a=1969,s=421}
{x=1277,m=864,a=1780,s=490}
{x=306,m=2550,a=53,s=2171}
{x=120,m=1296,a=474,s=301}
{x=1566,m=869,a=894,s=2050}
{x=698,m=2308,a=687,s=492}
{x=2410,m=2671,a=806,s=1228}
{x=988,m=3291,a=459,s=229}
{x=1481,m=204,a=646,s=2167}
{x=238,m=593,a=497,s=186}
{x=317,m=187,a=772,s=129}
{x=53,m=3624,a=751,s=554}
{x=503,m=59,a=699,s=2483}
{x=59,m=1139,a=442,s=334}
{x=297,m=473,a=2519,s=554}
{x=254,m=1241,a=1477,s=280}
{x=117,m=1332,a=251,s=571}
{x=543,m=2228,a=606,s=473}
{x=1241,m=822,a=1544,s=2650}
{x=656,m=665,a=2678,s=2842}
{x=111,m=1574,a=537,s=807}
{x=152,m=641,a=16,s=325}
{x=2814,m=230,a=2633,s=224}
{x=4,m=856,a=2263,s=32}
{x=1837,m=124,a=221,s=3059}
{x=320,m=311,a=1653,s=15}
{x=265,m=307,a=930,s=2960}
{x=796,m=87,a=2572,s=741}
{x=602,m=585,a=2755,s=381}
{x=455,m=441,a=610,s=1380}
{x=552,m=1554,a=1800,s=39}
{x=1707,m=170,a=92,s=789}
{x=772,m=490,a=1173,s=763}
{x=2930,m=2475,a=1873,s=1826}
{x=1571,m=572,a=1883,s=1534}
{x=2008,m=2416,a=155,s=799}
{x=2060,m=1868,a=372,s=41}
{x=1899,m=104,a=7,s=456}
{x=37,m=718,a=642,s=1394}
{x=2078,m=1001,a=2864,s=791}
{x=124,m=890,a=2174,s=2418}
{x=996,m=2173,a=318,s=2194}

54
19/part1.pl Normal file
View File

@@ -0,0 +1,54 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Workflows, Parts),
concurrent_maplist(wf(Workflows, in), Parts, Scores),
sum_list(Scores, Score),
write(Score), nl.
wf(_, accept, [x-X, m-M, a-A, s-S], Score) :- Score is X + M + A + S, !.
wf(_, reject, _Part, 0) :- !.
wf(Workflows, WorkflowName, Part, Score) :-
member(WorkflowName-Rules, Workflows),
rule(Workflows, Rules, Part, Score).
rule(Workflows, [EndRule], Part, Score) :- wf(Workflows, EndRule, Part, Score).
rule(Workflows, [Attr-Cond-N-Dest|Rules], Part, Score) :-
( satisfy(Attr-Cond-N, Part)
-> wf(Workflows, Dest, Part, Score)
; rule(Workflows, Rules, Part, Score)
).
satisfy(Attr-Cond-N, Part) :- member(Attr-M, Part), call(Cond, M, N).
% input parsing stuff below
input(FileName, Workflows, Parts) :-
phrase_from_file((workflows(Workflows), parts(Parts)), FileName).
workflows([]) --> "\n", !.
workflows([Name-Rules|Ws]) -->
string_without("{", NameStr), "{", rules(Rules), "}\n", workflows(Ws),
{atom_codes(Name, NameStr)}.
rules([End]) --> dest(End).
rules([Rule|Rules]) --> rule(Rule), ",", rules(Rules).
rule(Attr-Cond-N-Dest) --> attr(Attr), cond(Cond), number(N), ":", dest(Dest).
attr(x) --> "x".
attr(m) --> "m".
attr(a) --> "a".
attr(s) --> "s".
cond('>') --> ">".
cond('<') --> "<".
dest(reject) --> "R", !.
dest(accept) --> "A", !.
dest(Dest) --> endrule(Dest).
endrule(Rule) --> string_without(",}", RuleStr), {atom_codes(Rule, RuleStr)}.
parts([]) --> eos, !.
parts([[x-X, m-M, a-A, s-S]|Parts]) -->
"{x=", number(X), ",m=", number(M), ",a=", number(A), ",s=", number(S),
"}\n", parts(Parts).

83
19/part2.pl Normal file
View File

@@ -0,0 +1,83 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Workflows),
findall(
Count-Limit,
(
wf(Workflows, in, Route, Limit),
Limit = [x-Xmin-Xmax, m-Mmin-Mmax, a-Amin-Amax, s-Smin-Smax],
Count is (Xmax - Xmin)*(Mmax - Mmin)*(Amax - Amin)*(Smax - Smin),
write(Route), write(" - "), write(Limit), write(" - "), write(Count), nl
),
Limits),
length(Limits, LenLimits), write("lenlimits = "), write(LenLimits), nl,
maplist([Count-Limit, Count]>>(true), Limits, CountsOnly),
sum_list(CountsOnly, Answer), write("answer = "), write(Answer), nl,
true.
choices(Limit, N) :-
foldl([_-Min-Max, V0, V]>>(V is V0*(Max - Min + 1)), Limit, 1, N).
or_limits([Limit], Limit).
or_limits([Limit1, Limit2 | Limits], Limit) :-
findall(
Attr-Min-Max,
( member(Attr-Min1-Max1, Limit1), member(Attr-Min2-Max2, Limit2),
Min is min(Min1, Min2), Max is max(Max1, Max2)),
NewLimit),
or_limits([NewLimit|Limits], Limit).
% x -> [Min, Max)
wf(_, accept, [accept], [x-1-4001, m-1-4001, a-1-4001, s-1-4001]).
wf(Workflows, WorkflowName, [WorkflowName|Route], Limits) :-
\+ WorkflowName = accept, \+ WorkflowName = reject,
member(WorkflowName-Rules, Workflows),
with_rule(Workflows, Rules, Route, Limits).
with_rule(Workflows, [EndRule], Route, Limits) :-
wf(Workflows, EndRule, Route, Limits).
with_rule(Workflows, [Attr-Cond-N-Dest|Rules], Route, NewLimits) :-
% Either take the first route with its limit
( wf(Workflows, Dest, Route, Limits),
member(Attr-Min-Max, Limits),
combine(Min-Max, Cond-N, NewMin-NewMax)
% ...or skip the first route, given we satisfy its reverse limits
; with_rule(Workflows, Rules, Route, Limits),
member(Attr-Min-Max, Limits),
negate(Cond-N, NotCond-NotN),
combine(Min-Max, NotCond-NotN, NewMin-NewMax)
),
select(Attr-Min-Max, Limits, Attr-NewMin-NewMax, NewLimits).
negate('<'-N, '>'-NewN) :- NewN is N - 1.
negate('>'-N, '<'-NewN) :- NewN is N + 1.
combine(Min-Max, '<'-N, Min-NewMax) :- NewMax is min(N, Max), Min < NewMax.
combine(Min-Max, '>'-N, NewMin-Max) :- NewMin is max(N+1, Min), NewMin < Max.
% input parsing stuff below
input(FileName, Workflows) :-
phrase_from_file((workflows(Workflows), remainder(_)), FileName).
workflows([]) --> "\n", !.
workflows([Name-Rules|Ws]) -->
string_without("{", NameStr), "{", rules(Rules), "}\n", workflows(Ws),
{atom_codes(Name, NameStr)}.
rules([End]) --> dest(End).
rules([Rule|Rules]) --> rule(Rule), ",", rules(Rules).
rule(Attr-Cond-N-Dest) --> attr(Attr), cond(Cond), number(N), ":", dest(Dest).
attr(x) --> "x".
attr(m) --> "m".
attr(a) --> "a".
attr(s) --> "s".
cond('>') --> ">".
cond('<') --> "<".
dest(reject) --> "R", !.
dest(accept) --> "A", !.
dest(Dest) --> endrule(Dest).
endrule(Rule) --> string_without(",}", RuleStr), {atom_codes(Rule, RuleStr)}.

17
19/test.txt Normal file
View File

@@ -0,0 +1,17 @@
px{a<2006:qkq,m>2090:A,rfg}
pv{a>1716:R,A}
lnx{m>1548:A,A}
rfg{s<537:gd,x>2440:R,A}
qs{s>3448:A,lnx}
qkq{x<1416:A,crn}
crn{x>2662:A,R}
in{s<1351:px,qqz}
qqz{s>2770:qs,m<1801:hdj,R}
gd{a>3333:R,R}
hdj{m>838:A,pv}
{x=787,m=2655,a=1222,s=2876}
{x=1679,m=44,a=2067,s=496}
{x=2036,m=264,a=79,s=2244}
{x=2461,m=1339,a=466,s=291}
{x=2127,m=1623,a=2188,s=1013}

58
20/graphviz.dot Normal file
View File

@@ -0,0 +1,58 @@
hb -> mj
mx -> mt, NANDxz
xh -> qc
tg -> cq
kp -> NANDxz, nj
mj -> NANDjj, lv
cq -> jm
mt -> sj, NANDxz
NANDjj -> hb, lz, NANDrk, xv, vj, vh, lv
rm -> NANDbz, xq
hx -> NANDbz
xv -> lz
xx -> kp, NANDxz
pt -> vx
NANDxz -> bq, gr, sj, rv, NANDzf
vx -> NANDgf, cv
xb -> NANDxz, bq
xk -> NANDgf, rd
lv -> zk
NANDrk -> NANDgh
kn -> NANDgf, tz
NANDgh -> rx
sj -> vp
jm -> vm, NANDbz
rr -> rv, NANDxz
tz -> rz
gg -> kn
NANDcd -> NANDgh
qc -> kh, NANDbz
kb -> NANDgf
vp -> NANDxz, xx
fb -> NANDbz, tg
rd -> cp
qn -> vh, NANDjj
xr -> NANDjj
tp -> rm, NANDbz
cp -> gg
NANDbz -> NANDqx, cq, xh, fb, tg
qq -> pt, NANDgf
xq -> NANDbz, hx
gx -> NANDjj, qv
bq -> rr
cv -> NANDgf, kb
zk -> NANDjj, xv
NANDzf -> NANDgh
NANDqx -> NANDgh
vh -> gx
qv -> xr, NANDjj
lz -> qn
broadcaster -> fb, xk, gr, vj
nj -> NANDxz
gr -> NANDxz, xb
kh -> tp, NANDbz
vm -> NANDbz, xh
rz -> qq, NANDgf
NANDgf -> tz, NANDcd, rd, xk, pt, cp, gg
rv -> mx
vj -> hb, NANDjj

58
20/input.txt Normal file
View File

@@ -0,0 +1,58 @@
%hb -> mj
%mx -> mt, xz
%xh -> qc
%tg -> cq
%kp -> xz, nj
%mj -> jj, lv
%cq -> jm
%mt -> sj, xz
&jj -> hb, lz, rk, xv, vj, vh, lv
%rm -> bz, xq
%hx -> bz
%xv -> lz
%xx -> kp, xz
%pt -> vx
&xz -> bq, gr, sj, rv, zf
%vx -> gf, cv
%xb -> xz, bq
%xk -> gf, rd
%lv -> zk
&rk -> gh
%kn -> gf, tz
&gh -> rx
%sj -> vp
%jm -> vm, bz
%rr -> rv, xz
%tz -> rz
%gg -> kn
&cd -> gh
%qc -> kh, bz
%kb -> gf
%vp -> xz, xx
%fb -> bz, tg
%rd -> cp
%qn -> vh, jj
%xr -> jj
%tp -> rm, bz
%cp -> gg
&bz -> qx, cq, xh, fb, tg
%qq -> pt, gf
%xq -> bz, hx
%gx -> jj, qv
%bq -> rr
%cv -> gf, kb
%zk -> jj, xv
&zf -> gh
&qx -> gh
%vh -> gx
%qv -> xr, jj
%lz -> qn
broadcaster -> fb, xk, gr, vj
%nj -> xz
%gr -> xz, xb
%kh -> tp, bz
%vm -> bz, xh
%rz -> qq, gf
&gf -> tz, cd, rd, xk, pt, cp, gg
%rv -> mx
%vj -> hb, jj

82
20/part1.pl Normal file
View File

@@ -0,0 +1,82 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Circuit),
run1000(Circuit, Answer),
write(Answer), nl.
% part1
run1000(Circuit, Answer) :-
length(Range, 1000),
foldl(
{Circuit}/[_, In-Lin-Hin, Out-Lout-Hout]>>(
run([button-l-broadcaster], In, Out, L-H),
Lout is L+Lin, Hout is H+Hin),
Range, Circuit-0-0, _-Lx-Hx),
Answer is Lx*Hx.
% code to run one circuit
run([], Circuit, Circuit, 0-0).
run([Src-Level-Target|Pulses], CircuitIn, CircuitOut, Ls-Hs) :-
( member(Type-Target-State-Dests, CircuitIn)
-> /*write([Src, Level, Target]), write(" -> "),
write([Type, Target, State, Dests]), write(" = "),*/
call(Type, Src, Level, State, NewState, Out),
send(Target, Out, Dests, AdditionalPulses),
% write(NewState-AdditionalPulses), nl,
select(Type-Target-State-Dests, CircuitIn,
Type-Target-NewState-Dests, Circuit1),
append(Pulses, AdditionalPulses, NewPulses),
run(NewPulses, Circuit1, CircuitOut, NextLs-NextHs)
; run(Pulses, CircuitIn, CircuitOut, NextLs-NextHs)
),
call(Level, NextLs-NextHs, Ls-Hs).
l(L1s-H1s, L2s-H1s) :- L2s is L1s + 1.
h(L1s-H1s, L1s-H2s) :- H2s is H1s + 1.
broadcaster(_, l, x, x, l).
ff(_, h, State, State, none).
ff(_, l, 0, 1, h).
ff(_, l, 1, 0, l).
nand(Src, Level, State, NewState, OutLevel) :-
select(Src-_, State, Src-Level, NewState),
(maplist([_-h]>>(true), NewState) -> OutLevel = l; OutLevel = h).
send(_, none, _, []).
send(From, Level, Dests, Pulses) :-
\+ Level = none,
maplist({Level}/[Dest, From-Level-Dest]>>(true), Dests, Pulses).
% input initialization
prefill_nands([], Circuit, Circuit).
prefill_nands([_-Src-_-Dests|Nodes], CircuitIn, CircuitOut) :-
convlist(
{CircuitIn}/[Dest, Dest]>>(member(nand-Dest-_-_, CircuitIn)),
Dests, NandDests),
foldl(fill_one_nand(Src), NandDests, CircuitIn, Circuit1),
prefill_nands(Nodes, Circuit1, CircuitOut).
fill_one_nand(Src, Nand, CIn, COut) :-
select(nand-Nand-State-Dests, CIn, nand-Nand-[Src-l|State]-Dests, COut).
% input parsing stuff below
input(FileName, Circuit) :-
phrase_from_file(modules(EmptyCircuit), FileName),
prefill_nands(EmptyCircuit, EmptyCircuit, Circuit).
modules([]) --> eos, !.
modules([Module|Modules]) --> module(Module), "\n", modules(Modules).
module(broadcaster-broadcaster-x-Dests) --> "broadcaster -> ", dests(Dests).
module(ff-Name-0-Dests) --> "%", node(Name), " -> ", dests(Dests).
module(nand-Name-[]-Dests) --> "&", node(Name), " -> ", dests(Dests).
dests([Dest]) --> node(Dest).
dests([Dest|Dests]) --> node(Dest), ", ", dests(Dests).
node(Name) --> string_without(", \n", NameStr), {atom_codes(Name, NameStr)}.

82
20/part2.pl Normal file
View File

@@ -0,0 +1,82 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
% This code was only used to confirm the theory. Don't run it. Look at part2.txt.
run_until(Circuit, _, Node, State, N) :- member(_-Node-State-_, Circuit), write(N), nl, !.
run_until(Circuit, Init, Node, State, N) :-
maplist(extract(Circuit), [xr, qv, gx, vh, qn, lz, xv, zk, lv, mj, hb, vj], S1),
maplist(extract(Circuit), [nj, kp, xx, vp, sj, mt, mx, rv, rr, bq, xb, gr], S2),
maplist(extract(Circuit), [hx, xq, rm, tp, kh, qc, xh, vm, jm, cq, tg, fb], S3),
maplist(extract(Circuit), [kb, cv, vx, pt, qq, rz, tz, kn, gg, cp, rd, xk], S4),
write(N), write(": "),
string_codes(Str1, S1), string_codes(Str2, S2), string_codes(Str3, S3), string_codes(Str4, S4),
write(Str1), write(" "), write(Str2), write(" "), write(Str3), write(" "), write(Str4), nl,
run([Init], Circuit, CircuitNext),
NextN is N + 1,
run_until(CircuitNext, Init, Node, State, NextN).
extract(Circuit, Cell, AsciiState) :-
member(_-Cell-StateX-_, Circuit),
AsciiState is StateX + 48.
% code to run one circuit
run([], Circuit, Circuit) :- !.
run([Src-Level-Target|Pulses], CircuitIn, CircuitOut) :-
( member(Type-Target-State-Dests, CircuitIn)
-> /*write([Src, Level, Target]), write(" -> "),
write([Type, Target, State, Dests]), write(" = "),*/
call(Type, Src, Level, State, NewState, Out),
send(Target, Out, Dests, AdditionalPulses),
% write(NewState-AdditionalPulses), nl,
select(Type-Target-State-Dests, CircuitIn,
Type-Target-NewState-Dests, Circuit1),
append(Pulses, AdditionalPulses, NewPulses),
run(NewPulses, Circuit1, CircuitOut)
; run(Pulses, CircuitIn, CircuitOut)
).
broadcaster(_, l, x, x, l).
ff(_, h, State, State, none).
ff(_, l, 0, 1, h).
ff(_, l, 1, 0, l).
nand(Src, Level, State, NewState, OutLevel) :-
select(Src-_, State, Src-Level, NewState),
(maplist([_-h]>>(true), NewState) -> OutLevel = l; OutLevel = h).
send(_, none, _, []).
send(From, Level, Dests, Pulses) :-
\+ Level = none,
maplist({Level}/[Dest, From-Level-Dest]>>(true), Dests, Pulses).
% input initialization
prefill_nands([], Circuit, Circuit).
prefill_nands([_-Src-_-Dests|Nodes], CircuitIn, CircuitOut) :-
convlist(
{CircuitIn}/[Dest, Dest]>>(member(nand-Dest-_-_, CircuitIn)),
Dests, NandDests),
foldl(fill_one_nand(Src), NandDests, CircuitIn, Circuit1),
prefill_nands(Nodes, Circuit1, CircuitOut).
fill_one_nand(Src, Nand, CIn, COut) :-
select(nand-Nand-State-Dests, CIn, nand-Nand-[Src-l|State]-Dests, COut).
% input parsing stuff below
input(FileName, Circuit) :-
phrase_from_file(modules(EmptyCircuit), FileName),
prefill_nands(EmptyCircuit, EmptyCircuit, Circuit).
modules([]) --> eos, !.
modules([Module|Modules]) --> module(Module), "\n", modules(Modules).
module(broadcaster-broadcaster-x-Dests) --> "broadcaster -> ", dests(Dests).
module(ff-Name-0-Dests) --> "%", node(Name), " -> ", dests(Dests).
module(nand-Name-[]-Dests) --> "&", node(Name), " -> ", dests(Dests).
dests([Dest]) --> node(Dest).
dests([Dest|Dests]) --> node(Dest), ", ", dests(Dests).
node(Name) --> string_without(", \n", NameStr), {atom_codes(Name, NameStr)}.

1015
20/part2.svg Normal file

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 52 KiB

16
20/part2.txt Normal file
View File

@@ -0,0 +1,16 @@
gh <- [qx, zf, cd, rk]
Each of the 4 wings is a 12-bit counter, with custom resets at...
xr, qv, gx, vh, qn, lz, xv, zk, lv, mj, hb, vj (jj) <- (111010010101) vj, qv, zk, gx, xr, qn, mj
nj, kp, xx, vp, sj, mt, mx, rv, rr, bq, xb, gr (xz) <- (111101101011) gr, nj, vp, rr, xb, xx, mt, kp, mx
hx, xq, rm, tp, kh, qc, xh, vm, jm, cq, tg, fb (bz) <- (111111011001) vm, kh, xq, tp, fb, qc, jm, hx, rm
kb, cv, vx, pt, qq, rz, tz, kn, gg, cp, rd, xk (gf) <- (111011010001) rz, cv, qq, kb, kn, xk, vx
111010010101 = 3733
111101101011 = 3947
111111011001 = 4057
111011010001 = 3793
All 4 are primes.
LCD = 226732077152351

5
20/test.txt Normal file
View File

@@ -0,0 +1,5 @@
broadcaster -> a, b, c
%a -> b
%b -> c
%c -> inv
&inv -> a

5
20/test2.txt Normal file
View File

@@ -0,0 +1,5 @@
broadcaster -> a
%a -> inv, con
&inv -> b
%b -> con
&con -> output

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

131
21/input.txt Normal file
View File

@@ -0,0 +1,131 @@
...................................................................................................................................
.#....................#..#......##.#..#.#..#.#..#.#.........#....................##......##...#....#.....#..#.....#........#.......
.........#......#.#.........#...#.#...##..#.......#..#...................#........#..........#.#..##...#...#.......................
..#...#...#..#..#.#..#......#.....#...#....##..............................#......#.....#........#.......#.#.......#..#......#.....
............#................#...#.............#.........................#..................#....#..............#......##..#.......
.##....#...#....#............#.#..................#.#...#.................#......#.....#..........#.....#.#.#...........#..........
..###....#.#.#....#....##............##.#...#...............................#.#..#.#.........#....................#.#....###.#.....
..#....##.......#...##..#......#.....................#..........#...#............#.........#.........##......#......#..#......#....
....................#......##.............#.#.................................#.....#..#.#..#....#...##........#....#..............
................#...........#.#.....#.#....#.................#.....#...................#..............#....#..#......#......#......
.....#.....#...........#.............#.##......#.#..............#................#........#..#..#....#....#....#.#..........#.#....
..#......#..........#.............#..........#...#.................#.##..................#.##.....#......##.......#...#.........##.
.......#..#..#.......#.......#...##.#....#.....#...........#...#....#..#..............#.............#......#..#....................
..#..#.........#.......#...#..............#.............#......#....#...................#..........##.....##.##.....#.....#.#......
.....#....###....#......#..#..#..#........#..#............#.....#.....#.#.#..................#.......#...........##................
.........#....#.....#...........#..##..##.......................#..#.......#.............#....##...##...#...#...##........#...#....
.#...#..#.....#.......#.#......#.......#.#.............#..#.#.....#.#.#......#.........#.........#...........#..#.....##....#......
.....#.#.#...............#...........###.#..............#.#..#......#...................#...#....##.........#..#..#.#...#.#........
......#..........#....##.....#.......#.....#..........#......###....#.......#...........#......#..............#.........#..#.#.....
...#......#............#............#.#..#........#.....#.......#..............#.............#.........#...........#...............
..............##........#.##..#....................#.......#...............#...#.............#...#..##.........#.......#.........#.
..........##....#..#.........#...#..............#.....##..#.....................#...........##.......................#...........#.
......#..........##..............#...................#...#..#.....#.....#.......#...............#.........#..#......#........#.....
....#.#.............#.....#...#.....#.#........#.#...#...###..#........#..#...#.............#.........#.....................##.....
....#...#.........#..............#..............#.......####.........#..............##...........###.......#.....#........##..#..#.
..#...#...........#.#...#...#...............................#............#....................#...#....#..#...#.#...........#..#...
..#.....##..#.#.........................................#.........#....#......#....#..#....................#.#........#..........#.
..#...#..#...#......#.#.......#...............#.#......##..#..#.........#.............#......................#....##...##.#........
...#.#.#..............#...##..............#...#............#...............#...........#.#.........#..#.......#.#.#...........#....
............#.....#....#.....##...............#......#..........#.....#........#........#..............#...#.#.....#.#.............
............#........##.................#......#...............................#.#.#................#...#.................#..#.....
......#...#....#............#.........#.......#....#...................#........#..........#...............#...........#...........
........#............#......##.......#...........#.......#.............#.....................................#...........#.........
..........#.....#.........#.#.......................#..........##.........#....#........#.....#..........##..................##....
..........#........................#.............#.......##..#......#..........#...........#.............#....#..#..#....#......#..
......#........#........#.........#.......#....#..................#.........##.....#......#....................#...#.........#.....
...#..#....#....#.................#...#.....#.....##....................#.......##.....#...#...##.......................##.....#...
...##........#.....................#.........#..##....#.................###........#.......#..............#...#...#.......#....##..
.......#...........##.............#......##.............##.##........##...#..#..#.#.......#..#.#...........#...#...................
.........#.......##.............#............##.....#........#...............#.#.....#.....#.#..............#.......##...#.........
.......##...#....#...........#...........#..............#..#..#.........####..........#....##.................#..............#.....
....#..........................#..........#.............#..........#...#...........#.......#............................##.........
...#...#.....###.................................##...............................#....#...#..##.................................#.
..#....#.#......#............#...#......##.....#.....#..##...#.........#.....#.............#......#...#............#...............
......#.....#.##................#.......##.##........#.........................#..............................................#....
.#....#..................#.#...............#..#......##.......##...............#....####..........#...#........................#...
...#........#..........#.................#....#.........#....#.....#.........#.....#.#.......##.......#................#..##.......
.#.......#.#..#............##...........#......#.#.......................#.........#.....#.....#.....###...............#.#..##.....
.......................#...#...........#.........#....###.......................#.........###.#.#....#......#........##............
..#....................#.................#.#.##.#........#..#......#....#......##......#............#..#..............#....#....#..
.......................##.##.#..#.....###....#..#.#...#............#........#.....#....#.#.....#..#....#......................##...
....#.....#.........##..#....#..#.........#...#...#..##........#......###.....#..................#......................#..........
.#.#................#..#.#...#........................#....##..#.............#...#.#.......#........#.#.....##................#....
.##..#..........#..#.#........##............#.......#..........#....#.#...#....##.#...#..##...........#.......................#....
.#.#.##..................#............#.#......#.........#............#.#....#..#.........##..#..................##...........##.#.
..............#..........#...#...##..........#.....................#..#..............#.....#.#...........#.....#....#.......#......
..###.........#........###...#...........#..##.##..###....#...#................#.....#......#...##...................#...........#.
..#...........#.................##........#.........#...#.....##......##...#.........#......#.............#...#..#.................
..#.............#..........#.#.............#.....#............#....#.#......#....................#.#.......#.#.....#............##.
..........#...........#...#...#...#................#......#................#..........................##.##.....#..#..#.#..........
..............#....#......#............#.....#.......#..#..........................#..#...............................#............
............#.............#...........##...#.##..#.........#....#.....#..#.....#...............##....#.................#...........
...........#.....#.##..............#....#..........#......#.......#..##....##.......#.........#.......#....#...#..........#........
......#...#...#..............#.#...##.#..#........#.....#.......#...#.........#.....#.....##.........#.#...........................
..............#..#........#......##.......#......#...#.#........#.............#...##.............#.#.#...............##....#.......
.................................................................S.................................................................
.........#.....#.#.#..........#.#.............#..#.....................##......##.#.............#.#.......#.........#...#..........
.........#......#......#......#..........#...........................#...#......#..........#..#.#..........#......#..#..#...#......
........#.#...............##...............................#.#..............#.#....##..#...#...#....#..#.................#.........
.............#..#....#............#........#.#......#........#.......#....#......#...#.#..........#....##......#.#.................
............#..........#.............#.#..............#.....#.....#...#...#........#.#........#.#........#.#.......................
.................#........#......#....#.#................#..............#.....#.#.....#.............#.#........#.#...#.............
...........#....#.##...........#..#...................................#.#...#........#.......#.........#...#..##..##...............
.....................#.#..#...#.#..........#....##...#.................#...............#.......................#...................
..............#..#.............#.#...........#....#.......#.........#.......#.........#...#.#.........##............#..............
....#.............##....#......#..........................##.........#.#.........#.....#...#..............#........................
....#............#...........#.####....#...#.......#..#.........#...#.............#...........#...#...........#.##...........#.#...
..##..............#...............#...#.#..#....##.....................#..#.....#.............#....#.......#..###.............##.#.
.....#.##........#....#...##.#..###...#......#..##.#.....#..#.#.#........##......#............#.......#.....#....#...........##....
.#......................#.#.......#........#.#.#.........##.....#.....#..#........#..#..#...#..##.......#...............#..........
.....#...#.............................#.....#.....#......##......#...##........#.#.........................###.........#..........
...#........#...................#....#...........#..#....#...#.........#...........##.#..#..#..#...........##............#.........
......##..#..........##..#..#...........#......#...#........#.....#........#...........#........#............#........#.......#....
.......#.......................#....#......###..#..#..#.........#.##..#................#.....#...............................#.....
...#.........#............................................#...#....#...........#.#..................#.....##.......#...#........#..
..##..###.......#........#................#.....#..................##.........#..#..#...........#..##...###.........###...#...#....
..........#.....#.............#.#..#.#...........##.........#..............................#..#.......#.#................#.#.#.....
.......#..................#............##.............#..............#.#........#.#..............#...#..........................#..
...#.......#.....#..........................#...#...#...........#..#.............#.........#..........#.............#........#.....
..............#................##......#.#......#........................####...#.....#....#....#..................#.#.#........#..
........#.....#.#....#....................##......................................#...#..#.......#..........................##.....
...#.#.........#.#..#.............#...#....##.......#......................##...#.#.........#.....#..........#.....................
..#.#......#...#......##............#......#....#.#..#.#..........#....#......##..#................................#...............
........###..##....##...........................#.#.#.##..###........#....#............#........##.........#...#...................
....#............................#.#.........#.........#...#.......#.................#..#......#.........#..#.....#...#.#....#...#.
..#.#..........#..........#.........#..#...#..........#.....#....................#.#..#.......#...........#............#...........
.......#.....#...........##.........#.....#..........#....#..#......#...........#...##....#......................#....#.......#....
..##......#...##.............................##..........#....#..............#..........#..............##..#....##.#........##.....
....#......#....#.#..................#..........#....#.#..##.#.##........#.............................#...............#.......#.#.
.............#..#......#.................#.#..#....####........#......#......#....#.#.....................#.#.#.#..#...............
.##......#........##........#.............#.#.###.....#.............#.......#..##....#...#....................##..###.......#......
....#.#......##.#.......#......#......................#.......#.........#..#...........#................#.........#..#...#....#....
..........#....#..#...........#.................#...#..#.....................#.#........##.......#.#....#..#................##...#.
......#....#.......#.......#..#...........#......#............#......#................#..........#...#.......##...#....#.#.........
...#..##...##....#..#.......#....#.................#.#.....#....#........#..#.....####.#...........#..#............#...#..##.....#.
..#......#...................#...............................#............#.....##.............#..#...#..##.#......#...............
....#.........#.........#..............................#...#....#..#.#..............#...............#..##.........#.......#....#...
.....#.........#....#.............#............................#.................##............#...#......#..........#.....#.....#.
...#.#....#.......##...#....#............................##.#...#.......#...##...................#..........#.#....................
...........#..#.###.....##...#.........#...........#..#.#.........#.#.#.......#.................#....#............###.........#..#.
.##..........#..#......#.....##.##.#...##.........#.................#....#.#.....#.................#.....#...#....#.....###..#.#...
......#.#..##.#.......#...#..#........................#..#..........#.#....#..##........#...#.#.............#.#....................
.......#.#.............#..#.........................#.##..........##......................#...##.........#....#..........#..###....
...............#..#...#.........#.........#.................#.#......#....................#.........#.......................#.#....
.....#.................#.#...............##............................####...................##....#......#.#....##..#..#.......#.
....#..#.......#.#...#.......................##.........#..#.......#.#.#............#...........#.###............#......#...##.....
..#.#..........#..#.......#...#.........#....#.........#....#...#.......#.#..........##..##........................#..........##...
....#...##......##......#.......#....###..............................#.##........................#......##.......#............#...
...........#......#.#......#.............................#..........#...............#......##...............#.#........###.......#.
..............#......#.....#.........#.....##....#..................#..##.......#.........##.#..##..................#....#.........
....#...#...##.......###.......#....#...........#.#.............#...#.............#.#...#.#..................#..#.........#........
................#.........#....#....#.##.....#...###..........#.......#...........#..#....#.............#....................##....
.......................##..............#...#.#....##.#............#.#........#......##....#.......#........#.#.......#....#..#..#..
..#....#..#............#.#...................#.....................................#.......##.#............##..#........#..........
.....#............#.......##.........##...#........#........................................#................#.......#..........#..
......#.#.....#.......#............##......#.#..#....#.........................#...............#..#.......##....###......###.......
..#...................#.#...#.....#.......#..................................#..#....#......#.#...#..#..#......#.#.#....#....#.....
...#....#....#.....#.....#.............................#.##..............#.....#..#.........#...#.....#........#.......#......##...
.....#.......#.....#...#..#....#................#.......................#..#...#........#..........#....#..#....................#..
......#.............#.#...#.##.......#...#.....#.....#.#.#...............#.....##.#....#...............................#.#.....#...
...................................................................................................................................

47
21/part1.pl Normal file
View File

@@ -0,0 +1,47 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Map, Starting),
nsteps(Map, 64, [Starting], Ends), length(Ends, Answer),
write(Answer), nl.
nsteps(Map, N, Starts, Reachables) :-
length(Range, N),
foldl({Map}/[_, SIn, SOut]>>(step(Map, SIn, SOut)), Range, Starts, Reachables).
step(Map, CurrentCells, NextCells) :-
maplist(neighbors(Map), CurrentCells, NeighborsOfCells),
foldl(
[Neighbors, SetIn, SetOut]>>(
list_to_ord_set(Neighbors, NeighborsSet),
ord_union(SetIn, NeighborsSet, SetOut)),
NeighborsOfCells, [], NextCells).
neighbors(Map, X-Y, Neighbors) :-
findall(X1-Y1, neighbor(Map, X-Y, X1-Y1), Neighbors).
neighbor(Map, X-Y, X1-Y1) :-
( X1 is X, Y1 is Y+1; X1 is X, Y1 is Y-1;
X1 is X+1, Y1 is Y; X1 is X-1, Y1 is Y ),
nth0(X1, Map, Row), nth0(Y1, Row, '.').
replace(I, List, E, NewList) :- nth0(I, List, _, R), nth0(I, NewList, E, R).
% input parsing stuff below
input(FileName, Map, StartX-StartY) :-
phrase_from_file(lines(MapS), FileName),
nth0(StartX, MapS, Row), nth0(StartY, Row, s),
select(s, Row, '.', NewRow), replace(StartX, MapS, NewRow, Map).
lines([]) --> eos, !.
lines([Line|Lines]) --> line(Line), lines(Lines).
line([]) --> "\n"; eos.
line([s|Chars]) --> "S", line(Chars).
line(['.'|Chars]) --> ".", line(Chars).
line(['#'|Chars]) --> "#", line(Chars).
% Debug stuff
print(Map) :- maplist([X]>>(atomics_to_string(X, XStr), write(XStr), nl), Map).

11
21/test.txt Normal file
View File

@@ -0,0 +1,11 @@
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........

1479
22/input.txt Normal file

File diff suppressed because it is too large Load Diff

100
22/part1.pl Normal file
View File

@@ -0,0 +1,100 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- op(700, xfx, [supports, is_way_above]).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Bricks),
stack(Bricks, Stacked),
support_listing(Stacked, LookDown, LookUp),
max_member(MaxN-_-_-_, Bricks),
findall(N, (between(0, MaxN, N), can_remove(N, LookDown, LookUp)), Ns),
length(Ns, Answer),
write(Answer), nl.
can_remove(N, LookDown, LookUp) :-
BeingSupporteds = LookUp.N,
write(N), write("("), write(LookDown.N), write(") supporting "),
write(BeingSupporteds), nl,
maplist(supported_by_more_than_one(LookDown), BeingSupporteds).
supported_by_more_than_one(LookDown, N) :- LookDown.N > 1.
% StackedBricks is sorted by decreasing top edge
support_listing(StackedBricks, SupportedCount, SupportingMap) :-
findall(
N=SupportedListLen,
( append(_, [Brick1|Belows], StackedBricks),
supported_by(Brick1, Belows, SupportedList),
length(SupportedList, SupportedListLen),
Brick1 = N-_-_-_),
SupportedLists),
SupportedCount = brick{}.put(SupportedLists),
sort(2, @=<, StackedBricks, StackSortedByBottoms),
findall(
M=SupportingList,
( append(_, [Brick2|Aboves], StackSortedByBottoms),
supporting(Brick2, Aboves, SupportingList),
Brick2 = M-_-_-_),
SupportingLists),
SupportingMap = brick{}.put(SupportingLists).
% Aboves need to be sorted by increasing bot edge
supporting(_Brick, [], []).
supporting(Brick, [Above|_], []) :- Above is_way_above Brick, !.
supporting(Brick, [Above|Aboves], Supportings) :-
supporting(Brick, Aboves, NextSupportings),
( Brick supports Above
-> Above = N-_-_-_, Supportings = [N|NextSupportings]
; Supportings = NextSupportings).
% Belows need to be sorted decreasing by top edge
supported_by(_Brick, [], []).
supported_by(Brick, [Below|_], []) :- Brick is_way_above Below, !.
supported_by(Brick, [Below|Belows], SupportedBy) :-
supported_by(Brick, Belows, NextSupportedBy),
( Below supports Brick
-> Below = N-_-_-_, SupportedBy = [N|NextSupportedBy]
; SupportedBy = NextSupportedBy).
is_way_above(_-_-_-(Z2bot-_), _-_-_-(_-Z1top)) :- Z2bot > Z1top + 1.
supports(Brick1, Brick2) :-
overlap(Brick1, Brick2),
Brick1 = _-_-_-(_-Z1top), Brick2 = _-_-_-(Z2bot-_), Z2bot =:= Z1top + 1.
stack(Bricks, StackedBricks) :-
empty_heap(H0), foldl(stack1, Bricks, H0, Heap),
heap_to_list(Heap, HeapList),
maplist([_-Brick, Brick]>>(true), HeapList, StackedBricks).
stack1(Brick, Heap, NewHeap) :-
( max_of_heap(Heap, OldZtop, OldTop), overlap(Brick, OldTop)
; empty_heap(Heap), OldZtop is 0),
Brick = N-X-Y-(Zbot-Ztop),
NewZbot is OldZtop + 1, NewZtop is NewZbot + (Ztop - Zbot),
add_to_maxheap(Heap, NewZtop, N-X-Y-(NewZbot-NewZtop), NewHeap),
!.
stack1(Brick, Heap, NewHeap) :-
get_from_maxheap(Heap, LastZtop, LastTopBrick, HeapWithoutTop),
stack1(Brick, HeapWithoutTop, NewHeapWithoutTop),
add_to_maxheap(NewHeapWithoutTop, LastZtop, LastTopBrick, NewHeap).
overlap(_-(Xa1-Xa2)-(Ya1-Ya2)-_, _-(Xb1-Xb2)-(Yb1-Yb2)-_) :-
Xa1 =< Xb2, Xa2 >= Xb1, Ya1 =< Yb2, Ya2 >= Yb1.
% input parsing stuff below. Brick indexing is for debugging.
input(FileName, SortedBricks) :-
phrase_from_file(bricks(0, Bricks), FileName),
sort(2, @=<, Bricks, SortedBricks).
bricks(_, []) --> eos, !.
bricks(N, [Line|Lines]) --> {Next is N+1}, brick(N, Line), bricks(Next, Lines).
brick(N, N-(X1-X2)-(Y1-Y2)-(Z1-Z2)) -->
number(X1), ",", number(Y1), ",", number(Z1), "~",
number(X2), ",", number(Y2), ",", number(Z2), "\n".
% maxheap wrapper. PriX = -Pri, which is the Ztop
max_of_heap(H, Pri, Key) :- min_of_heap(H, PriX, Key), Pri is -PriX.
add_to_maxheap(H0, Pri, Key, H) :- PriX is -Pri, add_to_heap(H0, PriX, Key, H).
get_from_maxheap(H0, Pri, Key, H) :- get_from_heap(H0, PriX, Key, H), Pri is -PriX.
% Can probably do 2 maps, one by top edges and one by bottom edges and lookup

112
22/part2.pl Normal file
View File

@@ -0,0 +1,112 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- op(700, xfx, [supports, is_way_above]).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Bricks),
stack(Bricks, StackedBricks),
support_listing(StackedBricks, Supporters, SupportMap),
length(Bricks, Len), TopBrick is Len - 1,
findall(
Goners,
( between(0, TopBrick, Brick),
remove(SupportMap, [Brick], Supporters, Goners)),
GonerCounts),
sum_list(GonerCounts, Answer),
write(Answer), nl.
remove(_, [], _, -1).
remove(SupportMap, [N|ToKills], Supporters, Goners) :-
SupportedByN = SupportMap.N,
foldl(decrement_supporter_count, SupportedByN, Supporters, NewSupporters),
convlist({NewSupporters}/[X, X]>>(is_goner(NewSupporters, X)),
SupportedByN, ToKillMore),
append(ToKills, ToKillMore, ToKillNext),
remove(SupportMap, ToKillNext, NewSupporters, NextGoners),
Goners is NextGoners + 1.
is_goner(Supporters, M) :- Supporters.M =< 0.
decrement_supporter_count(M, Supporters, NewSupporters) :-
NewCount is Supporters.M - 1,
NewSupporters = Supporters.put(M, NewCount).
% StackedBricks is sorted by decreasing top edge
support_listing(StackedBricks, SupportedCount, SupportingMap) :-
findall(
N=SupportedListLen,
( append(_, [Brick1|Belows], StackedBricks),
supported_by(Brick1, Belows, SupportedList),
length(SupportedList, SupportedListLen),
Brick1 = N-_-_-_),
SupportedLists),
SupportedCount = brick{}.put(SupportedLists),
sort(2, @=<, StackedBricks, StackSortedByBottoms),
findall(
M=SupportingList,
( append(_, [Brick2|Aboves], StackSortedByBottoms),
supporting(Brick2, Aboves, SupportingList),
Brick2 = M-_-_-_),
SupportingLists),
SupportingMap = brick{}.put(SupportingLists).
% Aboves need to be sorted by increasing bot edge
supporting(_Brick, [], []).
supporting(Brick, [Above|_], []) :- Above is_way_above Brick, !.
supporting(Brick, [Above|Aboves], Supportings) :-
supporting(Brick, Aboves, NextSupportings),
( Brick supports Above
-> Above = N-_-_-_, Supportings = [N|NextSupportings]
; Supportings = NextSupportings).
% Belows need to be sorted decreasing by top edge
supported_by(_Brick, [], []).
supported_by(Brick, [Below|_], []) :- Brick is_way_above Below, !.
supported_by(Brick, [Below|Belows], SupportedBy) :-
supported_by(Brick, Belows, NextSupportedBy),
( Below supports Brick
-> Below = N-_-_-_, SupportedBy = [N|NextSupportedBy]
; SupportedBy = NextSupportedBy).
is_way_above(_-_-_-(Z2bot-_), _-_-_-(_-Z1top)) :- Z2bot > Z1top + 1.
supports(Brick1, Brick2) :-
overlap(Brick1, Brick2),
Brick1 = _-_-_-(_-Z1top), Brick2 = _-_-_-(Z2bot-_), Z2bot =:= Z1top + 1.
stack(Bricks, StackedBricks) :-
empty_heap(H0), foldl(stack1, Bricks, H0, Heap),
heap_to_list(Heap, HeapList),
maplist([_-Brick, Brick]>>(true), HeapList, StackedBricks).
stack1(Brick, Heap, NewHeap) :-
( max_of_heap(Heap, OldZtop, OldTop), overlap(Brick, OldTop)
; empty_heap(Heap), OldZtop is 0),
Brick = N-X-Y-(Zbot-Ztop),
NewZbot is OldZtop + 1, NewZtop is NewZbot + (Ztop - Zbot),
add_to_maxheap(Heap, NewZtop, N-X-Y-(NewZbot-NewZtop), NewHeap),
!.
stack1(Brick, Heap, NewHeap) :-
get_from_maxheap(Heap, LastZtop, LastTopBrick, HeapWithoutTop),
stack1(Brick, HeapWithoutTop, NewHeapWithoutTop),
add_to_maxheap(NewHeapWithoutTop, LastZtop, LastTopBrick, NewHeap).
overlap(_-(Xa1-Xa2)-(Ya1-Ya2)-_, _-(Xb1-Xb2)-(Yb1-Yb2)-_) :-
Xa1 =< Xb2, Xa2 >= Xb1, Ya1 =< Yb2, Ya2 >= Yb1.
% input parsing stuff below. Brick indexing is for debugging.
input(FileName, SortedBricks) :-
phrase_from_file(bricks(0, Bricks), FileName),
sort(2, @=<, Bricks, SortedBricks).
bricks(_, []) --> eos, !.
bricks(N, [Line|Lines]) --> {Next is N+1}, brick(N, Line), bricks(Next, Lines).
brick(N, N-(X1-X2)-(Y1-Y2)-(Z1-Z2)) -->
number(X1), ",", number(Y1), ",", number(Z1), "~",
number(X2), ",", number(Y2), ",", number(Z2), "\n".
% maxheap wrapper. PriX = -Pri, which is the Ztop
max_of_heap(H, Pri, Key) :- min_of_heap(H, PriX, Key), Pri is -PriX.
add_to_maxheap(H0, Pri, Key, H) :- PriX is -Pri, add_to_heap(H0, PriX, Key, H).
get_from_maxheap(H0, Pri, Key, H) :- get_from_heap(H0, PriX, Key, H), Pri is -PriX.
% Can probably do 2 maps, one by top edges and one by bottom edges and lookup

7
22/test.txt Normal file
View File

@@ -0,0 +1,7 @@
1,0,1~1,2,1
0,0,2~2,0,2
0,2,3~2,2,3
0,0,4~0,2,4
2,0,5~2,2,5
0,1,6~2,1,6
1,1,8~1,1,9

141
23/input.txt Normal file
View File

@@ -0,0 +1,141 @@
#.###########################################################################################################################################
#...###...#...#...#...###.....#...#...#.................#...#.......#.......###.....#.......#...#.....#####...#...#.....#.....###.....#.....#
###.###.#.#.#.#.#.#.#.###.###.#.#.#.#.#.###############.#.#.#.#####.#.#####.###.###.#.#####.#.#.#.###.#####.#.#.#.#.###.#.###.###.###.#.###.#
#...#...#.#.#.#.#.#.#.#...#...#.#.#.#.#...........#.....#.#.#.#.....#...#...#...#...#...#...#.#.#.#...#.....#...#.#.#...#...#.#...#...#...#.#
#.###.###.#.#.#.#.#.#.#.###.###.#.#.#.###########.#.#####.#.#.#.#######.#.###.###.#####.#.###.#.#.#.###.#########.#.#.#####.#.#.###.#####.#.#
#...#.#...#.#.#.#.#.#.#...#.#...#...#.#...###...#.#.....#.#...#.....#...#.#...#...###...#...#.#.#.#.###.......#...#.#.....#.#.#...#.#.....#.#
###.#.#.###.#v#.#.#.#.###.#.#.#######.#.#.###.#.#.#####.#.#########.#.###.#.###.#####.#####.#.#.#.#.#########.#.###.#####.#.#.###.#.#.#####.#
###...#.#...#.>.#.#.#.#...#...#.......#.#.#...#.#...#...#.....#.....#...#.#...#...#...#.....#.#.#.#.#.....#...#.#...#.....#.#.#...#...#.....#
#######.#.###v###.#.#.#.#######.#######.#.#.###.###.#.#######.#.#######.#.###.###.#.###.#####.#.#.#.#.###.#.###.#.###.#####.#.#.#######.#####
###...#...#...###.#.#.#.#.......#...###.#.#...#.###.#...#.....#...###...#.#...#...#...#.#...#.#.#.#.#...#.#...#.#...#.#...#.#.#.....#...#...#
###.#.#####.#####.#.#.#.#.#######.#.###.#.###.#.###.###.#.#######.###.###.#.###.#####.#.#.#.#.#.#.#.###.#.###.#.###.#.#.#.#.#.#####.#.###.#.#
#...#.......#...#.#.#...#...>.>.#.#.#...#.#...#...#.#...#.#.....#...#.#...#...#.....#.#.#.#.#.#.#.#.>.>.#.....#.#...#.#.#.#.#...#...#.###.#.#
#.###########.#.#.#.#########v#.#.#.#.###.#.#####.#.#.###.#.###.###.#.#.#####.#####.#.#.#.#.#.#.#.###v#########.#.###.#.#.#.###.#.###.###.#.#
#.............#.#.#...#.......#...#.#.#...#...#...#.#.###...#...#...#.#...#...#...#...#.#.#...#.#.###.#...#...#...#...#.#.#.#...#.###...#.#.#
###############.#.###.#.###########.#.#.#####.#.###.#.#######.###.###.###.#.###.#.#####.#.#####.#.###.#.#.#.#.#####.###.#.#.#.###.#####.#.#.#
#...............#.....#.......#.....#.#...#...#...#.#.#.>.>.#...#.###.#...#...#.#.....#.#.#.....#.#...#.#.#.#...#...#...#.#.#.....#...#...#.#
#.###########################.#.#####.###.#.#####.#.#.#.#v#.###.#.###.#.#####.#.#####.#.#.#.#####.#.###.#.#.###.#.###.###.#.#######.#.#####.#
#.....................#...#...#...#...#...#...###.#.#...#.#.#...#...#.#.#.>.>.#.#.....#.#.#.#...#.#...#.#.#.#...#.#...###.#.#.......#.......#
#####################.#.#.#.#####.#.###.#####.###.#.#####.#.#.#####.#.#.#.#v###.#.#####.#.#.#.#.#.###.#.#.#.#.###.#.#####.#.#.###############
#.....................#.#.#.....#.#...#.#...#.#...#.#.....#...###...#.#.#.#.#...#.....#...#...#...###...#...#...#...#...#...#...............#
#.#####################.#.#####.#.###.#.#.#.#.#.###.#.###########.###.#.#.#.#.#######.#########################.#####.#.###################.#
#...................#...#.......#.#...#...#...#.....#.........#...#...#.#.#.#.#.....#.......#...#...###.........#...#.#.###.......#.........#
###################.#.###########.#.#########################.#.###.###.#.#.#.#.###.#######.#.#.#.#.###.#########.#.#.#.###.#####.#.#########
#...#...............#...........#...#...#...###...#...........#.....###...#.#.#...#.........#.#.#.#...#...........#...#...#...#...#.........#
#.#.#.#########################.#####.#.#.#.###.#.#.#######################.#.###.###########.#.#.###.###################.###.#.###########.#
#.#.#...#.........#.....#.......###...#.#.#.#...#.#.....#.....#...#...#...#...###...#...#...#.#.#...#.#...................#...#...#...#.....#
#.#.###.#.#######.#.###.#.#########.###.#.#.#.###.#####.#.###.#.#.#.#.#.#.#########.#.#.#.#.#.#.###.#.#.###################.#####.#.#.#.#####
#.#...#...#...#...#...#.#.........#...#.#.#.#.#...#...#.#.#...#.#.#.#.#.#.#...#...#...#.#.#.#.#.....#.#.........#...###...#...#...#.#.#.....#
#.###.#####.#.#.#####.#.#########.###.#.#.#.#.#.###.#.#v#.#.###.#.#.#.#.#.#.#.#.#.#####v#.#.#.#######.#########.#.#.###.#.###.#.###.#.#####.#
#...#...#...#...###...#...........#...#.#.#.#.#...#.#.>.>.#...#.#...#.#.#.#.#...#.....>.>.#...#.......#...#.....#.#...#.#.#...#...#.#.#.....#
###.###.#.#########.###############.###.#.#.#.###.#.###v#####.#.#####.#.#.#.###########v#######.#######.#.#.#####.###.#.#.#.#####.#.#.#.#####
###...#.#.........#.#.............#...#...#...###...###.....#...#.....#.#...#.....#...#.#.......#.....#.#.#.....#...#.#.#.#...#...#.#.#...###
#####.#.#########.#.#.###########.###.#####################.#####.#####.#####.###.#.#.#.#.#######.###.#.#.#####.###.#.#.#.###.#.###.#.###v###
#.....#...........#...#...........###.......#.............#.....#.#...#.#.....###...#.#.#...#...#...#.#.#.###...#...#...#.#...#...#.#...>.###
#.#####################.###################.#.###########.#####.#.#.#.#.#.###########.#.###.#.#.###.#.#.#.###v###.#######.#.#####.#.#####v###
#.................#...#...#.......#...#.....#.#.....#...#...#...#...#...#.........#...#.#...#.#.#...#.#.#.#.>.>...###.....#...###.#.#.....###
#################.#.#.###.#.#####.#.#.#.#####.#.###.#.#.###.#.###################.#.###.#.###.#.#.###.#.#.#.#v#######.#######.###.#.#.#######
#.................#.#.#...#.#.....#.#...#...#.#.#...#.#...#...#.....#.............#.....#.#...#.#.#...#.#...#.....#...#.....#...#...#.......#
#.#################.#.#.###.#.#####.#####.#.#.#.#.###.###.#####.###.#.###################.#.###.#.#.###.#########.#.###.###.###.###########.#
#.....#.....###...#.#.#.....#.....#.......#.#.#.#.#...###...###.#...#...#...............#.#.#...#.#...#.###.....#.#...#...#...#.#...........#
#####.#.###v###.#.#.#.###########.#########.#.#.#.#.#######.###.#.#####.#.#############.#.#.#.###.###.#.###.###.#.###.###.###.#.#.###########
#####...###.>.#.#.#.#.#...###.....#.........#...#...#...###...#.#.....#.#.#.............#...#.....###...#...###.#.#...#...###...#...........#
###########v#.#.#.#.#.#.#.###v#####.#################.#.#####.#.#####.#.#.#.#############################.#####.#.#.###.###################.#
###...#...#.#.#.#.#.#.#.#.#.>.>.....#.....#...#...###.#.#...#...#.....#...#.........#.......###...........#####.#.#...#.#...........#.......#
###.#.#.#.#.#.#.#.#.#.#.#.#.#v#######.###.#.#.#.#.###.#.#.#.#####.#################.#.#####.###.###############.#.###.#.#.#########.#.#######
#...#...#...#...#...#...#...#.###...#...#...#.#.#.....#...#.......#...###.........#...#.....#...#.............#...###...#.........#.#...#####
#.###########################.###.#.###.#####.#.###################.#.###.#######.#####.#####.###.###########.###################.#.###.#####
#...#.........#...#...#.....#...#.#.#...#...#.#.........#...........#...#.....###.#...#...###.....#.........#.....#...............#...#.....#
###.#.#######.#.#.#.#.#.###.###.#.#.#.###.#.#.#########.#.#############.#####.###.#.#.###.#########.#######.#####.#.#################.#####.#
#...#.#.......#.#.#.#.#...#...#...#...#...#...###...###...#...#.........#...#...#...#.....#.....###.......#.#...#.#...#.....#.......#.......#
#.###.#.#######.#.#.#.###.###.#########.#########.#.#######.#.#.#########.#.###.###########.###.#########.#.#.#.#.###.#.###.#.#####.#########
#.....#.....###.#.#.#.###.#...#...#...#...###...#.#.###...#.#.#...###...#.#.#...#...###...#...#.#.........#...#...###...#...#.#.....#.......#
###########v###.#.#.#.###.#.###.#.#.#.###v###.#.#.#.###.#.#.#.###v###.#.#.#.#.###.#.###.#.###.#.#.#######################.###.#.#####.#####.#
#.........#.>.#.#.#.#...#.#.#...#.#.#...>.>.#.#.#.#.#...#.#.#...>.>.#.#.#.#...#...#...#.#.#...#.#.............#.........#.....#.....#.#.....#
#.#######.#v#.#.#.#.###.#.#.#.###.#.#####v#.#.#.#.#.#.###.#.#####v#.#.#.#.#####.#####.#.#.#.###.#############.#.#######.###########.#.#.#####
#...#...#...#...#.#.###.#.#.#.###...#...#.#.#.#...#.#.###.#.#.....#.#.#.#.....#.#.....#.#.#...#.###...........#.#.......#.......###...#...###
###.#.#.#########.#.###.#.#.#.#######.#.#.#.#.#####.#.###.#.#.#####.#.#.#####.#.#.#####.#.###.#.###v###########.#.#######.#####.#########.###
###.#.#...###.....#...#...#...###...#.#.#.#.#.....#.#.###...#.....#...#.#...#.#.#.#.....#.#...#...>.>.....#.....#.###...#.....#.........#...#
###.#.###.###.#######.###########.#.#.#.#.#.#####.#.#.###########.#####.#.#.#v#.#.#.#####.#.#######v#####.#.#####.###.#.#####.#########.###.#
###...###...#.....#...#.......#...#.#.#.#.#.#...#.#...#...#...###.#...#...#.>.>.#.#...#...#.....###.#.....#...#...#...#...###.........#.....#
###########.#####.#.###.#####.#.###.#.#.#.#.#.#.#.#####.#.#.#.###.#.#.#######v###.###.#.#######.###.#.#######.#.###.#####.###########.#######
#...#.......#...#...###.....#.#...#.#.#...#...#...#.....#...#.....#.#.......#...#.....#...#...#.#...#...#...#.#...#...#...#...#.......#...###
#.#.#.#######.#.###########.#.###.#.#.#############.###############.#######.###.#########.#.#.#.#.#####.#.#.#.###.###.#.###.#.#.#######.#.###
#.#.#.........#.....#...#...#.....#...#...........#.#...#...........#.......###.........#.#.#...#...#...#.#.#...#...#.#.###.#.#.........#...#
#.#.###############.#.#.#.#############.#########.#.#.#.#.###########.#################.#.#.#######.#.###.#.###.###.#.#.###.#.#############.#
#.#.........#.......#.#.#.............#...#.......#.#.#...###.........#...............#.#...#...#...#.###.#.###.#...#.#.###.#.#####.........#
#.#########.#.#######.#.#############.###.#.#######.#.#######.#########.#############.#.#####.#.#.###.###.#.###.#.###.#.###.#.#####v#########
#.#.......#.#...#...#.#.....#.......#.#...#.....###...###...#.........#.............#...#.....#.#...#.....#...#.#...#.#.#...#...#.>.#.......#
#.#.#####.#.###.#.#.#.#####.#.#####.#.#.#######.#########.#.#########.#############.#####.#####.###.#########.#.###.#.#.#.#####.#.#v#.#####.#
#...#.....#.....#.#...#.....#.....#...#...#.....#...#.....#...#.......#.......#...#...###...#...###.#...#...#...###...#.#.#...#.#.#.#.#.....#
#####.###########.#####.#########.#######.#.#####.#.#.#######.#.#######.#####.#.#.###.#####.#.#####.#.#.#.#.###########.#.#.#.#.#.#.#.#.#####
#.....#.........#.....#.#.........#...###.#.#...#.#.#.....#...#.......#.....#.#.#.....#...#.#.#.....#.#.#.#...#...#...#...#.#.#...#...#.....#
#.#####.#######.#####.#.#.#########.#.###.#.#.#.#.#.#####.#.#########.#####.#.#.#######.#.#.#.#.#####.#.#.###.#.#.#.#.#####.#.#############.#
#.....#.#.......#...#.#.#.......###.#.#...#...#.#.#.#...#.#.#.........#...#.#...###.....#.#.#.#.....#.#.#.#...#.#.#.#.#...#.#.....#.......#.#
#####.#.#.#######.#.#.#.#######v###.#.#.#######.#.#.#.#.#.#.#.#########.#.#.#######.#####.#.#.#####.#.#.#.#.###.#.#.#.#.#.#v#####.#.#####.#.#
#.....#.#...###...#.#.#...#...>.>.#.#.#.....#...#.#.#.#.#.#.#.......###.#...#.....#.....#.#.#.#...#.#.#.#.#...#.#.#.#...#.>.#...#.#.....#.#.#
#.#####.###.###.###.#.###.#.###v#.#.#.#####.#.###.#.#.#.#.#.#######v###.#####.###.#####.#.#.#.#.#.#v#.#.#.###.#.#.#.#######v#.#.#.#####.#.#.#
#.......#...#...###.#.###...#...#...#.....#.#.#...#.#.#.#.#.......>.>.#.......###.#...#.#...#.#.#.>.>.#...#...#.#.#.......#...#.#.......#...#
#########v###.#####.#.#######.###########.#.#.#.###.#.#.#.#########v#.###########.#.#.#.#####.#.###v#######.###.#.#######.#####.#############
###...###.>.#.....#.#...#.....#...###.....#.#...###...#.#.#.........#.....###...#.#.#.#.#.....#.#...###...#...#.#.#.....#.#.....###.....#...#
###.#.###v#.#####.#.###.#.#####.#.###.#####.###########.#.#.#############.###.#.#v#.#.#.#.#####.#.#####.#.###.#.#.#.###.#.#.#######.###.#.#.#
#...#.....#.......#.#...#...#...#...#.....#.........###...#.............#...#.#.>.>.#...#...#...#.......#...#.#.#.#...#.#.#.......#.#...#.#.#
#.#################.#.#####.#.#####.#####.#########.###################.###.#.###v#########.#.#############.#.#.#.###.#.#.#######.#.#.###.#.#
#...#...#.........#.#.#...#...#####.....#...#.....#.#.........#...#.....###.#...#...###...#...###...........#...#.#...#.#.#.......#.#...#.#.#
###.#.#.#.#######.#.#.#.#.#############.###.#.###.#.#.#######.#.#.#.#######.###.###.###.#.#######.###############.#.###.#.#.#######.###.#.#.#
###...#.#.#.......#...#.#...............###...#...#.#.......#...#...#.....#.....###.....#.....#...#.............#...###...#.......#.#...#.#.#
#######.#.#.###########.#######################.###.#######.#########.###.###################.#.###.###########.#################.#.#.###.#.#
#.....#...#...........#.......................#.....#.......#.....#...#...#...............#...#.....#...........###.....###...###...#.....#.#
#.###.###############.#######################.#######.#######.###.#.###.###.#############.#.#########.#############.###.###.#.#############.#
#...#.............#...###...............#...#.###...#.#.....#.#...#.#...###.............#.#.#.........#...#...#.....#...#...#...............#
###.#############.#.#####.#############.#.#.#.###.#.#.#.###.#.#.###.#.#################.#.#.#.#########.#.#.#.#.#####.###.###################
#...#...........#.#.#...#.............#.#.#.#.#...#.#...###...#.....#...#...#...........#...#...#.....#.#.#.#...#...#.#...#...#.............#
#.###.#########.#.#.#.#.#############.#.#.#.#.#.###.###################.#.#.#.#################.#.###.#.#.#.#####.#.#.#.###.#.#.###########.#
#.....#.........#...#.#.###...........#...#...#...#.....###...#...#...#...#.#.................#...###...#...#...#.#...#.....#...#...........#
#######.#############.#.###.#####################.#####.###.#.#.#.#.#.#####.#################.###############.#.#.###############.###########
###...#...........#...#...#...#.......###...#...#.....#...#.#.#.#...#.#...#.#.................###.............#.#.#...#...#...###...........#
###.#.###########.#.#####.###.#.#####.###.#.#.#.#####.###.#.#.#.#####.#.#.#.#.###################.#############.#.#.#.#.#.#.#.#############.#
#...#...#...#.....#...#...###.#.#.....#...#...#.#...#...#.#.#.#.#...#...#...#...............#...#.......#.....#...#.#.#.#.#.#.###...#.......#
#.#####.#.#.#v#######.#.#####.#.#.#####.#######.#.#.###.#.#.#.#v#.#.#######################.#.#.#######.#.###.#####.#.#.#.#.#.###.#.#.#######
#...#...#.#.#.>.#...#.#.....#...#.....#.#.......#.#.###.#.#.#.>.>.#.#.......#...#.........#.#.#.#.....#...###.....#.#.#.#.#.#.#...#...#.....#
###.#.###.#.#v#.#.#.#.#####.#########.#.#.#######.#.###.#.#.###v###.#.#####.#.#.#.#######.#.#.#.#.###.###########.#.#.#.#.#.#.#.#######.###.#
#...#...#.#.#.#.#.#...#.....#.........#.#.......#.#.#...#...#...###...#...#.#.#.#.......#...#.#.#...#...#.........#.#.#.#.#.#.#.......#.#...#
#.#####.#.#.#.#.#.#####.#####.#########.#######.#.#.#.#######.#########.#.#.#.#.#######.#####.#.###.###.#.#########.#.#.#.#.#.#######.#.#.###
#.....#...#.#.#.#.#.....#...#...#.....#.#.......#.#.#.......#.....#.....#...#.#.........#...#.#.###.#...#.....#...#.#.#.#...#.......#...#...#
#####.#####.#.#.#.#.#####.#.###.#.###.#.#.#######.#.#######.#####.#.#########.###########.#.#.#.###.#.#######v#.#.#.#.#.###########.#######.#
#.....#...#...#.#.#.#...#.#...#.#...#...#...#...#.#.#.......###...#.#.......#.......#...#.#.#.#.#...#...#...>.>.#.#.#.#.......#.....#...#...#
#.#####.#.#####.#.#.#.#.#.###.#.###.#######.#.#.#.#.#.#########.###.#.#####.#######.#.#.#.#.#.#.#.#####.#.###v###.#.#.#######.#.#####.#.#v###
#.......#.....#...#...#.#.#...#.....#.......#.#...#.#...#.......#...#.#.....#.......#.#...#.#.#.#.#...#...#...#...#.#.#...#...#...#...#.>.###
#############.#########.#.#.#########.#######.#####.###.#.#######.###.#.#####.#######.#####.#.#.#.#.#.#####.###.###.#.#.#.#.#####.#.#####v###
#...#.....#...###.......#.#.....#...#.......#.....#.#...#.......#...#.#.#...#.......#.....#...#.#.#.#.....#.#...#...#.#.#.#...#...#.#.....###
#.#.#.###.#.#####.#######.#####.#.#.#######.#####.#.#.#########.###.#.#.#.#.#######v#####.#####.#.#.#####.#.#.###.###.#.#.###.#.###.#.#######
#.#.#...#...#.....#.....#.#.....#.#.#...#...#...#.#.#.#.........###.#.#.#.#.#...#.>.>.....#.....#.#.....#.#.#...#.###.#.#.#...#.....#.....###
#.#.###.#####.#####.###.#.#.#####.#.#.#.#v###.#.#.#.#.#.###########.#.#.#.#.#.#.#.#v#######.#####.#####.#.#.###.#.###.#.#.#.#############.###
#.#.....#...#...#...#...#.#...#...#...#.>.>...#...#...#...........#.#.#.#.#.#.#...#.......#.#...#.#.....#.#.###.#...#...#...###...###...#...#
#.#######.#.###.#.###.###.###.#.#########v#######################.#.#.#.#.#.#.###########.#.#.#.#.#.#####.#.###.###.###########.#.###.#.###.#
#.........#...#.#...#...#.#...#.###.....#.........#.......#.......#...#...#...#...........#...#.#.#.....#.#...#.....#.....#...#.#.#...#...#.#
#############.#.###.###.#.#.###.###.###.#########.#.#####.#.###################.###############.#.#####.#.###.#######.###.#.#.#.#.#.#####.#.#
#.............#...#.###...#.....#...###.#.........#.....#.#...#...#.....#.....#...............#.#.#.....#...#...#...#.#...#.#.#.#...#####.#.#
#.###############.#.#############.#####.#.#############.#.###.#.#.#.###.#.###.###############.#.#.#.#######.###.#.#.#.#.###.#.#.#########.#.#
#...............#.#.#...#...#.....#...#.#.....#.........#.....#.#.#...#...#...#...........#...#...#.......#.....#.#...#.#...#.#.........#...#
###############.#.#.#.#.#.#.#.#####.#.#.#####.#.###############.#.###.#####.###.#########.#.#############.#######.#####.#.###.#########.#####
#.........#.....#...#.#.#.#.#.....#.#.#.......#.........#.....#.#.#...#.....###.........#...###...........###...#...#...#.#...#...#.....#...#
#.#######.#.#########.#.#.#.#####.#.#.#################.#.###.#.#.#.###.###############.#######.#############.#.###.#.###.#.###.#.#v#####.#.#
#.#...###...#...###...#.#.#...#...#.#.#.....#...#...#...#...#.#.#.#...#.#...#...#####...#.....#...........#...#...#.#.#...#...#.#.>.#...#.#.#
#.#.#.#######.#.###.###.#.###.#.###.#.#.###.#.#.#.#.#.#####.#.#.#.###.#.#.#.#.#.#####v###.###.###########.#.#####.#.#.#.#####.#.###v#.#.#.#.#
#...#.....#...#.....#...#.#...#.###.#.#...#.#.#.#.#.#...###.#.#.#.#...#.#.#.#.#...#.>.>.#.#...#...#.......#.....#...#.#...###.#.#...#.#.#.#.#
#########.#.#########.###.#.###v###.#.###.#.#.#.#.#.###v###.#.#.#.#.###.#.#.#.###.#.###.#.#.###.#.#.###########.#####.###.###.#.#.###.#.#.#.#
#...#.....#.........#...#.#.#.>.>.#.#...#.#.#.#.#.#.#.>.>.#.#.#.#.#.#...#.#.#...#.#...#...#...#.#.#.....#.....#.....#...#.#...#.#.#...#...#.#
#.#.#.#############.###.#.#.#.###.#.###.#.#.#.#.#.#.#.###.#.#.#.#.#.#.###.#.###.#.###.#######.#.#.#####v#.###.#####.###.#.#.###.#.#.#######.#
#.#.#.......#...###...#...#.#.#...#...#.#.#.#.#.#.#.#.###.#.#.#.#.#.#...#.#.#...#.#...#.......#.#...#.>.>.###...#...###.#.#...#.#.#.#.......#
#.#.#######.#.#.#####.#####.#.#.#####.#.#.#.#.#.#.#.#.###.#.#.#.#.#.###.#.#.#.###.#.###.#######.###.#.#########.#.#####.#.###.#.#.#.#.#######
#.#.........#.#.#...#.....#...#...#...#...#.#.#...#.#.#...#.#.#.#.#.#...#.#.#...#.#.###.#...#...#...#...#.......#.....#.#.#...#.#.#.#.#...###
#.###########.#.#.#.#####.#######.#.#######.#.#####.#.#.###.#.#.#.#.#.###.#.###.#.#.###.#.#.#.###.#####.#.###########.#.#.#.###.#.#.#.#.#.###
#.#...#...#...#.#.#.#...#.......#.#.....#...#.....#.#.#...#.#.#.#.#.#...#.#.#...#.#...#.#.#.#.###...#...#.......#.....#...#...#.#...#...#...#
#.#.#.#.#.#.###.#.#.#.#.#######.#.#####.#.#######.#.#.###.#.#.#.#.#.###.#.#.#.###.###.#.#.#.#.#####.#.#########.#.###########.#.###########.#
#.#.#.#.#.#...#.#.#.#.#.#.......#.#.....#.#...#...#.#...#.#.#.#.#.#.###.#.#.#...#...#.#.#.#.#...#...#.......###.#.......#...#...###...#...#.#
#.#.#.#.#.###.#.#.#.#.#.#.#######.#.#####.#.#.#.###.###.#.#.#.#.#.#.###.#.#.###.###.#.#.#.#.###.#.#########.###.#######.#.#.#######.#.#.#.#.#
#.#.#.#.#.#...#.#.#.#.#.#.......#.#.....#.#.#.#.#...#...#.#.#.#.#.#.#...#.#...#...#.#.#.#.#...#.#...#.....#...#...#.....#.#.........#...#...#
#.#.#.#.#.#.###.#.#.#.#.#######.#.#####.#.#.#.#.#.###.###.#.#.#.#.#.#.###.###.###.#.#.#.#.###.#.###.#.###.###.###.#.#####.###################
#...#...#...###...#...#.........#.......#...#...#.....###...#...#...#.....###.....#...#...###...###...###.....###...#####...................#
###########################################################################################################################################.#

51
23/part1.pl Normal file
View File

@@ -0,0 +1,51 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Map),
nth1(1, Map, Row1), nth1(StartY, Row1, '.'),
findall(N, route(Map, visited{}, 1-StartY, N), Ns),
max_list(Ns, Answer),
format('~w <- ~w', [Answer, Ns]), nl.
route(Map, _, X-_, 0) :- length(Map, Height), X =:= Height.
route(Map, Visiteds, X-Y, N) :-
Key is X*1000 + Y, NextVisiteds = Visiteds.put(Key, true),
neighbor(Map, X-Y, X1-Y1, Dist),
NeighborKey is X1*1000 + Y1, \+ _= Visiteds.get(NeighborKey),
route(Map, NextVisiteds, X1-Y1, N1),
N is N1 + Dist.
neighbor(Map, X-Y, NextX-NextY, Dist) :-
( X1 is X + 1, Y1 = Y;
X1 is X - 1, Y1 = Y;
X1 = X, Y1 is Y + 1;
X1 = X, Y1 is Y - 1
),
nth1(X1, Map, Row1), nth1(Y1, Row1, Cell1),
( Cell1 = '.' -> NextX = X1, NextY = Y1, Dist = 1
; Cell1 = '>' -> NextX = X1, NextY is Y1 + 1, Dist = 2
; Cell1 = 'v' -> NextX is X1 + 1, NextY = Y1, Dist = 2
),
( NextX =\= X; NextY =\= Y ).
% input parsing stuff below. Brick indexing is for debugging.
input(FileName, Map) :- phrase_from_file(lines(Map), FileName).
lines([]) --> eos, !.
lines([Line|Lines]) --> line(Line), lines(Lines).
line([]) --> ("\n"; eos), !.
line([C|Chars]) --> [Ascii], line(Chars), {atom_codes(C, [Ascii])}.
% debug
print(Map) :-
findall(
X,
( nth1(X, Map, Line),
format('~3d', [X]), write(" "),
atomic_list_concat(Line, Str), write(Str), nl
),
_),
nl.

90
23/part2.pl Normal file
View File

@@ -0,0 +1,90 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
:- op(700, xfx, is_key_of).
main([FileName|_]) :-
input(FileName, Map, Start, End),
graph(Map, Start, End, Graph),
StartKey is_key_of Start, EndKey is_key_of End,
findall(Dist, walk(Graph, visited{}, StartKey, EndKey, Dist), Dists),
max_list(Dists, Answer),
write(Answer), nl.
walk(_, _, Node, Node, 0).
walk(Graph, Visited, Start, End, Dist) :-
member(Next-NextDist, Graph.get(Start)), \+ _ = Visited.get(Next),
NextVisited = Visited.put(Next, true),
walk(Graph, NextVisited, Next, End, RemainingDist),
Dist is NextDist + RemainingDist.
graph(Map, Start, End, Graph) :-
branch_points(Map, Branches), Nodes = [Start, End|Branches],
findall(
Key-Dists,
( member(Node, Nodes),
dists_from(Map, Nodes, Node, Dists),
Key is_key_of Node),
GraphList),
dict_pairs(Graph, edge, GraphList).
branch_points(Map, Points) :- findall(Point, branch_point(Map, Point), Points).
branch_point(Map, X-Y) :-
nth1(X, Map, Row), nth1(Y, Row, Cell), Cell = '.',
findall(X1-Y1, cell_neighbor(Map, 0-0, X-Y, X1-Y1), Neighbors),
length(Neighbors, N), N > 2.
dists_from(Map, Nodes, N1, Dists) :-
findall(
Key-Dist, (
member(N2, Nodes), \+ N1 = N2,
nobranch_dist(Map, 0-0, N1, N2, Dist),
Key is_key_of N2),
Dists).
is_key_of(Key, X-Y) :- Key is X*1000 + Y.
% nobranch_dist(X1-Y1, X2-Y2, Dist) :- true.
nobranch_dist(_, _, X-Y, X-Y, 0).
nobranch_dist(Map, FromX-FromY, X1-Y1, X2-Y2, Dist) :-
findall(Xn-Yn, cell_neighbor(Map, FromX-FromY, X1-Y1, Xn-Yn), Neighbors),
length(Neighbors, NeighborCount),
\+ (NeighborCount =\= 1, FromX =\= 0), % on a route or at beginning
member(Neighbor, Neighbors),
nobranch_dist(Map, X1-Y1, Neighbor, X2-Y2, NextDist),
Dist is NextDist + 1.
% cell_neighbor finds an adjacent neighbor
cell_neighbor(Map, FromX-FromY, X-Y, X1-Y1) :-
( X1 is X + 1, Y1 = Y;
X1 is X - 1, Y1 = Y;
X1 = X, Y1 is Y + 1;
X1 = X, Y1 is Y - 1
),
nth1(X1, Map, Row1), nth1(Y1, Row1, '.'),
\+ (X1 = FromX, Y1 = FromY).
% input parsing stuff below. Brick indexing is for debugging.
input(FileName, Map, 1-StartY, EndX-EndY) :-
phrase_from_file(lines(Map), FileName),
length(Map, EndX),
nth1(1, Map, StartRow), nth1(StartY, StartRow, '.'),
nth1(EndX, Map, EndRow), nth1(EndY, EndRow, '.').
lines([]) --> eos, !.
lines([Line|Lines]) --> line(Line), lines(Lines).
line([]) --> ("\n"; eos), !.
line(['#'|Chars]) --> "#", line(Chars).
line(['.'|Chars]) --> ("."; ">"; "v"), line(Chars).
% debug
print(Map) :-
findall(
X,
( nth1(X, Map, Line),
format('~3d', [X]), write(" "),
atomic_list_concat(Line, Str), write(Str), nl
),
_),
nl.

23
23/test.txt Normal file
View File

@@ -0,0 +1,23 @@
#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#

300
24/input.txt Normal file
View File

@@ -0,0 +1,300 @@
320870677764563, 335750934489987, 282502845957937 @ -40, -24, 10
219235623600942, 408022798608755, 245679379684914 @ 127, -45, 66
171834827764229, 225154401936948, 232302441670972 @ -122, -521, 95
399408000414510, 365475460204869, 325051385807860 @ -110, -35, -35
382531108862210, 229601570088564, 368913364777462 @ -119, 127, -107
149909452680584, 220522474055239, 215024246295982 @ 198, -73, 190
234205136344037, 217641496634781, 245767047966874 @ 50, 126, 57
154245972353770, 254717416188789, 165260557711000 @ 191, 37, 220
369540770291762, 356366422983125, 93794725449906 @ -15, 24, 216
226276010405492, 209995120939680, 310956910929616 @ -25, 99, -149
268638972773930, 269486309393569, 248987782583720 @ -21, 26, 50
361178860307414, 374454477296229, 399143377389604 @ -6, 7, -83
202257892924214, 270170054947827, 246834210935308 @ -51, -252, 15
277024186353899, 205267962954879, 342506025642424 @ -178, 112, -245
285916480116416, 388917502500415, 279882602663788 @ 60, -19, 31
145282209670550, 224173312800453, 249344095056520 @ 236, -215, -56
208447142984970, 231890691603339, 298351303820520 @ 61, 67, -75
161755191939510, 174118992755389, 243050626677760 @ 55, 212, -35
318313224606524, 381722223896712, 347955048413278 @ 27, -11, -39
305673336971274, 370290487695419, 422379794963756 @ 29, -13, -129
328180566576385, 213760869384648, 251670316230916 @ -34, 151, 55
230518001853761, 324986205789729, 203877601589479 @ 28, -118, 142
144072041563378, 90185316765691, 182011664702202 @ 217, 456, 233
185151810500310, 221100644135477, 245109663655204 @ 91, 60, 43
373850664858040, 293374889307828, 175368782897875 @ -89, 49, 154
328502469179556, 412212496772722, 260184335059001 @ 7, -55, 50
223132851962520, 223914729021163, 327185491912702 @ 76, 119, -80
198895082054102, 271680428770347, 234537752577403 @ 69, -56, 76
252972518344820, 389690617977839, 374418656922750 @ -153, -532, -403
192164383825878, 82139993028623, 120091254828574 @ 55, 519, 466
156955794165198, 191592767579977, 233413147425100 @ 64, -100, 79
165008537629302, 183661085826489, 248545405693408 @ -68, 52, -182
373129120338738, 368249004318254, 320596847750514 @ -71, -33, -27
275929675024853, 194671117708439, 343770846827540 @ -120, 152, -201
173580271204630, 203823601071863, 239810510444710 @ -139, -204, -11
189671912659874, 259317534315745, 248510443915264 @ 33, -150, 16
172552678412603, 76290981681303, 250440858733279 @ 148, 428, 40
127051067028085, 224685590300118, 27951449994759 @ 286, 13, 860
231732307446206, 111700054896778, 381283992852198 @ 93, 285, -115
196775286792290, 250152880582527, 165723767492786 @ -39, -179, 422
198162199765890, 326088593352961, 293740956136336 @ 121, -46, -20
181567111184732, 234940800451055, 284267674024586 @ 60, -61, -144
343894774969218, 251446196757409, 324645022076636 @ -155, 62, -88
154322997480050, 321666831724779, 170189917536370 @ 193, -37, 182
269135111242526, 422914585575117, 44342942222152 @ 18, -177, 368
174234267626414, 206600028766149, 247203043132680 @ 48, 7, -6
301438105099034, 196682335636041, 242355832704292 @ -37, 168, 65
52382227177535, 137282743662489, 90222907787860 @ 304, 242, 231
287281969399048, 189476966098213, 378020645240557 @ -65, 174, -200
329003376176988, 435916939135103, 202654481427582 @ 20, -61, 110
183674690012150, 191781746274329, 227891029018460 @ -72, 68, 123
366646274315090, 480902014044223, 227077161038699 @ -39, -136, 86
209229044274730, 190608724313417, 187400386715900 @ 136, 185, 129
261618567625850, 400563976485889, 144954360500820 @ 12, -178, 228
264216158312450, 334972046672343, 305882984918398 @ 41, -21, -22
466350980510496, 230380017371646, 332577962045002 @ -125, 145, -23
266431863912996, 290874605525783, 509046541048002 @ -10, -7, -418
132224880034084, 176627699412807, 220106637500956 @ 263, 196, 127
300469550083263, 426413462614973, 209018833990362 @ 40, -66, 105
311030442665825, 223508982594159, 255317416767895 @ -72, 120, 42
517047998068110, 239075216009569, 372653857063880 @ -166, 138, -60
170904351760141, 270353076576237, 209565974699113 @ 128, -122, 160
290157904191917, 379443622665012, 427270086392227 @ 42, -29, -140
173867272759940, 201864684820176, 243051222520867 @ 31, 15, 12
241502019378006, 260671504666073, 310914265664160 @ 100, 108, -6
271283475933083, 246303519670521, 255211216146782 @ 78, 130, 57
388021238794645, 216488511621914, 298488270181975 @ -184, 135, -26
335856195264706, 301351728593361, 283609538939852 @ -7, 61, 23
247561156076045, 271177759902429, 244748631170605 @ -154, -146, 38
276696780291055, 333024920521025, 305604257091590 @ -134, -214, -111
190855793421101, 153164710734612, 127049898278632 @ 28, 293, 523
443492983001086, 525011795745025, 422682967987684 @ -88, -142, -107
501694282984570, 467198174503214, 473605302628600 @ -239, -163, -221
335390812504546, 362603263870063, 309797115541008 @ -23, -24, -13
202304759605965, 213333718249183, 246143159635191 @ -94, -12, 8
177782812262045, 206513164309989, 236356130506975 @ -40, -66, 54
388319746430552, 513573144680169, 445043621805025 @ -53, -158, -146
194320135718360, 233248821891459, 202359964193575 @ 107, 80, 144
216771660023300, 171002079515199, 475642130495202 @ -54, 217, -838
234091788605150, 329933100121089, 222620683383130 @ -105, -358, 118
270608669158866, 301404666421918, 259633482142765 @ -7, -15, 34
257130045685842, 324015469970377, 192079653460084 @ 29, -36, 145
177501821940794, 206397724473173, 236757119637960 @ 117, 108, 69
258491080589540, 328713305053641, 347685992911240 @ 96, 54, -31
300241327661250, 317780474139381, 395082844930752 @ -119, -101, -264
166701742849052, 170048803933150, 279009405626079 @ 99, 231, -190
232167716240370, 309180901229489, 364847973540700 @ 82, 9, -110
272472196902758, 211620478899945, 169919557370053 @ 13, 146, 176
155257558622970, 215412500302073, 224645492519800 @ 151, -144, 154
165140630756911, 183872378821537, 189847493429559 @ 88, 138, 392
222481991685145, 381866546445014, 402626810510600 @ 106, -67, -141
195176269606298, 148450429795245, 252663005801584 @ 75, 275, 26
208907792419362, 350794355038735, 294502109591690 @ 89, -129, -36
163731694743470, 222695183641289, 249450695542684 @ 126, -48, -5
209099853718520, 241562956480389, 283990363935025 @ 11, -10, -82
161530626632686, 237419576114933, 319638596916810 @ 175, 68, -106
159481040133596, 158629339034865, 210459590512168 @ 58, 434, 405
123469770101234, 257476537375091, 234494092643914 @ 273, -27, 76
292278978987570, 239866606409241, 199533371820276 @ -149, 43, 162
380447223996379, 374529302108629, 538133423675973 @ -100, -58, -318
192314223519770, 237171949514439, 327368019710830 @ 101, 57, -140
164228470258070, 152786375782989, 233781247174300 @ 40, 443, 75
172857923598290, 361486110825537, 216740814450508 @ 111, -514, 143
215968779962610, 275822816042585, 255939903446892 @ 71, 5, 35
224072378959655, 219760339836420, 250116723554098 @ -94, 25, 13
341826278658070, 323384093598739, 317050311826650 @ -197, -105, -94
358061026812176, 333431655135129, 501927131792690 @ -36, 22, -226
362057930007880, 364175461079693, 291565175187926 @ -44, -16, 12
254480531220540, 261585394784649, 315221878253440 @ -61, -14, -125
239345238738065, 232921068298368, 52215243122876 @ 66, 115, 351
174121671759746, 253098934851105, 265802255418028 @ 127, -32, -18
336492062606489, 22227894726016, 480141325107541 @ -6, 371, -194
159311142016790, 144443435124894, 222453887189935 @ 117, 461, 172
264027680313710, 285411784271013, 175063578354076 @ 91, 96, 135
162835672903750, 194990375099697, 241394520022980 @ 16, -74, -31
306159412517226, 349026413686307, 514099559091684 @ 29, 11, -229
361674598186586, 541486806728355, 358055452228678 @ -143, -392, -123
387616545088370, 458824259273879, 187757363559110 @ -39, -84, 125
169441075829130, 63304826516582, 231410285377464 @ 161, 425, 83
25747427352685, 141237559854084, 110612909830610 @ 344, 241, 221
224941003813670, 293572180292369, 343069410375740 @ 66, -10, -117
230260619234115, 246354839816571, 227616486618513 @ -64, -32, 98
122374062836488, 129460749486351, 248910762647847 @ 283, 340, 32
257926150662427, 296587117999220, 269098859305457 @ 52, 35, 30
356168259600515, 496710404337273, 355767267199108 @ -164, -366, -137
499138453043411, 241721200046286, 383865072357541 @ -150, 135, -72
354351640576530, 333708262751349, 240972379666960 @ -43, 13, 70
414646107949045, 490351310759496, 391588852973424 @ -74, -125, -85
182510517018696, 196037200020145, 254600487937890 @ -88, 18, -106
203067123583136, 283551214233744, 250446375434740 @ 75, -53, 39
252810044109290, 237002938926201, 75981980028076 @ 46, 109, 315
336333829055570, 388442894436689, 291634558906300 @ 18, -7, 22
395076042194198, 359212075803360, 298728466148813 @ -51, 13, 12
158785266271843, 234903117684176, 269092839411151 @ 82, -559, -374
309601884465086, 412842250362681, 306976533326464 @ -32, -144, -28
294030373468330, 396089847565113, 280064088752700 @ 46, -35, 29
331545729822620, 256988945451264, 265060418520925 @ -81, 75, 30
190647571880991, 101485268249820, 252635532573594 @ 68, 439, 17
286448585468055, 517134354733154, 186333249962180 @ 38, -205, 135
153617721701820, 178833230713415, 252336845995825 @ 151, 156, -146
337398961767230, 377411392580169, 378982185725528 @ 6, -8, -72
195250366116408, 193178125048569, 472013133254368 @ 50, 142, -707
185967157561902, 242667813003287, 287987773258452 @ 59, -61, -133
178872203125295, 113519768889594, 13727599749985 @ 166, 275, 340
319415310905105, 435440863922004, 41521512187915 @ -32, -155, 342
251586814655345, 344865748855428, 269612063292298 @ 26, -89, 17
269246730728402, 271578178622193, 279439460804668 @ -88, -31, -32
135533496860438, 278285521115205, 214907869435324 @ 219, 68, 103
276812075909858, 466037129861747, 395316831883066 @ 57, -127, -104
236930960411706, 243177338144695, 356325165559341 @ 65, 96, -112
380460503419598, 324370013927517, 371388836906704 @ -82, 19, -90
427398362574065, 546630525363764, 426883192743950 @ -72, -163, -111
177868966679682, 217731875079305, 213146155346340 @ -143, -315, 324
290284806845247, 119840422044798, 184970876358517 @ -52, 301, 166
340880756829785, 288068023675589, 315817226541790 @ -36, 62, -23
185573708687938, 225251128591507, 128884524503130 @ 137, 113, 264
186685995213874, 219001269459803, 214926674026246 @ -47, -91, 202
199184710396145, 215180367747336, 203151616358728 @ -174, -101, 307
171076784522930, 146981293827499, 60098514063820 @ 177, 233, 271
270206225348270, 249412790853189, 300466015667500 @ -44, 51, -57
234689751989636, 218746599271293, 241908203795056 @ -60, 68, 53
336725151372856, 147342236950433, 199748653670338 @ -16, 234, 118
171003890899833, 195424392282388, 258493155527802 @ -28, -15, -187
149412455352770, 330619018965931, 331721336873716 @ 201, -121, -125
290849090590752, 279180853742381, 288711681503108 @ -65, 6, -25
163782604097570, 201369533990859, 276104464321110 @ -39, -239, -634
383466451055234, 487708948895409, 243501681722620 @ -31, -108, 69
278873443047818, 270949972855161, 295581748376284 @ -178, -79, -103
275483328204728, 359753199304611, 281769098655988 @ 55, -12, 23
155550526665711, 194627020895100, 222697531404203 @ 142, 13, 182
272548165774242, 183113072391357, 219832002893320 @ -71, 184, 109
365051505965124, 358358344604291, 369447127951585 @ -13, 20, -56
226749420175018, 136295903261165, 369261631685233 @ 49, 278, -188
347337565099555, 206264170469455, 435941166700880 @ -14, 168, -141
195173574972300, 288059724160556, 314475702179696 @ 131, 29, -45
290901181489325, 322228161855234, 270950972726635 @ -44, -53, 14
160976286106050, 217995178956129, 225852181163340 @ 97, -185, 147
194862657814148, 311950660223715, 37627659899164 @ 58, -229, 695
187756065196068, 189433672405609, 237753933445096 @ 95, 162, 67
314662593059606, 457342368237267, 280627472484196 @ 19, -109, 27
267078738018370, 395588665670289, 329017002090900 @ 41, -98, -51
198453105662012, 204180092239497, 125941083981442 @ 60, 118, 388
325515244603174, 193963026836233, 83616169017080 @ -40, 176, 284
306120915804898, 216669056599065, 224566702076996 @ -47, 136, 93
262952419990622, 189332097246832, 339944472270492 @ -123, 161, -225
430090980639770, 542562712338489, 554533524742700 @ -264, -407, -453
153428204187620, 258973851297351, 146351712685552 @ 176, -321, 621
267157324234725, 253300767826406, 247830886471870 @ -44, 39, 49
172772522836510, 205083962044941, 244745265268276 @ -34, -101, -35
196853173091800, 235959594193899, 277157185859790 @ 124, 103, 8
252233329257530, 150771238871169, 214260239540880 @ 99, 226, 98
260490329235010, 420724181636745, 466676682577804 @ 41, -152, -257
184101537716370, 206927897278749, 158088282864140 @ 61, 73, 383
371707477810145, 476446047030789, 395602648788850 @ -32, -114, -91
274680779584238, 328310092921155, 187337441960652 @ 27, -11, 143
199636505804290, 225659286596792, 281910746142349 @ -119, -121, -230
401663498069128, 324128314956575, 308385593557516 @ -122, 11, -17
306117741289910, 332540256531302, 198589750550180 @ 21, 21, 119
303752166745505, 329304097065024, 339365273624437 @ 16, 17, -48
154413399453767, 281779422496148, 262521206679621 @ 162, -631, -148
236991415564934, 260590948682271, 232297491880996 @ -113, -105, 83
169296005492162, 193449873692505, 238539375195412 @ -55, -36, 13
283385836439282, 243921272996229, 338818675191694 @ -207, -8, -242
360218974366508, 393941699996637, 490793199709315 @ -57, -66, -236
164882077279529, 201483737546733, 335070662943802 @ 124, 70, -427
428414485445962, 492485352755959, 272973136766672 @ -67, -103, 41
264897981595185, 404913442349664, 276336971338210 @ 88, -23, 37
124722508278926, 2134756155381, 110913025404874 @ 227, 384, 208
536832023822247, 453183106296689, 554060222944721 @ -356, -198, -382
319673051050466, 266571988792245, 210279914314000 @ 17, 103, 104
150821661312904, 219455030985091, 231399996053480 @ 194, -19, 89
158037715359790, 284936450761794, 164842330369291 @ 181, -53, 238
255633864100187, 315497864599284, 295466891366890 @ -90, -183, -91
392762975015658, 291483914154943, 229288562346068 @ -35, 89, 83
309403838076114, 336694805164365, 277758585027766 @ 17, 16, 28
287207150932613, 479713650170448, 553028229703657 @ -40, -331, -480
269836626986969, 417558006790458, 212469813227848 @ -6, -215, 115
196645388374976, 244570813283961, 249157458432502 @ 87, 34, 41
339693128219614, 399751916312913, 284680478974716 @ 8, -26, 27
187559549440250, 195622840636569, 254727426356500 @ -148, 13, -116
304935421756516, 348400992185367, 261097863957704 @ 10, -11, 45
394511030167381, 223826480972951, 305918192064792 @ -110, 140, -13
223011594988398, 194915959669899, 221220518871005 @ -131, 112, 134
235558411474303, 407906141462802, 340419559289067 @ -22, -401, -198
168807003005795, 353652818048826, 276081152261755 @ 126, -489, -86
146617870930743, 156137711671265, 286804418224879 @ 220, 330, -284
286018415412552, 353012603278297, 251064479342937 @ 23, -30, 56
447713232441308, 372146255273187, 341273315411916 @ -153, -32, -49
209718676758750, 247605970547674, 218062472885396 @ -19, -63, 135
343626695642120, 155974548843997, 172127764933794 @ -24, 224, 150
345109049048735, 227227570932339, 273481589457805 @ -136, 112, 10
188575330043159, 118850415966657, 305893691628208 @ 134, 298, -45
326608598650070, 151338233232649, 416515828832310 @ -99, 242, -231
360202364064386, 436602737274865, 326789378915772 @ -27, -81, -22
213268194460580, 241549002519749, 394962709612380 @ 84, 80, -217
197614206616568, 139979243625984, 357286465189054 @ 15, 337, -399
227981330842760, 156692554162989, 177379530796580 @ -57, 262, 263
157229517762970, 231523131346089, 46953217581800 @ 167, -44, 890
305219322429448, 177060442180047, 49658860244982 @ -22, 199, 342
152688286391810, 215257107452481, 216706409125996 @ 171, -167, 232
177207555863420, 203871290203365, 289811933188528 @ -74, -84, -479
190420105339073, 167303615894628, 216289073804446 @ 110, 219, 117
266399617296604, 299362499051479, 325520686693096 @ -20, -33, -95
481245676627650, 436807969705569, 314107082127180 @ -202, -116, -19
401026926420116, 374922905725809, 350888777956543 @ -101, -38, -62
256839644239620, 270114181543499, 257354063805598 @ 26, 47, 40
412259775717406, 242736865487561, 424824610687152 @ -86, 128, -130
66312913166752, 101998933996761, 60120199695721 @ 283, 274, 250
506958247823068, 361333863900433, 280005516982136 @ -152, 18, 33
298773017825360, 317212540408089, 312439900056100 @ 12, 22, -21
220796554287875, 214622218414014, 268263970137525 @ -96, 37, -66
189208611146850, 404126141855649, 112689238573260 @ 52, -656, 531
234488363250114, 231264618839649, 204011914548812 @ 65, 112, 126
283632869172150, 177048553588739, 391393075492010 @ -121, 197, -300
502765928206235, 353125800556389, 304677713378740 @ -156, 22, 7
137557454494001, 97473160546677, 94622893468780 @ 242, 470, 559
254064108665930, 262174096766589, 289808760599414 @ 21, 52, -18
182103896038108, 247435234271202, 351160194849282 @ -25, -299, -735
249420082303430, 42226989901413, 97645217504204 @ 36, 421, 303
284785302989480, 255729203245377, 252686569800496 @ -94, 26, 37
233601301711332, 322776864011749, 366444377930614 @ 70, -28, -128
155098520524958, 244934576541158, 226621458469431 @ 165, -241, 122
146746875809578, 160303698184733, 228748383224514 @ 205, 224, 86
326652196225864, 355170482041167, 311279979189430 @ -38, -41, -26
277275275212730, 369459942642369, 282937479061000 @ 59, -14, 24
295687269134249, 340949989963329, 238600197559195 @ 60, 42, 74
279992481783853, 333185439596083, 324914479814764 @ 64, 36, -17
241428276856914, 266598398788449, 362846190951676 @ -40, -38, -260
259307794913743, 295678437418021, 269285719406143 @ 8, -10, 16
240223936950226, 473763454154241, 220230835580308 @ 77, -206, 97
318558193417106, 334499570437515, 230529981435367 @ 25, 36, 82
275715901048290, 73132232580276, 217625216713160 @ 41, 331, 99
193044713426762, 394683970973463, 372101396303146 @ 145, -80, -99
145897314675055, 223949571890769, 176036980234010 @ 232, -230, 585
161440895273330, 237913221256553, 291149659043788 @ 171, 45, -65
264388423069872, 379242075888111, 338900807126713 @ -5, -164, -110
335398015038338, 259239092404812, 434897503751132 @ 9, 115, -129
236716202526674, 332397372379329, 276349416527020 @ 79, -18, 19
322665826998882, 358742494848305, 303038334161388 @ -13, -25, -7
428381456361421, 547609324289011, 198413346560546 @ -70, -160, 113
157290994488974, 206478542217105, 303245653405090 @ 157, 26, -311
395914211954640, 425947992394193, 312165656677126 @ -184, -190, -44
244618900201418, 557301378477177, 207722519450068 @ -51, -810, 147
279386951121298, 308300290814141, 289436636980500 @ -31, -36, -21
306161107858370, 145704637204577, 363514529004748 @ -99, 258, -170
157069989582566, 150135411048405, 169136269193176 @ 168, 309, 356
149210988438152, 134368802376762, 209337851128249 @ 203, 605, 318
332584662588854, 503022806009046, 148583395713307 @ -43, -235, 192
470742777193259, 423305548535160, 228962588950855 @ -160, -77, 84
250671602810546, 250908746265621, 243056875754412 @ -135, -50, 47
531229816428916, 231708023716351, 470967021948546 @ -196, 143, -168
237893080628802, 139019131653901, 131107424449688 @ 73, 254, 227
218711687951546, 159752359004265, 320741448667460 @ 78, 229, -76
165313081944257, 184601129061069, 299424839638243 @ 168, 182, -58
307210181456654, 421282787395425, 479731549148236 @ 37, -54, -177
305412361284452, 343104362104491, 360276884155534 @ -56, -75, -130
368644982396221, 368636169652378, 540142064185444 @ -22, 5, -233
296336361357714, 453921261262757, 388278886187068 @ 53, -79, -77
431597308261298, 363087620944641, 307225389042377 @ -231, -86, -34
302152593338480, 335110106105791, 293583504813410 @ 36, 29, 14

20
24/lol.txt Normal file

File diff suppressed because one or more lines are too long

35
24/part1.pl Normal file
View File

@@ -0,0 +1,35 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Hails),
maplist(xyline, Hails, Lines),
findall(
[N1, N2, X, Y], (
member(N1-L1-X1assert, Lines), member(N2-L2-X2assert, Lines), N1 < N2,
intersect(L1, L2, X-Y), call(X1assert, X), call(X2assert, X),
X >= 200000000000000, X =< 400000000000000,
Y >= 200000000000000, Y =< 400000000000000),
Intersects),
length(Intersects, Answer),
write(Answer), nl.
intersect(A1-B1-C1, A2-B2-C2, X-Y) :-
Det is A1*B2 - A2*B1, Det =\= 0,
X is (B2*C1 - B1*C2) / Det,
Y is (-A2*C1 + A1*C2) / Det.
xyline(N-(X-Y-_-Dx-Dy-_), N-(A-B-C)-Xassert) :-
A = Dy, B is -Dx, C is X*Dy - Y*Dx,
( Dx >= 0 -> Xassert = =<(X); Xassert = >=(X) ).
% input parsing stuff below. Brick indexing is for debugging.
% assumption: no same hail. There are parallels, but no same
input(FileName, Hails) :- phrase_from_file(hails(0, Hails), FileName).
hails(_, []) --> eos, !.
hails(N, [N-(X-Y-Z-Dx-Dy-Dz)|Hails]) -->
blanks, number(X), ",", blanks, number(Y), ",", blanks, number(Z), " @",
blanks, number(Dx), ",", blanks, number(Dy), ",", blanks, number(Dz), "\n",
{NextN is N + 1}, hails(NextN, Hails).

35
24/part2.pl Normal file
View File

@@ -0,0 +1,35 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Hails),
maplist(xyline, Hails, Lines),
findall(
[N1, N2, X, Y], (
member(N1-L1-X1assert, Lines), member(N2-L2-X2assert, Lines), N1 < N2,
intersect(L1, L2, X-Y), call(X1assert, X), call(X2assert, X),
X >= 200000000000000, X =< 400000000000000,
Y >= 200000000000000, Y =< 400000000000000),
Intersects),
length(Intersects, Answer),
write(Answer), nl.
intersect(A1-B1-C1, A2-B2-C2, X-Y) :-
Det is A1*B2 - A2*B1, Det =\= 0,
X is (B2*C1 - B1*C2) / Det,
Y is (-A2*C1 + A1*C2) / Det.
xyline(N-(X-Y-_-Dx-Dy-_), N-(A-B-C)-Xassert) :-
A = Dy, B is -Dx, C is X*Dy - Y*Dx,
( Dx >= 0 -> Xassert = =<(X); Xassert = >=(X) ).
% input parsing stuff below. Brick indexing is for debugging.
% assumption: no same hail. There are parallels, but no same
input(FileName, Hails) :- phrase_from_file(hails(0, Hails), FileName).
hails(_, []) --> eos, !.
hails(N, [N-(X-Y-Z-Dx-Dy-Dz)|Hails]) -->
blanks, number(X), ",", blanks, number(Y), ",", blanks, number(Z), " @",
blanks, number(Dx), ",", blanks, number(Dy), ",", blanks, number(Dz), "\n",
{NextN is N + 1}, hails(NextN, Hails).

27
24/test.txt Normal file
View File

@@ -0,0 +1,27 @@
19, 13, 30 @ -2, 1, -2
18, 19, 22 @ -1, -1, -2
20, 25, 34 @ -2, -2, -4
12, 31, 28 @ -1, -2, -1
20, 19, 15 @ 1, -5, -3
Using first 3 rays
x + a*t1 = 19 - 2t1, y + b*t1 = 13 + t1, z + c*t1 = 30 - 2t1,
x + a*t2 = 18 - t2, y + b*t2 = 19 - t2, z + c*t2 = 22 - 2t2,
x + a*t3 = 20 - 2t3, y + b*t3 = 25 - 2t3, z + c*t3 = 34 - 4t3
9 eqs, 9 unks, thus solution in 3 points.
--> a = -3, b = 1, c = 2, t1 = 5, t2 = 3, t3 = 4, x = 24, y = 13, z = 10
x+a*t1 == 320870677764563-40*t1, y+b*t1 == 335750934489987-24*t1, z+c*t1 == 282502845957937+10*t1,
x+a*t2 == 219235623600942+127*t2, y+b*t2 == 408022798608755-45*t2, z+c*t2 == 245679379684914+66*t2,
x+a*t3 == 171834827764229-122*t3, y+b*t3 == 225154401936948-521*t3, z+c*t3 == 232302441670972+95*t3
sage: x, y, z, a, b, c, t1, t2, t3 = var('x, y, z, a, b, c, t1, t2, t3')
sage: solve([
....: x+a*t1 == 320870677764563-40*t1, y+b*t1 == 335750934489987-24*t1, z+c*t1 == 282502845957937+10*t1,
....: x+a*t2 == 219235623600942+127*t2, y+b*t2 == 408022798608755-45*t2, z+c*t2 == 245679379684914+66*t2,
....: x+a*t3 == 171834827764229-122*t3, y+b*t3 == 225154401936948-521*t3, z+c*t3 == 232302441670972+95*t3],
....: x,y,z,a,b,c,t1,t2,t3)
[[x == 149412455352770, y == 174964385672289, z == 233413147425100, a == 201, b == 202, c == 79, t1 == 711444906273, t2 == 943556327678, t3 == 69419109633]]
sage: 149412455352770+174964385672289+233413147425100
557789988450159

1205
25/input.txt Normal file

File diff suppressed because it is too large Load Diff

91
25/part1.pl Normal file
View File

@@ -0,0 +1,91 @@
% This needs more stack. Try --stack_limit=4G
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Conns),
dict_pairs(Conns, _, ConnsList),
length(ConnsList, NNodes),
findnsols(
1, PartitionSize, (
_ = Conns.StartNode,
iterate(StartNode, EndNode, Conns, _),
atom_length(EndNode, NodeSize3),
PartitionSize is NodeSize3 / 3),
[Side1]),
Side2 is NNodes - Side1,
Answer is Side1*Side2,
write(Answer), nl.
iterate(Node, Node, Graph, Graph) :- length(Graph.Node, 3), !.
iterate(Node0, Node, Graph0, Graph) :-
countall(Graph0.Node0, NeighborCounts),
NeighborCounts = [_-Max|_],
member(Neighbor-Max, NeighborCounts),
combine(Node0, Neighbor, Node1, Graph0, Graph1),
iterate(Node1, Node, Graph1, Graph).
% combine 2 nodes into one, keep all outbound connections
combine(Node1, Node2, Node12, Graph0, Graph) :-
% delete N1 -> N2's and N2 -> N1's
del_dict(Node1, Graph0, OldN1Outs, Graph1),
del_dict(Node2, Graph1, OldN2Outs, Graph2),
remove_all(Node2, OldN1Outs, N1Outs),
remove_all(Node1, OldN2Outs, N2Outs),
% replace N1 -> X and N2 -> Y with N12 -> [X|Y]
atom_concat(Node1, Node2, Node12),
append(N1Outs, N2Outs, N12Outs),
Graph3 = Graph2.put(Node12, N12Outs),
% replace X -> N1 or X -> N2 with X -> N12 (twice if needed)
foldl(replace_outbound_node(Node1, Node12), N1Outs, Graph3, Graph4),
foldl(replace_outbound_node(Node2, Node12), N2Outs, Graph4, Graph).
replace_outbound_node(Node1, Node2, Node, Graph0, Graph) :-
replace_all(Node1, Graph0.Node, Node2, NewList),
Graph = Graph0.put(Node, NewList).
% List has all items in List but with all ItemOut instances replaced with ItemIn
replace_all(_, [], _, []).
replace_all(ItemOut, [ItemOut|List0], ItemIn, [ItemIn|List]) :-
replace_all(ItemOut, List0, ItemIn, List).
replace_all(ItemOut, [X|List0], ItemIn, [X|List]) :-
\+ X = ItemOut,
replace_all(ItemOut, List0, ItemIn, List).
remove_all(_, [], []).
remove_all(X, [X|List], ListOut) :- remove_all(X, List, ListOut).
remove_all(X, [Y|List], [Y|ListOut]) :- \+ X = Y, remove_all(X, List, ListOut).
% countall means Counts is pairs of Item-Count where Item in List, sorted >
countall(List, Counts) :-
foldl(increment, List, count{}, CountsMap),
dict_pairs(CountsMap, _, CountsList),
sort(2, @>=, CountsList, Counts).
increment(X, Ns0, Ns) :- Next is Ns0.get(X, 0) + 1, Ns = Ns0.put(X, Next).
% input parsing stuff below.
input(FileName, Conns) :-
phrase_from_file(conns(ConnsList), FileName),
to_bidi_graph(ConnsList, Conns).
conns([]) --> eos, !.
conns([From-Tos|Conns]) --> node(From), ": ", tos(Tos), conns(Conns).
tos([To]) --> node(To), "\n".
tos([To|Tos]) --> node(To), " ", tos(Tos).
node(Node) --> string_without(": \n", NodeStr), {atom_codes(Node, NodeStr)}.
to_bidi_graph(ConnsList, BidiConnsGraph) :-
dict_pairs(Graph0, conn, ConnsList),
foldl(add_reverse_conns_for, ConnsList, Graph0, BidiConnsGraph).
add_reverse_conns_for(Node-Outbounds, Conns0, Conns1) :-
foldl(add_conn(Node), Outbounds, Conns0, Conns1).
add_conn(ToNode, FromNode, Conns0, Conns1) :-
Current = Conns0.get(FromNode, []),
Conns1 = Conns0.put(FromNode, [ToNode|Current]).
% debug
print(Graph) :- dict_pairs(Graph, _, Lines), maplist(format('~w~n'), Lines).

13
25/test.txt Normal file
View File

@@ -0,0 +1,13 @@
jqt: rhn xhk nvd
rsh: frs pzl lsr
xhk: hfx
cmg: qnr nvd lhk bvb
rhn: xhk bvb hfx
bvb: xhk hfx
pzl: lsr hfx nvd
qnr: nvd
ntq: jqt hfx bvb xhk
nvd: lhk
lsr: lhk
rzs: qnr cmg lsr rsh
frs: qnr lhk lsr