Advent of Code 2022 - Day 4
Mix.install([:kino, {:kino_aoc, git: "https://github.com/ljgago/kino_aoc"}])
Input
test_input =
"""
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
"""
|> String.trim()
{:ok, puzzle_input} = KinoAOC.download_puzzle("2022", "4", System.fetch_env!("LB_AOC_SESSION"))
input_field =
Kino.Input.select("input", [
{test_input, "test_input"},
{puzzle_input, "puzzle_input"}
])
Parsing
parsed =
input_field
|> Kino.Input.read()
|> String.split("\n", trim: true)
|> Enum.flat_map(&String.split(&1, ~r/[-,]/))
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(4)
Part 1
Enum.count(parsed, fn [ax, ay, bx, by] -> (ax <= bx and ay >= by) or (ax >= bx and ay <= by) end)
Part 2
Enum.count(parsed, fn [ax, ay, bx, by] -> bx <= ay and by >= ax end)