Powered by AppSignal & Oban Pro

Advent of Code 2021 - Day 1

advent-of-code/2021/day01.livemd

Advent of Code 2021 - Day 1

Mix.install([
  {:kino, "~> 0.6.1"}
])

Instructions

https://adventofcode.com/2021/day/1

Get input

Paste the test input from the Day 1 instructions, your own input from Advent of Code, or copy from sdball 2021 Advent of Code Day 1 input

input = Kino.Input.textarea("Please paste your input:")

Parse input

numbers =
  input
  |> Kino.Input.read()
  |> String.split()
  |> Enum.map(&String.to_integer/1)

Part 1

Count the number of times a depth measurement increases from the previous measurement

numbers
|> Enum.chunk_every(2, 1, :discard)
|> Enum.count(fn [left, right] -> left < right end)

Part 2

Problem

Now consider sums in a three-measurement sliding window.

199  A      
200  A B    
208  A B C  
210    B C D
200  E   C D
207  E F   D
240  E F G  
269    F G H
260      G H
263        H

Count the number of times the sum of measurements in this sliding window increases from the previous sum

Solution

numbers
|> Enum.chunk_every(3, 1, :discard)
|> Enum.map(&Enum.sum/1)
|> Enum.chunk_every(2, 1, :discard)
|> Enum.count(fn [left, right] -> left < right end)