Cleaner
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/all-checks-complete Pipeline was successful

This commit is contained in:
Smaug123
2023-12-06 09:01:04 +00:00
parent 4e0ffe6e64
commit c2d4820714

View File

@@ -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>) : 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