finish day 11 part 1
This commit is contained in:
parent
274f692004
commit
2f9d118169
83
2018/11.py
83
2018/11.py
@ -1,12 +1,14 @@
|
|||||||
class Grid:
|
class Grid:
|
||||||
def __init__(self, serial):
|
def __init__(self, serial):
|
||||||
self.serial = serial
|
self.serial = serial
|
||||||
|
self.cells = []
|
||||||
|
self.populate()
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
self.cells = []
|
|
||||||
for y in range(1, 301):
|
for y in range(1, 301):
|
||||||
self.cells.append([])
|
self.cells.append([])
|
||||||
for x in range(1, 301):
|
for x in range(1, 301):
|
||||||
|
self.cells[y - 1].append([])
|
||||||
power = self.cell_power(x, y)
|
power = self.cell_power(x, y)
|
||||||
self.set(x, y, power)
|
self.set(x, y, power)
|
||||||
|
|
||||||
@ -16,7 +18,7 @@ class Grid:
|
|||||||
def set(self, x, y, value):
|
def set(self, x, y, value):
|
||||||
self.cells[y - 1][x - 1] = value
|
self.cells[y - 1][x - 1] = value
|
||||||
|
|
||||||
def cell_power(x, y):
|
def cell_power(self, x, y):
|
||||||
rack = x + 10
|
rack = x + 10
|
||||||
p = (rack * y + self.serial) * rack
|
p = (rack * y + self.serial) * rack
|
||||||
p = (p // 100) % 10 # keep only hundreds digit
|
p = (p // 100) % 10 # keep only hundreds digit
|
||||||
@ -25,30 +27,75 @@ class Grid:
|
|||||||
|
|
||||||
class Cursor:
|
class Cursor:
|
||||||
def __init__(self, parent_grid):
|
def __init__(self, parent_grid):
|
||||||
self.direction_map = {
|
|
||||||
'up': (0, 1),
|
|
||||||
'left': (-1, 0),
|
|
||||||
'right': (1, 0),
|
|
||||||
'down': (0, -1)
|
|
||||||
}
|
|
||||||
self.grid = parent_grid
|
self.grid = parent_grid
|
||||||
self.x = self.y = 1
|
self.x = self.y = 1
|
||||||
self.total_power = sum(self.grid.cell_power(x, y) for y in range(3) for x in range(3))
|
self.calc_power()
|
||||||
|
|
||||||
def step(self, direction):
|
def step(self, direction):
|
||||||
i, j = self.direction_map(direction)
|
if direction in ('right', 'left'):
|
||||||
# figure out points to add and subtract via math
|
axis = 'vertical'
|
||||||
|
new_x, new_y = 1, 0
|
||||||
|
elif direction in ('up', 'down'):
|
||||||
|
axis = 'horizontal'
|
||||||
|
new_x, new_y = 0, 1
|
||||||
|
|
||||||
def total_power(self):
|
if direction in ('right', 'down'):
|
||||||
self.total = 0
|
delta_new, delta_old = 3, 0
|
||||||
|
elif direction in ('left', 'up'):
|
||||||
|
delta_new, delta_old = -1, 2
|
||||||
|
new_x, new_y = new_x * -1, new_y * -1
|
||||||
|
|
||||||
|
new_points = self.get_row(delta_new, axis)
|
||||||
|
old_points = self.get_row(delta_old, axis)
|
||||||
|
self.total_power += sum(new_points)
|
||||||
|
self.total_power -= sum(old_points)
|
||||||
|
self.x, self.y = self.x + new_x, self.y + new_y
|
||||||
|
|
||||||
|
def get_row(self, delta, axis):
|
||||||
|
points = []
|
||||||
|
if axis == 'vertical':
|
||||||
|
for y in range(self.y, self.y + 3):
|
||||||
|
points.append(self.grid.get(self.x + delta, y))
|
||||||
|
elif axis == 'horizontal':
|
||||||
|
for x in range(self.x, self.x + 3):
|
||||||
|
points.append(self.grid.get(x, self.y + delta))
|
||||||
|
return points
|
||||||
|
|
||||||
|
def jump(self, x, y):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.calc_power()
|
||||||
|
|
||||||
|
def calc_power(self):
|
||||||
|
self.total_power = 0
|
||||||
for y in range(3):
|
for y in range(3):
|
||||||
for x in range(3):
|
for x in range(3):
|
||||||
self.total += self.grid.get(x, y)
|
self.total_power += self.grid.get(self.x + x, self.y + y)
|
||||||
|
|
||||||
def walk(self):
|
def find_max(self):
|
||||||
max_power = 0
|
coords, max_power = (0, 0), 0
|
||||||
|
prev_dir = 'down'
|
||||||
while True:
|
while True:
|
||||||
if self.total_power > max_power:
|
if self.total_power > max_power:
|
||||||
max_power = self.total_power
|
coords, max_power = (self.x, self.y), self.total_power
|
||||||
# step right, down, or left
|
|
||||||
|
if self.x in (1, 298) and prev_dir != 'down': # cursor is at edge of grid
|
||||||
|
direction = 'down'
|
||||||
|
elif self.y % 2 == 0: # go left on even lines
|
||||||
|
direction = 'left'
|
||||||
|
else: # go right on odd lines
|
||||||
|
direction = 'right'
|
||||||
|
prev_dir = direction
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.step(direction)
|
||||||
|
except IndexError:
|
||||||
|
break
|
||||||
|
|
||||||
|
return coords, max_power
|
||||||
|
|
||||||
|
|
||||||
|
grid = Grid(7857)
|
||||||
|
cursor = Cursor(grid)
|
||||||
|
coords, max_power = cursor.find_max()
|
||||||
|
print(f'Part 1: {coords[0]}, {coords[1]}, {max_power}')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user