<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
- <StartArguments>7</StartArguments>
+ <StartArguments>8</StartArguments>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Compile Include="Day5.fs" />
<Compile Include="Day6.fs" />
<Compile Include="Day7.fs" />
+ <Compile Include="Day8.fs" />
<Compile Include="Program.fs" />
<None Include="App.config" />
<None Include="Data\day1.input">
<None Include="Data\day7.input">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
+ <None Include="Data\day8.input">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
--- /dev/null
+module AdventOfCode2017.Day8
+
+open System
+
+type Instruction = string * string * int * string * string * int
+
+let parseInput (lines : string[]) : Instruction list =
+ lines
+ |> List.ofArray
+ |> List.map (
+ fun line ->
+ let line = line.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries)
+ ( line.[0], line.[1], int line.[2], line.[4], line.[5], int line.[6])
+ )
+
+let execute (input : Instruction list) : int * int =
+ let highest, register =
+ input
+ |> List.fold (
+ fun (highest, register) (reg, ins, value, regCond, op, valueCond) ->
+ let regCondValue = register |> Map.tryFind regCond |> Option.defaultValue 0
+ let op' = match op with ">" -> (>) | "<" -> (<) | ">=" -> (>=) | "<=" -> (<=) | "!=" -> (<>) | "==" | _ -> (=)
+ if op' regCondValue valueCond then
+ let regValue = register |> Map.tryFind reg |> Option.defaultValue 0
+ let regValue' = match ins with "inc" -> regValue + value | "dec" -> regValue - value | _ -> regValue
+ max highest regValue', register |> Map.add reg regValue'
+ else
+ highest, register
+ ) (Int32.MinValue, Map.empty)
+ highest, (register |> Map.toList |> List.map snd |> List.max)
let tower = Day7.buildTower input
sprintf "part1 = %A, part2 = %A" tower.Name (Day7.findUnbalanced tower |> Option.map (fun (t, w) -> t.Name, w))
+let day8 () =
+ let input = File.ReadAllLines "Data/day8.input" |> Day8.parseInput
+ let part1, part2 = Day8.execute input
+ sprintf "part1 = %A, part2 = %A" part1 part2
+
let doDay (n : int) =
let sw = Diagnostics.Stopwatch ()
sw.Start ()
| 5 -> day5 ()
| 6 -> day6 ()
| 7 -> day7 ()
+ | 8 -> day8 ()
| _ -> raise <| NotImplementedException ()
printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds
else
for d = 1 to 25 do
doDay d
+
+ Console.ReadKey () |> ignore
0
--- /dev/null
+namespace AdventOfCode2017.Tests
+
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day8 tests`` (output : ITestOutputHelper) =
+
+ [<Fact>]
+ let ``(Part1) From web page`` () =
+ let input =
+ [|
+ "b inc 5 if a > 1"
+ "a inc 1 if b < 5"
+ "c dec -10 if a >= 1"
+ "c inc -20 if c == 10"
+ |]
+ let p1, _ = Day8.execute (Day8.parseInput input)
+ p1 = 1
+
+ [<Fact>]
+ let ``(Part2) From web page`` () =
+ let input =
+ [|
+ "b inc 5 if a > 1"
+ "a inc 1 if b < 5"
+ "c dec -10 if a >= 1"
+ "c inc -20 if c == 10"
+ |]
+ let _, p2 = Day8.execute (Day8.parseInput input)
+ p2 = 10
\ No newline at end of file
<Compile Include="Day5 tests.fs" />
<Compile Include="Day6 tests.fs" />
<Compile Include="Day7 tests.fs" />
+ <Compile Include="Day8 tests.fs" />
<Content Include="packages.config" />
<Content Include="App.config" />
</ItemGroup>