-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.solution.py
87 lines (71 loc) · 2.16 KB
/
day14.solution.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
import copy
with open('day14.input.txt') as f:
grid = f.readlines()
grid = [list(line.strip()) for line in grid]
rlen, clen = len(grid), len(grid[0])
def north(grd):
global rlen, clen
grid = copy.deepcopy(grd)
for col in range(clen):
to = 0
for row in range(rlen):
while to < row and grid[to][col] != '.':
to += 1
if grid[row][col] == '#':
to = row + 1
elif grid[row][col] == 'O' and grid[to][col] == '.':
grid[to][col], grid[row][col] = grid[row][col], grid[to][col]
to += 1
return grid
def south(grd):
global rlen, clen
grid = copy.deepcopy(grd)
for col in range(clen):
to = rlen - 1
for row in reversed(range(rlen)):
while to > row and grid[to][col] != '.':
to -= 1
if grid[row][col] == '#':
to = row - 1
elif grid[row][col] == 'O' and grid[to][col] == '.':
grid[to][col], grid[row][col] = grid[row][col], grid[to][col]
to -= 1
return grid
def east(grd):
global rlen, clen
grid = copy.deepcopy(grd)
for row in grid:
to = 0
for ridx in range(rlen):
while to < ridx and row[to] != '.':
to += 1
if row[ridx] == '#':
to = ridx + 1
elif row[ridx] == 'O' and row[to] == '.':
row[ridx], row[to] = row[to], row[ridx]
to += 1
return grid
def west(grd):
global rlen, clen
grid = [list(reversed(row)) for row in grd]
# print(grid)
grid = east(grid)
grid = [list(reversed(row)) for row in grid]
return grid
def weight(grid):
global rlen
wt = 0
for col in range(clen):
for row in range(rlen):
if grid[row][col] == 'O':
wt += rlen - row
return wt
print(weight(north(grid)))
# part 2
count = 1000000000
count = 87 + ((count - 87) % 11) # do visual inspection to get cycle repetition
transformed = copy.deepcopy(grid)
for i in range(count):
transformed = west(south(east(north(transformed))))
wt = weight(transformed)
print(wt)