|
| 1 | +defmodule AdventOfCode.Y2016.Day25 do |
| 2 | + @moduledoc """ |
| 3 | + --- Day 25: Clock Signal --- |
| 4 | + Problem Link: https://adventofcode.com/2016/day/25 |
| 5 | + Difficulty: m |
| 6 | + Tags: assembunny simulation optimization |
| 7 | + """ |
| 8 | + alias AdventOfCode.Helpers.{InputReader, Transformers} |
| 9 | + |
| 10 | + def input, do: InputReader.read_from_file(2016, 25) |
| 11 | + |
| 12 | + def run(input \\ input()) do |
| 13 | + instructions = parse(input) |> List.to_tuple() |
| 14 | + |
| 15 | + # We find the smallest 'a' that produces 0,1,0,1... |
| 16 | + # Based on architectural analysis: d = a + (byte1 * byte2). |
| 17 | + # d must be a number like 101010101010... in binary. |
| 18 | + # But let's use the interpreter for robustness. |
| 19 | + solution_1 = find_start_value(instructions, 0) |
| 20 | + |
| 21 | + {solution_1, :done} |
| 22 | + end |
| 23 | + |
| 24 | + defp find_start_value(instructions, a) do |
| 25 | + if matches_pattern?(instructions, a) do |
| 26 | + a |
| 27 | + else |
| 28 | + find_start_value(instructions, a + 1) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + defp matches_pattern?(instructions, a) do |
| 33 | + registers = %{"a" => a, "b" => 0, "c" => 0, "d" => 0} |
| 34 | + # Check first 10 outputs. If they are 0,1,0,1,0,1,0,1,0,1, it's likely correct. |
| 35 | + # We also keep track of visited PC + Registers to detect if it's truly infinite. |
| 36 | + # But 10 cycles for clock signal is usually definitive in AoC. |
| 37 | + execute(instructions, registers, 0, []) |
| 38 | + end |
| 39 | + |
| 40 | + defp execute(_instructions, _regs, _pc, output) when length(output) == 12 do |
| 41 | + # 12 pulses (0,1 * 6) is sufficient to confirm the pattern. |
| 42 | + true |
| 43 | + end |
| 44 | + |
| 45 | + defp execute(instructions, regs, pc, output) do |
| 46 | + if pc < 0 or pc >= tuple_size(instructions) do |
| 47 | + false |
| 48 | + else |
| 49 | + case elem(instructions, pc) do |
| 50 | + {:cpy, x, y} -> |
| 51 | + execute(instructions, Map.put(regs, y, val(x, regs)), pc + 1, output) |
| 52 | + |
| 53 | + {:inc, x} -> |
| 54 | + execute(instructions, Map.update!(regs, x, &(&1 + 1)), pc + 1, output) |
| 55 | + |
| 56 | + {:dec, x} -> |
| 57 | + execute(instructions, Map.update!(regs, x, &(&1 - 1)), pc + 1, output) |
| 58 | + |
| 59 | + {:jnz, x, y} -> |
| 60 | + new_pc = if val(x, regs) != 0, do: pc + val(y, regs), else: pc + 1 |
| 61 | + execute(instructions, regs, new_pc, output) |
| 62 | + |
| 63 | + {:out, x} -> |
| 64 | + v = val(x, regs) |
| 65 | + expected = rem(length(output), 2) |
| 66 | + |
| 67 | + if v == expected do |
| 68 | + execute(instructions, regs, pc + 1, output ++ [v]) |
| 69 | + else |
| 70 | + false |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + defp val(x, _regs) when is_integer(x), do: x |
| 77 | + defp val(x, regs), do: Map.get(regs, x) |
| 78 | + |
| 79 | + def parse(data \\ input()) do |
| 80 | + data |
| 81 | + |> Transformers.lines() |
| 82 | + |> Enum.map(fn line -> |
| 83 | + case String.split(line) do |
| 84 | + ["cpy", x, y] -> {:cpy, sanitize(x), y} |
| 85 | + ["inc", x] -> {:inc, x} |
| 86 | + ["dec", x] -> {:dec, x} |
| 87 | + ["jnz", x, y] -> {:jnz, sanitize(x), sanitize(y)} |
| 88 | + ["out", x] -> {:out, sanitize(x)} |
| 89 | + end |
| 90 | + end) |
| 91 | + end |
| 92 | + |
| 93 | + defp sanitize(val) do |
| 94 | + case Integer.parse(val) do |
| 95 | + {num, ""} -> num |
| 96 | + :error -> val |
| 97 | + end |
| 98 | + end |
| 99 | +end |
0 commit comments