Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Advent of Code 2022 - Day 3

2022/day03.livemd

Advent of Code 2022 - Day 3

Mix.install([:kino, {:kino_aoc, git: "https://github.com/ljgago/kino_aoc"}])

Input

test_input = """
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
"""
{:ok, puzzle_input} = KinoAOC.download_puzzle("2022", "3", 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.trim()
  |> String.split("\n", trim: true)

Part 1

parsed
|> Stream.map(&String.to_charlist/1)
|> Stream.map(&Enum.split(&1, &1 |> length() |> div(2)))
|> Stream.map(fn {left, right} -> Enum.find(left, &Enum.member?(right, &1)) end)
|> Stream.map(fn
  lower when lower in ?a..?z -> lower - ?a + 1
  upper when upper in ?A..?Z -> upper - ?A + 27
end)
|> Enum.sum()

Part 2

parsed
|> Stream.map(&String.to_charlist/1)
|> Stream.map(&Enum.uniq/1)
|> Stream.chunk_every(3)
|> Stream.map(fn group ->
  group
  |> Enum.map(&MapSet.new/1)
  |> Enum.reduce(&MapSet.intersection(&2, &1))
  |> MapSet.to_list()
  |> hd()
end)
|> Stream.map(fn
  lower when lower in ?a..?z -> lower - ?a + 1
  upper when upper in ?A..?Z -> upper - ?A + 27
end)
|> Enum.sum()