Rules: no spoilers.

The other rules are made up as we go along.

Share code by link to a forge, home page, pastebin (Eric Wastl has one here) or code section in a comment.

  • zogwarg@awful.systems
    link
    fedilink
    English
    arrow-up
    4
    ·
    11 months ago

    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.

    • gerikson@awful.systemsOP
      link
      fedilink
      English
      arrow-up
      4
      ·
      11 months ago

      Historically problems on Sat/Sun have been more challenging than weekdays. However given that the first 7 days are usually “warmup” problems, I’d say this years edition of AoC is more challenging than at least since 2019.