Skip to content

Commit

Permalink
2019 day 21 part 2 - an attempted solve
Browse files Browse the repository at this point in the history
But it's not going to ever actually finish. The refactored version is much better
for part 1 though!
  • Loading branch information
sevenseacat committed Dec 11, 2024
1 parent 8bd4d86 commit 1626682
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
My Elixir solutions for [Advent of Code](https://adventofcode.com/) (all years).

<!-- stars start -->
<p><img src="https://img.shields.io/static/v1?label=Total&message=442%20stars&style=for-the-badge&color=green" alt="442 stars" /></p>
<p><img src="https://img.shields.io/static/v1?label=Total&message=441%20stars&style=for-the-badge&color=green" alt="441 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=22%20stars&style=for-the-badge&color=orange" alt="22 stars" /></a><br />
<a href="./lib/y2023/"><img src="https://img.shields.io/static/v1?label=2023&message=44%20stars&style=for-the-badge&color=green" alt="44 stars" /></a><br />
<a href="./lib/y2022/"><img src="https://img.shields.io/static/v1?label=2022&message=%E2%AD%90%EF%B8%8F%2050%20stars%20%E2%AD%90%EF%B8%8F&style=for-the-badge&color=brightgreen" alt="50 stars" /></a><br />
<a href="./lib/y2021/"><img src="https://img.shields.io/static/v1?label=2021&message=46%20stars&style=for-the-badge&color=green" alt="46 stars" /></a><br />
<a href="./lib/y2020/"><img src="https://img.shields.io/static/v1?label=2020&message=39%20stars&style=for-the-badge&color=yellow" alt="39 stars" /></a><br />
<a href="./lib/y2019/"><img src="https://img.shields.io/static/v1?label=2019&message=41%20stars&style=for-the-badge&color=green" alt="41 stars" /></a><br />
<a href="./lib/y2019/"><img src="https://img.shields.io/static/v1?label=2019&message=40%20stars&style=for-the-badge&color=green" alt="40 stars" /></a><br />
<a href="./lib/y2018/"><img src="https://img.shields.io/static/v1?label=2018&message=%E2%AD%90%EF%B8%8F%2050%20stars%20%E2%AD%90%EF%B8%8F&style=for-the-badge&color=brightgreen" alt="50 stars" /></a><br />
<a href="./lib/y2017/"><img src="https://img.shields.io/static/v1?label=2017&message=%E2%AD%90%EF%B8%8F%2050%20stars%20%E2%AD%90%EF%B8%8F&style=for-the-badge&color=brightgreen" alt="50 stars" /></a><br />
<a href="./lib/y2016/"><img src="https://img.shields.io/static/v1?label=2016&message=%E2%AD%90%EF%B8%8F%2050%20stars%20%E2%AD%90%EF%B8%8F&style=for-the-badge&color=brightgreen" alt="50 stars" /></a><br />
Expand Down
4 changes: 2 additions & 2 deletions lib/y2019/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

My Elixir solutions for [Advent of Code 2019](https://adventofcode.com/2019).

<!-- stars 2019 start --><img src="https://img.shields.io/static/v1?label=2019&message=41%20stars&style=for-the-badge&color=green" alt="41 stars" /><!-- stars 2019 end -->
<!-- stars 2019 start --><img src="https://img.shields.io/static/v1?label=2019&message=40%20stars&style=for-the-badge&color=green" alt="40 stars" /><!-- stars 2019 end -->

## Benchmarks

Expand Down Expand Up @@ -53,5 +53,5 @@ day 19, part 1 14.37 69.61 ms ±1.76% 69.55 ms 72.
day 19, part 2 4.21 237.63 ms ±0.20% 237.70 ms 238.42 ms
day 20, part 1 46.46 21.52 ms ±5.19% 21.37 ms 24.56 ms
day 20, part 2 0.0133 1.25 min ±0.00% 1.25 min 1.25 min 🤭
day 21, part 1 0.74 1.35 s ±32.82% 1.36 s 1.75 s
day 21, part 1 1.33 749.68 ms ±29.82% 666.99 ms 1225.01 ms
```
90 changes: 65 additions & 25 deletions lib/y2019/day21.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,91 @@ defmodule Y2019.Day21 do

def part1(%Intcode{} = program) do
program
|> Intcode.add_inputs(
~c"#{find_formula(possible_var_combinations([:a, :b, :c, :d]))}\nWALK\n"
)
|> Intcode.add_inputs(~c"#{find_part_1_formula([:a, :b, :c, :d])}\nWALK\n")
|> Intcode.run()
|> Intcode.outputs()
|> List.last()
end

# @doc """
# iex> Day21.part2("update or delete me")
# "update or delete me"
# """
# def part2(input) do
# input
# This doesn't actually ever finish
# def part2(program) do
# result =
# program
# |> Intcode.add_inputs(
# ~c"#{find_part_2_formula([:a, :b, :c, :d, :e, :f, :g, :h, :i])}\nRUN\n"
# )
# |> Intcode.run()
# |> Intcode.outputs()

# IO.puts(result)

# List.last(result)
# end

def find_formula(var_combinations) do
def find_part_1_formula(vars) do
[
{[],
[
# If we're in front of a gap we have to jump
{%{a: false, b: false, c: false, d: true, t: false, j: false}, true},
{%{a: false, b: false, c: true, d: true, t: false, j: false}, true},
{%{a: false, b: true, c: false, d: true, t: false, j: false}, true},
{%{a: false, b: true, c: true, d: true, t: false, j: false}, true},
{".###", true},
{"..##", true},
{".#.#", true},
{"...#", true},

# Never jump when the fourth square is a hole because you'll fall in it
{%{a: true, b: true, c: true, d: false, t: false, j: false}, false},
{%{a: true, b: false, c: false, d: false, t: false, j: false}, false},
{%{a: true, b: true, c: false, d: false, t: false, j: false}, false},
{%{a: true, b: false, c: true, d: false, t: false, j: false}, false},
{"###.", false},
{"#...", false},
{"##..", false},
{"#.#.", false},

# Nothing to worry about, keep walking forward
{%{a: true, b: true, c: true, d: true, t: false, j: false}, false},
{"####", false},

# Jump just in case you can't after the gap
{%{a: true, b: false, c: false, d: true, t: false, j: false}, true},
{%{a: true, b: true, c: false, d: true, t: false, j: false}, true}
{"#..#", true},
{"##.#", true}

# These ones I don't know about yet because either move could work
# [true, false, true, 1] => ?,
]}
]
|> expand_combinations(vars)}
]
|> jump(var_combinations)
|> jump(possible_var_combinations(vars))
end

# def find_part_2_formula(vars) do
# [
# {
# [],
# [
# {".########", true},
# {"..#######", true},
# {"...######", true},
# {"#########", false},
# {"##.######", true},
# {"##...####", false},
# {".#.##.#.#", true},
# {"#.#.#####", false},
# {"#.#.##.#.", false},
# {"##.#.##.#", false},
# {"#...#####", false},
# {"##...####", false}
# ]
# |> expand_combinations(vars)
# }
# ]
# |> jump(possible_var_combinations(vars))
# end

defp expand_combinations(list, vars) do
Enum.map(list, fn {road, result} ->
bools =
road
|> String.graphemes()
|> Enum.map(&(&1 == "#"))

{Enum.zip(vars, bools) |> Map.new() |> Map.merge(%{t: false, j: false}), result}
end)
end

def jump(possible_paths, var_combinations) do
Expand All @@ -64,7 +105,6 @@ defmodule Y2019.Day21 do
op.([Map.fetch!(registers, one), Map.fetch!(registers, two)])
), result}
end)
|> Enum.uniq_by(fn data -> data end)
}
end
# If we're back where we started from, ditch this option
Expand Down Expand Up @@ -99,7 +139,7 @@ defmodule Y2019.Day21 do
end

def possible_var_combinations(range) do
[[:t, :j], [:j, :t] | Enum.flat_map(range, fn from -> [[from, :t], [from, :j]] end)]
[[:t, :j] | Enum.flat_map(range, fn from -> [[from, :t], [from, :j]] end)]
end

def parse_input(input) do
Expand Down
2 changes: 1 addition & 1 deletion test/y2019/day21_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ defmodule Y2019.Day21Test do
doctest Day21

test "verification, part 1", do: assert(Day21.part1_verify() == 19_349_939)
# test "verification, part 2", do: assert(Day21.part2_verify() == "update or delete me")
# test "verification, part 2", do: assert(Day21.part2_verify() == "update or delete me")
end

0 comments on commit 1626682

Please sign in to comment.