create 2023 dir

This commit is contained in:
Caleb Webber 2023-12-06 13:52:42 -05:00
parent 07125c75ce
commit fda0c5af94
9 changed files with 90 additions and 8 deletions

View file

@ -50,8 +50,8 @@ defmodule Day1_2024 do
0,
fn i, _ ->
cur_char = l |> String.at(len - i)
IO.puts("examining ")
IO.puts(cur_char)
# IO.puts("examining ")
# IO.puts(cur_char)
int_at_pos = cur_char |> Integer.parse()
case int_at_pos do
@ -74,7 +74,7 @@ defmodule Day1_2024 do
end
def search_for_written_number(line) do
IO.puts("searching " <> line)
# IO.puts("searching " <> line)
numbers = [
"one",
@ -111,7 +111,7 @@ defmodule Day1_2024 do
end)
|> Enum.filter(&(&1 |> elem(1) |> Enum.count() > 1))
IO.puts(inspect(splits))
# IO.puts(inspect(splits))
number_word =
splits

24
lib/2023/2023_day2_2.exs Normal file
View file

@ -0,0 +1,24 @@
max = %{"red" => 12, "green" => 13, "blue" => 14}
is_valid_pull = fn {count, color} ->
count <= max[color]
end
for {id, pulls} <-
(for line <- IO.stream(),
String.trim(line) != "",
"Game " <> game = line,
{id, ": " <> rest} = Integer.parse(game) do
{id,
for pull <- String.split(rest, "; ") |> Enum.map(&String.trim/1),
cube <- String.split(pull, ", ") |> Enum.map(&String.trim/1),
{count, color} = Integer.parse(cube),
color = String.trim(color) do
{count, color}
end}
end),
Enum.all?(pulls, &is_valid_pull.(&1)) do
id
end
|> Enum.sum()
|> IO.puts()

54
lib/2023/2023_day3.ex Normal file
View file

@ -0,0 +1,54 @@
import RegexUtil, only: [scan_index_with_binary: 2]
defmodule Mix.Tasks.Day3 do
use Mix.Task
@r_digits ~r/\d+/
def run(_) do
part2(IO.stream()) |> IO.puts()
end
def contains_symbol(s) do
Regex.match?(~r/[^\d.]/, s)
end
def part1(s) do
for [pre, curr, post] <-
s
|> then(fn x -> Enum.concat([""], x) end)
|> Enum.chunk(3, 1)
|> Enum.map(fn u -> Enum.map(u, &String.trim/1) end),
[{index, length}] <- Regex.scan(@r_digits, curr, return: :index),
contains_symbol(String.slice(curr, max(index - 1, 0), length + 2)) ||
contains_symbol(String.slice(pre, max(index - 1, 0), length + 2)) ||
contains_symbol(String.slice(post, max(index - 1, 0), length + 2)),
{number, _} = String.slice(curr, index, length) |> Integer.parse() do
number
end
|> Enum.sum()
end
def part2(s) do
for [pre, curr, post] <-
s
|> Enum.chunk(3, 1)
|> Enum.map(fn u -> Enum.map(u, &String.trim/1) end),
curr |> String.contains?("*"),
[{gear_idx, _}] <- Regex.scan(~r/\*/, curr, return: :index),
matches =
scan_index_with_binary(@r_digits, pre)
|> Enum.concat(scan_index_with_binary(@r_digits, post))
|> Enum.concat(scan_index_with_binary(@r_digits, curr)),
valid_matches =
matches
|> Enum.filter(fn {m_idx, m_len, _} ->
gear_idx in max(0, m_idx - 1)..(m_idx + m_len)
end),
valid_matches |> Enum.count() == 2,
[{_, _, a}, {_, _, b}] = valid_matches do
a * b
end
|> Enum.sum()
end
end

View file

@ -27,6 +27,8 @@ part1 = fn s ->
|> Enum.sum()
end
r_digits = ~r/\d+/
part2 = fn s ->
for [pre, curr, post] <-
s
@ -36,7 +38,9 @@ part2 = fn s ->
curr |> String.contains?("*"),
[{gear_idx, _}] <- Regex.scan(~r/\*/, curr, return: :index),
matches =
grab_numbers.(pre) |> Enum.concat(grab_numbers.(post)) |> Enum.concat(grab_numbers.(curr)),
grab_numbers.(pre)
|> Enum.concat(grab_numbers.(post))
|> Enum.concat(grab_numbers.(curr)),
valid_matches =
matches
|> Enum.filter(fn {m_idx, m_len, _} ->
@ -49,4 +53,4 @@ part2 = fn s ->
|> Enum.sum()
end
part2.(IO.stream()) |> IO.puts()
part1.(IO.stream()) |> IO.puts()

View file

@ -19,8 +19,8 @@ defmodule Card do
def parse(input) do
for card <- input |> String.split("\n"),
card != "",
[winning_numbers, our_numbers] = parse_card(card) do
card != "",
[winning_numbers, our_numbers] = parse_card(card) do
[winning_numbers, our_numbers]
end
end