Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 1: Sonar sweep

2021/01.livemd

Day 1: Sonar sweep

Intro

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

Input - common

input_test =
  """
  199
  200
  208
  210
  200
  207
  240
  269
  260
  263
  """
  |> String.split("\n", trim: true)
  |> Enum.map(fn x -> String.to_integer(x) end)
[199, 200, 208, 210, 200, 207, 240, 269, 260, 263]
input =
  File.read!("input01.txt")
  |> String.split("\n")
  |> Enum.map(fn x -> String.to_integer(x) end)
[140, 154, 165, 150, 151, 161, 172, 174, 166, 168, 176, 191, 192, 189, 190, 191, 202, 203, 206, 207,
 167, 179, 204, 206, 208, 209, 216, 198, 200, 207, 217, 218, 223, 227, 228, 243, 241, 246, 278, 255,
 256, 273, 295, 291, 288, 290, 303, 325, 301, 292, ...]

Part 1 - testing

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

Correct answer: 7

Part 1 -running

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

Correct answer: 1298

Part 2 - testing

groups_test =
  Enum.chunk_every(input_test, 3, 1)
  |> Enum.filter(fn x -> length(x) == 3 end)
  |> Enum.map(fn x -> Enum.sum(x) end)
  |> Enum.chunk_every(2, 1, :discard)
  |> Enum.count(fn [left, right] -> right > left end)
5

Correct: 5

Part 2 - running

groups =
  Enum.chunk_every(input, 3, 1)
  |> Enum.filter(fn x -> length(x) == 3 end)
  |> Enum.map(fn x -> Enum.sum(x) end)
  |> Enum.chunk_every(2, 1, :discard)
  |> Enum.count(fn [left, right] -> right > left end)
1248

Cottect: 1248