Skip to content

Instantly share code, notes, and snippets.

@Szer
Last active December 11, 2023 12:55
Show Gist options
  • Save Szer/07a4fc224042976209e056a2b5586163 to your computer and use it in GitHub Desktop.
Save Szer/07a4fc224042976209e056a2b5586163 to your computer and use it in GitHub Desktop.
Advent of Code 2023 9
let input = """0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45""" |> fun x -> x.Split("\n")
let parse (input:string[]) =
input
|> Array.map (fun x -> x.Split(" ") |> Array.map int)
let rec extrapolateSequence (sequence: int[]) =
let diffs =
sequence
|> Array.pairwise
|> Array.map (fun (a,b) -> b-a)
let diff = Array.last diffs
let allDiffsAreTheSame = diffs |> Array.forall ((=) diff)
let headDiff, lastDiff =
if allDiffsAreTheSame then
diff, diff
else
extrapolateSequence diffs
Array.head sequence - headDiff, Array.last sequence + lastDiff
let game = parse input
let part1 = Array.sumBy (extrapolateSequence >> snd) game // 114
let part2 = Array.sumBy (extrapolateSequence >> fst) game // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment