GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
+ GlobalSection(Performance) = preSolution
+ HasPerformanceSessions = true
+ EndGlobalSection
EndGlobal
<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
- <StartArguments>23</StartArguments>
+ <StartArguments>24</StartArguments>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
- <StartArguments>23</StartArguments>
+ <StartArguments>24</StartArguments>
</PropertyGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
<Compile Include="Day21.fs" />
<Compile Include="Day22.fs" />
<Compile Include="Day23.fs" />
+ <Compile Include="Day24.fs" />
<Compile Include="Program.fs" />
<None Include="App.config" />
<Content Include="Data\day01.input">
<Content Include="Data\day23.input">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
+ <Content Include="Data\day24.input">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
module AdventOfCode2017.Day23
-open System
-
type From =
| FromReg of char
| FromValue of int64
| 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 ->
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
h <- h + 1
``end`` <- b = c
-
b <- b + 17
h
--- /dev/null
+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
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 ()
| 21 -> day21 ()
| 22 -> day22 ()
| 23 -> day23 ()
+ | 24 -> day24 ()
| _ -> raise <| NotImplementedException ()
printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds
--- /dev/null
+namespace AdventOfCode2017.Tests
+
+open System
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day24 tests`` (output : ITestOutputHelper) =
+
+ [<Fact>]
+ 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
+
+ [<Fact>]
+ 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
<Compile Include="Day20 tests.fs" />
<Compile Include="Day21 tests.fs" />
<Compile Include="Day22 tests.fs" />
+ <Compile Include="Day24 tests.fs" />
<Content Include="App.config" />
<Content Include="packages.config" />
</ItemGroup>