-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.py
61 lines (48 loc) · 1.49 KB
/
14.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
print(chr(27)+'[2j')
print('\033c')
#f = open('14.input', 'r')
f = open('14.test', 'r')
lines = [x.strip() for x in f.readlines()]
def roll_north(lines):
changed = True
while changed:
changed = False
for y in range(1, len(lines)):
for x in range(len(lines[y])):
if lines[y][x] == 'O' and lines[y-1][x] == '.':
lines[y-1] = lines[y-1][:x] + 'O' + lines[y-1][x+1:]
lines[y] = lines[y][:x] + '.' + lines[y][x+1:]
changed = True
return lines
def rotate_lines(lines):
return list(map("".join, zip(*lines[::-1])))
def calculate_value(lines):
total = 0
for i, line in enumerate(lines[::-1]):
total += line.count('O') * (i+1)
return total
def part1(lines):
new_lines = roll_north(lines)
value = calculate_value(new_lines)
return value
def part2(lines):
visited = {}
values = []
start_index = 0
for i in range(1000):
for _ in range(4):
lines = roll_north(lines)
lines = rotate_lines(lines)
value = calculate_value(lines)
one_line = ''.join(lines)
if one_line in visited:
start_index = visited[one_line]
break
visited[one_line] = i
values.append(value)
cycle = values[start_index:]
mod = (1000000000-1 - start_index) % len(cycle)
result = cycle[mod]
return result
print('Part 1:', part1(lines.copy()))
print('Part 2:', part2(lines.copy()))