From 0b402a149cb4291e3c6cc6a4cdea2b2b2ea51456 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sun, 24 Dec 2017 14:06:31 +0100 Subject: [PATCH] Day 24 --- AdventOfCode2017.sln | 3 ++ AdventOfCode2017/AdventOfCode2017.fsproj | 8 +++-- AdventOfCode2017/Day23.fs | 6 ++-- AdventOfCode2017/Day24.fs | 36 ++++++++++++++++++++ AdventOfCode2017/Program.fs | 6 ++++ Tests/Day24 tests.fs | 42 ++++++++++++++++++++++++ Tests/Tests.fsproj | 1 + 7 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 AdventOfCode2017/Day24.fs create mode 100644 Tests/Day24 tests.fs diff --git a/AdventOfCode2017.sln b/AdventOfCode2017.sln index 9a4c28b..2c108a1 100644 --- a/AdventOfCode2017.sln +++ b/AdventOfCode2017.sln @@ -46,4 +46,7 @@ Global GlobalSection(Performance) = preSolution HasPerformanceSessions = true EndGlobalSection + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection EndGlobal diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 0faadbe..e568040 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 23 + 24 pdbonly @@ -37,7 +37,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 23 + 24 11 @@ -82,6 +82,7 @@ + @@ -153,6 +154,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day23.fs b/AdventOfCode2017/Day23.fs index 34cde9a..9b0556d 100644 --- a/AdventOfCode2017/Day23.fs +++ b/AdventOfCode2017/Day23.fs @@ -1,7 +1,5 @@ module AdventOfCode2017.Day23 -open System - type From = | FromReg of char | FromValue of int64 @@ -13,7 +11,7 @@ type Instruction = | Jump of From * From let parseInput (lines : string[]) : Instruction[] = - let readFrom (str : string) = if Char.IsLetter str.[0] then FromReg str.[0] else FromValue (int64 str) + let readFrom (str : string) = if System.Char.IsLetter str.[0] then FromReg str.[0] else FromValue (int64 str) lines |> Array.map ( fun line -> @@ -51,6 +49,7 @@ let debug () = while not ``end`` do let mutable f = 1 let mutable d = 2 + let mutable loop = true while loop do f <- if b % d = 0 then 0 else f @@ -61,7 +60,6 @@ let debug () = h <- h + 1 ``end`` <- b = c - b <- b + 17 h diff --git a/AdventOfCode2017/Day24.fs b/AdventOfCode2017/Day24.fs new file mode 100644 index 0000000..84b3802 --- /dev/null +++ b/AdventOfCode2017/Day24.fs @@ -0,0 +1,36 @@ +module AdventOfCode2017.Day24 + +type Components = (int * int) list + +let parseInput (lines : string[]) : Components = + lines + |> List.ofArray + |> List.map ( + fun line -> + let numbers = line.Split '/' + int numbers.[0], int numbers.[1] + ) + +let rec chooseAndRemove (f : 'a -> 'b option) (l : 'a list) : ('b * 'a list) list = + l + |> List.choose (fun e -> match f e with Some e' -> Some (e, e') | None -> None) + |> List.map (fun (e, e') -> e', l |> List.except [ e ]) + +let bridges (components : Components) : Components list = + let rec build (components : Components) (bridge : Components) : Components list = + let validComponents = + components + |> chooseAndRemove + (match bridge with + | [] -> fun (a, b) -> if a = 0 then Some (b, a) else None + | (a, _) :: _ -> fun (a', b') -> if a' = a then Some (b', a') elif b' = a then Some (a', b') else None) + + if List.isEmpty validComponents then + [ bridge ] + else + validComponents |> List.fold (fun bridges (c, cs) -> build cs (c :: bridge) @ bridges) [] + build components [] + +let bridgeValue = List.sumBy (fun (a, b) -> a + b) +let maxBridge = List.map bridgeValue >> List.max +let longestBridge = List.sortByDescending (fun bridge -> List.length bridge, bridgeValue bridge) >> List.head >> bridgeValue diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 781fb24..65f1fbf 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -104,6 +104,11 @@ let day23 () = let input = File.ReadAllLines "Data/day23.input" |> Day23.parseInput sprintf "part1 = %A, part2 = %A" (Day23.run input) (Day23.debug ()) +let day24 () = + let input = File.ReadAllLines "Data/day24.input" |> Day24.parseInput + let bridges = Day24.bridges input + sprintf "part1 = %A, part2 = %A" (Day24.maxBridge bridges) (Day24.longestBridge bridges) + let doDay (n : int) = let sw = Diagnostics.Stopwatch () sw.Start () @@ -132,6 +137,7 @@ let doDay (n : int) = | 21 -> day21 () | 22 -> day22 () | 23 -> day23 () + | 24 -> day24 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds diff --git a/Tests/Day24 tests.fs b/Tests/Day24 tests.fs new file mode 100644 index 0000000..eb78b76 --- /dev/null +++ b/Tests/Day24 tests.fs @@ -0,0 +1,42 @@ +namespace AdventOfCode2017.Tests + +open System +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day24 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + let input = + [| + "0/2" + "2/2" + "2/3" + "3/4" + "3/5" + "0/1" + "10/1" + "9/10" + |] |> Day24.parseInput + let bridges = Day24.bridges input + Day24.maxBridge bridges =! 31 + + [] + let ``(Part2) From web page`` () = + let input = + [| + "0/2" + "2/2" + "2/3" + "3/4" + "3/5" + "0/1" + "10/1" + "9/10" + |] |> Day24.parseInput + let bridges = Day24.bridges input + Day24.longestBridge bridges =! 19 \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index e48cd23..31314ef 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -77,6 +77,7 @@ + -- 2.45.2