Day 6
This commit is contained in:
37
lib/day6.ex
Normal file
37
lib/day6.ex
Normal file
@@ -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
|
||||||
@@ -7,6 +7,7 @@ defmodule Mix.Tasks.Aoc do
|
|||||||
defp module(3), do: Aoc2023.Day3
|
defp module(3), do: Aoc2023.Day3
|
||||||
defp module(4), do: Aoc2023.Day4
|
defp module(4), do: Aoc2023.Day4
|
||||||
defp module(5), do: Aoc2023.Day5
|
defp module(5), do: Aoc2023.Day5
|
||||||
|
defp module(6), do: Aoc2023.Day6
|
||||||
# [MODULE INSERTION POINT]
|
# [MODULE INSERTION POINT]
|
||||||
|
|
||||||
defp base_dir(), do: System.get_env("AOC_BASE")
|
defp base_dir(), do: System.get_env("AOC_BASE")
|
||||||
@@ -201,4 +202,4 @@ defmodule Mix.Tasks.Aoc do
|
|||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
2
tests/day6/1
Normal file
2
tests/day6/1
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
||||||
1
tests/day6/1.1
Normal file
1
tests/day6/1.1
Normal file
@@ -0,0 +1 @@
|
|||||||
|
288
|
||||||
Reference in New Issue
Block a user