Skip to content

Instantly share code, notes, and snippets.

@Szer
Created December 4, 2023 16:39
Show Gist options
  • Save Szer/a6c30b35a15e2118925ffe52d9a073c7 to your computer and use it in GitHub Desktop.
Save Szer/a6c30b35a15e2118925ffe52d9a073c7 to your computer and use it in GitHub Desktop.
Advent of Code 2023 4
open System
let input = [|
"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"
|]
let calculateCardWins (line: string) =
let [|winningRaw; yoursRaw|] = line.Split '|'
let winning =
winningRaw.Split(' ', StringSplitOptions.RemoveEmptyEntries)
|> Seq.skip 2
|> Seq.map int
|> set
let yours =
yoursRaw.Split(' ', StringSplitOptions.RemoveEmptyEntries)
|> Seq.map int
|> set
Set.intersect winning yours
|> Set.count
let part1 =
Array.sumBy (fun card ->
let cardWins = calculateCardWins card
if cardWins = 0 then 0 else 2. ** (float cardWins - 1.0) |> int
)
part1 input // 23847
let part2 (input: string[]) =
let allWinnings = Array.zeroCreate input.Length
Array.iteri (fun i card ->
let copiesWon = allWinnings.[i]
let cardWins = calculateCardWins card
for x = i+1 to min (input.Length-1) (i + cardWins) do
allWinnings.[x] <- allWinnings.[x] + 1 + copiesWon
) input
Array.sum allWinnings + input.Length
part2 input // 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment