-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_14.py
148 lines (126 loc) · 3.55 KB
/
Day_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
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
# -*- coding: utf-8 -*-
"""
Advent of Code 2023, Day 14
https://adventofcode.com/2023/day/14
2023-12-15, 11:31; 2023-12-15, 12:26
"""
### Load Data #################################################################
# read input
filename = 'Day_14.txt'
f = open(filename)
text = f.readlines()
# example input
# text = """
# O....#....
# O.OO#....#
# .....##...
# OO.#O....O
# .O.....O#.
# O.#..O.#.#
# ..O..#O..O
# .......O..
# #....###..
# #OO..#....
# """
# text = text.split('\n')
# text = text[1:-1]
# text = [line+'\n' for line in text]
### Part One ##################################################################
# remove linebreaks
text = [line[:-1] for line in text]
# for each column
for j in range(len(text[0])):
# travel through rows
i = 0
while i < len(text):
if i > 0:
if text[i][j] == 'O' and text[i-1][j] == '.':
text[i-1] = text[i-1][:j] + 'O' + text[i-1][j+1:]
text[i] = text[i][:j] + '.' + text[i][j+1:]
i = i - 1
else:
i = i + 1
else:
i = i + 1
# for each row
my_sum = 0
for i in range(len(text)):
for j in range(len(text[0])):
if text[i][j] == 'O':
my_sum = my_sum + (len(text)-i)
print(my_sum)
### Part Two ##################################################################
# re-read input
filename = 'Day_14.txt'
f = open(filename)
text = f.readlines()
text = [line[:-1] for line in text]
# prepare cycles
N = 200
rows = len(text)
cols = len(text[0])
loads = [0 for n in range(N)]
# perform all cycles
for n in range(N):
# roll north
for j in range(cols):
i = 0
while i < rows:
if i > 0:
if text[i][j] == 'O' and text[i-1][j] == '.':
text[i-1] = text[i-1][:j] + 'O' + text[i-1][j+1:]
text[i] = text[i][:j] + '.' + text[i][j+1:]
i = i - 2
i = i + 1
# roll west
for i in range(rows):
j = 0
while j < cols:
if j > 0:
if text[i][j] == 'O' and text[i][j-1] == '.':
text[i] = text[i][:j-1] + 'O.' + text[i][j+1:]
j = j - 2
j = j + 1
# roll south
for j in range(cols):
i = rows-1
while i > -1:
if i < rows-1:
if text[i][j] == 'O' and text[i+1][j] == '.':
text[i+1] = text[i+1][:j] + 'O' + text[i+1][j+1:]
text[i] = text[i][:j] + '.' + text[i][j+1:]
i = i + 2
i = i - 1
# roll east
for i in range(rows):
j = cols-1
while j > -1:
if j < cols-1:
if text[i][j] == 'O' and text[i][j+1] == '.':
text[i] = text[i][:j] + '.O' + text[i][j+2:]
j = j + 2
j = j - 1
# print result
if n in []:
print(n)
for line in text: print(line)
print()
# calculate total load
my_sum = 0
for i in range(len(text)):
for j in range(len(text[0])):
if text[i][j] == 'O':
my_sum = my_sum + (len(text)-i)
loads[n] = my_sum
print(str(n)+': '+str(my_sum))
# plot total loads
import matplotlib.pyplot as plt
plt.plot(list(range(N)), loads, '-b')
# get ultimale value
N = 1000000000
if max(loads[100:]) > loads[0]: m1 = max(loads[100:])
else: m1 = min(loads[100:])
i1 = loads.index(m1)
i2 = loads[i1+1:].index(m1) + (i1+1)
i3 = (N-1-i1) % (i2-i1) + i1
print(loads[i3])