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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
🔒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
Feels like the challenges are getting easier than harder currently. Fairly straightforward when doing it the lazy way with python.
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())
Puzzles on the weekend are usually a bit more involved than weekdays. 23 is probably going to be a monster this year…
That int-call on the return value for the point value is a good idea. I manually returned 0 if there were no matches.
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.