In [1]:
data = open('data/day_1-1.txt', 'r').readline().strip().split(', ')
In [2]:
class TaxiCab:
def __init__(self, data):
self.data = data
self.double_visit = []
self.position = {'x': 0, 'y': 0}
self.direction = {'x': 0, 'y': 1}
self.grid = {i: {j: 0 for j in range(-500, 501)} for i in range(-500, 501)}
def run(self):
for instruction in self.data:
toward = instruction[0]
length = int(instruction[1:])
self.move(toward, length)
def move(self, toward, length):
if toward == 'R':
if self.direction['x'] == 0:
# from UP
if self.direction['y'] == 1:
self.position['x'] += length
self.direction['x'] = 1
for i in range(self.position['x'] - length, self.position['x']):
self.grid[self.position['y']][i] += 1
if self.grid[self.position['y']][i] > 1:
self.double_visit.append((i, self.position['y']))
# from DOWN
else:
self.position['x'] -= length
self.direction['x'] = -1
for i in range(self.position['x'] + length, self.position['x'], -1):
self.grid[self.position['y']][i] += 1
if self.grid[self.position['y']][i] > 1:
self.double_visit.append((i, self.position['y']))
self.direction['y'] = 0
else:
# FROM RIGHT
if self.direction['x'] == 1:
self.position['y'] -= length
self.direction['y'] = -1
for i in range(self.position['y'] + length, self.position['y'], -1):
self.grid[i][self.position['x']] += 1
if self.grid[i][self.position['x']] > 1:
self.double_visit.append((self.position['x'], i))
# FROM LEFT
else:
self.position['y'] += length
self.direction['y'] = 1
for i in range(self.position['y'] - length, self.position['y']):
self.grid[i][self.position['x']] += 1
if self.grid[i][self.position['x']] > 1:
self.double_visit.append((self.position['x'], i))
self.direction['x'] = 0
else:
if self.direction['x'] == 0:
# from UP
if self.direction['y'] == 1:
self.position['x'] -= length
self.direction['x'] = -1
for i in range(self.position['x'] + length, self.position['x'], -1):
self.grid[self.position['y']][i] += 1
if self.grid[self.position['y']][i] > 1:
self.double_visit.append((i, self.position['y']))
# from DOWN
else:
self.position['x'] += length
self.direction['x'] = 1
for i in range(self.position['x'] - length, self.position['x']):
self.grid[self.position['y']][i] += 1
if self.grid[self.position['y']][i] > 1:
self.double_visit.append((i, self.position['y']))
self.direction['y'] = 0
else:
# FROM RIGHT
if self.direction['x'] == 1:
self.position['y'] += length
self.direction['y'] = 1
for i in range(self.position['y'] - length, self.position['y']):
self.grid[i][self.position['x']] += 1
if self.grid[i][self.position['x']] > 1:
self.double_visit.append((self.position['x'], i))
# FROM LEFT
else:
self.position['y'] -= length
self.direction['y'] = -1
for i in range(self.position['y'] + length, self.position['y'], -1):
self.grid[i][self.position['x']] += 1
if self.grid[i][self.position['x']] > 1:
self.double_visit.append((self.position['x'], i))
self.direction['x'] = 0
def get_distance(self):
return sum([abs(i) for i in self.position.values()])
def get_distance_first_double_visit(self):
return sum(self.double_visit[0]) if len(self.double_visit) > 0 else 0
In [3]:
# Test
def test(data, result):
tc = TaxiCab(data)
tc.run()
assert tc.get_distance() == result
In [4]:
test(data=['R2', 'L3'], result=5)
test(data=['R2', 'R2', 'R2'], result=2)
test(data=['R5', 'L5', 'R5', 'R3'], result=12)
In [5]:
tc = TaxiCab(data)
tc.run()
tc.get_distance()
Out[5]:
In [6]:
# Test
def test(data, result):
tc = TaxiCab(data)
tc.run()
assert tc.get_distance_first_double_visit() == result
In [7]:
test(data=['R8', 'R4', 'R4', 'R8'], result=4)
In [8]:
tc.get_distance_first_double_visit()
Out[8]: