Cleaner
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user