Compare commits

..

3 commits

2 changed files with 100 additions and 21 deletions

90
2023/day3.exs Executable file
View file

@ -0,0 +1,90 @@
#!/usr/bin/env elixir
defmodule Day3 do
def part1(_input) do
# for each number, check around its edges to see if there's a symbol
fn {{x, y}, number, length}, acc ->
top = for x <- (x - 1)..(x + length), do: {x, y - 1}
bottom = for x <- (x - 1)..(x + length), do: {x, y + 1}
box = [{x - 1, y}, {x + length, y}] ++ top ++ bottom
if part_number?(box), do: [number | acc], else: acc
end
|> :ets.foldl(_acc = [], :numbers)
|> Enum.sum()
end
def part_number?([]), do: false
def part_number?([coord | rest]) do
case :ets.lookup(:symbols, coord) do
[_symbol] -> true
[] -> part_number?(rest)
end
end
def part2(_input) do
:ok
end
def input do
with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do
:ets.new(:numbers, [:named_table])
:ets.new(:symbols, [:named_table])
parse_schematic(input, _x = 0, _y = 0)
else
_ -> :error
end
end
def parse_schematic("", _x, _y), do: :ok
def parse_schematic("." <> rest, x, y), do: parse_schematic(rest, x + 1, y)
def parse_schematic("\n" <> rest, _x, y), do: parse_schematic(rest, 0, y + 1)
def parse_schematic(<<digit::utf8>> <> rest, x, y) when digit in ?0..?9 do
{number, length, rest} = take_digits(<<digit::utf8>> <> rest, "", 0)
:ets.insert(:numbers, {{x, y}, number, length})
parse_schematic(rest, x + length, y)
end
def parse_schematic(<<symbol::utf8>> <> rest, x, y) do
:ets.insert(:symbols, {{x, y}, <<symbol::utf8>>})
parse_schematic(rest, x + 1, y)
end
def take_digits(<<digit::utf8>> <> rest, digits, count) when digit in ?0..?9,
do: take_digits(rest, digits <> <<digit::utf8>>, count + 1)
def take_digits(rest, digits, count), do: {String.to_integer(digits), count, rest}
#######################
# HERE BE BOILERPLATE #
#######################
def run do
case input() do
:error -> print_usage()
input -> run_parts_with_timer(input)
end
end
defp run_parts_with_timer(input) do
run_with_timer(1, fn -> part1(input) end)
run_with_timer(2, fn -> part2(input) end)
end
defp run_with_timer(part, fun) do
{time, result} = :timer.tc(fun)
IO.puts("Part #{part} (completed in #{format_time(time)}):\n")
IO.puts("#{inspect(result)}\n")
end
defp format_time(μsec) when μsec < 1_000, do: "#{μsec}μs"
defp format_time(μsec) when μsec < 1_000_000, do: "#{μsec / 1000}ms"
defp format_time(μsec), do: "#{μsec / 1_000_000}s"
defp print_usage do
IO.puts("Usage: elixir day3.exs input_filename")
end
end
# Day3.run()

View file

@ -2,35 +2,28 @@
defmodule Day4 do defmodule Day4 do
def part1(input) do def part1(input) do
input input
|> Enum.map(fn {_id, num_winners} -> |> Enum.map(fn 0 -> 0; num_winners -> 2 ** (num_winners - 1) end)
case num_winners do
0 -> 0
num_winners -> 2 ** (num_winners - 1)
end
end)
|> Enum.sum() |> Enum.sum()
end end
def part2(input) do def part2(input) do
input input
|> Enum.sort_by(fn {id, _num_winners} -> id end) |> Enum.map(fn num_winners -> {_num_copies = 1, num_winners} end)
|> Enum.map(fn {_id, num_winners} -> {_num_copies = 1, num_winners} end)
|> copy() |> copy()
|> Enum.map(fn {num_copies, _num_winners} -> num_copies end)
|> Enum.sum() |> Enum.sum()
end end
def copy([]), do: [] def copy([]), do: []
def copy([{above_num_copies, num_winners} = current | rest]) do def copy([{num_copies, num_winners} | rest]) do
{to_copy, left_alone} = Enum.split(rest, num_winners) {to_copy, left_alone} = Enum.split(rest, num_winners)
copied = copied =
Enum.map(to_copy, fn {below_num_copies, num_winners} -> Enum.map(to_copy, fn {child_num_copies, num_winners} ->
{above_num_copies + below_num_copies, num_winners} {num_copies + child_num_copies, num_winners}
end) end)
[current | copy(copied ++ left_alone)] [num_copies | copy(copied ++ left_alone)]
end end
def input do def input do
@ -39,16 +32,12 @@ defmodule Day4 do
input input
|> String.split("\n", trim: true) |> String.split("\n", trim: true)
|> Enum.map(fn line -> |> Enum.map(fn line ->
[card_id, winning, have] = String.split(line, [": ", " | "]) [_card_id, winning, have] = String.split(line, [": ", " | "])
["Card", id] = String.split(card_id, " ", trim: true)
[winning, have] = winning = winning |> String.split(" ", trim: true) |> MapSet.new()
for cards <- [winning, have], have = have |> String.split(" ", trim: true) |> MapSet.new()
do: cards |> String.split(" ", trim: true) |> MapSet.new(&String.to_integer/1)
num_winners = MapSet.intersection(winning, have) |> MapSet.size() MapSet.intersection(winning, have) |> MapSet.size()
{String.to_integer(id), num_winners}
end) end)
else else
_ -> :error _ -> :error