-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_11.py
117 lines (98 loc) · 3.23 KB
/
day_11.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from __future__ import division, print_function
import os
from my_utils.tests import test_function
from collections import Counter
def sign(n):
if n < 0:
return -1
else:
return 1
def part_1(steps):
"""Function which calculates the solution to part 1
Remap the hex grid to square grid
\ / \ / \ /
+--+ +--+ 0, 3 +--+
/ \ / \ / \
-+ +--+ 0, 2 +--+ 1, 4 +-
\ / \ / \ /
+--+ 0, 1 +--+ 1, 3 +--+
/ \ / \ / \
-+ 0, 0 +--+ 1, 2 +--+ 2, 4 +-
\ / \ / \ /
+--+ 1, 1 +--+ 2, 3 +--+
/ \ / \ / \
-+ 1, 0 +--+ 2, 2 +--+ 3, 4 +-
\ / \ / \ /
+--+ 2, 1 +--+ 3, 3 +--+
/ \ / \ / \
Arguments
---------
Returns
-------
"""
step_freq = Counter(steps)
# define ne sw as staying on same level
vert = step_freq['s'] + step_freq['se'] \
- step_freq['n'] - step_freq['nw']
horiz = step_freq['ne'] + step_freq['se'] \
- step_freq['sw'] - step_freq['nw']
if sign(horiz) == sign(vert):
diag_steps = min(abs(vert), abs(horiz))
if vert < 0:
vert += diag_steps
else:
vert -= diag_steps
return abs(horiz) + abs(vert)
def part_2(steps):
"""Function which calculates the solution to part 2
Arguments
---------
Returns
-------
"""
max_dist = 0
for ii in range(len(steps)):
dist = part_1(steps[:ii])
if dist > max_dist:
max_dist = dist
return max_dist
def main(test_datas, functions, puzzle_input=None):
for ii, (test_data, fun) in enumerate(zip(test_datas, functions)):
nr_errors = test_function(fun, test_data)
if nr_errors == 0:
print('Pt. {} Tests Passed'.format(ii+1))
if puzzle_input is not None:
fn = os.path.basename(__file__)
for ii, fun in enumerate(functions):
ans = fun(puzzle_input)
print('{} Pt. {} Solution: {}'.format(fn, ii+1, ans))
if __name__ == "__main__":
# Testing data:
# - each element of input list will be passed to function
# - the relative element in output list is the expected output
test_data1 = {
'inputs': [['ne', 'ne', 'ne'],
['ne', 'ne', 'sw', 'sw'],
['ne', 'ne', 's', 's'],
['se', 'sw', 'se', 'sw', 'sw'],
['se', 'sw', 'se', 'sw', 'sw', 'nw'],
['n', 'sw', 'n', 'sw', 'sw']],
'outputs': [3, 0, 2, 3, 3, 3]
}
test_data2 = {
'inputs': [],
'outputs': []
}
# Code to import the actual puzzle input
with open('./inputs/day_11.txt') as f:
puzzle_input = f.read().strip()
puzzle_input = puzzle_input.split(',')
# puzzle_input = f.read().strip()
# puzzle_input = [line.rstrip('\n') for line in f]
# Main call: performs testing and calculates puzzle outputs
main(test_datas=[test_data1],
functions=[part_1, part_2],
puzzle_input=puzzle_input)
# main(test_datas=[test_data1, test_data2],
# functions=[part_1, part_2],
# puzzle_input=puzzle_input)