Back to a more straightfoward day, do they make them harder on the weekends?
Day 4 Scratchcards
Part 1
#!/usr/bin/env jq -n -R -f
[
inputs
# Split winning numbers | card
| split(" | ")
# Get numbers, remove game id
| .[] |= [ match("\\d+"; "g").string | tonumber ] | .[0] |= .[1:]
# Get score for each line
| .[1] - (.[1] - .[0]) | length | select(. > 0) | pow(2; . - 1)
]
# Output total score sum
| add
Very suited to JQ, extra trick learned using: [ match("\\d+"; "g").string | tonumber ] as a parse all ints in line.
Part 2
#!/usr/bin/env jq -n -R -f
[
inputs
# Split winning numbers | card
| split(" | ")
# Get numbers, remove game id
| .[] |= [ match("\\d+"; "g").string | tonumber ] | .[0] |= .[1:]
# Set number of cards to 1, and further cards count
| .[1] - (.[1] - .[0]) | [ 1, length ]
]
| { cards: ., i: 0, l: length } | until (.i == .l;
# Get number for current card
.cards[.i][0] as $num
# Increase range of futher cards, by current number
| .cards[.i + range(.cards[.i][1]) + 1 ][0] += $num
| .i += 1
)
# Output total sum of cards
| [ .cards[][0] ] | add
Not too much of an edit compared to part one, being able to easily do operations on range of indices is convenient.