Skip to content

Instantly share code, notes, and snippets.

@spielkind
Last active December 6, 2021 14:25
Show Gist options
  • Save spielkind/7a099b92b8daf46a8bca3990602275ab to your computer and use it in GitHub Desktop.
Save spielkind/7a099b92b8daf46a8bca3990602275ab to your computer and use it in GitHub Desktop.
#!/bin/python
lines = []
with open('day5.txt') as f:
for line in f:
lines.append([list(map(int, c.strip().split(','))) for c in [p for p in line.strip().split('->')]])
def init_diagram(values):
max_x = max(c[0] for l in values for c in l)+1
max_y = max(c[1] for l in values for c in l)+1
return [[0] * max_y for i in range(max_x)]
def print_diagram(dia):
num_to_dot = lambda v: str(v) if v else '.'
for x in dia:
print('%s' % ' '.join(map(num_to_dot, x)))
def draw_line(line):
[[x1, y1], [x2, y2]] = line
while True:
diagram[x1][y1] += 1
if (x1 == x2 and y1 == y2): break
if x1 > x2: x1 -= 1
if x1 < x2: x1 += 1
if y1 > y2: y1 -= 1
if y1 < y2: y1 += 1
def count_overlap(dia):
return len([count for row in dia for count in row if count > 1])
diagram = init_diagram(lines)
# Part One
#no_diagonals = lambda l: l[0][0] == l[1][0] or l[0][1] == l[1][1]
#lines = filter(no_diagonals, lines)
for l in lines:
draw_line(l)
# Print diagram
#print_diagram(diagram)
print('Answer:', count_overlap(diagram))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment