-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.py
81 lines (53 loc) · 1.33 KB
/
day06.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
import fileinput
from copy import deepcopy
data = [ list(line.strip()) for line in fileinput.input() ]
w = len(data[0])
h = len(data)
for y in range(h):
for x in range(w):
if data[y][x] == '^':
break
else:
continue
break
ix = x
iy = y
themap = deepcopy(data)
d = 0
delta = [ (0,-1), (1,0), (0,1), (-1,0) ]
dx,dy = delta[d]
while True:
themap[y][x] = 'X'
if x+dx>=w or y+dy>=h or x+dx<0 or y+dy<0: break
while themap[y+dy][x+dx] == '#':
d = (d+1) % 4
dx, dy = delta[d]
x += dx
y += dy
#for line in data:
# print(''.join(line))
print(sum(len([x for x in line if x == 'X']) for line in themap))
dir = [ 1,2,4,8 ]
data[iy][ix] = '.'
for y in range(h):
for x in range(w):
if data[y][x] == '.': data[y][x] = 0
def test(ox,oy):
themap = deepcopy(data)
themap[oy][ox] = '#'
x = ix
y = iy
d = 0
dx,dy = delta[d]
while True:
if themap[y][x] & dir[d]:
return 1
themap[y][x] |= dir[d]
if x+dx>=w or y+dy>=h or x+dx<0 or y+dy<0:
return 0
while themap[y+dy][x+dx] == '#':
d = (d+1) % 4
dx, dy = delta[d]
x += dx
y += dy
print(sum( test(x,y) for x in range(w) for y in range(h) if not (x == ix and y == iy) and data[y][x] == 0 ))