Skip to content

Instantly share code, notes, and snippets.

@johngian
Created December 6, 2021 00:15
Show Gist options
  • Save johngian/9d3c8eeacc80af0ab11f79bfae539785 to your computer and use it in GitHub Desktop.
Save johngian/9d3c8eeacc80af0ab11f79bfae539785 to your computer and use it in GitHub Desktop.
Advent of code 2021 - day 5 - part 2 - solution
defmodule Vents do
def parsepoint(point) do
point
|> String.split(",", trim: true)
|> Enum.map(&String.trim/1)
|> Enum.map(&String.to_integer/1)
end
def parseline(line) do
line
|> String.split("->", trim: true)
|> Enum.map(&parsepoint/1)
end
def read(path) do
File.read!(path)
|> String.split("\n")
|> Enum.map(&parseline/1)
end
def points([x1, y1], [x2, y2]) when x1 != x2 and y1 != y2 do
xs = Enum.to_list(x1..x2)
ys = Enum.to_list(y1..y2)
Enum.zip(xs, ys)
end
def points([x1, y1], [x2, y2]) when x1 == x2 do
Enum.to_list(y1..y2)
|> Enum.map(fn y -> {x1, y} end)
end
def points([x1, y1], [x2, y2]) when y1 == y2 do
Enum.to_list(x1..x2)
|> Enum.map(fn x -> {x, y1} end)
end
def run() do
all =
read("./input/day5.txt")
|> Enum.map(fn [a, b] -> points(a, b) end)
|> List.flatten()
|> Enum.frequencies()
|> Enum.filter(fn {k, v} -> v >= 2 end)
|> Enum.count()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment