From 51a23be744fb922d5d7003d999a74d4efcda9782 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Tue, 4 Dec 2018 16:20:35 +0100 Subject: [PATCH] Beginning of day 4. --- AdventOfCode2018.fsproj | 4 ++++ Day04.fs | 53 +++++++++++++++++++++++++++++++++++++++++ Program.fs | 7 ++++-- Tests/Day04 tests.fs | 44 ++++++++++++++++++++++++++++++++++ Tests/Tests.fsproj | 1 + 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 Day04.fs create mode 100644 Tests/Day04 tests.fs diff --git a/AdventOfCode2018.fsproj b/AdventOfCode2018.fsproj index 66bd76c..87b4c6f 100644 --- a/AdventOfCode2018.fsproj +++ b/AdventOfCode2018.fsproj @@ -4,10 +4,14 @@ netcoreapp2.1 + + + PreserveNewest + PreserveNewest diff --git a/Day04.fs b/Day04.fs new file mode 100644 index 0000000..c3f2ee6 --- /dev/null +++ b/Day04.fs @@ -0,0 +1,53 @@ +module AdventOfCode2018.Day04 + +open System + +let parseInput (str : string) : string = + 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 ( + fun line -> + let trimmed = line.Trim () + 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 + + 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 + | _ -> + if line.Contains "wakes up" then + addShift id (fst lines.[i-1]) dateTime + + sprintf "%A" shifts + + + + + + + + //Map. + + + // \ No newline at end of file diff --git a/Program.fs b/Program.fs index 1fe631b..346eb36 100644 --- a/Program.fs +++ b/Program.fs @@ -17,11 +17,16 @@ let day03 () = let surface, claimId = Day03.overlappingSurface claims sprintf "part1 = %A, part2 = %A" surface claimId +let day04 () = + // sprintf "part1 = %A, part2 = %A" surface claimId + "" + let days : Map string> = [ 1, day01 2, day02 3, day03 + 4, day04 ] |> Map.ofList let doDay (n : int) = @@ -48,6 +53,4 @@ let main argv = else for day in days do doDay day.Key - - Console.Read () |> ignore 0 diff --git a/Tests/Day04 tests.fs b/Tests/Day04 tests.fs new file mode 100644 index 0000000..b952e14 --- /dev/null +++ b/Tests/Day04 tests.fs @@ -0,0 +1,44 @@ +namespace AdventOfCode2018.Tests + +open System + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2018 + +type ``Day04 tests`` (output : ITestOutputHelper) = + + [] + 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.parseInput input) + + 1 =! 2 + () + + [] + let ``(Part2) From web page`` () = + () + diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 7ed0d16..f239854 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -6,6 +6,7 @@ + -- 2.45.2