solve: day 1
This commit is contained in:
parent
b4c93e39f4
commit
bddcdc82c8
7 changed files with 2066 additions and 12 deletions
|
@ -2,6 +2,12 @@ defmodule AdventOfCode.Day01 do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Day 1
|
Day 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def parse_input_stream(input_stream) do
|
||||||
|
input_stream
|
||||||
|
|> Stream.map(&String.trim/1)
|
||||||
|
|> Stream.map(&String.to_integer/1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defmodule AdventOfCode.Day01.Part1 do
|
defmodule AdventOfCode.Day01.Part1 do
|
||||||
|
@ -15,9 +21,23 @@ defmodule AdventOfCode.Day01.Part1 do
|
||||||
import AdventOfCode.Day01, warn: false
|
import AdventOfCode.Day01, warn: false
|
||||||
|
|
||||||
@impl PuzzleSolver
|
@impl PuzzleSolver
|
||||||
def solve(_input_stream) do
|
def solve(input_stream) do
|
||||||
:ok |> to_string()
|
depth_stream =
|
||||||
|
input_stream
|
||||||
|
|> parse_input_stream()
|
||||||
|
|> Enum.to_list()
|
||||||
|
|
||||||
|
Enum.zip_with(
|
||||||
|
depth_stream,
|
||||||
|
Enum.drop(depth_stream, 1),
|
||||||
|
&</2
|
||||||
|
)
|
||||||
|
|> Stream.filter(&is_true?/1)
|
||||||
|
|> Enum.count()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp is_true?(true), do: true
|
||||||
|
defp is_true?(_), do: false
|
||||||
end
|
end
|
||||||
|
|
||||||
defmodule AdventOfCode.Day01.Part2 do
|
defmodule AdventOfCode.Day01.Part2 do
|
||||||
|
@ -31,7 +51,18 @@ defmodule AdventOfCode.Day01.Part2 do
|
||||||
import AdventOfCode.Day01, warn: false
|
import AdventOfCode.Day01, warn: false
|
||||||
|
|
||||||
@impl PuzzleSolver
|
@impl PuzzleSolver
|
||||||
def solve(_input_stream) do
|
def solve(input_stream) do
|
||||||
:ok |> to_string()
|
parse_input_stream(input_stream)
|
||||||
|
|> Enum.to_list()
|
||||||
|
|> window_increases()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp window_increases(list, acc \\ 0)
|
||||||
|
|
||||||
|
defp window_increases([a, b, c, d | _] = list, acc) do
|
||||||
|
acc = if a + b + c < b + c + d, do: acc + 1, else: acc
|
||||||
|
window_increases(Enum.drop(list, 1), acc)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp window_increases(_, acc), do: acc
|
||||||
end
|
end
|
|
@ -6,7 +6,7 @@ defmodule AdventOfCode.PuzzleSolver do
|
||||||
@doc """
|
@doc """
|
||||||
Given the input as a stream, return the solution as a string
|
Given the input as a stream, return the solution as a string
|
||||||
"""
|
"""
|
||||||
@callback solve(Enumerable.t()) :: String.t()
|
@callback solve(Enumerable.t()) :: any()
|
||||||
|
|
||||||
def solve(mod, stream), do: apply(mod, :solve, [stream])
|
def solve(mod, stream), do: apply(mod, :solve, [stream])
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@ defmodule AdventOfCode.MixProject do
|
||||||
defp deps do
|
defp deps do
|
||||||
[
|
[
|
||||||
{:finch, "~> 0.9.0"},
|
{:finch, "~> 0.9.0"},
|
||||||
|
{:mix_test_interactive, "~> 1.0", only: :dev, runtime: false},
|
||||||
{:progress_bar, "~> 2.0"}
|
{:progress_bar, "~> 2.0"}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
%{
|
%{
|
||||||
"castore": {:hex, :castore, "0.1.13", "ccf3ab251ffaebc4319f41d788ce59a6ab3f42b6c27e598ad838ffecee0b04f9", [:mix], [], "hexpm", "a14a7eecfec7e20385493dbb92b0d12c5d77ecfd6307de10102d58c94e8c49c0"},
|
"castore": {:hex, :castore, "0.1.13", "ccf3ab251ffaebc4319f41d788ce59a6ab3f42b6c27e598ad838ffecee0b04f9", [:mix], [], "hexpm", "a14a7eecfec7e20385493dbb92b0d12c5d77ecfd6307de10102d58c94e8c49c0"},
|
||||||
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
|
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
|
||||||
|
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
|
||||||
"finch": {:hex, :finch, "0.9.0", "8b772324aebafcaba763f1dffaa3e7f52f8c4e52485f50f48bbb2f42219a2e87", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a93bfcad9ca50fa3cb2d459f27667d9a87cfbb7fecf9b29b2e78a50bc2ab445d"},
|
"finch": {:hex, :finch, "0.9.0", "8b772324aebafcaba763f1dffaa3e7f52f8c4e52485f50f48bbb2f42219a2e87", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a93bfcad9ca50fa3cb2d459f27667d9a87cfbb7fecf9b29b2e78a50bc2ab445d"},
|
||||||
"mint": {:hex, :mint, "1.4.0", "cd7d2451b201fc8e4a8fd86257fb3878d9e3752899eb67b0c5b25b180bde1212", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "10a99e144b815cbf8522dccbc8199d15802440fc7a64d67b6853adb6fa170217"},
|
"mint": {:hex, :mint, "1.4.0", "cd7d2451b201fc8e4a8fd86257fb3878d9e3752899eb67b0c5b25b180bde1212", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "10a99e144b815cbf8522dccbc8199d15802440fc7a64d67b6853adb6fa170217"},
|
||||||
|
"mix_test_interactive": {:hex, :mix_test_interactive, "1.1.0", "e3e048ea8b56637c8e5614044483e11d78f81f70e9afb811c06ad3db69704c32", [:mix], [{:file_system, "~> 0.2", [hex: :file_system, repo: "hexpm", optional: false]}, {:typed_struct, "~> 0.2.1", [hex: :typed_struct, repo: "hexpm", optional: false]}], "hexpm", "2288a2edc735ca4879a7c17b9fd9fd4b8b84a57a775c3228db4655806a0a4799"},
|
||||||
"nimble_options": {:hex, :nimble_options, "0.3.7", "1e52dd7673d36138b1a5dede183b5d86dff175dc46d104a8e98e396b85b04670", [:mix], [], "hexpm", "2086907e6665c6b6579be54ef5001928df5231f355f71ed258f80a55e9f63633"},
|
"nimble_options": {:hex, :nimble_options, "0.3.7", "1e52dd7673d36138b1a5dede183b5d86dff175dc46d104a8e98e396b85b04670", [:mix], [], "hexpm", "2086907e6665c6b6579be54ef5001928df5231f355f71ed258f80a55e9f63633"},
|
||||||
"nimble_pool": {:hex, :nimble_pool, "0.2.4", "1db8e9f8a53d967d595e0b32a17030cdb6c0dc4a451b8ac787bf601d3f7704c3", [:mix], [], "hexpm", "367e8071e137b787764e6a9992ccb57b276dc2282535f767a07d881951ebeac6"},
|
"nimble_pool": {:hex, :nimble_pool, "0.2.4", "1db8e9f8a53d967d595e0b32a17030cdb6c0dc4a451b8ac787bf601d3f7704c3", [:mix], [], "hexpm", "367e8071e137b787764e6a9992ccb57b276dc2282535f767a07d881951ebeac6"},
|
||||||
"progress_bar": {:hex, :progress_bar, "2.0.1", "7b40200112ae533d5adceb80ff75fbe66dc753bca5f6c55c073bfc122d71896d", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "2519eb58a2f149a3a094e729378256d8cb6d96a259ec94841bd69fdc71f18f87"},
|
"progress_bar": {:hex, :progress_bar, "2.0.1", "7b40200112ae533d5adceb80ff75fbe66dc753bca5f6c55c073bfc122d71896d", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "2519eb58a2f149a3a094e729378256d8cb6d96a259ec94841bd69fdc71f18f87"},
|
||||||
"telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"},
|
"telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"},
|
||||||
|
"typed_struct": {:hex, :typed_struct, "0.2.1", "e1993414c371f09ff25231393b6430bd89d780e2a499ae3b2d2b00852f593d97", [:mix], [], "hexpm", "8f5218c35ec38262f627b2c522542f1eae41f625f92649c0af701a6fab2e11b3"},
|
||||||
}
|
}
|
||||||
|
|
2000
2021/priv/inputs/01.input
Normal file
2000
2021/priv/inputs/01.input
Normal file
File diff suppressed because it is too large
Load diff
|
@ -3,9 +3,18 @@ defmodule AdventOfCode.Day01.Part1Test do
|
||||||
|
|
||||||
test "returns :ok" do
|
test "returns :ok" do
|
||||||
input = ~S"""
|
input = ~S"""
|
||||||
input
|
199
|
||||||
|
200
|
||||||
|
208
|
||||||
|
210
|
||||||
|
200
|
||||||
|
207
|
||||||
|
240
|
||||||
|
269
|
||||||
|
260
|
||||||
|
263
|
||||||
"""
|
"""
|
||||||
|
|
||||||
assert_solution(input, "ok")
|
assert_solution(input, 7)
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -3,9 +3,19 @@ defmodule AdventOfCode.Day01.Part2Test do
|
||||||
|
|
||||||
test "returns :ok" do
|
test "returns :ok" do
|
||||||
input = ~S"""
|
input = ~S"""
|
||||||
input
|
199
|
||||||
|
200
|
||||||
|
208
|
||||||
|
210
|
||||||
|
200
|
||||||
|
207
|
||||||
|
240
|
||||||
|
269
|
||||||
|
260
|
||||||
|
263
|
||||||
"""
|
"""
|
||||||
|
|
||||||
assert_solution(input, "ok")
|
assert_solution(input, 5)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue