dominicletz
Advent of Code 2020 - Day 8
This topic is about Day 8 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/leaderboard/private/view/39276
The join code is:
39276-eeb74f9a
Most Liked
dominicletz
Loved part 2 today.
My solution, not clean but working. Everytime a branch fails (returns false) it tries another branch interpreting jmp as nop or vice versa – but only once tracked by the changed bool - finishes as soon as a valid branch has been found:
#!/usr/bin/env elixir
defmodule Day8 do
def reduce(instr, ptr, state, changed) do
case Map.pop(instr, ptr) do
{{"nop", n}, instr} -> reduce(instr, ptr + 1, state, changed) || (unless changed, do: reduce(instr, ptr + n, state, true))
{{"jmp", n}, instr} -> reduce(instr, ptr + n, state, changed) || (unless changed, do: reduce(instr, ptr + 1, state, true))
{{"acc", n}, instr} -> reduce(instr, ptr + 1, state + n, changed)
{nil, _instr} -> false
{:fin, _instr} -> state
end
end
end
instr = File.read!("8.csv")
|> String.split("\n", trim: true)
|> Enum.with_index()
|> Enum.map(fn {row, idx} ->
[op, num] = String.split(row)
num = String.to_integer(num)
{idx, {op, num}}
end)
|> Map.new()
instr = Map.put(instr, map_size(instr), :fin)
Day8.reduce(instr, 0, 0, false)
|> IO.inspect()
LostKobrakai
I build a struct + proper API for the bootloader today, before even trying to get to the answers – the goal being someone should be able to understand the code even without knowing the problem. This approach made part two quite simple because all I needed to add was brute-forcing the intended instruction changes and attempting to run the bootloader for each attempt like for part 1.
kwando
This one is really clever, it completes in 38 microseconds instead of 28ms which my brute force method does 
JEG2
I got to use some cool Stream functions to solve this one:








