diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/day16.exs | 194 | ||||
-rw-r--r-- | src/day17.exs | 127 | ||||
-rw-r--r-- | src/day18.exs | 94 |
3 files changed, 415 insertions, 0 deletions
diff --git a/src/day16.exs b/src/day16.exs new file mode 100644 index 0000000..827dc25 --- /dev/null +++ b/src/day16.exs @@ -0,0 +1,194 @@ +defmodule Day16 do + defmodule State do + @enforce_keys [:pqueue, :map, :path, :dist] + defstruct [:pqueue, :map, :path, :dist] + + def new(game) do + pqueue = :gb_sets.from_list([{0, game.start}]) + map = :gb_trees.insert(game.start, 0, :gb_trees.empty()) + path = Map.new([{game.start, []}]) + dist = Map.new([{game.start, 0}]) + %State{pqueue: pqueue, map: map, path: path, dist: dist} + end + + def upsert(state, posdir, dist, prev) do + case :gb_trees.lookup(posdir, state.map) do + :none -> + pqueue = :gb_sets.insert({dist, posdir}, state.pqueue) + map = :gb_trees.insert(posdir, dist, state.map) + path = Map.put(state.path, posdir, [prev]) + distmap = Map.update(state.dist, posdir, dist, &min(&1, dist)) + %State{pqueue: pqueue, map: map, path: path, dist: distmap} + {_, odist} -> + distmap = Map.update!(state.dist, posdir, &min(&1, dist)) + cond do + dist > odist -> + %State{state | dist: distmap} + dist == odist -> + path = Map.update!(state.path, posdir, &[prev | &1]) + %State{state | path: path, dist: distmap} + true -> + pqueue = :gb_sets.delete({odist, posdir}, state.pqueue) + pqueue = :gb_sets.insert({dist, posdir}, pqueue) + map = :gb_trees.update(posdir, dist, state.map) + path = Map.put(state.path, posdir, [prev]) + %State{pqueue: pqueue, map: map, path: path, dist: distmap} + end + end + end + + def empty?(state) do + :gb_sets.is_empty(state.pqueue) + end + + def pop(state) do + {v, pqueue} = :gb_sets.take_smallest(state.pqueue) + {v, %State{state | pqueue: pqueue}} + end + + def optimal_dist(state, node) do + state.dist + |> Enum.filter(fn {{pos, _}, _} -> pos == node end) + |> Enum.map(fn {_, v} -> v end) + |> Enum.min() + end + + def optimal_path_nodes(state, node) do + nodes = state.dist + |> Enum.filter(fn {{pos, _}, _} -> pos == node end) + + mindist = nodes + |> Enum.map(fn {_, v} -> v end) + |> Enum.min() + + nodes = nodes + |> Enum.filter(fn {_, v} -> v == mindist end) + |> Enum.map(fn {posdir, _} -> posdir end) + + optimal_path_nodes(state, nodes, []) + end + + defp optimal_path_nodes(state, queue, acc) do + case queue do + [] -> + acc + |> Enum.map(fn {pos, _} -> pos end) + |> Enum.uniq() + [h | t] -> + acc = [h | acc] + queue = Map.fetch!(state.path, h) ++ t + optimal_path_nodes(state, queue, acc) + end + end + end + + defmodule Game do + @derive Inspect + defstruct [:grid, :start, :end] + + def from_data(data) do + grid = data + |> String.split("\n", trim: true) + |> Enum.with_index() + |> Enum.flat_map(fn {s, i} -> + String.codepoints(s) + |> Enum.with_index() + |> Enum.map(fn {c, j} -> {{i, j}, c} end) + end) + |> Map.new() + + start = grid + |> Enum.find_value(fn {pos, c} -> c == "S" and pos end) + |> then(& {&1, :right}) + + endpos = grid + |> Enum.find_value(fn {pos, c} -> c == "E" and pos end) + + %Game{grid: grid, start: start, end: endpos} + end + + def shortest(game) do + game + |> shortest(State.new(game)) + |> State.optimal_dist(game.end) + end + + def shortest_path_nodes(game) do + game + |> shortest(State.new(game)) + |> State.optimal_path_nodes(game.end) + end + + defp shortest(game, state) do + if State.empty?(state) do + state + else + {{dist, {pos, dir}}, state} = State.pop(state) + if pos == game.end do + shortest(game, state) + else + neighbors(game, {dist, {pos, dir}}) + for {ndist, nposdir} <- neighbors(game, {dist, {pos, dir}}), reduce: state do + state -> State.upsert(state, nposdir, ndist, {pos, dir}) + end + |> then(& shortest(game, &1)) + end + end + end + + defp neighbors(game, {dist, {{i, j}, dir}}) do + case dir do + :right -> + [{dist+1, {{i, j+1}, :right}}, + {dist+1001, {{i-1, j}, :up}}, + {dist+1001, {{i+1, j}, :down}}, + {dist+2001, {{i, j-1}, :left}}] + :left -> + [{dist+1, {{i, j-1}, :left}}, + {dist+1001, {{i-1, j}, :up}}, + {dist+1001, {{i+1, j}, :down}}, + {dist+2001, {{i, j+1}, :right}}] + :up -> + [{dist+1, {{i-1, j}, :up}}, + {dist+1001, {{i, j-1}, :left}}, + {dist+1001, {{i, j+1}, :right}}, + {dist+2001, {{i+1, j}, :down}}] + :down -> + [{dist+1, {{i+1, j}, :down}}, + {dist+1001, {{i, j-1}, :left}}, + {dist+1001, {{i, j+1}, :right}}, + {dist+2001, {{i-1, j}, :up}}] + end + |> Enum.filter(fn {_, {p, _}} -> + case Map.fetch(game.grid, p) do + :error -> false + {:ok, "#"} -> false + _ -> true + end + end) + end + end + + def part1(data) do + data + |> Game.from_data() + |> Game.shortest() + end + + def part2(data) do + data + |> Game.from_data() + |> Game.shortest_path_nodes() + |> Enum.count() + end +end + +data = IO.read(:stdio, :eof) + +{time1, ans1} = :timer.tc(fn -> Day16.part1(data) end) +IO.puts("Time : #{time1 / 1000000}") +IO.puts("Answer: #{ans1}") + +{time2, ans2} = :timer.tc(fn -> Day16.part2(data) end) +IO.puts("Time : #{time2 / 1000000}") +IO.puts("Answer: #{ans2}") diff --git a/src/day17.exs b/src/day17.exs new file mode 100644 index 0000000..9f9db29 --- /dev/null +++ b/src/day17.exs @@ -0,0 +1,127 @@ +defmodule Day17 do + defmodule Computer do + @derive Inspect + @enforce_keys [:a, :b, :c, :out, :instructions, :pointer] + defstruct @enforce_keys + + def from_data(data) do + [a, b, c | instructions] = Regex.scan(~r/\d+/, data) + |> Enum.map(&String.to_integer(hd(&1))) + + %Computer{a: a, b: b, c: c, out: [], instructions: instructions, pointer: 0} + end + + def run(comp) do + case instruction(comp) do + {:halt, comp} -> + comp + {:ok, comp} -> + run(comp) + end + end + + defp instruction(comp) do + case comp.instructions |> Enum.drop(comp.pointer) do + [] -> + {:halt, comp} + [0, r | _] -> + cr = combo(comp, r) + {:ok, %Computer{comp | a: Bitwise.bsr(comp.a, cr), pointer: comp.pointer+2}} + [1, r | _] -> + {:ok, %Computer{comp | b: Bitwise.bxor(comp.b, r), pointer: comp.pointer+2}} + [2, r | _] -> + cr = combo(comp, r) + {:ok, %Computer{comp | b: rem(cr, 8), pointer: comp.pointer+2}} + [3, r | _] -> + if comp.a == 0 do + {:ok, %Computer{comp | pointer: comp.pointer+2}} + else + {:ok, %Computer{comp | pointer: r}} + end + [4, _ | _] -> + {:ok, %Computer{comp | b: Bitwise.bxor(comp.b, comp.c), pointer: comp.pointer+2}} + [5, r | _] -> + cr = combo(comp, r) + {:ok, %Computer{comp | out: [rem(cr, 8) | comp.out], pointer: comp.pointer+2}} + [6, r | _] -> + cr = combo(comp, r) + {:ok, %Computer{comp | b: Bitwise.bsr(comp.a, cr), pointer: comp.pointer+2}} + [7, r | _] -> + cr = combo(comp, r) + {:ok, %Computer{comp | c: Bitwise.bsr(comp.a, cr), pointer: comp.pointer+2}} + end + end + + defp combo(comp, v) do + case v do + 0 -> 0 + 1 -> 1 + 2 -> 2 + 3 -> 3 + 4 -> comp.a + 5 -> comp.b + 6 -> comp.c + 7 -> raise "Invalid combo operand (7)" + end + end + end + + def part1(data) do + data + |> Computer.from_data() + |> Computer.run() + |> then(& &1.out) + |> Enum.reverse() + |> Enum.join(",") + end + + def part2(data) do + data + |> Computer.from_data() + |> then(& &1.instructions) + |> Enum.reverse() + |> solve2(0) + end + + defp solve2(rout, a) do + case rout do + [] -> + a + [h | t] -> + # I deduced the following by hand for my input, but maybe it could + # have been done programatically. An important takeway is that + # (for my input, and probably for all) `a` incerases by 3 bits for each + # iteration of the program. AS such, we can only need to check the + # possibilities over the last 3 bits (a..a+7) in each iteration ourselves + a = Bitwise.bsl(a, 3) + possible = a..a+7 + |> Enum.filter(fn a -> + Bitwise.band(a, 7) + |> Bitwise.bxor(5) + |> Bitwise.bxor(6) + |> Bitwise.bxor(Bitwise.bsr(a, Bitwise.band(a, 7) |> Bitwise.bxor(5))) + |> Bitwise.band(7) == h + end) + + case possible do + [] -> + :none + possible -> + possible + |> Enum.map(& solve2(t, &1)) + |> Enum.find(& &1 != :none) + || :none + end + end + end +end + +data = IO.read(:stdio, :eof) + +{time1, ans1} = :timer.tc(fn -> Day17.part1(data) end) +IO.puts("Time : #{time1 / 1000000}") +IO.puts("Answer: #{ans1}") + +{time2, ans2} = :timer.tc(fn -> Day17.part2(data) end) +IO.puts("Time : #{time2 / 1000000}") +IO.puts("Answer: #{ans2}") diff --git a/src/day18.exs b/src/day18.exs new file mode 100644 index 0000000..e97da36 --- /dev/null +++ b/src/day18.exs @@ -0,0 +1,94 @@ +defmodule Day18 do + defmodule Game do + @derive Inspect + @enforce_keys [:obj, :source, :target, :w, :h] + defstruct @enforce_keys + + def from_data(data, source, target) do + obj = data + |> String.split("\n", trim: true) + |> Enum.map(&integer_list/1) + |> Enum.map(&List.to_tuple/1) + + {w, h} = target + + %Game{obj: obj, source: source, target: target, w: w, h: h} + end + + defp integer_list(s), do: String.split(s, ",") |> Enum.map(&String.to_integer/1) + + def shortest(game, count_obj \\ nil) do + obj = + if count_obj == nil do + game.obj + else + Enum.take(game.obj, count_obj) + end + + %Game{game | obj: MapSet.new(obj)} + |> shortest(0, [game.source], MapSet.new([game.source])) + end + + defp shortest(game, i, q, v) do + case q do + [] -> + :none + q -> + if Enum.member?(q, game.target) do + i + else + for n <- q, reduce: {[], v} do + {q, v} -> + neighbors(game, n) + |> Enum.reject(&MapSet.member?(v, &1)) + |> Enum.reduce({q, v}, fn n, {q, v} -> {[n | q], MapSet.put(v, n)} end) + end + |> then(fn {q, v} -> shortest(game, i+1, q, v) end) + end + end + end + + defp neighbors(game, {x, y}) do + [{x+1, y}, {x-1, y}, {x, y+1}, {x, y-1}] + |> Enum.filter(fn {x, y} -> x >= 0 and x <= game.w and y >= 0 and y <= game.h end) + |> Enum.reject(&MapSet.member?(game.obj, &1)) + end + end + + def part1(data) do + data + |> Game.from_data({0, 0}, {70, 70}) + |> Game.shortest(1024) + end + + def part2(data) do + game = data + |> Game.from_data({0, 0}, {70, 70}) + + part2(game, 1, Enum.count(game.obj)) + end + + defp part2(game, l, r) do + if l == r do + Enum.at(game.obj, l-1) + |> then(fn {x, y} -> "#{x},#{y}" end) + else + m = div(l + r, 2) + if Game.shortest(game, m) != :none do + part2(game, m+1, r) + else + part2(game, l, m) + end + end + end +end + +data = IO.read(:stdio, :eof) + +{time1, ans1} = :timer.tc(fn -> Day18.part1(data) end) +IO.puts("Time : #{time1 / 1000000}") +IO.puts("Answer: #{ans1}") + +{time2, ans2} = :timer.tc(fn -> Day18.part2(data) end) +IO.puts("Time : #{time2 / 1000000}") +IO.puts("Answer: #{ans2}") |