-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.py
245 lines (220 loc) · 6.57 KB
/
10.py
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
243
244
245
import sys
from collections import defaultdict
from itertools import permutations, combinations
import functools
import itertools
import math
import re
import heapq
import time
from aoctools import dijkstra
from aoctools import get_input, submit_result
from math import *
day = 10
year = 2023
inp = get_input(day, year)
inp1 = """
-L|F7
7S-7|
L|7||
-L-J|
L|-JF
"""
inp1 = """
...........
.S-------7.
.|F-----7|.
.||.....||.
.||.....||.
.|L-7.F-J|.
.|..|.|..|.
.L--J.L--J.
...........
"""
inp = inp.strip().split('\n')
pipes = {}
s = None
for r, l in enumerate(inp):
for c, p in enumerate(l.strip()):
if p == '|':
pipes[(r,c)] = [(r-1,c),(r+1,c)], '|'
elif p == '-':
pipes[(r,c)] = [(r,c-1),(r,c+1)], '-'
elif p == 'L':
pipes[(r,c)] = [(r-1,c),(r,c+1)], 'L'
elif p == 'J':
pipes[(r,c)] = [(r-1,c), (r,c-1)], 'J'
elif p == '7':
pipes[(r,c)] = [(r+1,c), (r,c-1)], '7'
elif p == 'F':
pipes[(r,c)] = [(r,c+1), (r+1,c)], 'F'
elif p == '.':
pass
elif p == 'S':
s = (r,c)
nr = len(inp)
nc = len(inp[0].strip())
def part_1():
for j1 in [(-1,0),(1,0),(0,-1),(0,1)]:
n1 = pipes.get((s[0] + j1[0], s[1] + j1[1]))
if not n1:
continue
if n1[0].count(s) == 0:
continue
for j2 in [(-1,0),(1,0),(0,-1),(0,1)]:
if j1 == j2:
continue
n2 = pipes.get((s[0] + j2[0], s[1] + j2[1]))
if not n2:
continue
if n2[0].count(s) == 0:
continue
pipes[s] = [ (s[0] + j1[0], s[1] + j1[1]), (s[0] + j2[0], s[1] + j2[1]) ], None
c = 0
prev = pipes[s][0][0]
curr = s
path = [s]
valid = True
while True:
(d1, d2),w = pipes.get(curr)
if d1 == prev:
prev = curr
curr = d2
elif d2 == prev:
prev = curr
curr = d1
else:
print ("both d1 and d2 not found", n1, n2)
valid = False
break
c += 1
if curr == s:
valid = True
return path, c//2
break
else:
path.append(curr)
if c > len(pipes):
print ("out of range", n1, n2)
valid = False
break
if valid:
break
if valid:
break
inside = set()
def part2():
ps = pipes[s][0]
n1 = ps[0]
n2 = ps[1]
m1 = pipes[n1][1]
m2 = pipes[n2][1]
if m1 in ('-', '7', 'J', 'F', 'L'):
if m2 in ('-', '7', 'J', 'F', 'L'):
w = '-'
elif m2[1] == '|':
if n1[0] < n2[0] and n1[1] < n2[1]:
w = '7'
elif n2[0] < n1[0] and n2[1] < n1[1]:
w = '7'
elif n1[0] > n2[0] and n1[1] < n2[1]:
w = 'J'
elif n2[0] > n1[0] and n2[1] < n1[1]:
w = 'J'
if n1[0] < n2[0] and n1[1] > n2[1]:
w = 'F'
elif n2[0] < n1[0] and n2[1] > n1[1]:
w = 'F'
if n1[0] > n2[0] and n1[1] > n2[1]:
w = 'L'
elif n2[0] > n1[0] and n2[1] > n1[1]:
w = 'L'
elif m2 in ('-', '7', 'J', 'F', 'L'):
if m1 in ('-', '7', 'J', 'F', 'L'):
w = '-'
elif m1 == '|':
if n1[0] < n2[0] and n1[1] < n2[1]:
w = '7'
elif n2[0] < n1[0] and n2[1] < n1[1]:
w = '7'
elif n1[0] > n2[0] and n1[1] < n2[1]:
w = 'J'
elif n2[0] > n1[0] and n2[1] < n1[1]:
w = 'J'
if n1[0] < n2[0] and n1[1] > n2[1]:
w = 'F'
elif n2[0] < n1[0] and n2[1] > n1[1]:
w = 'F'
if n1[0] > n2[0] and n1[1] > n2[1]:
w = 'L'
elif n2[0] > n1[0] and n2[1] > n1[1]:
w = 'L'
if w is None:
valid = False
print ("invalid configuration", n1, n2, m1, m2)
exit(1)
pipes[s] = pipes[s][0], w
sum = 0
for r in range(nr):
nint = 0
inF = False
inL = False
for c in range(nc):
if (r,c) in path:
w = pipes.get((r,c))
if w[1] == 'F':
inF = True
elif w[1] == 'L':
inL = True
elif w[1] == '7':
if inF:
inF = False
elif inL:
nint += 1
inL = False
else:
print( "got 7 but not in F or L", r, c)
break
elif w[1] == 'J':
if inF:
nint += 1
inF = False
elif inL:
inL = False
pass
else:
print( "got 7 but not in F or L", r, c)
break
elif w[1] == '-':
if not inL and not inF:
print( "got - but not in F or L", r, c)
break
elif w[1] == '|':
nint += 1
else:
print ("Unknown w", w)
elif nint % 2 == 1:
inside.add((r,c))
sum += 1
return sum
def print_path():
for r in range(nr):
for c in range(nc):
if (r,c) in path:
w = pipes[(r,c)]
m = { '-': '2500', '|' : '2502', 'L' : '2514', 'J' : '2518', '7' : '2510', 'F' : '250C' }
print ( eval("u'\\u" + m[w[1]] + "'"), end='')
elif (r,c) in inside:
print ( eval("u'\\u" + '2588' + "'"), end='')
else:
print(' ',end='')
print()
print()
path, part1 = part_1()
result_1 = part1
result_2 = part2()
print("Part 1:", result_1)
print("Part 2:", result_2)
print_path()
#submit_result(day, year, 1, result_1)
#submit_result(day, year, 2, result_2)