-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11-linked-list-nope.py
100 lines (73 loc) · 1.79 KB
/
day11-linked-list-nope.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
import sys
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
stones = ""
class Stone:
def __init__(self, value):
self.value = value
self.next = None
def set_next(self,next):
self.next = next
def __str__(self):
return "value: "+str(self.value)
def blink(self):
if self.value == 0:
self.value = 1
if self.next:
self.next.blink()
elif len(str(self.value)) % 2 == 0:
print(self.value)
firstval = str(self.value)[:int(len(str(self.value))/2)]
secondval = str(self.value)[int(len(str(self.value))/2):]
self.value = int(firstval)
st = Stone(int(secondval))
st.set_next(self.next)
self.next = st
if st.get_next():
st.get_next().blink()
else:
self.value = self.value * 2024
if self.next:
self.next.blink()
def get_next(self):
return self.next
def get_value(self):
return self.value
def print_chain(start):
ret = str(start.get_value())
add = start.get_next()
while add:
ret += " "+str(add.get_value())
add = add.get_next()
print(ret)
def count_chain(start):
ret = 1
add = start.get_next()
while add:
ret += 1
add = add.get_next()
return ret
with open(inputfile,"r") as input:
for line in input:
stones += line.rstrip()
stones = [int(x) for x in stones.split(" ")]
def run_part1():
firstval = stones.pop(0)
first = Stone(firstval)
prev = first
for val in stones:
stone = Stone(val)
prev.set_next(stone)
prev = stone
for i in range(25):
print("Blink "+str(i+1))
first.blink()
print_chain(first)
print("Number of stones: "+str(count_chain(first)))
def run_part2():
print("result")
run_part1()
run_part2()