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 de0f2b8
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions 2023/day03/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
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):
Expand All @@ -25,13 +24,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 == "*"
)
)

0 comments on commit de0f2b8

Please sign in to comment.