-
-
Save Tomalak/be72d43f9e4f5014d27f508ac00b456a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
# solution for https://adventofcode.com/2021/day/5 | |
data = [] | |
with open('day5.txt') as f: | |
for line in f: | |
data.append([tuple(map(int, c.split(','))) for c in line.strip().split(' -> ')]) | |
def get_line_points(pair): | |
yield '%s,%s' % pair[0] | |
[[x1, y1], [x2, y2]] = pair | |
while not (x1 == x2 and y1 == y2): | |
if x1 < x2: x1 += 1 | |
if y1 < y2: y1 += 1 | |
if x1 > x2: x1 -= 1 | |
if y1 > y2: y1 -= 1 | |
yield '%s,%s' % (x1, y1) | |
# part 1 filter, comment out for part 2 | |
data = filter(lambda c: c[0][0] == c[1][0] or c[0][1] == c[1][1], data) | |
point_groups = {} | |
for pair in data: | |
for point in get_line_points(pair): | |
if point not in point_groups: | |
point_groups[point] = 0 | |
point_groups[point] += 1 | |
high_risk_areas = list(filter(lambda v: v > 1, point_groups.values())) | |
print(len(high_risk_areas)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment