Day 6 Part 2

This commit is contained in:
Adam Millerchip 2023-12-07 10:44:36 +09:00
parent 4d3293cd3e
commit 6503a02d1e

View file

@ -2,17 +2,22 @@
defmodule Day6 do defmodule Day6 do
def part1(input) do def part1(input) do
input input
|> Enum.map(fn {race, record} -> |> Enum.map(fn line -> Enum.map(line, &String.to_integer/1) end)
scores = for n <- 1..div(race, 2), do: n * (race - n) |> Enum.zip()
[_middle | rev_no_middle] = reversed = Enum.reverse(scores) |> Enum.map(fn {race, record} -> count_winning(race, record) end)
scores = scores ++ if rem(race, 2) == 1, do: reversed, else: rev_no_middle
Enum.count(scores, fn score -> score > record end)
end)
|> Enum.product() |> Enum.product()
end end
def part2(_input) do def count_winning(race, record) do
:ok scores = for n <- 1..div(race, 2), do: n * (race - n)
[_middle | rev_no_middle] = reversed = Enum.reverse(scores)
scores = scores ++ if rem(race, 2) == 1, do: reversed, else: rev_no_middle
Enum.count(scores, fn score -> score > record end)
end
def part2(input) do
[race, record] = Enum.map(input, fn line -> line |> Enum.join() |> String.to_integer() end)
count_winning(race, record)
end end
def input do def input do
@ -24,9 +29,7 @@ defmodule Day6 do
line line
|> String.split() |> String.split()
|> Enum.drop(1) |> Enum.drop(1)
|> Enum.map(&String.to_integer/1)
end) end)
|> Enum.zip()
else else
_ -> :error _ -> :error
end end