aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/04/solver.ex
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/04/solver.ex')
-rw-r--r--AoC2022/04/solver.ex36
1 files changed, 36 insertions, 0 deletions
diff --git a/AoC2022/04/solver.ex b/AoC2022/04/solver.ex
new file mode 100644
index 0000000..0478795
--- /dev/null
+++ b/AoC2022/04/solver.ex
@@ -0,0 +1,36 @@
+defmodule Solver do
+ def to_interval(range) do
+ [left, right] = String.split(range, "-")
+ {left, ""} = Integer.parse(left)
+ {right, ""} = Integer.parse(right)
+ {left, right}
+ end
+
+ def contained({a0, a1}, {b0, b1}) do
+ (a0 - b0) * (a1 - b1) <= 0
+ end
+
+ def overlap({a0, a1}, {b0, b1}) do
+ a0 <= b1 && b0 <= a1
+ end
+end
+
+{:ok, input} = File.read("input")
+
+parsed =
+ input
+ |> String.split("\n", trim: true)
+ |> Enum.map(&String.split(&1, ","))
+ |> Enum.map(fn [left, right] -> {Solver.to_interval(left), Solver.to_interval(right)} end)
+
+parsed
+|> Enum.map(fn {left, right} -> Solver.contained(left, right) end)
+|> Enum.filter(& &1)
+|> length
+|> IO.puts()
+
+parsed
+|> Enum.map(fn {left, right} -> Solver.overlap(left, right) end)
+|> Enum.filter(& &1)
+|> length
+|> IO.puts()