From: Grégory Burri <gregory.burri@matisa.ch>
Date: Wed, 5 Dec 2018 08:48:47 +0000 (+0100)
Subject: Day 4, part 2.
X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=04b8840df243cd608dba5873a8963199e9967eed;p=advent_of_code_2018.git

Day 4, part 2.
---

diff --git a/AdventOfCode2018.fsproj b/AdventOfCode2018.fsproj
index 87b4c6f..4a31bd3 100644
--- a/AdventOfCode2018.fsproj
+++ b/AdventOfCode2018.fsproj
@@ -4,11 +4,15 @@
     <TargetFramework>netcoreapp2.1</TargetFramework>
   </PropertyGroup>
   <ItemGroup>
+    <Compile Include="Day05.fs" />
     <Compile Include="Day04.fs" />
     <Compile Include="Day03.fs" />
     <Compile Include="Day02.fs" />
     <Compile Include="Day01.fs" />
     <Compile Include="Program.fs" />
+    <Content Include="Data/day05.input">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
     <Content Include="Data/day04.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
diff --git a/Day04.fs b/Day04.fs
index c56203d..b78ce9c 100644
--- a/Day04.fs
+++ b/Day04.fs
@@ -3,7 +3,7 @@ module AdventOfCode2018.Day04
 open System
 open System.Collections.Generic
 
-let getSleepestGuard (str : string) : int * int =
+let getSleepestGuard (str : string) : (int * int) * (int * int) =
     let shifts : int list array = Array.create 60 []
 
     let lines =
@@ -15,22 +15,22 @@ let getSleepestGuard (str : string) : int * int =
         )
         |> Array.sortBy fst
 
-    let mutable id = 0
+    let mutable guardId = 0
     let minutesAsleep = Dictionary<int, int> ()
 
     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
+        minutesAsleep.[guardId] <- minutesAsleep.GetValueOrDefault (guardId, 0) + min2 - min1
 
         for m = min1 to min2 - 1 do
-            shifts.[m] <- id :: shifts.[m]
+            shifts.[m] <- guardId :: shifts.[m]
 
     for i = 0 to lines.Length - 1 do
         let dateTime, line = lines.[i]
         match line.IndexOf '#' with
-        | i when i <> -1 -> id <- line.Substring (i + 1, line.IndexOf (' ', i) - i - 1) |> int
+        | i when i <> -1 -> guardId <- line.Substring (i + 1, line.IndexOf (' ', i) - i - 1) |> int
         | _ ->
             if line.Contains "wakes up" then
                 addShift (fst lines.[i-1]) dateTime
@@ -40,11 +40,23 @@ let getSleepestGuard (str : string) : int * int =
     let mutable sleepestTime = 0
     let mutable asleepCountMax = 0
 
+    let mutable sleepestTimeSameTime = 0
+    let mutable asleepCountMaxSameTime = 0
+    let mutable sleepestGuardIdSameTime = 0
+
     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
+        match shifts.[i] |> List.groupBy id |> List.map (fun (id, ids) -> id, List.length ids) |> List.sortByDescending snd |> List.tryHead with
+        | Some (id, nb) ->
+            if nb > asleepCountMaxSameTime then
+                asleepCountMaxSameTime <- nb
+                sleepestTimeSameTime <- i
+                sleepestGuardIdSameTime <- id
+
+        | None -> ()
 
+    (sleepestGuardId, sleepestTime), (sleepestGuardIdSameTime, sleepestTimeSameTime)
diff --git a/Day05.fs b/Day05.fs
new file mode 100644
index 0000000..f2a0f10
--- /dev/null
+++ b/Day05.fs
@@ -0,0 +1,7 @@
+module AdventOfCode2018.Day05
+
+open System
+open System.Collections.Generic
+
+let parseInput (str : string) : string =
+    ""
diff --git a/Program.fs b/Program.fs
index 6e7f256..418610f 100644
--- a/Program.fs
+++ b/Program.fs
@@ -18,8 +18,12 @@ let day03 () =
     sprintf "part1 = %A, part2 = %A" surface claimId
 
 let day04 () =
-    let sleepestGuardId, sleepestTime = File.ReadAllText "Data/day04.input" |> Day04.getSleepestGuard
-    sprintf "part1 = %A, part2 = " (sleepestGuardId * sleepestTime)
+    let (sleepestGuardId, sleepestTime), (sleepestGuardIdSameTime, sleepestTimeSameTime) = File.ReadAllText "Data/day04.input" |> Day04.getSleepestGuard
+    sprintf "part1 = %A, part2 = %A" (sleepestGuardId * sleepestTime) (sleepestGuardIdSameTime * sleepestTimeSameTime)
+
+let day05 () =
+    let foo = File.ReadAllText "Data/day05.input" |> Day05.parseInput
+    sprintf "part1 = , part2 = "
 
 let days : Map<int, unit -> string> =
     [
@@ -27,6 +31,7 @@ let days : Map<int, unit -> string> =
         2, day02
         3, day03
         4, day04
+        5, day05
     ] |> Map.ofList
 
 let doDay (n : int) =
diff --git a/Tests/Day04 tests.fs b/Tests/Day04 tests.fs
index 149aae0..f66fa86 100644
--- a/Tests/Day04 tests.fs	
+++ b/Tests/Day04 tests.fs	
@@ -9,36 +9,36 @@ open Swensen.Unquote
 open AdventOfCode2018
 
 type ``Day04 tests`` (output : ITestOutputHelper) =
+    let input =
+        """
+            [1518-11-01 00:00] Guard #10 begins shift
+            [1518-11-01 00:05] falls asleep
+            [1518-11-01 00:25] wakes up
+            [1518-11-01 00:30] falls asleep
+            [1518-11-01 00:55] wakes up
+            [1518-11-01 23:58] Guard #99 begins shift
+            [1518-11-02 00:40] falls asleep
+            [1518-11-02 00:50] wakes up
+            [1518-11-03 00:05] Guard #10 begins shift
+            [1518-11-03 00:24] falls asleep
+            [1518-11-03 00:29] wakes up
+            [1518-11-04 00:02] Guard #99 begins shift
+            [1518-11-04 00:36] falls asleep
+            [1518-11-04 00:46] wakes up
+            [1518-11-05 00:03] Guard #99 begins shift
+            [1518-11-05 00:45] falls asleep
+            [1518-11-05 00:55] wakes up
+        """
 
     [<Fact>]
     let ``(Part1) From web page`` () =
-        let input =
-            """
-                [1518-11-01 00:00] Guard #10 begins shift
-                [1518-11-01 00:05] falls asleep
-                [1518-11-01 00:25] wakes up
-                [1518-11-01 00:30] falls asleep
-                [1518-11-01 00:55] wakes up
-                [1518-11-01 23:58] Guard #99 begins shift
-                [1518-11-02 00:40] falls asleep
-                [1518-11-02 00:50] wakes up
-                [1518-11-03 00:05] Guard #10 begins shift
-                [1518-11-03 00:24] falls asleep
-                [1518-11-03 00:29] wakes up
-                [1518-11-04 00:02] Guard #99 begins shift
-                [1518-11-04 00:36] falls asleep
-                [1518-11-04 00:46] wakes up
-                [1518-11-05 00:03] Guard #99 begins shift
-                [1518-11-05 00:45] falls asleep
-                [1518-11-05 00:55] wakes up
-            """
-
-        output.WriteLine (Day04.getSleepestGuard input |> string)
-
-        1 =! 2
-        ()
+        let (sleepestGuardId, sleepestTime), _ = Day04.getSleepestGuard input
+        sleepestGuardId =! 10
+        sleepestTime =! 24
 
     [<Fact>]
     let ``(Part2) From web page`` () =
-        ()
+        let _, (sleepestGuardIdSameTime, sleepestTimeSameTime) = Day04.getSleepestGuard input
+        sleepestGuardIdSameTime =! 99
+        sleepestTimeSameTime =! 45
 
diff --git a/Tests/Day05 tests.fs b/Tests/Day05 tests.fs
new file mode 100644
index 0000000..aa0794c
--- /dev/null
+++ b/Tests/Day05 tests.fs	
@@ -0,0 +1,20 @@
+namespace AdventOfCode2018.Tests
+
+open System
+
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2018
+
+type ``Day05 tests`` (output : ITestOutputHelper) =
+
+    [<Fact>]
+    let ``(Part1) From web page`` () =
+        ()
+
+    [<Fact>]
+    let ``(Part2) From web page`` () =
+        ()
+
diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj
index f239854..a7712db 100644
--- a/Tests/Tests.fsproj
+++ b/Tests/Tests.fsproj
@@ -6,6 +6,7 @@
     <ProjectReference Include="..\AdventOfCode2018.fsproj" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Day05 tests.fs" />
     <Compile Include="Day04 tests.fs" />
     <Compile Include="Day03 tests.fs" />
     <Compile Include="Day02 tests.fs" />