Skip to content

Instantly share code, notes, and snippets.

@object
Created December 4, 2023 06:03
Show Gist options
  • Select an option

  • Save object/ec540fc5838dac3784092101361428f3 to your computer and use it in GitHub Desktop.

Select an option

Save object/ec540fc5838dac3784092101361428f3 to your computer and use it in GitHub Desktop.
Advent of Code 2023, December 4
#time "on"
open System
open System.IO
open System.Collections.Generic
let parseCards (str: string) =
str.Trim().Split(' ') |> Seq.filter(not << String.IsNullOrWhiteSpace) |> Seq.map Int32.Parse |> Seq.toArray
let parseLine (str: string) =
let items = str.Split([|':'; '|'|])
parseCards items[1], parseCards items[2]
let input =
File.ReadAllLines(__SOURCE_DIRECTORY__ + "/../data/input04.txt")
|> Seq.toArray
|> Array.map parseLine
// Part One
input
|> Array.map (fun (winning, cards) ->
cards |> Array.fold (fun acc card -> if Array.contains card winning then (if acc = 0 then 1 else acc * 2) else acc) 0)
|> Array.sum
// Part Two
let acc = Dictionary<int, (int * int)>()
input
|> Array.iteri (fun ndx (winning, cards) ->
acc.Add(ndx + 1, (cards |> Array.filter (fun card -> Array.contains card winning) |> Array.length, 1)))
for ndx in acc.Keys do
for i in [1..fst acc[ndx]] do
if acc.ContainsKey (ndx + i) then
let x, y = acc[ndx + i]
acc[ndx + i] <- (x, y + (snd acc[ndx]))
acc |> Seq.sumBy(fun kv -> snd kv.Value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment