-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.pl
156 lines (137 loc) · 4.26 KB
/
06.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
:- use_module(dcgs/dcgs_utils).
:- use_module(fp).
/*
Both parts at once, because we use the path from part 1 on part 2.
Part 1:
1. Parse a list of obstacle positions, and find the starting point.
2. Starting up, 'move/5' until an obstacle, then turn.
3. Repeat until no obstacle was hit.
Part 2 (brute-force):
For each position in the path of part 1, put an obstacle and try to find a
loop.
We record in which direction we hit each obstacle. If the direction repeats
then there is a loop. If the path terminates without the mentioned hit, there
is no loop.
*/
real("06.txt", 129).
sample("06.sample", 9).
matrix(_, _, A, A) --> call(eos).
matrix(_, Y, A0, A1) -->
"\n",
{ Y1 #= Y + 1 },
matrix(0, Y1, A0, A1).
matrix(X, Y, A0-Init0, Res) -->
[Char],
{ Char = (#),
A1 = [X-Y|A0];
dif(Char, (#)),
A1 = A0
},
{ Char = (^),
Init1 = X-Y;
dif(Char, (^)),
Init1 = Init0
},
{ X1 #= X + 1 },
matrix(X1, Y, A1-Init1, Res).
nextDir(up, right).
nextDir(right, down).
nextDir(down, left).
nextDir(left, up).
nextInDirection(_, up, X0-Y0, Map, Positions, Obstacle) :-
findall(Y1, (member(X0-Y1, Map), Y1 #< Y0), Obstacles),
(
list_max(Obstacles, ObstacleY),
Last #= ObstacleY+1,
numlist(Last, Y0, Pos),
maplist(tuple(X0), Pos, Positions),
Obstacle = X0-ObstacleY;
Obstacles = [],
numlist(0, Y0, Pos),
Obstacle = none,
maplist(tuple(X0), Pos, Positions)
).
nextInDirection(Max, down, X0-Y0, Map, Positions, Obstacle) :-
findall(Y1, (member(X0-Y1, Map), Y1 #> Y0), Obstacles),
(
list_min(Obstacles, ObstacleY),
Last #= ObstacleY-1,
numlist(Y0, Last, Pos),
maplist(tuple(X0), Pos, Positions0),
reverse(Positions0, Positions),
Obstacle = X0-ObstacleY;
Obstacles = [],
numlist(Y0, Max, Pos),
maplist(tuple(X0), Pos, Positions0),
reverse(Positions0, Positions),
Obstacle = none
).
nextInDirection(Max, right, X0-Y0, Map, Positions, Obstacle) :-
findall(X1, (member(X1-Y0, Map), X1 #> X0), Obstacles),
(
list_min(Obstacles, ObstacleX),
Last #= ObstacleX-1,
numlist(X0, Last, Pos),
maplist(fliple(Y0), Pos, Positions0),
reverse(Positions0, Positions),
Obstacle = ObstacleX-Y0;
Obstacles = [],
numlist(X0, Max, Pos),
maplist(fliple(Y0), Pos, Positions0),
reverse(Positions0, Positions),
Obstacle = none
).
nextInDirection(_, left, X0-Y0, Map, Positions, Obstacle) :-
findall(X1, (member(X1-Y0, Map), X1 #< X0), Obstacles),
(
list_max(Obstacles, ObstacleX),
Last #= ObstacleX+1,
numlist(Last, X0, Pos),
maplist(fliple(Y0), Pos, Positions),
Obstacle = ObstacleX-Y0;
Obstacles = [],
numlist(0, X0, Pos),
maplist(fliple(Y0), Pos, Positions),
Obstacle = none
).
move(Max, Dir, Map, (X-Y)-Visited, Sol) :-
nextInDirection(Max, Dir, X-Y, Map, Positions, Obstacle),
foldl(\( (Px-Py)^A0^A1^put_assoc(Px-Py, A0, 0, A1)), Positions, Visited, Visited1),
(Obstacle = none,
assoc_to_list(Visited1, Sol);
dif(Obstacle, none),
Positions = [Last|_],
nextDir(Dir, Dir1),
move(Max, Dir1, Map, Last-Visited1, Sol)
).
makesLoop(Max, Map, P) :-
makesLoop(Max, up, Map, P).
makesLoop(Max, Dir, Map, (X-Y)-Obstacles) :-
nextInDirection(Max, Dir, X-Y, Map, Positions, Obstacle),
dif(Obstacle, none),
Positions = [Last|_],
( get_assoc(Obstacle-Dir, Obstacles, bogus), !;
put_assoc(Obstacle-Dir, Obstacles, bogus, Obstacles1),
nextDir(Dir, Dir1),
makesLoop(Max, Dir1, Map, Last-Obstacles1)
).
part1and2(Mode, Sol1, Sol2) :-
call(Mode, F, Max),
empty_assoc(A0),
phrase_from_file(matrix(0, 0, []-(0-0), A1-Init), F),
move(Max, up, A1, Init-A0, Pos),
maplist(fst, Pos, Pos1),
length(Pos, Sol1),
!,
findall(P, (dif(P, Init), member(P, Pos1), makesLoop(Max, [P|A1], Init-A0)), NewObstacles),
length(NewObstacles, Sol2).
run :-
time(
(
part1and2(real, X, Y),
format("Task 1: ~w~n", [X]),
format("Task 2: ~w~n", [Y])
)
),
halt.
:- initialization(run).