From b9dad03ac72a2cd15c6c4deb09c7886da78175a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Fuglede=20J=C3=B8rgensen?= Date: Sun, 3 Dec 2023 16:22:14 +0100 Subject: [PATCH] Only use the symbol '*' in part 2 --- 2023/day03/solutions.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/2023/day03/solutions.py b/2023/day03/solutions.py index ba94eee..cde8245 100644 --- a/2023/day03/solutions.py +++ b/2023/day03/solutions.py @@ -1,21 +1,19 @@ import itertools import math import re -from collections import defaultdict with open("input") as f: ls = f.read().strip().split("\n") box = list(itertools.product((-1, 0, 1), (-1, 0, 1))) -symbols = { - (i, j) +parts_by_symbol = { + (i, j): (x, []) for i, l in enumerate(ls) for j, x in enumerate(l) if x != "." and not x.isdigit() } part_sum = 0 -parts_by_symbol = defaultdict(list) for i, l in enumerate(ls): for match in re.finditer(r"\d+", l): @@ -25,13 +23,19 @@ for di, dj in box for j in range(match.start(), match.end()) } - if symbols & boundary: + if parts_by_symbol.keys() & boundary: part_sum += n - for symbol in symbols & boundary: - parts_by_symbol[symbol].append(n) + for symbol in parts_by_symbol.keys() & boundary: + parts_by_symbol[symbol][1].append(n) # Part 1 print(part_sum) # Part 2 -print(sum(math.prod(v) for v in parts_by_symbol.values() if len(v) == 2)) +print( + sum( + math.prod(parts) + for symbol, parts in parts_by_symbol.values() + if len(parts) == 2 and symbol == "*" + ) +)