Rust
This one wasn't too bad. The example for part 2 even tells you how to process everything by visiting each card once in order. Another option could be to recursively look at all won copies, but that's probably much less efficient.
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.
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 |
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')
This one wasn't too bad. The example for part 2 even tells you how to process everything by visiting each card once in order. Another option could be to recursively look at all won copies, but that's probably much less efficient.
I had to give Uiua another go today. (run it here)
{"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53"
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19"
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1"
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83"
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36"
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"}
LtoDec ← ∧(+ ×10:) :0
StoDec ← LtoDec▽≥0. ▽≤9. -@0
# Split on spaces, drop dross, parse ints
≡(⊜□≠0.⊐∵(StoDec)↘ 2⊜(□)≠@\s.⊔)
# Find matches
≡(/+/+⊠(⌕)⊃(⊔⊢↙ 1)(⊔⊢↙¯1))
# part 1
/+ⁿ:2-1 ▽±..
# part 2 - start with matches and initial counts
=..:
# len times: get 1st of each, rotate both, add new counts
⍥(⬚0+↯: ⊙⊙∩(↻1) ⊙:∩(⊢.))⧻.
/+⊙;
WTF
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())
That int-call on the return value for the point value is a good idea. I manually returned 0 if there were no matches.