From f6096218e4f86ec82801a9edc115877d5165ee06 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Tue, 4 Dec 2018 22:39:54 +0100 Subject: [PATCH] Day 4 , part 1. --- Day04.fs | 47 +++++++++++++++++++++----------------------- Program.fs | 6 +++--- Tests/Day04 tests.fs | 2 +- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/Day04.fs b/Day04.fs index c3f2ee6..c56203d 100644 --- a/Day04.fs +++ b/Day04.fs @@ -1,18 +1,11 @@ module AdventOfCode2018.Day04 open System +open System.Collections.Generic -let parseInput (str : string) : string = +let getSleepestGuard (str : string) : int * int = let shifts : int list array = Array.create 60 [] - let addShift (id : int) (date1 : DateTime) (date2 : DateTime) = - - let min1 = if date1.Hour > 0 then 0 else date1.Minute - let min2 = if date2.Hour > 0 then 59 else date2.Minute - - for m = min1 to min2 - 1 do - shifts.[m] <- id :: shifts.[m] - let lines = str.Split ([| '\r'; '\n'; |], StringSplitOptions.RemoveEmptyEntries) |> Array.choose ( @@ -21,15 +14,18 @@ let parseInput (str : string) : string = if trimmed = "" then None else Some (DateTime.Parse (trimmed.Substring (1, 16)), trimmed) ) |> Array.sortBy fst - (* - |> Array.fold ( - fun id (dateTime, line) -> - match line.IndexOf '#' with - | i when i <> -1 -> line.Substring (i + 1, line.IndexOf (' ', i) - i - 1) |> int - | _ -> - ) 0*) let mutable id = 0 + let minutesAsleep = Dictionary () + + let addShift (date1 : DateTime) (date2 : DateTime) = + let min1 = if date1.Hour > 0 then 0 else date1.Minute + let min2 = if date2.Hour > 0 then 60 else date2.Minute + + minutesAsleep.[id] <- minutesAsleep.GetValueOrDefault (id, 0) + min2 - min1 + + for m = min1 to min2 - 1 do + shifts.[m] <- id :: shifts.[m] for i = 0 to lines.Length - 1 do let dateTime, line = lines.[i] @@ -37,17 +33,18 @@ let parseInput (str : string) : string = | i when i <> -1 -> id <- line.Substring (i + 1, line.IndexOf (' ', i) - i - 1) |> int | _ -> if line.Contains "wakes up" then - addShift id (fst lines.[i-1]) dateTime - - sprintf "%A" shifts - - - - + addShift (fst lines.[i-1]) dateTime + let sleepestGuardId, _ = minutesAsleep |> Seq.fold (fun (id, max) guard -> if guard.Value > max then guard.Key, guard.Value else id, max) (0, 0) + let mutable sleepestTime = 0 + let mutable asleepCountMax = 0 - //Map. + for i = 0 to shifts.Length - 1 do + let asleepCount = shifts.[i] |> List.where ((=) sleepestGuardId) |> List.length + if asleepCount > asleepCountMax then + sleepestTime <- i + asleepCountMax <- asleepCount + sleepestGuardId, sleepestTime - // \ No newline at end of file diff --git a/Program.fs b/Program.fs index 346eb36..6e7f256 100644 --- a/Program.fs +++ b/Program.fs @@ -15,11 +15,11 @@ let day02 () = let day03 () = let claims = File.ReadAllText "Data/day03.input" |> Day03.parseInput let surface, claimId = Day03.overlappingSurface claims - sprintf "part1 = %A, part2 = %A" surface claimId + sprintf "part1 = %A, part2 = %A" surface claimId let day04 () = - // sprintf "part1 = %A, part2 = %A" surface claimId - "" + let sleepestGuardId, sleepestTime = File.ReadAllText "Data/day04.input" |> Day04.getSleepestGuard + sprintf "part1 = %A, part2 = " (sleepestGuardId * sleepestTime) let days : Map string> = [ diff --git a/Tests/Day04 tests.fs b/Tests/Day04 tests.fs index b952e14..149aae0 100644 --- a/Tests/Day04 tests.fs +++ b/Tests/Day04 tests.fs @@ -33,7 +33,7 @@ type ``Day04 tests`` (output : ITestOutputHelper) = [1518-11-05 00:55] wakes up """ - output.WriteLine (Day04.parseInput input) + output.WriteLine (Day04.getSleepestGuard input |> string) 1 =! 2 () -- 2.45.2