From: Greg Burri Date: Fri, 8 Dec 2017 05:50:58 +0000 (+0100) Subject: Day 8 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=3d1f45f883bc4fb574ec86187d4a50c2ee8183f9;p=advent_of_code_2017.git Day 8 --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index dcabd03..b0c1b37 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 7 + 8 pdbonly @@ -63,6 +63,7 @@ + @@ -83,6 +84,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day8.fs b/AdventOfCode2017/Day8.fs new file mode 100644 index 0000000..04ec413 --- /dev/null +++ b/AdventOfCode2017/Day8.fs @@ -0,0 +1,30 @@ +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) diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 7344c5c..a53a948 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -33,6 +33,11 @@ let day7 () = 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 () @@ -45,6 +50,7 @@ let doDay (n : int) = | 5 -> day5 () | 6 -> day6 () | 7 -> day7 () + | 8 -> day8 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds @@ -57,4 +63,6 @@ let main argv = else for d = 1 to 25 do doDay d + + Console.ReadKey () |> ignore 0 diff --git a/Tests/Day8 tests.fs b/Tests/Day8 tests.fs new file mode 100644 index 0000000..823ca8d --- /dev/null +++ b/Tests/Day8 tests.fs @@ -0,0 +1,33 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day8 tests`` (output : ITestOutputHelper) = + + [] + 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 + + [] + 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 diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 0bec47c..fe9fc9c 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -62,6 +62,7 @@ +