aoc23/10/part2.gimp.pl

129 lines
183 KiB
Perl
Raw Normal View History

2023-12-10 05:36:41 -06:00
:- table valid/1.
:- table pipe_at/2.
% :- table path/3.
% :- table path/5.
:- table vert/3. :- table horz/3. :- table el/3. :- table jay/3.
:- table seven/3. :- table eff/3. :- table ess/3.
answer(Answer) :-
% pipe_at(From, ess),
% once(path(From, Via, From)),
% [From | Via] = [74-18, 73-18,73-17,74-17,74-16,75-16,76-16,76-15,76-14,77-14,78-14,79-14,79-13,78-13,77-13,76-13,76-12,76-11,77-11,77-10,76-10,75-10,75-11,75-12,74-12,74-11,74-10,74-9,75-9,75-8,74-8,73-8,73-9,73-10,73-11,72-11,72-10,72-9,72-8,71-8,70-8,70-9,71-9,71-10,71-11,71-12,72-12,73-12,73-13,74-13,75-13,75-14,75-15,74-15,74-14,73-14,72-14,72-15,73-15,73-16,72-16,72-17,72-18,72-19,73-19,73-20,72-20,72-21,72-22,72-23,72-24,72-25,71-25,71-24,71-23,71-22,71-21,71-20,71-19,71-18,71-17,70-17,70-16,71-16,71-15,70-15,70-14,71-14,71-13,70-13,69-13,69-12,70-12,70-11,69-11,69-10,69-9,68-9,68-10,68-11,68-12,67-12,67-11,66-11,66-10,66-9,66-8,66-7,65-7,65-8,65-9,65-10,65-11,64-11,64-10,64-9,63-9,63-10,63-11,63-12,64-12,65-12,66-12,66-13,67-13,68-13,68-14,69-14,69-15,69-16,68-16,67-16,67-17,68-17,69-17,69-18,70-18,70-19,69-19,69-20,70-20,70-21,69-21,69-22,70-22,70-23,69-23,69-24,70-24,70-25,70-26,71-26,72-26,72-27,72-28,72-29,72-30,73-30,73-31,72-31,71-31,71-30,71-29,71-28,71-27,70-27,69-27,69-28,70-28,70-29,69-29,69-30,70-30,70-31,70-32,71-32,72-32,73-32,74-32,74-33,74-34,74-35,74-36,75-36,75-37,74-37,74-38,74-39,75-39,76-39,76-40,76-41,76-42,77-42,77-43,76-43,76-44,77-44,77-45,77-46,77-47,78-47,78-48,77-48,77-49,77-50,77-51,76-51,76-52,76-53,75-53,75-52,75-51,75-50,76-50,76-49,75-49,75-48,76-48,76-47,75-47,75-46,76-46,76-45,75-45,75-44,75-43,75-42,74-42,74-41,75-41,75-40,74-40,73-40,73-41,73-42,73-43,74-43,74-44,74-45,74-46,73-46,73-45,73-44,72-44,72-45,72-46,72-47,73-47,74-47,74-48,73-48,72-48,72-49,72-50,73-50,73-49,74-49,74-50,74-51,73-51,72-51,72-52,73-52,74-52,74-53,73-53,72-53,71-53,71-54,72-54,72-55,73-55,73-54,74-54,75-54,75-55,74-55,74-56,73-56,72-56,72-57,73-57,74-57,75-57,75-56,76-56,76-57,76-58,76-59,76-60,77-60,77-61,78-61,78-62,77-62,77-63,77-64,76-64,76-63,75-63,75-62,76-62,76-61,75-61,75-60,75-59,75-58,74-58,74-59,74-60,74-61,74-62,74-63,73-63,73-62,73-61,72-61,72-60,73-60,73-59,73-58,72-58,72-59,71-59,71-58,71-57,70-57,70-58,70-59,70-60,71-60,71-61,70-61,70-62,69-62,69-61,69-60,68-60,68-59,69-59,69-58,69-57,69-56,70-56,71-56,71-55,70-55,69-55,69-54,70-54,70-53,69-53,68-53,68-52,69-52,70-52,71-52,71-51,70-51,70-50,71-50,71-49,71-48,70-48,70-47,71-47,71-46,71-45,71-44,71-43,72-43,72-42,72-41,72-40,72-39,73-39,73-38,73-37,72-37,72-36,73-36,73-35,73-34,73-33,72-33,71-33,71-34,72-34,72-35,71-35,71-36,71-37,71-38,71-39,71-40,70-40,70-41,71-41,71-42,70-42,70-43,70-44,70-45,70-46,69-46,69-47,69-48,68-48,68-47,68-46,68-45,69-45,69-44,68-44,68-43,69-43,69-42,68-42,68-41,69-41,69-40,68-40,68-39,69-39,70-39,70-38,70-37,70-36,70-35,69-35,69-36,69-37,69-38,68-38,68-37,68-36,68-35,68-34,69-34,70-34,70-33,69-33,69-32,69-31,68-31,68-30,68-29,68-28,67-28,67-27,67-26,68-26,69-26,69-25,68-25,67-25,67-24,68-24,68-23,68-22,68-21,68-20,68-19,68-18,67-18,67-19,67-20,67-21,67-22,67-23,66-23,66-24,65-24,64-24,64-25,65-25,66-25,66-26,65-26,64-26,64-27,65-27,66-27,66-28,66-29,67-29,67-30,67-31,67-32,68-32,68-33,67-33,67-34,67-35,67-36,67-37,67-38,67-39,66-39,66-38,66-37,66-36,66-35,66-34,66-33,66-32,66-31,66-30,65-30,65-29,65-28,64-28,64-29,63-29,63-30,64-30,64-31,65-31,65-32,65-33,65-34,65-35,65-36,65-37,64-37,64-38,64-39,65-39,65-40,66-40,67-40,67-41,67-42,67-43,67-44,67-45,66-45,66-46,66-47,67-47,67-48,67-49,68-49,69-49,69-50,69-51,68-51,68-50,67-50,67-51,66-51,66-50,65-50,65-51,65-52,66-52,66-53,67-53,67-54,68-54,68-55,67-55,67-56,67-57,68-57,68-58,67-58,67-59,66-59,66-60,67-60,67-61,66-61,65-61,65-60,65-59,65-58,66-58,66-57,65-57,64-57,64-56,64-55,65-55,65-56,66-56,66-55,66-54,65-54,65-53,64-53,64-52,63-52,63-51,64-51,64-50,64-49,65-49,66-49,66-48,65-48,64-48,64-47,65-47,65-46,64-46,64-45,65-45,65-44,66-44,66-43,66-42,66-41,65-41,65-42,65-43,64-43,64-42,63-42,63-43,63-44,63-45,63-46,62-46,62-45,62-44,62-43,62-42,61-42,61-41,62-41,63-41,64-41,64-40,63-40,63-39,62-39,62-40,61-40,61-39,61-38,62-38,63-38,63-37,63-36,64-36,64-35,64-34,64-33,64-32,63-32,63-33,63-34,63-35,62-35,62-36,62-37,61-37,61-36,61-35,60-35,60-34,60-33,61-33,61-34,62-34,62-33,62-32,62-31,62-30,62-29,61-29,61-30,61-31,61-32,60-32,60-31,59-31,59-30,58-30,58-29,58
% length([From | Via], ViaLen),
% once((ess_adjacent(Curr), path(From, Curr, From, 1, ViaLen))),
% writef('%t -> %t\n', [From, Via]),
input(Map), length(Map, MapHeight), MapMax is MapHeight - 1,
findall(X, between(0, MapMax, X), Xs),
maplist([X, 0]>>(println(X)), Xs, _),
% Answer is ViaLen div 2
true .
println(X) :-
input([Line1 | _]), length(Line1, LineLen), LineMax is LineLen - 1,
findall(X-Y, between(0, LineMax, Y), Coords),
maplist([A, 0]>>((
[From | Via] = [74-18, 73-18,73-17,74-17,74-16,75-16,76-16,76-15,76-14,77-14,78-14,79-14,79-13,78-13,77-13,76-13,76-12,76-11,77-11,77-10,76-10,75-10,75-11,75-12,74-12,74-11,74-10,74-9,75-9,75-8,74-8,73-8,73-9,73-10,73-11,72-11,72-10,72-9,72-8,71-8,70-8,70-9,71-9,71-10,71-11,71-12,72-12,73-12,73-13,74-13,75-13,75-14,75-15,74-15,74-14,73-14,72-14,72-15,73-15,73-16,72-16,72-17,72-18,72-19,73-19,73-20,72-20,72-21,72-22,72-23,72-24,72-25,71-25,71-24,71-23,71-22,71-21,71-20,71-19,71-18,71-17,70-17,70-16,71-16,71-15,70-15,70-14,71-14,71-13,70-13,69-13,69-12,70-12,70-11,69-11,69-10,69-9,68-9,68-10,68-11,68-12,67-12,67-11,66-11,66-10,66-9,66-8,66-7,65-7,65-8,65-9,65-10,65-11,64-11,64-10,64-9,63-9,63-10,63-11,63-12,64-12,65-12,66-12,66-13,67-13,68-13,68-14,69-14,69-15,69-16,68-16,67-16,67-17,68-17,69-17,69-18,70-18,70-19,69-19,69-20,70-20,70-21,69-21,69-22,70-22,70-23,69-23,69-24,70-24,70-25,70-26,71-26,72-26,72-27,72-28,72-29,72-30,73-30,73-31,72-31,71-31,71-30,71-29,71-28,71-27,70-27,69-27,69-28,70-28,70-29,69-29,69-30,70-30,70-31,70-32,71-32,72-32,73-32,74-32,74-33,74-34,74-35,74-36,75-36,75-37,74-37,74-38,74-39,75-39,76-39,76-40,76-41,76-42,77-42,77-43,76-43,76-44,77-44,77-45,77-46,77-47,78-47,78-48,77-48,77-49,77-50,77-51,76-51,76-52,76-53,75-53,75-52,75-51,75-50,76-50,76-49,75-49,75-48,76-48,76-47,75-47,75-46,76-46,76-45,75-45,75-44,75-43,75-42,74-42,74-41,75-41,75-40,74-40,73-40,73-41,73-42,73-43,74-43,74-44,74-45,74-46,73-46,73-45,73-44,72-44,72-45,72-46,72-47,73-47,74-47,74-48,73-48,72-48,72-49,72-50,73-50,73-49,74-49,74-50,74-51,73-51,72-51,72-52,73-52,74-52,74-53,73-53,72-53,71-53,71-54,72-54,72-55,73-55,73-54,74-54,75-54,75-55,74-55,74-56,73-56,72-56,72-57,73-57,74-57,75-57,75-56,76-56,76-57,76-58,76-59,76-60,77-60,77-61,78-61,78-62,77-62,77-63,77-64,76-64,76-63,75-63,75-62,76-62,76-61,75-61,75-60,75-59,75-58,74-58,74-59,74-60,74-61,74-62,74-63,73-63,73-62,73-61,72-61,72-60,73-60,73-59,73-58,72-58,72-59,71-59,71-58,71-57,70-57,70-58,70-59,70-60,71-60,71-61,70-61,70-62,69-62,69-61,69-60,68-60,68-59,69-59,69-58,69-57,69-56,70-56,71-56,71-55,70-55,69-55,69-54,70-54,70-53,69-53,68-53,68-52,69-52,70-52,71-52,71-51,70-51,70-50,71-50,71-49,71-48,70-48,70-47,71-47,71-46,71-45,71-44,71-43,72-43,72-42,72-41,72-40,72-39,73-39,73-38,73-37,72-37,72-36,73-36,73-35,73-34,73-33,72-33,71-33,71-34,72-34,72-35,71-35,71-36,71-37,71-38,71-39,71-40,70-40,70-41,71-41,71-42,70-42,70-43,70-44,70-45,70-46,69-46,69-47,69-48,68-48,68-47,68-46,68-45,69-45,69-44,68-44,68-43,69-43,69-42,68-42,68-41,69-41,69-40,68-40,68-39,69-39,70-39,70-38,70-37,70-36,70-35,69-35,69-36,69-37,69-38,68-38,68-37,68-36,68-35,68-34,69-34,70-34,70-33,69-33,69-32,69-31,68-31,68-30,68-29,68-28,67-28,67-27,67-26,68-26,69-26,69-25,68-25,67-25,67-24,68-24,68-23,68-22,68-21,68-20,68-19,68-18,67-18,67-19,67-20,67-21,67-22,67-23,66-23,66-24,65-24,64-24,64-25,65-25,66-25,66-26,65-26,64-26,64-27,65-27,66-27,66-28,66-29,67-29,67-30,67-31,67-32,68-32,68-33,67-33,67-34,67-35,67-36,67-37,67-38,67-39,66-39,66-38,66-37,66-36,66-35,66-34,66-33,66-32,66-31,66-30,65-30,65-29,65-28,64-28,64-29,63-29,63-30,64-30,64-31,65-31,65-32,65-33,65-34,65-35,65-36,65-37,64-37,64-38,64-39,65-39,65-40,66-40,67-40,67-41,67-42,67-43,67-44,67-45,66-45,66-46,66-47,67-47,67-48,67-49,68-49,69-49,69-50,69-51,68-51,68-50,67-50,67-51,66-51,66-50,65-50,65-51,65-52,66-52,66-53,67-53,67-54,68-54,68-55,67-55,67-56,67-57,68-57,68-58,67-58,67-59,66-59,66-60,67-60,67-61,66-61,65-61,65-60,65-59,65-58,66-58,66-57,65-57,64-57,64-56,64-55,65-55,65-56,66-56,66-55,66-54,65-54,65-53,64-53,64-52,63-52,63-51,64-51,64-50,64-49,65-49,66-49,66-48,65-48,64-48,64-47,65-47,65-46,64-46,64-45,65-45,65-44,66-44,66-43,66-42,66-41,65-41,65-42,65-43,64-43,64-42,63-42,63-43,63-44,63-45,63-46,62-46,62-45,62-44,62-43,62-42,61-42,61-41,62-41,63-41,64-41,64-40,63-40,63-39,62-39,62-40,61-40,61-39,61-38,62-38,63-38,63-37,63-36,64-36,64-35,64-34,64-33,64-32,63-32,63-33,63-34,63-35,62-35,62-36,62-37,61-37,61-36,61-35,60-35,60-34,60-33,61-33,61-34,62-34,62-33,62-32,62-31,62-30,62-29,61-29,61-30,61-31,61-32,60-32,60-31,59-31,59-30,58-30,58-29,
member(A, [From | Via]) -> nth0_2(A, E), writef('%n', [E]); write('.'))),
Coords, _),
write('\n').
% path(From, Curr, To, Dist, Final) :-
% pipe_at(Curr, CurrPipe),
% writef('%t -> %t (%t)\n', [From, Curr, CurrPipe]),
% CurrG =.. [CurrPipe, Curr, From, Next], CurrG,
% ( Next = To
% -> Final = Dist
% ; NewDist is Dist + 1,
% path(Curr, Next, To, NewDist, Final)
% ).
path(From, [Via], To) :-
pipe_at(Via, ViaPipe),
ViaG =.. [ViaPipe, Via, From, To], ViaG.
path(From, [Via1, Via2 | Vias], To) :-
path(From, [Via1], Via2),
path(Via1, [Via2 | Vias], To).
dot(_, _, _) :- fail.
vert(X-Y, X1-Y, X2-Y) :-
pipe_at(X-Y, vert),
( X1 is X+1, X2 is X-1
; X1 is X-1, X2 is X+1),
valid(X1-Y), valid(X2-Y).
horz(X-Y, X-Y1, X-Y2) :-
pipe_at(X-Y, horz),
( Y1 is Y+1, Y2 is Y-1
; Y1 is Y-1, Y2 is Y+1),
valid(X-Y1), valid(X-Y2).
el(X-Y, X1-Y1, X2-Y2) :-
pipe_at(X-Y, el),
( Y1 = Y, X1 is X-1, Y2 is Y+1, X2 = X
; X1 = X, Y1 is Y+1, X2 is X-1, Y2 = Y),
valid(X1-Y1), valid(X2-Y2).
jay(X-Y, X1-Y1, X2-Y2) :-
pipe_at(X-Y, jay),
( Y1 = Y, X1 is X-1, Y2 is Y-1, X2 = X
; X1 = X, Y1 is Y-1, X2 is X-1, Y2 = Y),
valid(X1-Y1), valid(X2-Y2).
seven(X-Y, X1-Y1, X2-Y2) :-
pipe_at(X-Y, seven),
( Y1 = Y, X1 is X+1, Y2 is Y-1, X2 = X
; X1 = X, Y1 is Y-1, X2 is X+1, Y2 = Y),
valid(X1-Y1), valid(X2-Y2).
eff(X-Y, X1-Y1, X2-Y2) :-
pipe_at(X-Y, eff),
( Y1 = Y, X1 is X+1, Y2 is Y+1, X2 = X
; X1 = X, Y1 is Y+1, X2 is X+1, Y2 = Y),
valid(X1-Y1), valid(X2-Y2).
ess(X-Y, X1-Y1, X2-Y2) :-
pipe_at(X-Y, ess),
findall(Coord, ess_adjacent(Coord), [X1-Y1, X2-Y2]).
ess_adjacent(X1-Y1) :-
pipe_at(X-Y, ess),
( X1 is X-1, Y1 = Y, pipe_at(X1-Y1, El), points_down(El)
; X1 = X, Y1 is Y+1, pipe_at(X1-Y1, El), points_left(El)
; X1 is X+1, Y1 = Y, pipe_at(X1-Y1, El), points_up(El)
; X1 = X, Y1 is Y-1, pipe_at(X1-Y1, El), points_right(El)),
valid(X1-Y1).
points_down(El) :- (El=ess; El=vert; El=seven; El=eff).
points_left(El) :- (El=ess; El=horz; El=jay; El=seven).
points_up(El) :- (El=ess; El=vert; El=el; El=jay).
points_right(El) :- (El=ess; El=horz; El=el; El=eff).
valid(X-Y) :-
input(Map), length(Map, NRows),
Map = [Row0 | _], length(Row0, NCols),
X >= 0, Y >= 0,
X < NRows, Y < NCols.
% Element is at (Row, Column) in the map.
pipe_at(Row-Column, Element) :-
input(Map),
nth0(Row, Map, SubList),
nth0(Column, SubList, Symbol),
symbol_predicate(Symbol, Element).
nth0_2(Row-Column, Element) :-
input(Map),
nth0(Row, Map, SubList),
nth0(Column, SubList, Element).
symbol_predicate("|", vert).
symbol_predicate("-", horz).
symbol_predicate("L", el).
symbol_predicate("J", jay).
symbol_predicate("7", seven).
symbol_predicate("F", eff).
symbol_predicate(".", dot).
symbol_predicate("S", ess).