Skip to content

Commit 1906b3c

Browse files
committed
feat(puzzles): Add spec and tests for 2022/day01 puzzle
1 parent ae84c43 commit 1906b3c

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Package day01 contains solution for https://adventofcode.com/2022/day/1 puzzle.
2+
package day01
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.Year2022.String()
18+
}
19+
20+
func (s solution) Day() string {
21+
return puzzles.Day01.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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package day01
2+
3+
import (
4+
"errors"
5+
"io"
6+
"strings"
7+
"testing"
8+
"testing/iotest"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func Test_solution_Year(t *testing.T) {
14+
var s solution
15+
16+
want := "2022"
17+
got := s.Year()
18+
19+
assert.Equal(t, want, got)
20+
}
21+
22+
func Test_solution_Day(t *testing.T) {
23+
var s solution
24+
25+
want := "1"
26+
got := s.Day()
27+
28+
assert.Equal(t, want, got)
29+
}
30+
31+
func Test_solution_Part1(t *testing.T) {
32+
var s solution
33+
34+
type args struct {
35+
input io.Reader
36+
}
37+
38+
tests := []struct {
39+
name string
40+
args args
41+
want string
42+
wantErr assert.ErrorAssertionFunc
43+
}{
44+
{
45+
name: "test example from description",
46+
args: args{
47+
input: strings.NewReader("1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000"),
48+
},
49+
want: "24000",
50+
wantErr: assert.NoError,
51+
},
52+
{
53+
name: "",
54+
args: args{
55+
input: iotest.ErrReader(errors.New("custom error")),
56+
},
57+
want: "",
58+
wantErr: assert.Error,
59+
},
60+
}
61+
62+
for _, tt := range tests {
63+
t.Run(tt.name, func(t *testing.T) {
64+
got, err := s.Part1(tt.args.input)
65+
if !tt.wantErr(t, err) {
66+
return
67+
}
68+
69+
assert.Equal(t, tt.want, got)
70+
})
71+
}
72+
}
73+
74+
func Test_solution_Part2(t *testing.T) {
75+
t.Skip("Not implemented")
76+
77+
var s solution
78+
79+
type args struct {
80+
input io.Reader
81+
}
82+
83+
tests := []struct {
84+
name string
85+
args args
86+
want string
87+
wantErr assert.ErrorAssertionFunc
88+
}{
89+
{},
90+
}
91+
92+
for _, tt := range tests {
93+
t.Run(tt.name, func(t *testing.T) {
94+
got, err := s.Part2(tt.args.input)
95+
if !tt.wantErr(t, err) {
96+
97+
}
98+
99+
assert.Equal(t, tt.want, got)
100+
})
101+
}
102+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# --- Day 1: Calorie Counting ---
2+
3+
## Part 1
4+
5+
Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to deliver presents on
6+
Christmas. For that, their favorite snack is a special type of star fruit that only grows deep in the jungle.
7+
The Elves have brought you on their annual expedition to the grove where the fruit grows.
8+
9+
To supply enough magical energy, the expedition needs to retrieve a minimum of fifty stars by December 25th.
10+
Although the Elves assure you that the grove has plenty of fruit, you decide to grab any fruit you see along the way,
11+
just in case.
12+
13+
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar;
14+
the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
15+
16+
The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves'
17+
expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies.
18+
One important consideration is food - in particular, the number of Calories each Elf is carrying (your puzzle input).
19+
20+
The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc.
21+
that they've brought with them, one item per line. Each Elf separates their own inventory from the previous
22+
Elf's inventory (if any) by a blank line.
23+
24+
For example, suppose the Elves finish writing their items' Calories and end up with the following list:
25+
26+
```text
27+
1000
28+
2000
29+
3000
30+
31+
4000
32+
33+
5000
34+
6000
35+
36+
7000
37+
8000
38+
9000
39+
40+
10000
41+
```
42+
43+
This list represents the Calories of the food carried by five Elves:
44+
45+
- The first Elf is carrying food with `1000`, `2000`, and `3000` Calories, a total of `6000` Calories.
46+
- The second Elf is carrying one food item with `4000` Calories.
47+
- The third Elf is carrying food with `5000` and `6000` Calories, a total of `11000` Calories.
48+
- The fourth Elf is carrying food with `7000`, `8000`, and `9000` Calories, a total of `24000` Calories.
49+
- The fifth Elf is carrying one food item with `10000` Calories.
50+
51+
In case the Elves get hungry and need extra snacks, they need to know which Elf to ask:
52+
they'd like to know how many Calories are being carried by the Elf carrying the most Calories.
53+
In the example above, this is `24000` `(carried by the fourth Elf)`.
54+
55+
Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?

0 commit comments

Comments
 (0)