stevensonmt

stevensonmt

Advent of Code 2022 - Day 6

Reasonably pleased with my solution. The bitstring packet problems are so well suited to Erlang/Elixir it’s almost not fair.

defmodule Day6 do
  defmodule Input do
    def sample_data(1), do: "mjqjpqmgbljsphdztnvjfqwrcgsmlb"

    def sample_data(2), do: "bvwbjplbgvbhsrlpgdmjqwftvncz"

    def sample_data(3), do: "nppdvjthqldpwncqszvftbrmjlhg"

    def sample_data(4), do: "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"

    def sample_data(5), do: "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"

    def load() do
      ReqAOC.fetch!({2022, 06, System.fetch_env!("AOC2022Session")})
    end
  end

  defmodule Solve do
    defp find_packet(input, n, i) do
      <<pkthd, pkttl::binary-size(n - 1), rest::binary>> = input

      if <<pkthd, pkttl::binary>> |> uniq_chars?() do
        i
      else
        find_packet(<<pkttl::binary, rest::binary>>, n, i + 1)
      end
    end

    defp uniq_chars?(<<>>), do: true

    defp uniq_chars?(<<a, rest::binary>>) do
      not String.contains?(rest, <<a>>) and uniq_chars?(rest)
    end

    def part1(input), do: find_packet(input, 4, 4)

    def part2(input), do: find_packet(input, 14, 14)
  end
end

Not an issue for the given data sets, but I did just realize this would crash and burn if there was no packet of unique elements of N length. I could handle it but, getting late.

Most Liked

Aetherus

Aetherus

Today’s quiz is easy again. In order to leverage Stream and Enum functions, I just read the file into a charlist.

defmodule Day06 do
  def part1(input_path) do
    solve(input_path, 4)
  end

  def part2(input_path) do
    solve(input_path, 14)
  end

  defp solve(input_path, chunk_size) do
    input_path
    |> File.open!([:read, :charlist], &IO.read(&1, :eof))
    |> Stream.chunk_every(chunk_size, 1, :discard)
    |> Stream.map(&Enum.uniq/1)
    |> Stream.map(&length/1)
    |> Enum.find_index(& &1 == chunk_size)
    |> Kernel.+(chunk_size)
  end
end
hst337

hst337

First part is beautiful and perfect for binary matching

defmodule AOC do

  defguard are_different(a, b, c, d) when
    a != b and a != c and a != d and
    b != c and b != d and
    c != d

  def traverse(string, acc \\ 0) do
    case string do
      <<a, b, c, d, _tail :: binary>> when are_different(a, b, c, d) ->
        acc + 4

      <<_, tail :: binary>> ->
        traverse(tail, acc + 1)
    end
  end

end

IO.inspect AOC.traverse IO.read :eof
LostKobrakai

LostKobrakai

I did use Stream.chunk_every as well. Makes this really short and sweet.

Solution
defmodule Day6 do
  def find_marker(text) do
    find_unique_character_string_of_length(text, 4)
  end

  def find_message_marker(text) do
    find_unique_character_string_of_length(text, 14)
  end

  defp find_unique_character_string_of_length(text, length) do
    {_list, index} =
      text
      |> String.to_charlist()
      |> Stream.chunk_every(length, 1, :discard)
      |> Stream.with_index(length)
      |> Enum.find(fn {list, _index} -> list |> Enum.uniq() |> length == length end)

    index
  end
end

Where Next?

Popular in Challenges Top

LostKobrakai
This topic is about Day 9 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
maennchen
Ok, that was a rough one today. I haven’t found a way to improve the algorithm further. Part 1 runs in .5 seconds, Part 2 in ~ 5 minutes...
New
lud
Hello everyone! This year is going to be shorter, but the difficulty will grow faster. Today I already feel that this is not standard “D...
New
kwando
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm.. and that...
New
rugyoga
part 1 https://github.com/rugyoga/aoc2021/blob/main/day7.exs part 2 https://github.com/rugyoga/aoc2021/blob/main/day7b.exs
New
ehayun
I have 2 arrays: a1 can be any combination of value or nil like that a1 = [1,nil,3] and array 2 the same a2 = [4,2, nil] How do I com...
New
bjorng
Note: This topic is to talk about Day 1 of the Advent of Code 2019.
New
Aetherus
I spent 3 hours struggling in part 2, until I noticed a very basic mistake :joy: Here’s my code: https://github.com/Aetherus/advent-of-...
New
stevensonmt
Anyone else think the prompt for this challenge is contradictory? The rules for comparing packets include If both values are lists, c...
New
bjorng
Note: This topic is to talk about Day 9 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement