From c9cada67e8a9d351e27f6fa42a6921bf7d932ccf Mon Sep 17 00:00:00 2001 From: dave Date: Sat, 4 Dec 2021 21:38:39 -0800 Subject: [PATCH] day 5 time tag and alternative solution --- 5/a_better.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 5/b.py | 2 ++ 2 files changed, 45 insertions(+) create mode 100644 5/a_better.py diff --git a/5/a_better.py b/5/a_better.py new file mode 100644 index 0000000..aeffc25 --- /dev/null +++ b/5/a_better.py @@ -0,0 +1,43 @@ +from collections import defaultdict + + +def myrange(x1, y1, x2, y2): + while True: + yield(x1, y1) + if x1 == x2 and y1 == y2: + return + if x1 != x2: + x1 += (1 if x1 <= x2 else -1) + if y1 != y2: + y1 += (1 if y1 <= y2 else -1) + + +with open("input.txt") as f: + lines = [i.strip() for i in f.readlines()] + + +points = defaultdict(lambda: 0) + +for line in lines: + p1, p2 = line.split(" -> ") + + x1, y1 = p1.split(",") + x2, y2 = p2.split(",") + + x1 = int(x1) + y1 = int(y1) + x2 = int(x2) + y2 = int(y2) + + # removing this if condition will include diagonal lines, the requirement for part b + if x1 == x2 or y1 == y2: + for xr, yr in myrange(x1, y1, x2, y2): + points[(xr, yr)] += 1 + +overlaps = 0 + +for point, hits in points.items(): + if hits > 1: + overlaps += 1 + +print(overlaps) diff --git a/5/b.py b/5/b.py index 504b0ee..076b417 100644 --- a/5/b.py +++ b/5/b.py @@ -49,3 +49,5 @@ for point, hits in points.items(): overlaps += 1 print(overlaps) + +# 26m