start day 11
This commit is contained in:
parent
c198ad1f80
commit
274f692004
54
2018/11.py
Normal file
54
2018/11.py
Normal file
@ -0,0 +1,54 @@
|
||||
class Grid:
|
||||
def __init__(self, serial):
|
||||
self.serial = serial
|
||||
|
||||
def populate(self):
|
||||
self.cells = []
|
||||
for y in range(1, 301):
|
||||
self.cells.append([])
|
||||
for x in range(1, 301):
|
||||
power = self.cell_power(x, y)
|
||||
self.set(x, y, power)
|
||||
|
||||
def get(self, x, y):
|
||||
return self.cells[y - 1][x - 1]
|
||||
|
||||
def set(self, x, y, value):
|
||||
self.cells[y - 1][x - 1] = value
|
||||
|
||||
def cell_power(x, y):
|
||||
rack = x + 10
|
||||
p = (rack * y + self.serial) * rack
|
||||
p = (p // 100) % 10 # keep only hundreds digit
|
||||
return p - 5
|
||||
|
||||
|
||||
class Cursor:
|
||||
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.x = self.y = 1
|
||||
self.total_power = sum(self.grid.cell_power(x, y) for y in range(3) for x in range(3))
|
||||
|
||||
def step(self, direction):
|
||||
i, j = self.direction_map(direction)
|
||||
# figure out points to add and subtract via math
|
||||
|
||||
def total_power(self):
|
||||
self.total = 0
|
||||
for y in range(3):
|
||||
for x in range(3):
|
||||
self.total += self.grid.get(x, y)
|
||||
|
||||
def walk(self):
|
||||
max_power = 0
|
||||
while True:
|
||||
if self.total_power > max_power:
|
||||
max_power = self.total_power
|
||||
# step right, down, or left
|
||||
|
Loading…
x
Reference in New Issue
Block a user