<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
- <StartArguments>22</StartArguments>
+ <StartArguments>23</StartArguments>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
- <StartArguments>22</StartArguments>
+ <StartArguments>23</StartArguments>
</PropertyGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
<Compile Include="Day20.fs" />
<Compile Include="Day21.fs" />
<Compile Include="Day22.fs" />
+ <Compile Include="Day23.fs" />
<Compile Include="Program.fs" />
<None Include="App.config" />
<Content Include="Data\day01.input">
<Content Include="Data\day22.input">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
+ <Content Include="Data\day23.input">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
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 =
--- /dev/null
+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<char, int64>
+
+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
+
+
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 ()
| 20 -> day20 ()
| 21 -> day21 ()
| 22 -> day22 ()
+ | 23 -> day23 ()
| _ -> raise <| NotImplementedException ()
printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds