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.
I liked the slight trickiness of part 2, that the naive implementation would never complete in time.
As always doing a JQ implementation:
Part 1
Some comments:
input
first to get the seeds, theninputs
to get remaining lines.Part 2
Some comments:
[1,2,3] | [ .[] | if . == 2 then . * 10 + 1 , . * 10 + 2 else . end ]
->[1, 21, 22, 3]
Replaced less-than (and greater-than for symmetry) symbols with full-width version, since lemmy apparently doesn't handle them well within a code block: replacing less than with <