-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.py
More file actions
118 lines (107 loc) · 4.67 KB
/
day3.py
File metadata and controls
118 lines (107 loc) · 4.67 KB
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
class Walker(object):
def __init__(self, steps):
self.orientation = 'S'
self.coords = (0, 0)
self.steps = steps
self.next_step = 2
self.points = {(0, 0): {'step': 1, 'value': 1}}
self.tabulator = [1]
for i in range(steps)[1:]:
if self.can_i_go_left():
self.turn_left()
self.step_forward()
else:
self.step_forward()
def get_current_distance(self):
return abs(self.coords[0]) + abs(self.coords[1])
def get_current_value(self):
return self.points[self.coords]['value']
def can_i_go_left(self):
if self.orientation == 'S':
target = (self.coords[0] + 1, self.coords[1])
if self.orientation == 'E':
target = (self.coords[0], self.coords[1] + 1)
if self.orientation == 'N':
target = (self.coords[0] - 1, self.coords[1])
if self.orientation == 'W':
target = (self.coords[0], self.coords[1] - 1)
if target in self.points:
return False
else:
return True
def turn_left(self):
if self.orientation == 'S':
self.orientation = 'E'
elif self.orientation == 'E':
self.orientation = 'N'
elif self.orientation == 'N':
self.orientation = 'W'
elif self.orientation == 'W':
self.orientation = 'S'
def sum_value_of_neighbors(self):
sum_neighbors = 0
# N
if (self.coords[0], self.coords[1] + 1) in self.points:
sum_neighbors += self.points[(self.coords[0],
self.coords[1] + 1)]['value']
# E
if (self.coords[0] + 1, self.coords[1]) in self.points:
sum_neighbors += self.points[(self.coords[0] + 1,
self.coords[1])]['value']
# S
if (self.coords[0], self.coords[1] - 1) in self.points:
sum_neighbors += self.points[(self.coords[0],
self.coords[1] - 1)]['value']
# W
if (self.coords[0] - 1, self.coords[1]) in self.points:
sum_neighbors += self.points[(self.coords[0] - 1,
self.coords[1])]['value']
# NE
if (self.coords[0] + 1, self.coords[1] + 1) in self.points:
sum_neighbors += self.points[(self.coords[0] + 1,
self.coords[1] + 1)]['value']
# SE
if (self.coords[0] + 1, self.coords[1] - 1) in self.points:
sum_neighbors += self.points[(self.coords[0] + 1,
self.coords[1] - 1)]['value']
# SW
if (self.coords[0] - 1, self.coords[1] - 1) in self.points:
sum_neighbors += self.points[(self.coords[0] - 1,
self.coords[1] - 1)]['value']
# NW
if (self.coords[0] - 1, self.coords[1] + 1) in self.points:
sum_neighbors += self.points[(self.coords[0] - 1,
self.coords[1] + 1)]['value']
return sum_neighbors
def step_forward(self):
if self.orientation == 'S':
self.coords = (self.coords[0], self.coords[1] - 1)
self.points[self.coords] = {'step': self.next_step,
'value': self.sum_value_of_neighbors()}
self.next_step += 1
elif self.orientation == 'E':
self.coords = (self.coords[0] + 1, self.coords[1])
self.points[self.coords] = {'step': self.next_step,
'value': self.sum_value_of_neighbors()}
self.next_step += 1
elif self.orientation == 'N':
self.coords = (self.coords[0], self.coords[1] + 1)
self.points[self.coords] = {'step': self.next_step,
'value': self.sum_value_of_neighbors()}
self.next_step += 1
elif self.orientation == 'W':
self.coords = (self.coords[0] - 1, self.coords[1])
self.points[self.coords] = {'step': self.next_step,
'value': self.sum_value_of_neighbors()}
self.next_step += 1
def main(steps): # pragma: no cover
w = Walker(steps)
print(w.get_current_distance())
for step in range(steps)[1:]:
w = Walker(step)
if w.get_current_value() > steps:
print('First value bigger than %s: %s' % (steps,
w.get_current_value()))
break
if __name__ == '__main__': # pragma: no cover
main(312051)