Skip to content

Commit

Permalink
day 2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
krystiangryczon committed Dec 2, 2023
1 parent 391cef1 commit 7d01761
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 8 deletions.
6 changes: 4 additions & 2 deletions lib/advent_of_code/day_02.ex
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
65 changes: 65 additions & 0 deletions lib/code/day2_1.ex
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
32 changes: 32 additions & 0 deletions lib/code/day2_2.ex
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
28 changes: 22 additions & 6 deletions test/advent_of_code/day_02_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@ defmodule AdventOfCode.Day02Test do

import AdventOfCode.Day02

@tag :skip
defp test_case1,
do: """
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
"""

test "part1" do
input = nil
input = test_case1()
result = part1(input)

assert result
assert result == 8
end

@tag :skip
defp test_case2,
do: """
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 gree
"""

test "part2" do
input = nil
input = test_case2()
result = part2(input)

assert result
assert result == 2286
end
end

0 comments on commit 7d01761

Please sign in to comment.