this post was submitted on 04 Dec 2023
12 points (100.0% liked)

Advent Of Code

15 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 4: Scratchcards


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Unlocked after 8 mins

you are viewing a single comment's thread
view the rest of the comments
[–] UlrikHD@programming.dev 2 points 11 months ago (2 children)

Feels like the challenges are getting easier than harder currently. Fairly straightforward when doing it the lazy way with python. ::: spoiler Python

import re

winning_number_pattern: re.Pattern = re.compile(r' +([\d ]*?) +\|')
lottery_number_pattern: re.Pattern = re.compile(r'\| +([\d ]*)')


def get_winning_numbers(line: str) -> set[str]:
    return set(winning_number_pattern.search(line).group(1).split())


def get_lottery_numbers(line: str) -> set[str]:
    return set(lottery_number_pattern.search(line).group(1).split())


def get_winnings(winning_numbers: set[str], lottery_numbers: set[str]) -> int:
    return int(2 ** (len(winning_numbers.intersection(lottery_numbers)) - 1))


def puzzle_1() -> int:
    points: int = 0
    with open('day4_scratchcards.txt', 'r', encoding='utf-8') as file:
        for line in file:
            points += get_winnings(get_winning_numbers(line), get_lottery_numbers(line))
    return points


class ScratchCard:
    def __init__(self, line: str):
        self.amount: int = 1
        self.winnings: int = len(get_winning_numbers(line).intersection(get_lottery_numbers(line)))

    def update(self, extra: int) -> None:
        self.amount = self.amount + extra

    def __radd__(self, other):
        return self.amount + other


def puzzle_2() -> int:
    scratch_card_list: list[ScratchCard] = []
    with open('day4_scratchcards.txt', 'r', encoding='utf-8') as file:
        for line in file:
            scratch_card_list.append(ScratchCard(line))

    for i, scratch_card in enumerate(scratch_card_list):
        for j in range(1, scratch_card.winnings + 1):
            try:
                scratch_card_list[i + j].update(scratch_card.amount)
            except IndexError:
                pass
    return sum(scratch_card_list)


if __name__ == '__main__':
    print(puzzle_1())
    print(puzzle_2())
[–] bob_lemon@feddit.de 1 points 11 months ago (1 children)

That int-call on the return value for the point value is a good idea. I manually returned 0 if there were no matches.

[–] UlrikHD@programming.dev 1 points 11 months ago

I personally would prefer the if check and return 0 in most instances just because it's clearer and more readable. But the two previous functions were one-liners so it just looked better if get_winnings() also was.

[–] lwhjp@lemmy.sdf.org 1 points 11 months ago

Puzzles on the weekend are usually a bit more involved than weekdays. 23 is probably going to be a monster this year...