Day 6 Part 2
This commit is contained in:
parent
4d3293cd3e
commit
6503a02d1e
1 changed files with 13 additions and 10 deletions
|
@ -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)
|
||||||
|
|> Enum.zip()
|
||||||
|
|> Enum.map(fn {race, record} -> count_winning(race, record) end)
|
||||||
|
|> Enum.product()
|
||||||
|
end
|
||||||
|
|
||||||
|
def count_winning(race, record) do
|
||||||
scores = for n <- 1..div(race, 2), do: n * (race - n)
|
scores = for n <- 1..div(race, 2), do: n * (race - n)
|
||||||
[_middle | rev_no_middle] = reversed = Enum.reverse(scores)
|
[_middle | rev_no_middle] = reversed = Enum.reverse(scores)
|
||||||
scores = scores ++ if rem(race, 2) == 1, do: reversed, else: rev_no_middle
|
scores = scores ++ if rem(race, 2) == 1, do: reversed, else: rev_no_middle
|
||||||
Enum.count(scores, fn score -> score > record end)
|
Enum.count(scores, fn score -> score > record end)
|
||||||
end)
|
|
||||||
|> Enum.product()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def part2(_input) do
|
def part2(input) do
|
||||||
:ok
|
[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
|
||||||
|
|
Loading…
Reference in a new issue