From c2d48207146bd9476e54a69fd029e0e3d5e17289 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Wed, 6 Dec 2023 09:01:04 +0000 Subject: [PATCH] Cleaner --- .../AdventOfCode2023.FSharp.Lib/Day6.fs | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day6.fs b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day6.fs index 8547640..c01194e 100644 --- a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day6.fs +++ b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day6.fs @@ -7,26 +7,33 @@ module Day6 = let parse (s : string) = use mutable lines = StringSplitEnumerator.make '\n' s + let times = lines.MoveNext () |> ignore use mutable line = StringSplitEnumerator.make' ' ' lines.Current StringSplitEnumerator.chomp "Time:" &line line.MoveNext () |> ignore let times = ResizeArray () + while line.MoveNext () do if not line.Current.IsEmpty then times.Add (UInt64.Parse line.Current) + times + let distance = lines.MoveNext () |> ignore use mutable line = StringSplitEnumerator.make' ' ' lines.Current StringSplitEnumerator.chomp "Distance:" &line line.MoveNext () |> ignore let distance = ResizeArray () + while line.MoveNext () do if not line.Current.IsEmpty then distance.Add (UInt64.Parse line.Current) + distance + times, distance let furthest (distance : uint64) (toBeat : uint64) = @@ -50,37 +57,30 @@ module Day6 = let part1 (s : string) = let times, distance = parse s let mutable answer = 1uL + for i = 0 to times.Count - 1 do let time = times.[i] let distance = distance.[i] let winners = furthest time distance answer <- answer * winners + + answer + + let concat (digits : ResizeArray) : uint64 = + let mutable answer = 0uL + + for digit in digits do + let mutable power = 10uL + + while digit >= power do + power <- power * 10uL + + answer <- answer * power + digit + answer let part2 (s : string) = let times, distance = parse s - let mutable concatTime = 0uL - for time in times do - // four digits will do - if time < 10uL then - concatTime <- concatTime * 10uL + time - elif time < 100uL then - concatTime <- concatTime * 100uL + time - elif time < 1000uL then - concatTime <- concatTime * 1000uL + time - else - concatTime <- concatTime * 10000uL + time - - let mutable concatDist = 0uL - for dist in distance do - // four digits will do - if dist < 10uL then - concatDist <- concatDist * 10uL + dist - elif dist < 100uL then - concatDist <- concatDist * 100uL + dist - elif dist < 1000uL then - concatDist <- concatDist * 1000uL + dist - else - concatDist <- concatDist * 10000uL + dist - + let concatTime = concat times + let concatDist = concat distance furthest concatTime concatDist