Skip to content

Commit

Permalink
⚡ Add solution to part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrainerson committed Dec 13, 2023
1 parent 78de9f4 commit 4baa32d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions advent_of_code/lib/advent_of_code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ defmodule AdventOfCode do
print(1, 1, Day1.part1())
print(1, 2, Day1.part2())
print(2, 1, Day2.part1())
print(2, 2, Day2.part2())
end
end
7 changes: 7 additions & 0 deletions advent_of_code/lib/day_2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ defmodule Day2 do
|> Stream.map(&Map.get(&1, :id))
|> Enum.sum()
end

def part2(stream \\ Inputs.stream(2)) do
stream
|> Stream.map(&Game.parse_str/1)
|> Stream.map(&(Map.get(&1, :red) * Map.get(&1, :green) * Map.get(&1, :blue)))
|> Enum.sum()
end
end
11 changes: 11 additions & 0 deletions advent_of_code/test/day2_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,16 @@ defmodule Day2Test do
"Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green"
])
end

test "part2/1" do
2286 =
Day2.part2([
"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"
])
end
end
end

0 comments on commit 4baa32d

Please sign in to comment.