Skip to content

Commit 5433ffa

Browse files
committed
feat: Add 2023 day02 puzzle spec and init implementation
1 parent b957234 commit 5433ffa

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Package day02 contains solution for https://adventofcode.com/2023/day/2 puzzle.
2+
package day02
3+
4+
import (
5+
"io"
6+
7+
"github.com/obalunenko/advent-of-code/internal/puzzles"
8+
)
9+
10+
func init() {
11+
puzzles.Register(solution{})
12+
}
13+
14+
type solution struct{}
15+
16+
func (s solution) Year() string {
17+
return puzzles.Year2023.String()
18+
}
19+
20+
func (s solution) Day() string {
21+
return puzzles.Day02.String()
22+
}
23+
24+
func (s solution) Part1(input io.Reader) (string, error) {
25+
return "", puzzles.ErrNotImplemented
26+
}
27+
28+
func (s solution) Part2(input io.Reader) (string, error) {
29+
return "", puzzles.ErrNotImplemented
30+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package day02
2+
3+
import (
4+
"errors"
5+
"io"
6+
"path/filepath"
7+
"testing"
8+
"testing/iotest"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/obalunenko/advent-of-code/internal/puzzles/common/utils"
13+
)
14+
15+
func Test_solution_Year(t *testing.T) {
16+
var s solution
17+
18+
want := "2023"
19+
got := s.Year()
20+
21+
assert.Equal(t, want, got)
22+
}
23+
24+
func Test_solution_Day(t *testing.T) {
25+
var s solution
26+
27+
want := "2"
28+
got := s.Day()
29+
30+
assert.Equal(t, want, got)
31+
}
32+
33+
func Test_solution_Part1(t *testing.T) {
34+
var s solution
35+
36+
type args struct {
37+
input io.Reader
38+
}
39+
40+
tests := []struct {
41+
name string
42+
args args
43+
want string
44+
wantErr assert.ErrorAssertionFunc
45+
}{
46+
{
47+
name: "test example from description",
48+
args: args{
49+
input: utils.ReaderFromFile(t, filepath.Join("testdata", "input.txt")),
50+
},
51+
want: "8",
52+
wantErr: assert.NoError,
53+
},
54+
{
55+
name: "",
56+
args: args{
57+
input: iotest.ErrReader(errors.New("custom error")),
58+
},
59+
want: "",
60+
wantErr: assert.Error,
61+
},
62+
}
63+
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
got, err := s.Part1(tt.args.input)
67+
if !tt.wantErr(t, err) {
68+
return
69+
}
70+
71+
assert.Equal(t, tt.want, got)
72+
})
73+
}
74+
}
75+
76+
func Test_solution_Part2(t *testing.T) {
77+
t.Skip("not implemented yet")
78+
79+
var s solution
80+
81+
type args struct {
82+
input io.Reader
83+
}
84+
85+
tests := []struct {
86+
name string
87+
args args
88+
want string
89+
wantErr assert.ErrorAssertionFunc
90+
}{
91+
{
92+
name: "",
93+
args: args{
94+
input: utils.ReaderFromFile(t, filepath.Join("testdata", "input.txt")),
95+
},
96+
want: "",
97+
wantErr: assert.NoError,
98+
},
99+
{
100+
name: "",
101+
args: args{
102+
input: iotest.ErrReader(errors.New("custom error")),
103+
},
104+
want: "",
105+
wantErr: assert.Error,
106+
},
107+
}
108+
109+
for _, tt := range tests {
110+
t.Run(tt.name, func(t *testing.T) {
111+
got, err := s.Part2(tt.args.input)
112+
if !tt.wantErr(t, err) {
113+
return
114+
}
115+
116+
assert.Equal(t, tt.want, got)
117+
})
118+
}
119+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# --- Day 2: Cube Conundrum ---
2+
3+
## Part One
4+
5+
6+
You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large
7+
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.
8+
An Elf runs over to greet you.
9+
10+
The Elf explains that you've arrived at Snow Island and apologizes for the lack of snow. He'll be happy to explain
11+
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
12+
to play a game in the meantime?
13+
14+
As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue.
15+
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
16+
figure out information about the number of cubes.
17+
18+
To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random
19+
cubes, show them to you, and then put them back in the bag. He'll do this a few times per game.
20+
21+
You play several games and record the information from each game (your puzzle input). Each game is listed with its
22+
ID number (like the `11` in `Game 11: ...`) followed by a semicolon-separated list of subsets of cubes that were revealed
23+
from the bag (like `3 red`, `5 green`, `4 blue`).
24+
25+
For example, the record of a few games might look like this:
26+
27+
```text
28+
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
29+
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
30+
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
31+
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
32+
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
33+
```
34+
35+
In game 1, three sets of cubes are revealed from the bag (and then put back again).
36+
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;
37+
the third set is only 2 green cubes.
38+
39+
40+
The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes,
41+
13 green cubes, and 14 blue cubes?
42+
43+
In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration.
44+
However, game 3 would have been impossible because at one point the Elf showed you 20 red cubes at once; similarly,
45+
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
46+
games that would have been possible, you get 8.
47+
48+
Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes,
49+
and 14 blue cubes. What is the sum of the IDs of those games?
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
2+
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
3+
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
4+
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
5+
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

internal/puzzles/solutions/register_2023.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ import (
66
*/
77
// register day01 solution.
88
_ "github.com/obalunenko/advent-of-code/internal/puzzles/solutions/2023/day01"
9+
// register day02 solution.
10+
_ "github.com/obalunenko/advent-of-code/internal/puzzles/solutions/2023/day02"
911
)

0 commit comments

Comments
 (0)