1
0
Fork 0

day 1 cleanup

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent b9e9ecc85f
commit 766ae24cb2
3 changed files with 9 additions and 17 deletions

View file

@ -7,6 +7,7 @@ defmodule AdventOfCode.Day01 do
input_stream input_stream
|> Stream.map(&String.trim/1) |> Stream.map(&String.trim/1)
|> Stream.map(&String.to_integer/1) |> Stream.map(&String.to_integer/1)
|> Enum.to_list()
end end
end end
@ -22,22 +23,15 @@ defmodule AdventOfCode.Day01.Part1 do
@impl PuzzleSolver @impl PuzzleSolver
def solve(input_stream) do def solve(input_stream) do
depth_stream = depths = parse_input_stream(input_stream)
input_stream
|> parse_input_stream()
|> Enum.to_list()
Enum.zip_with( Enum.zip_reduce(
depth_stream, depths,
Enum.drop(depth_stream, 1), Enum.drop(depths, 1),
&</2 0,
&if(&1 < &2, do: &3 + 1, else: &3)
) )
|> 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
@ -53,7 +47,6 @@ defmodule AdventOfCode.Day01.Part2 do
@impl PuzzleSolver @impl PuzzleSolver
def solve(input_stream) do def solve(input_stream) do
parse_input_stream(input_stream) parse_input_stream(input_stream)
|> Enum.to_list()
|> window_increases() |> window_increases()
end end

View file

@ -1,7 +1,7 @@
defmodule AdventOfCode.Day01.Part1Test do defmodule AdventOfCode.Day01.Part1Test do
use AdventOfCode.PuzzleCase, module: AdventOfCode.Day01.Part1 use AdventOfCode.PuzzleCase, module: AdventOfCode.Day01.Part1
test "returns :ok" do test "solves for a small input" do
input = ~S""" input = ~S"""
199 199
200 200

View file

@ -1,7 +1,7 @@
defmodule AdventOfCode.Day01.Part2Test do defmodule AdventOfCode.Day01.Part2Test do
use AdventOfCode.PuzzleCase, module: AdventOfCode.Day01.Part2 use AdventOfCode.PuzzleCase, module: AdventOfCode.Day01.Part2
test "returns :ok" do test "solves for a small input" do
input = ~S""" input = ~S"""
199 199
200 200
@ -18,4 +18,3 @@ defmodule AdventOfCode.Day01.Part2Test do
assert_solution(input, 5) assert_solution(input, 5)
end end
end end