-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathday_01.ex
More file actions
28 lines (24 loc) · 734 Bytes
/
day_01.ex
File metadata and controls
28 lines (24 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
defmodule AdventOfCode.Y2021.Day01 do
@moduledoc """
--- Day 1: Sonar Sweep ---
Problem Link: https://adventofcode.com/2021/day/1
Difficulty: xs
Tags: sequence sliding-window
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2021, 1)
def run(input \\ input()) do
input = Transformers.int_lines(input)
{depth_increase(input), depth_increase(sliding_window(input))}
end
defp depth_increase(measurements) do
measurements
|> Enum.chunk_every(2, 1, :discard)
|> Enum.count(fn [a, b] -> b - a > 0 end)
end
defp sliding_window(measurements) do
measurements
|> Enum.chunk_every(3, 1, :discard)
|> Enum.map(&Enum.sum/1)
end
end