Powered by AppSignal & Oban Pro

Day 6: Tuning Trouble -- AoC 22

2022/06.livemd

Day 6: Tuning Trouble -- AoC 22

file = File.read!("/Users/pw/src/weiland/adventofcode/2022/input/06.txt")
stream = File.stream!("/Users/pw/src/weiland/adventofcode/2022/input/06.txt")
%File.Stream{
  path: "/Users/pw/src/weiland/adventofcode/2022/input/06.txt",
  modes: [:raw, :read_ahead, :binary],
  line_or_bytes: :line,
  raw: true
}

Part One

amount = 4

[{_list, index}] =
  file
  |> String.trim()
  # perhaps look into String.graphemes
  |> String.split("")
  |> Enum.filter(fn item -> item != "" end)
  |> Enum.chunk_every(amount, 1)
  # adds index
  |> Enum.with_index()
  # use a reducer or find?
  |> Enum.filter(fn {lst, _index} -> Enum.uniq(lst) == lst end)
  |> Enum.take(1)

index + amount
1658

Part Two

amount = 14

reducer = fn {lst, index}, acc ->
  if Enum.uniq(lst) == lst do
    {:halt, index + amount}
  else
    {:cont, acc}
  end
end

stream
|> Stream.flat_map(&String.to_charlist/1)
|> Enum.chunk_every(amount, 1)
|> Enum.find_index(fn lst -> length(Enum.uniq(lst)) == amount end)

# |> Enum.reduce_while(0, reducer)
2246