From de47d10a64bbc19bec07fa60091a866506ed15ba Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Sat, 7 Dec 2024 22:57:33 -0700 Subject: Day 8 2024 --- 8_2024.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 8_2024.py (limited to '8_2024.py') diff --git a/8_2024.py b/8_2024.py new file mode 100644 index 0000000..e18f383 --- /dev/null +++ b/8_2024.py @@ -0,0 +1,42 @@ +from collections import defaultdict + +input = "data/8_2024.txt" +data = open(input).read().strip() +line = data.split("\n") +row, col = len(line), len(line[0]) +a1, a2, dic = set(), set(), defaultdict(list) + + +for r in range(row): + for c in range(col): + if line[r][c] != ".": + dic[line[r][c]].append((r, c)) + + +for r in range(row): + for c in range(col): + for k, vs in dic.items(): + for r1, c1 in vs: + for r2, c2 in vs: + if (r1, c1) != (r2, c2): + d1 = abs(r - r1) + abs(c - c1) + d2 = abs(r - r2) + abs(c - c2) + dr1 = r - r1 + dr2 = r - r2 + dc1 = c - c1 + dc2 = c - c2 + if ( + (d1 == 2 * d2 or d1 * 2 == d2) + and 0 <= r < row + and 0 <= c < col + and (dr1 * dc2 == dc1 * dr2) + ): + a1.add((r, c)) + if 0 <= r < row and 0 <= c < col and (dr1 * dc2 == dc1 * dr2): + a2.add((r, c)) + + +p1 = len(a1) +p2 = len(a2) +print(p1) +print(p2) -- cgit v1.2.3