From 19cef44652bad540b9046dc2a05e50061e010ab8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Fri, 7 Dec 2018 14:02:29 +0100 Subject: [PATCH] Day 7, part 1. --- AdventOfCode2018.fsproj | 4 ++++ Day07.fs | 26 ++++++++++++++++++++++++++ Program.fs | 5 +++++ Tests/Day07 tests.fs | 29 +++++++++++++++++++++++++++++ Tests/Tests.fsproj | 1 + 5 files changed, 65 insertions(+) create mode 100644 Day07.fs create mode 100644 Tests/Day07 tests.fs diff --git a/AdventOfCode2018.fsproj b/AdventOfCode2018.fsproj index 7026dc3..e78a65c 100644 --- a/AdventOfCode2018.fsproj +++ b/AdventOfCode2018.fsproj @@ -4,6 +4,7 @@ netcoreapp2.1 + @@ -11,6 +12,9 @@ + + PreserveNewest + PreserveNewest diff --git a/Day07.fs b/Day07.fs new file mode 100644 index 0000000..9274501 --- /dev/null +++ b/Day07.fs @@ -0,0 +1,26 @@ +module AdventOfCode2018.Day07 + +open System + +type Constraint = char * char + +let parseInput (str : string) : Constraint array = + str.Split ([| '\r'; '\n'; |], StringSplitOptions.RemoveEmptyEntries) + |> Array.map ( + fun line -> + let trimmedLine = line.Trim () + trimmedLine.[5], trimmedLine.[36] + ) + +let order (constraints : Constraint array) : char array = + let constraints = constraints |> Array.sortBy fst + + let rec loop (constraints : Constraint array) : char list = + match constraints with + | [| (a, b) |] -> [ a; b ] + | _ -> + let next = constraints |> Array.pick (fun (a, _) -> if constraints |> Array.forall (fun (_, b) -> a <> b) then Some a else None) + let rest = constraints |> Array.filter (fst >> ((<>) next)) + next :: (loop rest) + + loop constraints |> Array.ofList \ No newline at end of file diff --git a/Program.fs b/Program.fs index fcb5fc1..ac4a5d3 100644 --- a/Program.fs +++ b/Program.fs @@ -29,6 +29,10 @@ let day06 () = let coords = File.ReadAllText "Data/day06.input" |> Day06.parseInput sprintf "part1 = %A, part2 = %A" (Day06.getLargestArea coords) (Day06.getAreaWithTotalDistanceLessThan 10000 coords) +let day07 () = + let constraints = File.ReadAllText "Data/day07.input" |> Day07.parseInput + sprintf "part1 = %A, part2 =" (Day07.order constraints |> String.Concat) + let days : Map string> = [ 1, day01 @@ -37,6 +41,7 @@ let days : Map string> = 4, day04 5, day05 6, day06 + 7, day07 ] |> Map.ofList let doDay (n : int) = diff --git a/Tests/Day07 tests.fs b/Tests/Day07 tests.fs new file mode 100644 index 0000000..6a96601 --- /dev/null +++ b/Tests/Day07 tests.fs @@ -0,0 +1,29 @@ +namespace AdventOfCode2018.Tests + +open System + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2018 + +type ``Day07 tests`` (output : ITestOutputHelper) = + let input = + """Step C must be finished before step A can begin. + Step C must be finished before step F can begin. + Step A must be finished before step B can begin. + Step A must be finished before step D can begin. + Step B must be finished before step E can begin. + Step D must be finished before step E can begin. + Step F must be finished before step E can begin.""" + + [] + let ``(Part1) From web page`` () = + let constrains = Day07.parseInput input + Day07.order constrains |> String.Concat =! "CABDFE" + + [] + let ``(Part2) From web page`` () = + () + diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 0042788..b905858 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -6,6 +6,7 @@ + -- 2.45.2