-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.jl
242 lines (188 loc) · 5.24 KB
/
20.jl
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
data = split(read(stdin, String), "\n\n", keepempty=false)
mutable struct Tile
name
l
u
r
d
rotation
borders
end
new_tile(name, l, u, r, d; borders=[]) = Tile(name, l, u, r, d, 0, borders)
# Reverse rotation (ltr)
tiles = []
for tile in data
tile = split(tile, '\n', keepempty=false)
name = parse(Int, tile[1][6:end - 1])
tile = [tile[i][j] for i = 1:length(tile), j = 1:length(tile[1])]
l = tile[end:-1:2, 1]
u = tile[2, :]
r = tile[2:end, end]
d = tile[end, end:-1:begin]
push!(tiles, new_tile(name, l, u, r, d, borders=[l, u, r, d]))
end
# Find borders
count = 0
# ids[tileid] = #edge
ids = Dict()
for tile in tiles
global count
borders = [tile.l, tile.u, tile.r, tile.d]
for (borderi, border) in enumerate(borders)
match = false
for tile2 in tiles
if tile2 == tile
continue
end
borders2 = [tile2.l, tile2.u, tile2.r, tile2.d]
for border2 in borders2
# Found matching border
if border2 == border || border2 == border[end:-1:begin]
match = true
break
end
end
if match
break
end
end
if !match
count += 1
if haskey(ids, tile.name)
push!(ids[tile.name], borderi)
else
ids[tile.name] = [borderi]
end
end
end
end
corners = [tile for (tile, v) in ids if length(v) == 2]
@assert length(corners) == 4
prod = reduce(*, corners)
println("Part 1 : $prod")
# TODO : Remove
# # Top left corner
# first = [t for t in tiles if t.name == corners[end]][begin]
# println("First : $(first.name)")
# current = first
# for tile in tiles
# global current
# if tile == current
# continue
# end
# match = false
# for (borderi, border) in enumerate(tile.borders)
# if border == current.borders[3]
# # Not flipped
# println(tile.name)
# match = true
# break
# elseif border == current.borders[3][end:-1:begin]
# # Flipped
# println("$(tile.name) flipped $borderi")
# match = true
# break
# end
# end
# if match
# break
# end
# end
# TODO : Remove
# Old part 1
# visited = repeat([false], length(tiles))
# # map[i, j] = tile at i, j
# map = Dict()
# map[(0, 0)] = tiles[begin]
# visited[begin] = true
# println(tiles[begin])
# println(tiles[end])
# mini = -0xffff
# minj = -0xffff
# maxi = 0xffff
# maxj = 0xffff
# # Left
# # rot_id : left = 1, down = 2, ...
# function solve(i, j, ij_new, rot_id)
# global map, visited, tiles
# global mini, minj, maxi, maxj
# if !(mini <= i <= maxi) || !(minj <= j <= maxj)
# return false
# end
# current = map[(i, j)]
# # if current == nothing
# # return false
# # end
# cedges = [current.l, current.u, current.r, current.d]
# if !haskey(map, ij_new)
# for (tilei, (visit, tile)) in enumerate(zip(visited, tiles))
# if !visit
# matches = false
# newedges = [tile.l, tile.u, tile.r, tile.d]
# # Test each rotation possibility
# for rot = 0:3
# if newedges[1 + rot] == cedges[1 + (rot_id + current.rotation - 1) % 4][end:-1:begin] || newedges[1 + rot] == cedges[1 + (rot_id + current.rotation - 1) % 4]
# matches = true
# tile.rotation = rot
# break
# end
# end
# if matches
# visited[tilei] = true
# map[ij_new] = tile
# return true
# end
# end
# end
# end
# # map[ij_new] = nothing
# if rot_id == 1
# minj = max(minj, j)
# elseif rot_id == 2
# mini = max(mini, i)
# elseif rot_id == 3
# maxj = min(maxj, j)
# elseif rot_id == 4
# maxi = min(maxi, i)
# end
# return false
# end
# function solveall(i, j)
# solve(i, j, (i, j - 1), 1) && solveall(i, j - 1)
# solve(i, j, (i - 1, j), 2) && solveall(i - 1, j)
# solve(i, j, (i, j + 1), 3) && solveall(i, j + 1)
# solve(i, j, (i + 1, j), 4) && solveall(i + 1, j)
# end
# # function solvefrom(i, j)
# # global map, visited, tiles
# # solveall(i, j)
# # end
# # solve(0, 0, (-1, 0), 2)
# # solve(0, 0, (-1, 0), 2)
# # solvefrom(0, 0, (-1, 0), 2)
# solveall(0, 0)
# # last = (0, 0)
# # for i = 1:dim(tiles, 1)
# # for j = 1:dim(tiles, 2)
# # solveall(last)
# # end
# # end
# @assert all(visited) "Error : All tiles not visited"
# println()
# println(map)
# # for (k, v) in map
# # if v != nothing
# # (i, j) = k
# # global mini = min(mini, i)
# # global minj = min(minj, j)
# # global maxi = max(maxi, i)
# # global maxj = max(maxj, j)
# # end
# # end
# println(mini)
# println(minj)
# println(maxi)
# println(maxj)
# # println("> $(map[(1, 0)])")
# prod = map[(mini, minj)] * map[(maxi, minj)] * map[(maxi, maxj)] * map[(mini, maxj)]
# println("Part 1 : $prod")