Skip to content

Instantly share code, notes, and snippets.

@albertein
Created December 8, 2021 04:12
Show Gist options
  • Save albertein/cb88ded6d7c6c33fb5b3507c56047365 to your computer and use it in GitHub Desktop.
Save albertein/cb88ded6d7c6c33fb5b3507c56047365 to your computer and use it in GitHub Desktop.
from collections import defaultdict
def key(x, y):
return "{0}-{1}".format(x, y)
def walk(grid, from_x, from_y, to_x, to_y):
grid[key(from_x, from_y)] += 1
while from_x != to_x or from_y != to_y:
if from_x < to_x:
from_x += 1
elif from_x > to_x:
from_x -= 1
if from_y < to_y:
from_y += 1
elif from_y > to_y:
from_y -= 1
grid[key(from_x, from_y)] += 1
if __name__ == '__main__':
grid = defaultdict(lambda: 0)
with open('input.txt') as data:
for line in data:
line = line.strip()
from_str, to_str = line.split(' -> ')
from_x, from_y = [int(value) for value in from_str.split(',')]
to_x, to_y = [int(value) for value in to_str.split(',')]
walk(grid, from_x, from_y, to_x, to_y)
count = 0
for key in grid:
if grid[key] > 1:
count += 1
print(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment