-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.py
151 lines (115 loc) · 4.27 KB
/
day6.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""for http://adventofcode.com/day/6"""
import numpy as np
from collections import namedtuple
ParsedLine = namedtuple('ParsedLine', ['op', 'start', 'end'])
def read_data(input_file='day6.txt'):
"""reads and loads the contents of input_file, returns a list of the contents"""
with open(input_file, 'r') as f:
data = f.readlines()
data = [line.strip() for line in data]
return data
def generate_grid(grid_type='bool'):
"""generates a fresh light grid with all the lights off"""
if grid_type == 'bool':
grid = np.zeros((1000, 1000), dtype=np.bool)
elif grid_type == 'int':
grid = np.zeros((1000, 1000), dtype=np.int)
return grid
def count_lights(grid):
"""returns the number of lights on in the grid"""
return int(np.sum(grid))
def turn_on(grid, start, end):
"""returns grid with lights turned on from start tuple (x1, y1) to end tuple (x2, y2)"""
start_x, start_y = start
end_x, end_y = end
grid[start_x:end_x + 1, start_y:end_y + 1] = True
return grid
def turn_off(grid, start, end):
"""returns grid with lights turned off from start tuple (x1, y1) to end tuple (x2, y2)"""
start_x, start_y = start
end_x, end_y = end
grid[start_x:end_x + 1, start_y:end_y + 1] = False
return grid
def toggle(grid, start, end):
"""returns a grid of the with the cells toggled from start tuple (x1, y1) to end tuple (x2, y2)"""
start_x, start_y = start
end_x, end_y = end
grid[start_x:end_x + 1, start_y:end_y + 1] = np.invert(grid[start_x:end_x + 1, start_y:end_y + 1])
return grid
def parse_line(line):
"""parses a line of day6.txt in the format:
"turn off 339,840 through 341,844"
"toggle 918,857 through 944,886"
"turn on 68,419 through 86,426"
Usage:
>>> parse_line("turn on 68,419 through 86,426")
ParsedLine(op='turn on', start=(68, 419), end=(86, 426))
>>> pl = parse_line("turn on 68,419 through 86,426")
>>> pl.op
turn on
>>> pl.start
(68, 419)
>>> pl[1]
(68, 419)
returns: namedtuple(op='toggle|turn on|turn off', start=start_tuple, end=end_tuple)
"""
# could get fancier with this, e.g. for op in ('toggle', 'turn on', 'turn off'): if ...
if 'toggle' in line:
op = 'toggle'
if 'turn on' in line:
op = 'turn on'
if 'turn off' in line:
op = 'turn off'
# transform "turn on 68,419 through 86,426" into ['68,419', 'through', '86,426']
coords = ''.join(line.split(op)).strip().split()
start = tuple(int(c) for c in coords[0].split(','))
end = tuple(int(c) for c in coords[2].split(','))
return ParsedLine(op, start, end)
def part1():
"""returns Day 6 Part 1 answer"""
op_map = {
'toggle': toggle,
'turn on': turn_on,
'turn off': turn_off,
}
data = read_data()
grid = generate_grid()
for line in data:
pl = parse_line(line)
grid = op_map[pl.op](grid, pl.start, pl.end)
number_of_lights = count_lights(grid)
print ('Day 6 Part 1 answer: {}'.format(number_of_lights))
return number_of_lights
def turn_on_2(grid, start, end):
"""returns grid with lights turned on from start tuple (x1, y1) to end tuple (x2, y2)"""
start_x, start_y = start
end_x, end_y = end
grid[start_x:end_x + 1, start_y:end_y + 1] += 1
return grid
def turn_off_2(grid, start, end):
"""returns grid with lights turned off from start tuple (x1, y1) to end tuple (x2, y2)"""
start_x, start_y = start
end_x, end_y = end
grid[start_x:end_x + 1, start_y:end_y + 1] -= 1
grid[grid < 0] = 0
return grid
def toggle_2(grid, start, end):
"""returns a grid of the with the cells toggled from start tuple (x1, y1) to end tuple (x2, y2)"""
grid = turn_on_2(grid, start, end)
grid = turn_on_2(grid, start, end)
return grid
def part2():
"""returns Day 6 Part 2 answer"""
op_map = {
'toggle': toggle_2,
'turn on': turn_on_2,
'turn off': turn_off_2,
}
data = read_data()
grid = generate_grid('int')
for line in data:
pl = parse_line(line)
grid = op_map[pl.op](grid, pl.start, pl.end)
number_of_lights = count_lights(grid)
print ('Day 6 Part 2 answer: {}'.format(number_of_lights))
return number_of_lights