1
0
Fork 0
advent-of-code/2021/lib/advent_of_code/day01.ex

69 lines
1.3 KiB
Elixir
Raw Normal View History

2022-09-21 09:19:53 -04:00
defmodule AdventOfCode.Day01 do
@moduledoc """
Day 1
"""
2022-09-21 09:19:53 -04:00
def parse_input_stream(input_stream) do
input_stream
|> Stream.map(&String.trim/1)
|> Stream.map(&String.to_integer/1)
end
2022-09-21 09:19:53 -04:00
end
defmodule AdventOfCode.Day01.Part1 do
@moduledoc """
Day 1, Part 1
"""
alias AdventOfCode.PuzzleSolver
use PuzzleSolver
import AdventOfCode.Day01, warn: false
@impl PuzzleSolver
2022-09-21 09:19:53 -04:00
def solve(input_stream) do
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()
2022-09-21 09:19:53 -04:00
end
2022-09-21 09:19:53 -04:00
defp is_true?(true), do: true
defp is_true?(_), do: false
2022-09-21 09:19:53 -04:00
end
defmodule AdventOfCode.Day01.Part2 do
@moduledoc """
Day 1, Part 2
"""
alias AdventOfCode.PuzzleSolver
use PuzzleSolver
import AdventOfCode.Day01, warn: false
@impl PuzzleSolver
2022-09-21 09:19:53 -04:00
def solve(input_stream) do
parse_input_stream(input_stream)
|> Enum.to_list()
|> window_increases()
2022-09-21 09:19:53 -04:00
end
2022-09-21 09:19:53 -04:00
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