diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2024-10-20 18:01:18 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2024-10-20 18:01:18 +0200 |
commit | 9fb42bf830560502aaf5acb3241c3bc8b53a35c2 (patch) | |
tree | f09095ca5e88a665d60ef644fcf4cd69eeaa72bf | |
parent | b97d59a40674787776947ad6485e22c5ef5862b9 (diff) |
Day 23
-rw-r--r-- | 2015/day23.exs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/2015/day23.exs b/2015/day23.exs new file mode 100644 index 0000000..433f70a --- /dev/null +++ b/2015/day23.exs @@ -0,0 +1,57 @@ +require Integer + +program = + File.stream!("inputs/day23.txt") + |> Enum.map(&String.trim(&1)) + +programLength = length(program) + +step = fn {programCounter, a, b} -> + instruction = Enum.at(program, programCounter) + + case instruction do + "hlf a" -> + {programCounter + 1, div(a, 2), b} + + "tpl a" -> + {programCounter + 1, a * 3, b} + + "inc a" -> + {programCounter + 1, a + 1, b} + + "inc b" -> + {programCounter + 1, a, b + 1} + + <<"jmp " <> offset::binary>> -> + {offset, ""} = Integer.parse(offset) + {programCounter + offset, a, b} + + <<"jie a, " <> offset::binary>> -> + if Integer.is_even(a) do + {offset, ""} = Integer.parse(offset) + {programCounter + offset, a, b} + else + {programCounter + 1, a, b} + end + + <<"jio a, " <> offset::binary>> -> + if a == 1 do + {offset, ""} = Integer.parse(offset) + {programCounter + offset, a, b} + else + {programCounter + 1, a, b} + end + end +end + +{_, _, terminalState1} = + Stream.iterate({0, 0, 0}, &step.(&1)) + |> Enum.find(fn {programCounter, _, _} -> programCounter >= programLength end) + +IO.puts("Part 1: #{terminalState1}") + +{_, _, terminalState2} = + Stream.iterate({0, 1, 0}, &step.(&1)) + |> Enum.find(fn {programCounter, _, _} -> programCounter >= programLength end) + +IO.puts("Part 2: #{terminalState2}") |