diff --git a/lib/day6.ex b/lib/day6.ex new file mode 100644 index 0000000..b1abc8a --- /dev/null +++ b/lib/day6.ex @@ -0,0 +1,37 @@ +defmodule Aoc2023.Day6 do + use Aoc2023 + + def parse(input) do + input + |> lines + |> Enum.map(pipe( + String.split(":") + |> Enum.at(1) + )) + end + + def part1(input) do + input + |> Enum.map(pipe( + String.split() + |> Enum.map(&String.to_integer/1) + )) + |> Enum.zip() + |> Enum.map(fn {time, dist} -> + 1..time + |> Enum.map(&(&1 * (time - &1))) + |> Enum.filter(&(&1 > dist)) + |> length + end) + |> Enum.product() + end + + def part2(input) do + [time, dist] = input + |> Enum.map(pipe(String.replace(~r(\s+), "") |> String.to_integer())) + 1..(time) + |> Stream.map(&(&1 * (time - &1))) + |> Stream.filter(&(&1 > dist)) + |> Enum.count + end +end diff --git a/lib/mix_tasks.ex b/lib/mix_tasks.ex index fc1b0ca..655ba66 100644 --- a/lib/mix_tasks.ex +++ b/lib/mix_tasks.ex @@ -7,6 +7,7 @@ defmodule Mix.Tasks.Aoc do defp module(3), do: Aoc2023.Day3 defp module(4), do: Aoc2023.Day4 defp module(5), do: Aoc2023.Day5 + defp module(6), do: Aoc2023.Day6 # [MODULE INSERTION POINT] defp base_dir(), do: System.get_env("AOC_BASE") @@ -201,4 +202,4 @@ defmodule Mix.Tasks.Aoc do {:noreply, state} end end -end +end \ No newline at end of file diff --git a/tests/day6/1 b/tests/day6/1 new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/tests/day6/1 @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 diff --git a/tests/day6/1.1 b/tests/day6/1.1 new file mode 100644 index 0000000..ea80947 --- /dev/null +++ b/tests/day6/1.1 @@ -0,0 +1 @@ +288