Last active
December 5, 2024 16:27
-
-
Save Serabe/0744ae6ff8f60d4db8627b99cb593fff to your computer and use it in GitHub Desktop.
Day 01 of AoC 2024
This file contains hidden or 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
input | |
#example | |
|> String.split("\n", trim: true) | |
|> Enum.map(fn line -> | |
line | |
|> String.split(" ", trim: true) | |
|> Enum.map(&String.to_integer/1) | |
|> List.to_tuple() | |
end) | |
|> Enum.unzip() | |
|> Tuple.to_list() | |
|> Enum.map(&Enum.sort/1) | |
|> Enum.zip() | |
|> Enum.map(fn {a, b} -> abs(a - b) end) | |
|> Enum.sum() |
This file contains hidden or 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
{left_list, right_list} = | |
input | |
#example | |
|> String.split("\n", trim: true) | |
|> Enum.map(fn line -> | |
line | |
|> String.split(" ", trim: true) | |
|> Enum.map(&String.to_integer(&1)) | |
|> List.to_tuple() | |
end) | |
|> Enum.unzip() | |
right_map = right_list |> Enum.group_by(& &1) |> Map.new(fn {k, v} -> {k, Enum.count(v)} end) | |
Enum.reduce(left_list, 0, fn el, acc -> acc + el * Map.get(right_map, el, 0) end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment