generated from mhanberg/advent-of-code-elixir-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
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
391cef1
commit 7d01761
Showing
4 changed files
with
123 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
defmodule AdventOfCode.Day02 do | ||
def part1(_args) do | ||
def part1(input) do | ||
Advent.Code2_1.sum(My.Utils.split_file(input)) | ||
end | ||
|
||
def part2(_args) do | ||
def part2(input) do | ||
Advent.Code2_2.sum(My.Utils.split_file(input)) | ||
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,65 @@ | ||
defmodule Advent.Code2_1 do | ||
def conditions(), | ||
do: %{ | ||
red: 12, | ||
green: 13, | ||
blue: 14 | ||
} | ||
|
||
@spec is_possible(String.t(), String.t(), any()) :: integer() | ||
def is_possible(game, color, color_symbol) do | ||
[_, data] = String.split(game, ":") | ||
draws = String.split(data, ";") | ||
|
||
possible = | ||
draws | ||
|> Enum.map(fn draw -> Regex.run(~r/\d+ #{color}/, draw) end) | ||
|> Enum.filter(fn el -> el != nil end) | ||
|> Enum.flat_map(fn el -> el end) | ||
|> Enum.map(fn el -> | ||
case String.split(el, " ", parts: 2) do | ||
[num, _] -> | ||
{_, maximum} = Map.fetch(conditions(), color_symbol) | ||
{real_num, _} = Integer.parse(num) | ||
|
||
if(maximum >= real_num) do | ||
true | ||
else | ||
false | ||
end | ||
|
||
[] -> | ||
false | ||
end | ||
end) | ||
|> Enum.filter(fn el -> el != true end) | ||
|> length() == 0 | ||
|
||
if(possible) do | ||
1 | ||
else | ||
0 | ||
end | ||
end | ||
|
||
def extract_id(game) do | ||
[game_id, _] = String.split(game, ":") | ||
[_, id] = String.split(game_id, " ") | ||
{result, _} = Integer.parse(id) | ||
result | ||
end | ||
|
||
def sum(str_list) do | ||
Enum.reduce(str_list, 0, fn el, acc -> | ||
acc + | ||
case is_possible(el, "red", :red) + is_possible(el, "green", :green) + | ||
is_possible(el, "blue", :blue) do | ||
3 -> | ||
extract_id(el) | ||
|
||
_ -> | ||
0 | ||
end | ||
end) | ||
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,32 @@ | ||
defmodule Advent.Code2_2 do | ||
def get_max_balls(game, color) do | ||
[_, data] = String.split(game, ":") | ||
draws = String.split(data, ";") | ||
|
||
draws | ||
|> Enum.map(fn draw -> Regex.run(~r/\d+ #{color}/, draw) end) | ||
|> Enum.filter(fn el -> el != nil end) | ||
|> Enum.flat_map(fn el -> el end) | ||
|> Enum.map(fn el -> | ||
[num, _] = String.split(el) | ||
{result, _} = Integer.parse(num, 10) | ||
result | ||
end) | ||
|> Enum.max() | ||
end | ||
|
||
def extract_id(game) do | ||
[game_id] = String.split(game, ":") | ||
[_, id] = String.split(game_id, " ") | ||
{result, _} = Integer.parse(id) | ||
result | ||
end | ||
|
||
def sum(str_list) do | ||
Enum.reduce(str_list, 0, fn el, acc -> | ||
acc + | ||
get_max_balls(el, "red") * get_max_balls(el, "green") * | ||
get_max_balls(el, "blue") | ||
end) | ||
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