Skip to content

Instantly share code, notes, and snippets.

@johngian
Created December 10, 2021 10:26
Show Gist options
  • Save johngian/e7a86159e4b0fe02f6ef10509de8cd6b to your computer and use it in GitHub Desktop.
Save johngian/e7a86159e4b0fe02f6ef10509de8cd6b to your computer and use it in GitHub Desktop.
Advent of code 2021 - day 9 - part 1 - solution
defmodule Smoke do
def read(path) do
[h | t] =
File.read!(path)
|> String.split("\n")
|> Enum.map(fn x ->
String.codepoints(x)
|> Enum.map(&String.to_integer/1)
|> (fn x -> [10] ++ x ++ [10] end).()
end)
margin = List.duplicate(10, Enum.count(h))
[margin] ++ [h | t] ++ [margin]
end
def check([[_, _ | t1] | _], acc) when t1 == [] do
acc
end
def check([[_, b, c | t1], [d, e, f | t2], [_, h, i | t3]], acc) do
case e < b and e < d and e < f and e < h do
true -> check([[b, c | t1], [e, f | t2], [h, i | t3]], acc + e + 1)
false -> check([[b, c | t1], [e, f | t2], [h, i | t3]], acc)
end
end
def getsub([a, b, c | t], acc) when t == [] do
check([a, b, c], acc)
end
def getsub([a, b, c | t], acc) do
getsub([b, c | t], check([a, b, c], acc))
end
def run() do
read("../data/day9.txt")
|> getsub(0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment