Reviewing my Pangram exercise notes this morning for Day 03 helped out a lot.
Julia Solution
module day04
function part01()
count = 0
for l in eachline("/Users/anagy/Documents/AdventOfCode2022/AdventOfCode2022-Julia/input/04/input.in")
start1, stop1, start2, stop2 = parse.(Int, split(l, r"[,-]"))
pair1 = range(start1, stop1)
pair2 = range(start2, stop2)
if (pair1 ⊆ pair2) || (pair2 ⊆ pair1)
count += 1
end
end
count
end
function part02()
count = 0
for l in eachline("/Users/anagy/Documents/AdventOfCode2022/AdventOfCode2022-Julia/input/04/input.in")
start1, stop1, start2, stop2 = parse.(Int, split(l, r"[,-]"))
pair1 = range(start1, stop1)
pair2 = range(start2, stop2)
if (!isempty(pair1 ∩ pair2))
count += 1
end
end
count
end
end
println("P01: $(day04.part01())")
println("P02: $(day04.part02())")
I was also surprised that this was so smooth. I didn’t even leave the console to solve that one. So part 1 is a one-liner, as I forgot to make it as “pretty” as part 2.
defmodule AdventOfCode.Day04 do
def part1(args) do
args|> String.split() |> Enum.map(&String.split(&1, ~r{[\-\,]})) |> Enum.map(&Enum.map(&1, fn x -> String.to_integer(x) end)) |> Enum.map(fn [a,b,c,d] -> (a >= c and b <= d) or (c >= a and d <= b) end) |> Enum.count(fn x -> x end)
end
def part2(args) do
args
|> String.split()
|> Enum.map(&String.split(&1, ~r{[\-\,]}))
|> Enum.map(&Enum.map(&1, fn x -> String.to_integer(x) end))
|> Enum.map(fn [a,b,c,d] -> (a >= c and a <= d) or (b >= c and b <= d) or (c >= a and c <= b) or (d >= a and d <= b)
end)
|> Enum.count(fn x -> x end)
end
end