-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd5.py
90 lines (77 loc) · 2.02 KB
/
d5.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
file = open('d5_input.txt')
# file = open('d5_example.txt')
data = []
data2 = []
# Load data into array
for row in file:
data.append([int(row.strip()), 0])
data2.append([int(row.strip()), 0])
# Mark the current position
data[0][1] = 1
data2[0][1] = 1
num_entries = len(data)
def step_partone(data, entries):
done = False
# find current position
steps_to_move = 0
current_position = 0
pos = 0
for entry in data:
if entry[1] == 1:
# print('found stepper at pos: ' + str(pos))
current_position = pos
steps_to_move = entry[0]
pos += 1
# move position
data[current_position][1] = 0
data[current_position][0] += 1
new_pos = current_position + steps_to_move
if (new_pos + 1) > entries:
done = True
else:
data[new_pos][1] = 1
return data, done
def step_parttwo(data, entries):
done = False
# find current position
steps_to_move = 0
current_position = 0
pos = 0
for entry in data:
if entry[1] == 1:
# print('found stepper at pos: ' + str(pos))
current_position = pos
steps_to_move = entry[0]
pos += 1
# move position
data[current_position][1] = 0
if steps_to_move >= 3:
data[current_position][0] -= 1
else:
data[current_position][0] += 1
new_pos = current_position + steps_to_move
if (new_pos + 1) > entries:
done = True
else:
data[new_pos][1] = 1
return data, done
num_steps = 0
finished = False
# print(data)
while not finished:
num_steps += 1
process = step_partone(data, num_entries)
finished = process[1]
data = process[0]
# print(data)
print(' Part one: It took ' + str(num_steps) + ' steps to finish!')
num_steps = 0
finished = False
# print(data2)
while not finished:
num_steps += 1
process = step_parttwo(data2, num_entries)
finished = process[1]
data2 = process[0]
# print(data2)
print(' Part two: It took ' + str(num_steps) + ' steps to finish!')