In [18]:
import csv
import numpy as np
with open('input.txt', 'rt') as f_input:
csv_reader = csv.reader(f_input)
l = np.array([int(a[0]) for a in csv_reader])
In [20]:
class Maze(object):
def __init__(self, curr_pos, state):
self.curr_pos = curr_pos
self.state = state.copy()
self.length = len(self.state)
def evolve(self):
self.state[self.curr_pos] += 1
self.curr_pos += self.state[self.curr_pos] - 1
def outside(self):
return (self.curr_pos >= self.length) or (self.curr_pos < 0)
In [21]:
def steps_maze(l):
maze = Maze(0, l)
count = 0
while not maze.outside():
maze.evolve()
count += 1
return count, maze.state
In [22]:
t = np.array([0, 3, 0, 1, -3])
steps_maze(t)
Out[22]:
In [23]:
steps_maze(l)
Out[23]:
In [78]:
class NewMaze(Maze):
def evolve(self):
if self.state[self.curr_pos] >= 3:
self.state[self.curr_pos] -= 1
self.curr_pos += self.state[self.curr_pos] + 1
else:
self.state[self.curr_pos] += 1
self.curr_pos += self.state[self.curr_pos] - 1
In [79]:
def steps_new_maze(l):
new_maze = NewMaze(0, l)
count = 0
while not new_maze.outside():
new_maze.evolve()
count += 1
return count, new_maze.curr_pos, new_maze.state
In [92]:
t = np.array([0, 3, 0, 1, -3])
steps_new_maze(t)
Out[92]:
In [93]:
steps_new_maze(l)
Out[93]:
In [ ]: