Powered by AppSignal & Oban Pro

闘魂 Elixir ── Camp Cleanup

notebooks/toukonex/camp_cleanup.livemd

闘魂 Elixir ── Camp Cleanup

# Mix.install([{:benchee, "~> 1.1"}])

About

Answer 1 - support only one-digit numbers

# expected answer: 2
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
"""
48 = ?0
49 = ?1
50 = ?2
51 = ?3
52 = ?4
53 = ?5
54 = ?6
55 = ?7
56 = ?8
57 = ?9

0 = ?0 - ?0
1 = ?1 - ?0
2 = ?2 - ?0
3 = ?3 - ?0
4 = ?4 - ?0
5 = ?5 - ?0
6 = ?6 - ?0
7 = ?7 - ?0
8 = ?8 - ?0
9 = ?9 - ?0

:ok
judge = fn
  a1, a2, b1, b2 when a1 in b1..b2 and a2 in b1..b2 -> true
  a1, a2, b1, b2 when b1 in a1..a2 and b2 in a1..a2 -> true
  _, _, _, _ -> false
end

for <<(<<a1::utf8, "-"::utf8, a2::utf8, ","::utf8, b1::utf8, "-"::utf8, b2::utf8, "\n"::utf8>> <-
         input)>>,
    reduce: 0 do
  count -> if judge.(a1 - ?0, a2 - ?0, b1 - ?0, b2 - ?0), do: count + 1, else: count
end

Answer 2 - support two-digit numbers

# expected answer: 3
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
10-50,0-99
"""
parse_row = fn row ->
  row |> String.split(["-", ","]) |> Enum.map(&String.to_integer/1)
end

[10, 50, 0, 99] = parse_row.("10-50,0-99")
judge = fn
  [a1, a2, b1, b2] when a1 in b1..b2 and a2 in b1..b2 -> true
  [a1, a2, b1, b2] when b1 in a1..a2 and b2 in a1..a2 -> true
  [_, _, _, _] -> false
end

false = judge.([2, 4, 6, 8])
true = judge.([10, 50, 0, 99])

:ok
parse_row = fn row ->
  row |> String.split(["-", ","]) |> Enum.map(&String.to_integer/1)
end

judge = fn
  [a1, a2, b1, b2] when a1 in b1..b2 and a2 in b1..b2 -> true
  [a1, a2, b1, b2] when b1 in a1..a2 and b2 in a1..a2 -> true
  [_, _, _, _] -> false
end

input
|> String.split("\n", trim: true)
|> Enum.reduce(0, fn row, acc ->
  if row |> parse_row.() |> judge.() do
    acc + 1
  else
    acc
  end
end)