DEV Community

Cover image for Advent of Code 2023 - December 4th
Rob van der Leek
Rob van der Leek

Posted on

Advent of Code 2023 - December 4th

In this series, I'll share my progress with the 2023 version of Advent of Code.

Check the first post for a short intro to this series.

You can also follow my progress on GitHub.

December 4th

The puzzle of day 4 was pretty easy, a much-needed laid-back start of the week. Although the input looked intimidating at first glance, it only took me 28 lines to solve both parts of the puzzle.

My pitfall for this puzzle: Not really, the description and input data looked intimidating, but programming the solution was straightforward.

Solution here, do not click if you want to solve the puzzle first yourself
#!/usr/bin/env python3
import re

with open('input.txt') as infile:
    lines = infile.readlines()

total = 0
multiplier = [1 for line in lines]

for index, line in enumerate(lines):
    [card, numbers] = line.strip().split(':')
    [winning, mine] = numbers.split('|')
    winning_numbers = [int(n) for n in re.split('\s+', winning.strip())]
    my_numbers = [int(n) for n in re.split('\s+', mine.strip())]
    card_winning = 0
    for n in my_numbers:
        if n in winning_numbers:
            card_winning += 1
    if card_winning > 0:
        total += pow(2, card_winning - 1)
        for i in range(1, card_winning + 1):
            if index + i >= len(lines):
                break 
            for j in range(multiplier[index]):
                multiplier[index + i] = multiplier[index + i] + 1

print(total)
print(sum(multiplier))
Enter fullscreen mode Exit fullscreen mode

That's it! See you again tomorrow!

Top comments (0)