Skip to content

Commit b394ee2

Browse files
committed
feat: Add 2024/day01 initial code
1 parent 163fd32 commit b394ee2

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ SHELL := env APP_NAME=$(APP_NAME) $(SHELL)
88

99
SHELL := env GOTOOLS_IMAGE_TAG=$(GOTOOLS_IMAGE_TAG) $(SHELL)
1010

11+
AOC_PUZZLE_URL=
12+
SHELL := env AOC_PUZZLE_URL=$(AOC_PUZZLE_URL) $(SHELL)
13+
1114
COMPOSE_TOOLS_FILE=deployments/docker-compose/go-tools-docker-compose.yml
1215
COMPOSE_TOOLS_CMD_BASE=docker compose -f $(COMPOSE_TOOLS_FILE)
1316
COMPOSE_TOOLS_CMD_UP=$(COMPOSE_TOOLS_CMD_BASE) up --exit-code-from
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/2024/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.Year2024.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: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package day01
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 := "1"
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: "11",
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Puzzle https://adventofcode.com/2024/day/1
2+
3+
# --- Day 1: Historian Hysteria ---
4+
5+
## --- Part One ---
6+
7+
The Chief Historian is always present for the big Christmas sleigh launch, but nobody has seen him in months!
8+
Last anyone heard, he was visiting locations that are historically significant to the North Pole;
9+
a group of Senior Historians has asked you to accompany them as they check the places they think he was most likely to visit.
10+
11+
As each location is checked, they will mark it on their list with a star. They figure the Chief Historian must be
12+
in one of the first fifty places they'll look, so in order to save Christmas, you need to help them get fifty stars on
13+
their list before Santa takes off on December 25th.
14+
15+
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar;
16+
the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
17+
18+
You haven't even left yet and the group of Elvish Senior Historians has already hit a problem: their list of locations
19+
to check is currently empty. Eventually, someone decides that the best place to check first would be the
20+
Chief Historian's office.
21+
22+
Upon pouring into the office, everyone confirms that the Chief Historian is indeed nowhere to be found.
23+
Instead, the Elves discover an assortment of notes and lists of historically significant locations! T
24+
his seems to be the planning the Chief Historian was doing before he left.
25+
Perhaps these notes can be used to determine which locations to search?
26+
27+
Throughout the Chief's office, the historically significant locations are listed not by name but by a unique number
28+
called the location ID. To make sure they don't miss anything, The Historians split into two groups,
29+
each searching the office and trying to create their own complete list of location IDs.
30+
31+
There's just one problem: by holding the two lists up side by side (your puzzle input), it quickly becomes clear that
32+
the lists aren't very similar. Maybe you can help The Historians reconcile their lists?
33+
34+
For example:
35+
36+
```text
37+
3 4
38+
4 3
39+
2 5
40+
1 3
41+
3 9
42+
3 3
43+
```
44+
45+
Maybe the lists are only off by a small amount! To find out, pair up the numbers and measure how far apart they are.
46+
Pair up the smallest number in the left list with the smallest number in the right list, then the second-smallest left
47+
number with the second-smallest right number, and so on.
48+
49+
Within each pair, figure out how far apart the two numbers are; you'll need to add up all of those distances.
50+
For example, if you pair up a 3 from the left list with a 7 from the right list, the distance apart is 4; if you pair
51+
up a 9 with a 3, the distance apart is 6.
52+
53+
In the example list above, the pairs and distances would be as follows:
54+
55+
- The smallest number in the left list is 1, and the smallest number in the right list is 3. The distance between them is 2.
56+
- The second-smallest number in the left list is 2, and the second-smallest number in the right list is another 3. The distance between them is 1.
57+
- The third-smallest number in both lists is 3, so the distance between them is 0.
58+
- The next numbers to pair up are 3 and 4, a distance of 1.
59+
- The fifth-smallest numbers in each list are 3 and 5, a distance of 2.
60+
- Finally, the largest number in the left list is 4, while the largest number in the right list is 9; these are a distance 5 apart.
61+
62+
To find the total distance between the left list and the right list, add up the distances between all of the pairs you
63+
found. In the example above, this is `2 + 1 + 0 + 1 + 2 + 5`, a total distance of `11`!
64+
65+
Your actual left and right lists contain many location IDs. What is the total distance between your lists?
66+
67+
## --- Part Two ---
68+
69+
<!--- Pass here the description for part two --->
70+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
3 4
2+
4 3
3+
2 5
4+
1 3
5+
3 9
6+
3 3
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package solutions
2+
3+
import (
4+
/*
5+
2024 solutions.
6+
*/
7+
// register day01 solution.
8+
_ "github.com/obalunenko/advent-of-code/internal/puzzles/solutions/2024/day01"
9+
)

0 commit comments

Comments
 (0)