day 12
This commit is contained in:
parent
a1cefee9ea
commit
d0b7ca24ef
3 changed files with 118 additions and 0 deletions
2
lib/2023/day12.ex
Normal file
2
lib/2023/day12.ex
Normal file
|
@ -0,0 +1,2 @@
|
|||
{_, content} = File.read("./input/2023_12.txt")
|
||||
SprintParseWorker.solve_in_parallel(content) |> IO.puts()
|
30
lib/2023/day12_2.ex
Normal file
30
lib/2023/day12_2.ex
Normal file
|
@ -0,0 +1,30 @@
|
|||
defmodule SprintParseWorker do
|
||||
def loop_receive(n, acc) do
|
||||
case n do
|
||||
0 ->
|
||||
acc
|
||||
|
||||
_ ->
|
||||
receive do
|
||||
m ->
|
||||
loop_receive(n - 1, acc + m)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def solve_in_parallel(i) do
|
||||
parent = self()
|
||||
|
||||
u = SpringParse.parse(i)
|
||||
|
||||
pid_count =
|
||||
for [pattern, expectation] <- u do
|
||||
spawn_link(fn ->
|
||||
send(parent, SpringParse.count_solutions(pattern, expectation))
|
||||
end)
|
||||
end
|
||||
|> Enum.count()
|
||||
|
||||
loop_receive(pid_count, 0)
|
||||
end
|
||||
end
|
86
lib/2023/day12_3.ex
Normal file
86
lib/2023/day12_3.ex
Normal file
|
@ -0,0 +1,86 @@
|
|||
defmodule SpringParse do
|
||||
defp enum_repeat(enum, n) do
|
||||
for _ <- 1..n do
|
||||
enum
|
||||
end
|
||||
|> Enum.flat_map(& &1)
|
||||
end
|
||||
|
||||
def into_spring_count(str \\ ".........#####") do
|
||||
str
|
||||
|> String.split(".")
|
||||
|> Stream.reject(&(&1 == ""))
|
||||
|> Enum.map(&String.length(&1))
|
||||
end
|
||||
|
||||
def generate(s \\ "????....?#####") do
|
||||
chars = s |> String.graphemes()
|
||||
|
||||
q_map =
|
||||
for {{i, _}, j} <-
|
||||
(for {q, i} <- chars |> Stream.with_index(), q == "?" do
|
||||
{i, q}
|
||||
end)
|
||||
|> Stream.with_index(),
|
||||
into: %{} do
|
||||
{i, j}
|
||||
end
|
||||
|
||||
possibilities = chars |> Stream.filter(&(&1 == "?")) |> Enum.count()
|
||||
|
||||
for p <- 0..(2 ** possibilities - 1) do
|
||||
for {l, i} <- chars |> Stream.with_index() do
|
||||
if l == "?" do
|
||||
case Bitwise.band(2 ** Map.get(q_map, i), p) do
|
||||
0 -> "."
|
||||
n when n > 0 -> "#"
|
||||
end
|
||||
else
|
||||
l
|
||||
end
|
||||
end
|
||||
end
|
||||
|> Stream.map(&Enum.join(&1, ""))
|
||||
end
|
||||
|
||||
def parse(input) do
|
||||
input
|
||||
|> String.split("\n")
|
||||
|> Stream.reject(&(&1 === ""))
|
||||
|> Stream.map(fn line ->
|
||||
[pattern, exp] = line |> String.split(" ")
|
||||
exp = String.split(exp, ",") |> Enum.map(&String.to_integer/1)
|
||||
[pattern, exp]
|
||||
end)
|
||||
end
|
||||
|
||||
def parse_2(input) do
|
||||
input
|
||||
|> String.split("\n")
|
||||
|> Stream.reject(&(&1 == ""))
|
||||
|> Stream.map(fn line ->
|
||||
[pattern, exp] = line |> String.split(" ")
|
||||
pattern = enum_repeat([pattern], 5) |> Enum.join("?")
|
||||
exp = String.split(exp, ",") |> Stream.map(&String.to_integer/1) |> enum_repeat(5)
|
||||
|
||||
[pattern, exp]
|
||||
end)
|
||||
end
|
||||
|
||||
def count_solutions(pattern, expectation) do
|
||||
matches =
|
||||
SpringParse.generate(pattern)
|
||||
|> Stream.map(&{&1, SpringParse.into_spring_count(&1)})
|
||||
|> Stream.filter(&(&1 |> elem(1) == expectation))
|
||||
|
||||
Enum.count(matches)
|
||||
end
|
||||
|
||||
def solve(input) do
|
||||
for [pattern, expectation] <- parse(input),
|
||||
reduce: 0 do
|
||||
acc ->
|
||||
acc + count_solutions(pattern, expectation)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue