aoc23/10/part1.bak
2023-12-10 02:33:41 -08:00

81 lines
2.5 KiB
Plaintext

% distance_from_ess(X-Y, 0) :- nth0_2(X-Y, ess).
distance_from_ess(X-Y, 1) :- ess(X-Y).
path(From, [Via], To, 1) :-
nth0_2(Via, Pipe),
G =.. [Pipe, Via, From, To], G.
path(From, [Via1, Via2 | Vias], To, Dist) :-
Dist1 is Dist - 1,
path(From, [Via1], Via2, 1),
path(Via1, [Via2 | Vias], To, Dist1).
vert(X-Y, X1-Y, X2-Y) :-
( X1 is X+1, X2 is X-1, points_up(X1-Y), points_down(X2-Y)
; X1 is X-1, X2 is X+1, points_down(X1-Y), points_up(X2-Y)),
valid(X1-Y), valid(X2-Y).
horz(X-Y, X-Y1, X-Y2) :-
( Y1 is Y+1, Y2 is Y-1, points_left(X-Y1), points_right(X-Y2)
; Y1 is Y-1, Y2 is Y+1, points_right(X-Y1), points_left(X-Y2)),
valid(X-Y1), valid(X-Y2).
el(X-Y, X1-Y1, X2-Y2) :-
( Y1 = Y, X1 is X-1, Y2 is Y+1, X2 = X, points_down(X1-Y1), points_left(X2-Y2)
; X1 = X, Y1 is Y+1, X2 is X-1, Y2 = Y, points_left(X1-Y1), points_down(X2-Y2)),
valid(X1-Y1), valid(X2-Y2).
jay(X-Y, X1-Y1, X2-Y2) :-
( 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) :-
( 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) :-
( 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).
dot(_, _, _) :- fail.
ess(X-Y, X1-Y1, X2-Y2) :-
nth0_2(X-Y, ess),
findall(Coord, ess_adjacent(Coord), [X1-Y1, X2-Y2]).
ess_adjacent(X1-Y1) :-
nth0_2(X-Y, ess),
( X1 is X-1, Y1 = Y, nth0_2(X1-Y1, El), points_down(El)
; X1 = X, Y1 is Y+1, nth0_2(X1-Y1, El), points_left(El)
; X1 is X+1, Y1 = Y, nth0_2(X1-Y1, El), points_up(El)
; X1 = X, Y1 is Y-1, nth0_2(X1-Y1, El), points_right(El)).
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.
nth0_2(Row-Column, Element) :-
input(Map),
nth0(Row, Map, SubList),
nth0(Column, SubList, Symbol),
symbol_predicate(Symbol, 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).