module AdventOfCode2018.Day01\r
\r
-let readDigit d = int d - int '0'\r
+open System\r
\r
-let parseInput (str : string) : string = str\r
+\r
+let parseInput (str : string) : int[] =\r
+ str.Split ([| "\r\n"; "\r"; "\n" |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int\r
+\r
+let finalFrequency : seq<int> -> int =\r
+ Seq.sum\r
+\r
+let firstDuplicate (changes : seq<int>) : int =\r
+ let repeatedChanges = seq { while true do yield! changes }\r
+ Seq.scan (\r
+ fun (_, sum, frequencies) change ->\r
+ let sum' = sum + change\r
+ if Set.contains sum' frequencies then\r
+ (true, sum', frequencies)\r
+ else\r
+ (false, sum', Set.add sum' frequencies)\r
+\r
+ ) (false, 0, Set.empty) repeatedChanges\r
+ |> Seq.pick (fun (duplicate, sum, _) -> if duplicate then Some sum else None)\r
open System.IO\r
\r
let day01 () =\r
- let input = File.ReadAllText "Data/day01.input" |> Day01.parseInput\r
- input\r
+ let changes = File.ReadAllText "Data/day01.input" |> Day01.parseInput\r
+ sprintf "part1 = %A, part2 = %A" (Day01.finalFrequency changes) (Day01.firstDuplicate changes)\r
\r
let days : (unit -> string) array =\r
[|\r