<TargetFramework>netcoreapp2.1</TargetFramework>\r
</PropertyGroup>\r
<ItemGroup>\r
+ <Compile Include="Day05.fs" />\r
<Compile Include="Day04.fs" />\r
<Compile Include="Day03.fs" />\r
<Compile Include="Day02.fs" />\r
<Compile Include="Day01.fs" />\r
<Compile Include="Program.fs" />\r
+ <Content Include="Data/day05.input">\r
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>\r
+ </Content>\r
<Content Include="Data/day04.input">\r
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>\r
</Content>\r
open System\r
open System.Collections.Generic\r
\r
-let getSleepestGuard (str : string) : int * int =\r
+let getSleepestGuard (str : string) : (int * int) * (int * int) =\r
let shifts : int list array = Array.create 60 []\r
\r
let lines =\r
)\r
|> Array.sortBy fst\r
\r
- let mutable id = 0\r
+ let mutable guardId = 0\r
let minutesAsleep = Dictionary<int, int> ()\r
\r
let addShift (date1 : DateTime) (date2 : DateTime) =\r
let min1 = if date1.Hour > 0 then 0 else date1.Minute\r
let min2 = if date2.Hour > 0 then 60 else date2.Minute\r
\r
- minutesAsleep.[id] <- minutesAsleep.GetValueOrDefault (id, 0) + min2 - min1\r
+ minutesAsleep.[guardId] <- minutesAsleep.GetValueOrDefault (guardId, 0) + min2 - min1\r
\r
for m = min1 to min2 - 1 do\r
- shifts.[m] <- id :: shifts.[m]\r
+ shifts.[m] <- guardId :: shifts.[m]\r
\r
for i = 0 to lines.Length - 1 do\r
let dateTime, line = lines.[i]\r
match line.IndexOf '#' with\r
- | i when i <> -1 -> id <- line.Substring (i + 1, line.IndexOf (' ', i) - i - 1) |> int\r
+ | i when i <> -1 -> guardId <- line.Substring (i + 1, line.IndexOf (' ', i) - i - 1) |> int\r
| _ ->\r
if line.Contains "wakes up" then\r
addShift (fst lines.[i-1]) dateTime\r
let mutable sleepestTime = 0\r
let mutable asleepCountMax = 0\r
\r
+ let mutable sleepestTimeSameTime = 0\r
+ let mutable asleepCountMaxSameTime = 0\r
+ let mutable sleepestGuardIdSameTime = 0\r
+\r
for i = 0 to shifts.Length - 1 do\r
let asleepCount = shifts.[i] |> List.where ((=) sleepestGuardId) |> List.length\r
if asleepCount > asleepCountMax then\r
sleepestTime <- i\r
asleepCountMax <- asleepCount\r
\r
- sleepestGuardId, sleepestTime\r
+ match shifts.[i] |> List.groupBy id |> List.map (fun (id, ids) -> id, List.length ids) |> List.sortByDescending snd |> List.tryHead with\r
+ | Some (id, nb) ->\r
+ if nb > asleepCountMaxSameTime then\r
+ asleepCountMaxSameTime <- nb\r
+ sleepestTimeSameTime <- i\r
+ sleepestGuardIdSameTime <- id\r
+\r
+ | None -> ()\r
\r
+ (sleepestGuardId, sleepestTime), (sleepestGuardIdSameTime, sleepestTimeSameTime)\r
--- /dev/null
+module AdventOfCode2018.Day05\r
+\r
+open System\r
+open System.Collections.Generic\r
+\r
+let parseInput (str : string) : string =\r
+ ""\r
sprintf "part1 = %A, part2 = %A" surface claimId\r
\r
let day04 () =\r
- let sleepestGuardId, sleepestTime = File.ReadAllText "Data/day04.input" |> Day04.getSleepestGuard\r
- sprintf "part1 = %A, part2 = " (sleepestGuardId * sleepestTime)\r
+ let (sleepestGuardId, sleepestTime), (sleepestGuardIdSameTime, sleepestTimeSameTime) = File.ReadAllText "Data/day04.input" |> Day04.getSleepestGuard\r
+ sprintf "part1 = %A, part2 = %A" (sleepestGuardId * sleepestTime) (sleepestGuardIdSameTime * sleepestTimeSameTime)\r
+\r
+let day05 () =\r
+ let foo = File.ReadAllText "Data/day05.input" |> Day05.parseInput\r
+ sprintf "part1 = , part2 = "\r
\r
let days : Map<int, unit -> string> =\r
[\r
2, day02\r
3, day03\r
4, day04\r
+ 5, day05\r
] |> Map.ofList\r
\r
let doDay (n : int) =\r
open AdventOfCode2018\r
\r
type ``Day04 tests`` (output : ITestOutputHelper) =\r
+ let input =\r
+ """\r
+ [1518-11-01 00:00] Guard #10 begins shift\r
+ [1518-11-01 00:05] falls asleep\r
+ [1518-11-01 00:25] wakes up\r
+ [1518-11-01 00:30] falls asleep\r
+ [1518-11-01 00:55] wakes up\r
+ [1518-11-01 23:58] Guard #99 begins shift\r
+ [1518-11-02 00:40] falls asleep\r
+ [1518-11-02 00:50] wakes up\r
+ [1518-11-03 00:05] Guard #10 begins shift\r
+ [1518-11-03 00:24] falls asleep\r
+ [1518-11-03 00:29] wakes up\r
+ [1518-11-04 00:02] Guard #99 begins shift\r
+ [1518-11-04 00:36] falls asleep\r
+ [1518-11-04 00:46] wakes up\r
+ [1518-11-05 00:03] Guard #99 begins shift\r
+ [1518-11-05 00:45] falls asleep\r
+ [1518-11-05 00:55] wakes up\r
+ """\r
\r
[<Fact>]\r
let ``(Part1) From web page`` () =\r
- let input =\r
- """\r
- [1518-11-01 00:00] Guard #10 begins shift\r
- [1518-11-01 00:05] falls asleep\r
- [1518-11-01 00:25] wakes up\r
- [1518-11-01 00:30] falls asleep\r
- [1518-11-01 00:55] wakes up\r
- [1518-11-01 23:58] Guard #99 begins shift\r
- [1518-11-02 00:40] falls asleep\r
- [1518-11-02 00:50] wakes up\r
- [1518-11-03 00:05] Guard #10 begins shift\r
- [1518-11-03 00:24] falls asleep\r
- [1518-11-03 00:29] wakes up\r
- [1518-11-04 00:02] Guard #99 begins shift\r
- [1518-11-04 00:36] falls asleep\r
- [1518-11-04 00:46] wakes up\r
- [1518-11-05 00:03] Guard #99 begins shift\r
- [1518-11-05 00:45] falls asleep\r
- [1518-11-05 00:55] wakes up\r
- """\r
-\r
- output.WriteLine (Day04.getSleepestGuard input |> string)\r
-\r
- 1 =! 2\r
- ()\r
+ let (sleepestGuardId, sleepestTime), _ = Day04.getSleepestGuard input\r
+ sleepestGuardId =! 10\r
+ sleepestTime =! 24\r
\r
[<Fact>]\r
let ``(Part2) From web page`` () =\r
- ()\r
+ let _, (sleepestGuardIdSameTime, sleepestTimeSameTime) = Day04.getSleepestGuard input\r
+ sleepestGuardIdSameTime =! 99\r
+ sleepestTimeSameTime =! 45\r
\r
--- /dev/null
+namespace AdventOfCode2018.Tests\r
+\r
+open System\r
+\r
+open Xunit\r
+open Xunit.Abstractions\r
+open Swensen.Unquote\r
+\r
+open AdventOfCode2018\r
+\r
+type ``Day05 tests`` (output : ITestOutputHelper) =\r
+\r
+ [<Fact>]\r
+ let ``(Part1) From web page`` () =\r
+ ()\r
+\r
+ [<Fact>]\r
+ let ``(Part2) From web page`` () =\r
+ ()\r
+\r
<ProjectReference Include="..\AdventOfCode2018.fsproj" />\r
</ItemGroup>\r
<ItemGroup>\r
+ <Compile Include="Day05 tests.fs" />\r
<Compile Include="Day04 tests.fs" />\r
<Compile Include="Day03 tests.fs" />\r
<Compile Include="Day02 tests.fs" />\r