Skip to content

Commit

Permalink
Only use the symbol '*' in part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fuglede committed Dec 3, 2023
1 parent 301ac48 commit 7c24c02
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions 2023/day03/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

box = list(itertools.product((-1, 0, 1), (-1, 0, 1)))
symbols = {
(i, j)
(i, j): x
for i, l in enumerate(ls)
for j, x in enumerate(l)
if x != "." and not x.isdigit()
Expand All @@ -25,13 +25,19 @@
for di, dj in box
for j in range(match.start(), match.end())
}
if symbols & boundary:
if symbols.keys() & boundary:
part_sum += n
for symbol in symbols & boundary:
for symbol in symbols.keys() & boundary:
parts_by_symbol[symbol].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(v)
for k, v in parts_by_symbol.items()
if len(v) == 2 and symbols[k] == "*"
)
)

0 comments on commit 7c24c02

Please sign in to comment.