From aa62b0c47489864f3c4bee436b56617207c89f4a Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 23 Dec 2017 22:38:23 +0100 Subject: [PATCH] Day 23 --- AdventOfCode2017/AdventOfCode2017.fsproj | 8 ++- AdventOfCode2017/Day22.fs | 4 +- AdventOfCode2017/Day23.fs | 68 ++++++++++++++++++++++++ AdventOfCode2017/Program.fs | 5 ++ 4 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 AdventOfCode2017/Day23.fs diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 1e530b7..0faadbe 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 22 + 23 pdbonly @@ -37,7 +37,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 22 + 23 11 @@ -81,6 +81,7 @@ + @@ -149,6 +150,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day22.fs b/AdventOfCode2017/Day22.fs index 1de16e8..532c465 100644 --- a/AdventOfCode2017/Day22.fs +++ b/AdventOfCode2017/Day22.fs @@ -13,9 +13,9 @@ let parseInput (lines : string[]) : M = m.Add ((x - lines.[y].Length / 2, -y + lines.Length / 2), Infected) m -let reverse (dx, dy) = -dx, -dy +let inline reverse (dx, dy) = -dx, -dy let turnRight = function 1, 0 -> 0, -1 | 0, 1 -> 1, 0 | -1, 0 -> 0, 1 | _ -> -1, 0 -let turnLeft = turnRight >> reverse +let turnLeft = turnRight >> reverse let infection (rule : CellState -> CellState * ((int * int) -> (int * int))) (nbIterations : int) (m : M) : int = let rec burst (x, y) d n becomeInfected = diff --git a/AdventOfCode2017/Day23.fs b/AdventOfCode2017/Day23.fs new file mode 100644 index 0000000..34cde9a --- /dev/null +++ b/AdventOfCode2017/Day23.fs @@ -0,0 +1,68 @@ +module AdventOfCode2017.Day23 + +open System + +type From = + | FromReg of char + | FromValue of int64 + +type Instruction = + | Set of char * From + | Sub of char * From + | Mul of char * From + | 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) + lines + |> Array.map ( + fun line -> + match line.Split ' ' with + | [| "set"; reg; v |] -> Set (reg.[0], readFrom v) + | [| "sub"; reg; v |] -> Sub (reg.[0], readFrom v) + | [| "mul"; reg; v |] -> Mul (reg.[0], readFrom v) + | [| "jnz"; v1; v2 |] -> Jump (readFrom v1, readFrom v2) + | _ -> failwithf "Can't parse line: %s" line + ) + +type Register = Map + +let run (instructions : Instruction[]) : int = + let rec exec (register : Register) (cursor : int) (nbMul : int) : int = + let get = function FromReg reg -> register |> Map.tryFind reg |> Option.defaultValue 0L | FromValue v -> v + let set (reg : char) (v : int64) = register |> Map.add reg v + + if cursor < 0 || cursor >= instructions.Length then + nbMul + else + match instructions.[cursor] with + | Set (reg, from) -> exec (set reg (get from)) (cursor + 1) nbMul + | Sub (reg, from) -> exec (set reg (get (FromReg reg) - get from)) (cursor + 1) nbMul + | Mul (reg, from) -> exec (set reg (get (FromReg reg) * get from)) (cursor + 1) (nbMul + 1) + | Jump (from1, from2) -> exec register (cursor + if get from1 <> 0L then get from2 |> int else 1) nbMul + exec Map.empty 0 0 + +let debug () = + let mutable b = 99 * 100 + 100000 + let c = b + 17000 + let mutable h = 0 + + let mutable ``end`` = false + 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 + d <- d + 1 + loop <- d <> b + + if f = 0 then + h <- h + 1 + + ``end`` <- b = c + + b <- b + 17 + h + + diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 379137e..781fb24 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -100,6 +100,10 @@ let day22 () = let input = File.ReadAllLines "Data/day22.input" |> Day22.parseInput sprintf "part1 = %A, part2 = %A" (Day22.infection1 input) (Day22.infection2 input) +let day23 () = + let input = File.ReadAllLines "Data/day23.input" |> Day23.parseInput + sprintf "part1 = %A, part2 = %A" (Day23.run input) (Day23.debug ()) + let doDay (n : int) = let sw = Diagnostics.Stopwatch () sw.Start () @@ -127,6 +131,7 @@ let doDay (n : int) = | 20 -> day20 () | 21 -> day21 () | 22 -> day22 () + | 23 -> day23 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds -- 2.45.2