From 4baa32d7911e1b230bfa49f3d3bc3e04bf0851ed Mon Sep 17 00:00:00 2001 From: Simon Rainerson Date: Wed, 13 Dec 2023 08:43:40 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Add=20solution=20to=20part=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- advent_of_code/lib/advent_of_code.ex | 1 + advent_of_code/lib/day_2.ex | 7 +++++++ advent_of_code/test/day2_test.exs | 11 +++++++++++ 3 files changed, 19 insertions(+) diff --git a/advent_of_code/lib/advent_of_code.ex b/advent_of_code/lib/advent_of_code.ex index 3c0dfd6..33537ff 100644 --- a/advent_of_code/lib/advent_of_code.ex +++ b/advent_of_code/lib/advent_of_code.ex @@ -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 diff --git a/advent_of_code/lib/day_2.ex b/advent_of_code/lib/day_2.ex index 121c5fc..812d052 100644 --- a/advent_of_code/lib/day_2.ex +++ b/advent_of_code/lib/day_2.ex @@ -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 diff --git a/advent_of_code/test/day2_test.exs b/advent_of_code/test/day2_test.exs index c802f24..5f803ec 100644 --- a/advent_of_code/test/day2_test.exs +++ b/advent_of_code/test/day2_test.exs @@ -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