Day 9
This commit is contained in:
@@ -1,37 +1,55 @@
|
||||
namespace AdventOfCode2023
|
||||
|
||||
open System
|
||||
open System.Collections.Generic
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day9 =
|
||||
|
||||
let parse (s : string) =
|
||||
use mutable lines = StringSplitEnumerator.make '\n' s
|
||||
let extrapolate (isStart : bool) (arr : ResizeArray<int64>) =
|
||||
let mutable answer = 0L
|
||||
let pos = if isStart then -1L else int64 arr.Count
|
||||
|
||||
lines.MoveNext () |> ignore
|
||||
for i = 0 to arr.Count - 1 do
|
||||
let mutable product = Rational.ofInt arr.[i]
|
||||
|
||||
let stepsLine = lines.Current.TrimEnd ()
|
||||
let steps = Array.zeroCreate stepsLine.Length
|
||||
for j = 0 to arr.Count - 1 do
|
||||
if j <> i then
|
||||
product <- product * Rational.make (pos - int64 j) (int64 i - int64 j)
|
||||
|
||||
for i = 0 to stepsLine.Length - 1 do
|
||||
steps.[i] <- (stepsLine.[i] = 'R')
|
||||
answer <- answer + Rational.assertIntegral product
|
||||
|
||||
let dict = Dictionary ()
|
||||
|
||||
while lines.MoveNext () do
|
||||
if not lines.Current.IsEmpty then
|
||||
use mutable line = StringSplitEnumerator.make' ' ' lines.Current
|
||||
line.MoveNext () |> ignore
|
||||
{
|
||||
Steps = steps
|
||||
Nodes = dict
|
||||
}
|
||||
answer
|
||||
|
||||
let part1 (s : string) =
|
||||
let data = parse s
|
||||
use s = StringSplitEnumerator.make '\n' s
|
||||
let mutable answer = 0L
|
||||
let arr = ResizeArray ()
|
||||
|
||||
for line in s do
|
||||
arr.Clear ()
|
||||
use line = StringSplitEnumerator.make' ' ' line
|
||||
|
||||
for number in line do
|
||||
let number = Int64.Parse number
|
||||
arr.Add number
|
||||
|
||||
answer <- answer + extrapolate false arr
|
||||
|
||||
answer
|
||||
|
||||
let part2 (s : string) =
|
||||
let data = parse s
|
||||
0
|
||||
use s = StringSplitEnumerator.make '\n' s
|
||||
let mutable answer = 0L
|
||||
let arr = ResizeArray ()
|
||||
|
||||
for line in s do
|
||||
arr.Clear ()
|
||||
use line = StringSplitEnumerator.make' ' ' line
|
||||
|
||||
for number in line do
|
||||
let number = Int64.Parse number
|
||||
arr.Add number
|
||||
|
||||
answer <- answer + extrapolate true arr
|
||||
|
||||
answer
|
||||
|
||||
Reference in New Issue
Block a user