Skip to content

Instantly share code, notes, and snippets.

@asimpson
Last active December 2, 2021 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asimpson/360847dedecba562dc8323389f2b63ce to your computer and use it in GitHub Desktop.
Save asimpson/360847dedecba562dc8323389f2b63ce to your computer and use it in GitHub Desktop.
readings =
File.read!("day-1-input.txt")
|> String.split("\n", trim: true)
|> Enum.map(&String.to_integer/1)
count =
Enum.with_index(readings)
|> Enum.count(fn {x, i} ->
prev = if i > 0, do: i - 1, else: 0
x > Enum.at(readings, prev)
end)
IO.inspect(count, label: "count is")
chunks =
File.read!("day-1-input.txt")
|> String.split("\n", trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(3, 1)
chunk_size = length(chunks)
count =
Enum.with_index(chunks)
|> Enum.count(fn {x, i} ->
next = Enum.at(chunks, i + 1)
if i < chunk_size - 1 && length(next) == 3 do
c = Enum.sum(x)
n = Enum.sum(next)
n > c
end
end)
IO.inspect(count, label: "count is")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment