2017 - Day 3 (Spiral Memory)
View puzzle description on AdventOfCode.com
Part 1
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
#!/usr/bin/env python3
#Advent of Code
#2017 Day 3, Part 1
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
def solve(puzzle_input):
x = y = 0
while ((x * 2) + 1) ** 2 <= puzzle_input:
x += 1
y += 1
x -= 1
y -= 1
i = ((x * 2) + 1) ** 2
max_coord = x
stage = 3 #0 is moving up, 1 left, 2 down and 3 right
while i < puzzle_input:
if stage == 0:
if y == -max_coord:
stage = 1
y -= 1
elif stage == 1:
if x == -max_coord:
stage = 2
x -= 1
elif stage == 2:
if y == max_coord:
stage = 3
y += 1
elif stage == 3:
if x == max_coord:
stage = 0
x += 1
i += 1
return abs(x) + abs(y)
def main():
puzzle_input = util.read.as_int()
steps = solve(puzzle_input)
print("The number of steps required is " + str(steps) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
self.assertEqual(solve(1), 0)
def test_ex2(self):
self.assertEqual(solve(12), 3)
def test_ex3(self):
self.assertEqual(solve(23), 2)
def test_ex4(self):
self.assertEqual(solve(1024), 31)
if __name__ == "__main__":
run(main)
Part 2
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
#!/usr/bin/env python3
#Advent of Code
#2017 Day 3, Part 2
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
grid = {(0, 0): 1}
def get_grid(x, y):
global grid
try:
to_return = grid[(x,y)]
return to_return
except:
grid[(x,y)] = 0
return grid[(x,y)]
def solve(puzzle_input):
global grid
x = y = 0
max_coord = 0
first_stage_0 = False
last_calculated = 1
stage = 3 #0 is moving up, 1 left, 2 down and 3 right
while last_calculated < puzzle_input:
if stage == 0:
if first_stage_0:
max_coord += 1
y += 1
first_stage_0 = False
if y == -max_coord:
stage = 1
else:
y -= 1
elif stage == 1:
if x == -max_coord:
stage = 2
else:
x -= 1
elif stage == 2:
if y == max_coord:
stage = 3
else:
y += 1
elif stage == 3:
if x == max_coord:
stage = 0
first_stage_0 = True
x += 1
grid[(x,y)] = 0
for i in range(-1, 2):
for j in range(-1, 2):
if i == 0 and j == 0:
continue
else:
grid[(x,y)] += get_grid(x+i,y+j)
last_calculated = grid[(x,y)]
return grid[(x,y)]
def main():
puzzle_input = util.read.as_int()
value = solve(puzzle_input)
print("The first value written that is greater than the puzzle input is " + str(value) + ".")
class AOC_Tests(unittest.TestCase):
def setUp(self):
global grid
grid = {(0, 0): 1}
solve(5)
def test_ex1(self):
self.assertEqual(get_grid(0, 0), 1)
def test_ex2(self):
self.assertEqual(get_grid(1, 0), 1)
def test_ex3(self):
self.assertEqual(get_grid(1, -1), 2)
def test_ex4(self):
self.assertEqual(get_grid(0, -1), 4)
def test_ex5(self):
self.assertEqual(get_grid(-1, -1), 5)
if __name__ == "__main__":
run(main)