From 5433ffa9c44ae7b4628e0129a19b2345e83cb588 Mon Sep 17 00:00:00 2001 From: Oleg Balunenko Date: Sat, 2 Dec 2023 22:51:10 +0400 Subject: [PATCH] feat: Add 2023 day02 puzzle spec and init implementation --- .../puzzles/solutions/2023/day02/solution.go | 30 +++++ .../solutions/2023/day02/solution_test.go | 119 ++++++++++++++++++ internal/puzzles/solutions/2023/day02/spec.md | 49 ++++++++ .../solutions/2023/day02/testdata/input.txt | 5 + internal/puzzles/solutions/register_2023.go | 2 + 5 files changed, 205 insertions(+) create mode 100644 internal/puzzles/solutions/2023/day02/solution.go create mode 100644 internal/puzzles/solutions/2023/day02/solution_test.go create mode 100644 internal/puzzles/solutions/2023/day02/spec.md create mode 100644 internal/puzzles/solutions/2023/day02/testdata/input.txt diff --git a/internal/puzzles/solutions/2023/day02/solution.go b/internal/puzzles/solutions/2023/day02/solution.go new file mode 100644 index 00000000..7c02158b --- /dev/null +++ b/internal/puzzles/solutions/2023/day02/solution.go @@ -0,0 +1,30 @@ +// Package day02 contains solution for https://adventofcode.com/2023/day/2 puzzle. +package day02 + +import ( + "io" + + "github.com/obalunenko/advent-of-code/internal/puzzles" +) + +func init() { + puzzles.Register(solution{}) +} + +type solution struct{} + +func (s solution) Year() string { + return puzzles.Year2023.String() +} + +func (s solution) Day() string { + return puzzles.Day02.String() +} + +func (s solution) Part1(input io.Reader) (string, error) { + return "", puzzles.ErrNotImplemented +} + +func (s solution) Part2(input io.Reader) (string, error) { + return "", puzzles.ErrNotImplemented +} diff --git a/internal/puzzles/solutions/2023/day02/solution_test.go b/internal/puzzles/solutions/2023/day02/solution_test.go new file mode 100644 index 00000000..f9a55b42 --- /dev/null +++ b/internal/puzzles/solutions/2023/day02/solution_test.go @@ -0,0 +1,119 @@ +package day02 + +import ( + "errors" + "io" + "path/filepath" + "testing" + "testing/iotest" + + "github.com/stretchr/testify/assert" + + "github.com/obalunenko/advent-of-code/internal/puzzles/common/utils" +) + +func Test_solution_Year(t *testing.T) { + var s solution + + want := "2023" + got := s.Year() + + assert.Equal(t, want, got) +} + +func Test_solution_Day(t *testing.T) { + var s solution + + want := "2" + got := s.Day() + + assert.Equal(t, want, got) +} + +func Test_solution_Part1(t *testing.T) { + var s solution + + type args struct { + input io.Reader + } + + tests := []struct { + name string + args args + want string + wantErr assert.ErrorAssertionFunc + }{ + { + name: "test example from description", + args: args{ + input: utils.ReaderFromFile(t, filepath.Join("testdata", "input.txt")), + }, + want: "8", + wantErr: assert.NoError, + }, + { + name: "", + args: args{ + input: iotest.ErrReader(errors.New("custom error")), + }, + want: "", + wantErr: assert.Error, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := s.Part1(tt.args.input) + if !tt.wantErr(t, err) { + return + } + + assert.Equal(t, tt.want, got) + }) + } +} + +func Test_solution_Part2(t *testing.T) { + t.Skip("not implemented yet") + + var s solution + + type args struct { + input io.Reader + } + + tests := []struct { + name string + args args + want string + wantErr assert.ErrorAssertionFunc + }{ + { + name: "", + args: args{ + input: utils.ReaderFromFile(t, filepath.Join("testdata", "input.txt")), + }, + want: "", + wantErr: assert.NoError, + }, + { + name: "", + args: args{ + input: iotest.ErrReader(errors.New("custom error")), + }, + want: "", + wantErr: assert.Error, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := s.Part2(tt.args.input) + if !tt.wantErr(t, err) { + return + } + + assert.Equal(t, tt.want, got) + }) + } +} diff --git a/internal/puzzles/solutions/2023/day02/spec.md b/internal/puzzles/solutions/2023/day02/spec.md new file mode 100644 index 00000000..96415abc --- /dev/null +++ b/internal/puzzles/solutions/2023/day02/spec.md @@ -0,0 +1,49 @@ +# --- Day 2: Cube Conundrum --- + +## Part One + + +You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large +island floating in the sky. You gently land in a fluffy pile of leaves. It's quite cold, but you don't see much snow. +An Elf runs over to greet you. + +The Elf explains that you've arrived at Snow Island and apologizes for the lack of snow. He'll be happy to explain +the situation, but it's a bit of a walk, so you have some time. They don't get many visitors up here; would you like +to play a game in the meantime? + +As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue. +Each time you play this game, he will hide a secret number of cubes of each color in the bag, and your goal is to +figure out information about the number of cubes. + +To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random +cubes, show them to you, and then put them back in the bag. He'll do this a few times per game. + +You play several games and record the information from each game (your puzzle input). Each game is listed with its +ID number (like the `11` in `Game 11: ...`) followed by a semicolon-separated list of subsets of cubes that were revealed +from the bag (like `3 red`, `5 green`, `4 blue`). + +For example, the record of a few games might look like this: + +```text +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 +``` + +In game 1, three sets of cubes are revealed from the bag (and then put back again). +The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green cubes, and 6 blue cubes; +the third set is only 2 green cubes. + + +The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, +13 green cubes, and 14 blue cubes? + +In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration. +However, game 3 would have been impossible because at one point the Elf showed you 20 red cubes at once; similarly, +game 4 would also have been impossible because the Elf showed you 15 blue cubes at once. If you add up the IDs of the +games that would have been possible, you get 8. + +Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, +and 14 blue cubes. What is the sum of the IDs of those games? \ No newline at end of file diff --git a/internal/puzzles/solutions/2023/day02/testdata/input.txt b/internal/puzzles/solutions/2023/day02/testdata/input.txt new file mode 100644 index 00000000..1cd7d331 --- /dev/null +++ b/internal/puzzles/solutions/2023/day02/testdata/input.txt @@ -0,0 +1,5 @@ +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 \ No newline at end of file diff --git a/internal/puzzles/solutions/register_2023.go b/internal/puzzles/solutions/register_2023.go index 8fa0e3d0..3dec92d9 100644 --- a/internal/puzzles/solutions/register_2023.go +++ b/internal/puzzles/solutions/register_2023.go @@ -6,4 +6,6 @@ import ( */ // register day01 solution. _ "github.com/obalunenko/advent-of-code/internal/puzzles/solutions/2023/day01" + // register day02 solution. + _ "github.com/obalunenko/advent-of-code/internal/puzzles/solutions/2023/day02" )