Advent of Code F# – Day 25

Yan Cui

I help clients go faster for less using serverless technologies.

ps. look out for all my other solutions for Advent of Code challenges here.

 

Day 25

See details of the challenge here.

Today’s input looks like this:

cpy a d
cpy 7 c
cpy 362 b
inc d
dec b
jnz b -2

At last, we’re at the end of this year’s Advent of Code challenges, and today’s challenge is another twist of the assembunny code we’ve seen a few times this year, most recently on Day 23.

Ad before, let’s define the instruction set as a union type and make short work of the parsing the input file.

open System
open System.Collections.Generic
open System.IO
let inputs = File.ReadAllLines (__SOURCE_DIRECTORY__ + "/Day25Input.txt")
type Operant =
| Value of int
| Reg of string
type Instruction =
| Cpy of n: Operant * dest: Operant
| Inc of string
| Dec of string
| Jnz of n: Operant * offset: Operant
| Out of n: Operant
let (|Operant|) x =
match Int32.TryParse x with
| true, n -> Value n
| _ -> Reg x
let instructions =
inputs
|> Array.map (fun l -> l.Split())
|> Array.map (function
| [| "cpy"; Operant n; Operant dest |] -> Cpy (n, dest)
| [| "inc"; reg |] -> Inc reg
| [| "dec"; reg |] -> Dec reg
| [| "jnz"; Operant n; Operant offset |] -> Jnz (n, offset)
| [| "out"; Operant n |] -> Out n)
view raw Day25_parse.fsx hosted with ❤ by GitHub

One notable difference to today’s challenge though is that we’re no longer interested in the value of the registers but the output from executing the program.

So instead of returning the registers we’ll use a sequence comprehension to return the output values as an infinite, and lazy sequence instead.

(ps. I initially wrote the execute function with a nested recursive function, but on second thought the problem feels more concisely expressed as a while loop so I rewrote it as such)

let execute initValues (instructions : Instruction[]) =
let registers = new Dictionary<string, int>()
initValues |> Seq.iter (fun (key, value) -> registers.[key] <- value)
let fetch = function
| Value n -> n
| Reg reg ->
match registers.TryGetValue reg with
| true, n -> n
| _ -> 0
seq {
let mutable idx = 0
while idx < instructions.Length do
match instructions.[idx] with
| Cpy (n, Reg dest) ->
registers.[dest] <- fetch n
idx <- idx + 1
| Inc reg ->
registers.[reg] <- registers.[reg] + 1
idx <- idx + 1
| Dec reg ->
registers.[reg] <- registers.[reg] - 1
idx <- idx + 1
| Jnz (n, offset) when fetch n <> 0 ->
idx <- idx + fetch offset
| Out n ->
yield fetch n
idx <- idx + 1
| _ ->
idx <- idx + 1
}

To wrap up today’s challenge (part 2 just requires you to click a link to wrap up your AOC journey for another year) we need to find the first value of n that will yield pairs of [ 0, 1 ] indefinitely. Of course you can’t be sure that the pattern repeats indefinitely without deeper understanding of the pattern of change in the register values (perhaps it’d be a better approach rather than the trial-and-error route I have gone with). I have gone with 1000 values all following that pattern = repeating indefinitely, but turns out the answer is the same even when you use Seq.take 10.

let part1 =
Seq.initInfinite id
|> Seq.find (fun n ->
execute [ "a", n ] instructions
|> Seq.take 1000
|> Seq.chunkBySize 2
|> Seq.forall (fun [| a; b |] -> a = 0 && b = 1))
view raw Day25_part1.fsx hosted with ❤ by GitHub

So there it is folks, another year and another set of wonderfully enjoyable challenges. With you all a happy new year and see you soon!

 

Links

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

1 thought on “Advent of Code F# – Day 25”

  1. Pingback: F# Weekly #1, 2017 – New Year Edition – Sergey Tihon's Blog

Comments are closed.