Skip to content

Commit 03111b3

Browse files
committed
feat: Generate boilerplate for 2024/day02
1 parent 7aee4c9 commit 03111b3

File tree

6 files changed

+209
-1
lines changed

6 files changed

+209
-1
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/2024/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.Year2024.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: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 := "2024"
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: "2",
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+
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+
name: "",
91+
args: args{
92+
input: utils.ReaderFromFile(t, filepath.Join("testdata", "input.txt")),
93+
},
94+
want: "",
95+
wantErr: assert.NoError,
96+
},
97+
{
98+
name: "",
99+
args: args{
100+
input: iotest.ErrReader(errors.New("custom error")),
101+
},
102+
want: "",
103+
wantErr: assert.Error,
104+
},
105+
}
106+
107+
for _, tt := range tests {
108+
t.Run(tt.name, func(t *testing.T) {
109+
got, err := s.Part2(tt.args.input)
110+
if !tt.wantErr(t, err) {
111+
return
112+
}
113+
114+
assert.Equal(t, tt.want, got)
115+
})
116+
}
117+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Puzzle https://adventofcode.com/2024/day/2
2+
3+
# --- Day 2: Red-Nosed Reports ---
4+
5+
## --- Part One ---
6+
7+
Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
8+
9+
While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian,
10+
the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved
11+
through molecular synthesis from a single electron.
12+
13+
They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data
14+
from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already
15+
divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
16+
17+
The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers
18+
called levels that are separated by spaces. For example:
19+
20+
```text
21+
7 6 4 2 1
22+
1 2 7 8 9
23+
9 7 6 2 1
24+
1 3 2 4 5
25+
8 6 4 4 1
26+
1 3 6 7 9
27+
```
28+
29+
This example data contains six reports each containing five levels.
30+
31+
The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate
32+
levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the
33+
following are true:
34+
35+
The levels are either all increasing or all decreasing.
36+
Any two adjacent levels differ by at least one and at most three.
37+
In the example above, the reports can be found safe or unsafe by checking those rules:
38+
39+
- `7 6 4 2 1`: Safe because the levels are all decreasing by 1 or 2.
40+
- `1 2 7 8 9`: Unsafe because 2 7 is an increase of 5.
41+
- `9 7 6 2 1`: Unsafe because 6 2 is a decrease of 4.
42+
- `1 3 2 4 5`: Unsafe because 1 3 is increasing but 3 2 is decreasing.
43+
- `8 6 4 4 1`: Unsafe because 4 4 is neither an increase or a decrease.
44+
- `1 3 6 7 9`: Safe because the levels are all increasing by 1, 2, or 3.
45+
-
46+
So, in this example, 2 reports are safe.
47+
48+
Analyze the unusual data from the engineers. How many reports are safe?
49+
50+
## --- Part Two ---
51+
52+
<!--- Pass here the description for part two --->
53+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
7 6 4 2 1
2+
1 2 7 8 9
3+
9 7 6 2 1
4+
1 3 2 4 5
5+
8 6 4 4 1
6+
1 3 6 7 9

internal/puzzles/solutions/register_2024.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/2024/day01"
9+
// register day02 solution.
10+
_ "github.com/obalunenko/advent-of-code/internal/puzzles/solutions/2024/day02"
911
)

internal/puzzles/solutions/templates/solution_test.go.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func Test_solution_Part1(t *testing.T) {
4848
args: args{
4949
input: utils.ReaderFromFile(t, filepath.Join("testdata", "input.txt")),
5050
},
51-
want: "8",
51+
want: "",
5252
wantErr: assert.NoError,
5353
},
5454
{

0 commit comments

Comments
 (0)