-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.py
48 lines (40 loc) · 1.35 KB
/
day9.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
# Description: Advent of Code - Day 9
class Oasis:
def __init__(self):
self.lines = []
self.chal1 = 0
self.chal2 = 0
def add(self, line):
self.lines.append(Value(line))
new_values = self.lines[-1].extrapolate()
self.chal1 += new_values[1]
self.chal2 += new_values[0]
class Value:
def __init__(self, line):
self.line = line
self.history = [[int(i) for i in line.split(' ')]]
self.get_diference()
def get_diference(self):
tmp = self.history[-1][0]
res = []
for i in self.history[-1][1::]:
res.append(i - tmp)
tmp = i
self.history.append(res)
if not all(i == 0 for i in self.history[-1]):
self.get_diference()
def extrapolate(self):
last = self.history[-1][-1]
first = self.history[-1][0]
del self.history[-1]
self.history[-1] = [self.history[-1][0]-first] + self.history[-1] + [last+self.history[-1][-1]]
if len(self.history) != 1:
return self.extrapolate()
return (self.history[0][0], self.history[0][-1])
if __name__ == '__main__':
oasis = Oasis()
with open('day9.input', 'r') as f:
for line in f.readlines():
oasis.add(line)
print(f"Challenge 1: {oasis.chal1}")
print(f"Challenge 2: {oasis.chal2}")