-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2a2c8b
commit 1202c33
Showing
10 changed files
with
2,546 additions
and
31 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule AdventOfCode.Y2024.Day01 do | ||
@moduledoc """ | ||
--- Day 1: Historian Hysteria --- | ||
Problem Link: https://adventofcode.com/2024/day/1 | ||
Difficulty: xs | ||
Tags: list | ||
""" | ||
alias AdventOfCode.Helpers.{InputReader, Transformers} | ||
|
||
def input, do: InputReader.read_from_file(2024, 1) | ||
|
||
def run(input \\ input()) do | ||
input = parse(input) | ||
|
||
{run_1(input), run_2(input)} | ||
end | ||
|
||
defp run_1({left_list, right_list, _}) do | ||
for {left, right} <- Enum.zip(left_list, right_list), reduce: 0 do | ||
acc -> acc + abs(left - right) | ||
end | ||
end | ||
|
||
defp run_2({left_list, _, tally}) do | ||
for id_value <- left_list, reduce: 0 do | ||
acc -> acc + id_value * Map.get(tally, id_value, 0) | ||
end | ||
end | ||
|
||
def parse(data \\ input()) do | ||
{left, right} = | ||
for line <- Transformers.lines(data), reduce: {[], []} do | ||
{left_list, right_list} -> | ||
[left, right] = String.split(line) | ||
{[String.to_integer(left) | left_list], [String.to_integer(right) | right_list]} | ||
end | ||
|
||
{Enum.sort(left), Enum.sort(right), Enum.frequencies(right)} | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule AdventOfCode.Y2024.Day05 do | ||
@moduledoc """ | ||
--- Day 5: Print Queue --- | ||
Problem Link: https://adventofcode.com/2024/day/5 | ||
Difficulty: xs | ||
Tags: set sort | ||
""" | ||
alias AdventOfCode.Helpers.{InputReader, Transformers} | ||
|
||
def input, do: InputReader.read_from_file(2024, 5) | ||
|
||
def run(input \\ input()) do | ||
input = parse(input) | ||
|
||
{run_1(input), run_2(input)} | ||
end | ||
|
||
defp run_1(input) do | ||
input | ||
|> Enum.filter(fn {a, b} -> a == b end) | ||
|> Enum.map(fn {a, _} -> a |> Enum.at(div(length(a), 2)) end) | ||
|> Enum.sum() | ||
end | ||
|
||
defp run_2(input) do | ||
input | ||
|> Enum.filter(fn {a, b} -> a != b end) | ||
|> Enum.map(fn {_, b} -> b |> Enum.at(div(length(b), 2)) end) | ||
|> Enum.sum() | ||
end | ||
|
||
def parse(data \\ input()) do | ||
[deps, updates] = Transformers.sections(data) | ||
given_sorted_pair({parse_deps(deps), parse_updates(updates)}) | ||
end | ||
|
||
defp parse_deps(deps) do | ||
for line <- Transformers.lines(deps), | ||
into: MapSet.new(), | ||
do: String.split(line, "|") |> Enum.map(&String.to_integer/1) |> List.to_tuple() | ||
end | ||
|
||
defp parse_updates(updates) do | ||
for line <- Transformers.lines(updates) do | ||
for update <- String.split(line, ","), do: String.to_integer(update) | ||
end | ||
end | ||
|
||
defp given_sorted_pair({deps, updates}) do | ||
for update <- updates do | ||
{update, Enum.sort(update, &({&1, &2} in deps))} | ||
end | ||
end | ||
end |
Oops, something went wrong.