-
-
Save al2o3cr/04c18cc3da91768cc703d8d04131c2e4 to your computer and use it in GitHub Desktop.
2022 AOC Day 13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Packets do | |
def read(filename) do | |
File.stream!(filename) | |
|> Stream.map(&String.trim/1) | |
|> Stream.chunk_by(& &1 == "") | |
|> Stream.reject(& &1 == [""]) | |
|> Stream.map(fn [a, b] -> {expand_line(a), expand_line(b)} end) | |
end | |
defp expand_line(line) do | |
{:ok, result} = Code.string_to_quoted(line) | |
result | |
end | |
def compare(a, b) when is_integer(a) and is_integer(b) and a < b, do: :lt | |
def compare(a, b) when is_integer(a) and is_integer(b) and a == b, do: :eq | |
def compare(a, b) when is_integer(a) and is_integer(b) and a > b, do: :gt | |
def compare([], []), do: :eq | |
def compare([], b) when is_list(b), do: :lt | |
def compare(a, []) when is_list(a), do: :gt | |
def compare([a | a_rest], [b | b_rest]) do | |
case compare(a, b) do | |
:lt -> :lt | |
:gt -> :gt | |
:eq -> compare(a_rest, b_rest) | |
end | |
end | |
def compare(a, b) when is_integer(a) and is_list(b), do: compare([a], b) | |
def compare(a, b) when is_list(a) and is_integer(b), do: compare(a, [b]) | |
end | |
Packets.read("input.txt") | |
|> Stream.map(fn {a, b} -> Packets.compare(a, b) end) | |
|> Stream.with_index(1) | |
|> Stream.filter(fn {c, _} -> c == :lt end) | |
|> Stream.map(fn {_, idx} -> idx end) | |
|> Enum.sum() | |
|> IO.inspect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Packets do | |
def read(filename) do | |
File.stream!(filename) | |
|> Stream.map(&String.trim/1) | |
|> Stream.chunk_by(& &1 == "") | |
|> Stream.reject(& &1 == [""]) | |
|> Stream.map(fn [a, b] -> {expand_line(a), expand_line(b)} end) | |
end | |
defp expand_line(line) do | |
{:ok, result} = Code.string_to_quoted(line) | |
result | |
end | |
def compare(a, b) when is_integer(a) and is_integer(b) and a < b, do: :lt | |
def compare(a, b) when is_integer(a) and is_integer(b) and a == b, do: :eq | |
def compare(a, b) when is_integer(a) and is_integer(b) and a > b, do: :gt | |
def compare([], []), do: :eq | |
def compare([], b) when is_list(b), do: :lt | |
def compare(a, []) when is_list(a), do: :gt | |
def compare([a | a_rest], [b | b_rest]) do | |
case compare(a, b) do | |
:lt -> :lt | |
:gt -> :gt | |
:eq -> compare(a_rest, b_rest) | |
end | |
end | |
def compare(a, b) when is_integer(a) and is_list(b), do: compare([a], b) | |
def compare(a, b) when is_list(a) and is_integer(b), do: compare(a, [b]) | |
end | |
divider_packets = [[[2]], [[6]]] | |
Packets.read("input.txt") | |
|> Stream.flat_map(fn {a, b} -> [a, b] end) | |
|> Stream.concat(divider_packets) | |
|> Enum.sort(Packets) | |
|> Stream.with_index(1) | |
|> Stream.filter(fn {p, _} -> p in divider_packets end) | |
|> Stream.map(fn {_, idx} -> idx end) | |
|> Enum.to_list() | |
|> then(fn [x, y] -> x * y end) | |
|> IO.inspect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment