Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
cbe6ad7a00 | |||
f59363eaf6 |
10 changed files with 90 additions and 47 deletions
|
@ -6,8 +6,7 @@ end
|
|||
Benchee.run(
|
||||
%{
|
||||
"day8_part2" => {fn input -> Mix.Tasks.Day8.solve(input, :part2) end, before_scenario: fn _ -> puzzle_input_for_day.(2023, 8) end},
|
||||
"day9_part1" => {fn input -> Mix.Tasks.Day9.solve(input, :part1) end, before_scenario: fn _ -> puzzle_input_for_day.(2023, 9) end},
|
||||
"day9_part2" => {fn input -> Mix.Tasks.Day9.solve(input, :part2) end, before_scenario: fn _ -> puzzle_input_for_day.(2023, 9) end},
|
||||
# "day9_part1" => {fn input -> Mix.Tasks.Day9.solve(input, :part1) end, before_scenario: fn _ -> puzzle_input_for_day.(2023, 9) end},
|
||||
# "day9_part2" => {fn input -> Mix.Tasks.Day9.solve(input, :part2) end, before_scenario: fn _ -> puzzle_input_for_day.(2023, 9) end},
|
||||
}
|
||||
)
|
||||
|
||||
|
|
0
hello.exs
Normal file
0
hello.exs
Normal file
|
@ -1,4 +1,5 @@
|
|||
defmodule Mix.Tasks.Day8 do
|
||||
alias AoC.Util.Math
|
||||
use Mix.Task
|
||||
defp instruction_to_index("L"), do: 0
|
||||
defp instruction_to_index("R"), do: 1
|
||||
|
@ -22,49 +23,39 @@ defmodule Mix.Tasks.Day8 do
|
|||
end
|
||||
|
||||
def loop_receive(max_len, acc, count) do
|
||||
receive do
|
||||
n -> loop_receive(max_len, AoC.Util.Math.lcm(acc, n), count + 1)
|
||||
end
|
||||
receive do
|
||||
n -> loop_receive(max_len, AoC.Util.Math.lcm(acc, n), count + 1)
|
||||
end
|
||||
end
|
||||
|
||||
def to_rooms(
|
||||
<<from::binary-size(3)>> <>
|
||||
" = (" <> <<a::binary-size(3)>> <> ", " <> <<b::binary-size(3)>> <> ")"
|
||||
) do
|
||||
{from, {a, b}}
|
||||
end
|
||||
|
||||
def solve(input, _) do
|
||||
[instructions, nodes] = input |> String.split("\n\n")
|
||||
|
||||
nodes =
|
||||
for {key, steps} <-
|
||||
nodes
|
||||
|> String.split("\n")
|
||||
|> Stream.reject(&(&1 == ""))
|
||||
|> Stream.map(&String.split(String.trim(&1), " = "))
|
||||
|> Stream.map(fn [
|
||||
from,
|
||||
"(" <>
|
||||
<<a::binary-size(3)>> <> ", " <> <<b::binary-size(3)>> <> ")"
|
||||
] ->
|
||||
{from, {a, b}}
|
||||
end),
|
||||
into: %{} do
|
||||
{key, steps}
|
||||
nodes
|
||||
|> String.split("\n")
|
||||
|> Stream.reject(&AoC.Util.String.empty?/1)
|
||||
|> Stream.map(&String.trim/1)
|
||||
|> Stream.map(&to_rooms/1)
|
||||
|> Map.new()
|
||||
|
||||
nodes
|
||||
|> Map.keys()
|
||||
|> Stream.filter(&(&1 |> String.ends_with?("A")))
|
||||
|> Task.async_stream(fn entry -> walk(instructions, nodes, 0, entry) end)
|
||||
|> Enum.reduce(
|
||||
1,
|
||||
fn
|
||||
{:ok, e}, acc -> Math.lcm(acc, e)
|
||||
end
|
||||
|
||||
parent = self()
|
||||
|
||||
pids =
|
||||
for entry <- nodes |> Map.keys() |> Stream.filter(&(&1 |> String.ends_with?("A"))) do
|
||||
spawn(fn ->
|
||||
send(
|
||||
parent,
|
||||
walk(
|
||||
instructions,
|
||||
nodes,
|
||||
0,
|
||||
entry
|
||||
)
|
||||
)
|
||||
end)
|
||||
end
|
||||
|
||||
loop_receive(pids |> Enum.count(), 1, 0)
|
||||
)
|
||||
end
|
||||
|
||||
def run(_) do
|
||||
|
|
13
lib/Y2023/day8_genserver.ex
Normal file
13
lib/Y2023/day8_genserver.ex
Normal file
|
@ -0,0 +1,13 @@
|
|||
defmodule Advent.Y2023.Day8.GenServer do
|
||||
use GenServer
|
||||
|
||||
|
||||
def init(), do: init([])
|
||||
|
||||
def init(init_arg), do: {:ok, init_arg}
|
||||
|
||||
def handle_cast(request, state) do
|
||||
|
||||
end
|
||||
end
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
defmodule Mix.Tasks.Day9 do
|
||||
use Mix.Task
|
||||
|
||||
defmodule AoC.Y2023.Day9 do
|
||||
def run(_) do
|
||||
{_, content} = File.read("./input/2023_9.txt")
|
||||
solve(content, :part2) |> inspect() |> IO.puts()
|
||||
|
|
3
lib/run.exs
Normal file
3
lib/run.exs
Normal file
|
@ -0,0 +1,3 @@
|
|||
|
||||
IO.puts("Hello")
|
||||
AOCRunner.run(Day1, :part2)
|
|
@ -33,4 +33,26 @@ defmodule AoC.Util.Math do
|
|||
def lcm(a, b) do
|
||||
div(a * b, gcd(a, b))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the facorial of n.
|
||||
|
||||
iex> AoC.Util.Math.factorial(0)
|
||||
1
|
||||
iex> AoC.Util.Math.factorial(1)
|
||||
1
|
||||
iex> AoC.Util.Math.factorial(2)
|
||||
2
|
||||
iex> AoC.Util.Math.factorial(5)
|
||||
120
|
||||
iex> AoC.Util.Math.factorial(-1)
|
||||
nil
|
||||
"""
|
||||
def factorial(0), do: 1
|
||||
def factorial(1), do: 1
|
||||
def factorial(n) when n < 0, do: nil
|
||||
def factorial(n), do: factorial(n-1, n)
|
||||
|
||||
defp factorial(1, k), do: k
|
||||
defp factorial(n, k), do: factorial(n-1, k*n)
|
||||
end
|
||||
|
|
|
@ -16,9 +16,22 @@ defmodule AoC.Util.String do
|
|||
def transpose(str) do
|
||||
AoC.Util.Enum.transpose(
|
||||
str
|
||||
|> String.split("\n")
|
||||
|> Enum.map(&String.graphemes(&1))
|
||||
|> String.split("\n")
|
||||
|> Enum.map(&String.graphemes(&1))
|
||||
)
|
||||
|> Enum.map(&Enum.join(&1, ""))
|
||||
|> Enum.join("\n")
|
||||
end
|
||||
|
||||
)|> Enum.map(&Enum.join(&1,"")) |> Enum.join("\n")
|
||||
@doc """
|
||||
Returns true if the String is empty, else.
|
||||
|
||||
iex> AoC.Util.String.empty?("")
|
||||
true
|
||||
iex> AoC.Util.String.empty?("foo")
|
||||
false
|
||||
"""
|
||||
def empty?(s) do
|
||||
s == ""
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,10 +6,10 @@ defmodule Day9Tests do
|
|||
10 13 16 21 30 45"
|
||||
|
||||
assert "gets part 2 example input correct" do
|
||||
assert Mix.Tasks.Day9.solve(@puzzle_input, :part2) === 2
|
||||
assert AoC.Y2023.Day9.solve(@puzzle_input, :part2) === 2
|
||||
end
|
||||
|
||||
assert "gets part 1 example input correct" do
|
||||
assert Mix.Tasks.Day9.solve(@puzzle_input, :part1) === 114
|
||||
assert AoC.Y2023.Day9.solve(@puzzle_input, :part1) === 114
|
||||
end
|
||||
end
|
||||
|
|
4
test/util/math_test.exs
Normal file
4
test/util/math_test.exs
Normal file
|
@ -0,0 +1,4 @@
|
|||
defmodule AoC.Util.MathTest do
|
||||
use ExUnit.Case
|
||||
doctest AoC.Util.Math
|
||||
end
|
Loading…
Add table
Reference in a new issue