Skip to content

Commit 90c20a6

Browse files
committed
Refactor into small packages
1 parent 4f5fc70 commit 90c20a6

File tree

9 files changed

+80
-57
lines changed

9 files changed

+80
-57
lines changed

cmd/day1/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package main
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/gdejong/advent-of-code-2024/internal"
6+
"github.com/gdejong/advent-of-code-2024/internal/must"
77
"io"
88
"os"
99
"slices"
1010
)
1111

1212
func main() {
13-
f := internal.Must(os.Open("cmd/day1/real_input.txt"))
13+
f := must.NoError(os.Open("cmd/day1/real_input.txt"))
1414

1515
part1answer := part1(f)
1616
fmt.Println(part1answer)
1717

1818
// Reset the file so we can it again for part 2
19-
internal.Must(f.Seek(0, io.SeekStart))
19+
must.NoError(f.Seek(0, io.SeekStart))
2020

2121
part2answer := part2(f)
2222
fmt.Println(part2answer)
@@ -33,7 +33,7 @@ func part2(f *os.File) int {
3333

3434
var leftNumber int
3535
var rightNumber int
36-
internal.Must(fmt.Sscanf(line, "%d %d", &leftNumber, &rightNumber))
36+
must.NoError(fmt.Sscanf(line, "%d %d", &leftNumber, &rightNumber))
3737
leftList = append(leftList, leftNumber)
3838
rightList = append(rightList, rightNumber)
3939
}
@@ -72,7 +72,7 @@ func part1(f *os.File) int {
7272

7373
var leftNumber int
7474
var rightNumber int
75-
internal.Must(fmt.Sscanf(line, "%d %d", &leftNumber, &rightNumber))
75+
must.NoError(fmt.Sscanf(line, "%d %d", &leftNumber, &rightNumber))
7676
leftList = append(leftList, leftNumber)
7777
rightList = append(rightList, rightNumber)
7878
}

cmd/day1/main_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package main
22

33
import (
4-
"github.com/gdejong/advent-of-code-2024/internal"
4+
"github.com/gdejong/advent-of-code-2024/internal/must"
55
"os"
66
"testing"
77
)
88

99
func TestPart1(t *testing.T) {
10-
f := internal.Must(os.Open("test_input.txt"))
10+
f := must.NoError(os.Open("test_input.txt"))
1111

1212
answer := part1(f)
1313

@@ -17,7 +17,7 @@ func TestPart1(t *testing.T) {
1717
}
1818

1919
func TestPart2(t *testing.T) {
20-
f := internal.Must(os.Open("test_input.txt"))
20+
f := must.NoError(os.Open("test_input.txt"))
2121

2222
answer := part2(f)
2323

cmd/day2/main.go

+23-37
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package main
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/gdejong/advent-of-code-2024/internal"
6+
"github.com/gdejong/advent-of-code-2024/internal/input"
7+
"github.com/gdejong/advent-of-code-2024/internal/math"
8+
"github.com/gdejong/advent-of-code-2024/internal/must"
9+
"github.com/gdejong/advent-of-code-2024/internal/slices"
710
"os"
8-
"strconv"
911
"strings"
1012
)
1113

1214
func main() {
13-
f := internal.Must(os.Open("cmd/day2/real_input.txt"))
15+
f := must.NoError(os.Open("cmd/day2/real_input.txt"))
1416

1517
fmt.Println(part1(f))
1618
}
@@ -22,50 +24,34 @@ func part1(f *os.File) int {
2224
for s.Scan() {
2325
line := s.Text()
2426

25-
fields := strings.Fields(line)
26-
levels := make([]int, len(fields))
27+
levels := input.ToIntegers(strings.Fields(line))
2728

28-
// Convert to integer values
29-
for k, v := range fields {
30-
levels[k] = internal.Must(strconv.Atoi(v))
31-
}
32-
33-
// Calculate the differences
34-
diffs := make([]int, len(levels)-1)
35-
for index := range len(levels) - 1 {
36-
diffs[index] = levels[index+1] - levels[index]
37-
}
38-
39-
allIncreasing := func(val int) bool { return val > 0 }
40-
allDecreasing := func(val int) bool { return val < 0 }
41-
allBetweenOneAndThree := func(val int) bool { return Abs(val) >= 1 && Abs(val) <= 3 }
42-
43-
isAllIncreasing := All(diffs, allIncreasing)
44-
isAllDecreasing := All(diffs, allDecreasing)
45-
IsAllDifferByAtLeastOneAndAtMostThree := All(diffs, allBetweenOneAndThree)
46-
47-
if (isAllIncreasing || isAllDecreasing) && IsAllDifferByAtLeastOneAndAtMostThree {
29+
if isSafeReport(levels) {
4830
safeCounter++
4931
}
50-
5132
}
5233

5334
return safeCounter
5435
}
5536

56-
func Abs(value int) int {
57-
if value < 0 {
58-
value = value * -1
37+
func isSafeReport(levels []int) bool {
38+
// Calculate the differences
39+
diffs := make([]int, len(levels)-1)
40+
for index := range len(levels) - 1 {
41+
diffs[index] = levels[index+1] - levels[index]
5942
}
6043

61-
return value
62-
}
44+
allIncreasing := func(val int) bool { return val > 0 }
45+
allDecreasing := func(val int) bool { return val < 0 }
46+
allBetweenOneAndThree := func(val int) bool { return math.Abs(val) >= 1 && math.Abs(val) <= 3 }
6347

64-
func All[T any](ts []T, pred func(T) bool) bool {
65-
for _, t := range ts {
66-
if !pred(t) {
67-
return false
68-
}
48+
isAllIncreasing := slices.All(diffs, allIncreasing)
49+
isAllDecreasing := slices.All(diffs, allDecreasing)
50+
IsAllDifferByAtLeastOneAndAtMostThree := slices.All(diffs, allBetweenOneAndThree)
51+
52+
if (isAllIncreasing || isAllDecreasing) && IsAllDifferByAtLeastOneAndAtMostThree {
53+
return true
6954
}
70-
return true
55+
56+
return false
7157
}

cmd/day2/main_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package main
22

33
import (
4-
"github.com/gdejong/advent-of-code-2024/internal"
4+
"github.com/gdejong/advent-of-code-2024/internal/must"
55
"os"
66
"testing"
77
)
88

99
func TestPart1(t *testing.T) {
10-
f := internal.Must(os.Open("test_input.txt"))
10+
f := must.NoError(os.Open("test_input.txt"))
1111

1212
answer := part1(f)
1313

internal/input/input.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package input
2+
3+
import (
4+
"github.com/gdejong/advent-of-code-2024/internal/must"
5+
"strconv"
6+
)
7+
8+
func ToIntegers(input []string) []int {
9+
integers := make([]int, len(input))
10+
11+
// Convert to integer values
12+
for k, v := range input {
13+
integers[k] = must.NoError(strconv.Atoi(v))
14+
}
15+
16+
return integers
17+
}

internal/math/math.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package math
2+
3+
func Abs(value int) int {
4+
if value < 0 {
5+
value = value * -1
6+
}
7+
8+
return value
9+
}

internal/must.go

-10
This file was deleted.

internal/must/must.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package must
2+
3+
// NoError takes in some value and an error, and panics if the error is not nil.
4+
func NoError[t any](something t, err error) t {
5+
if err != nil {
6+
panic(err)
7+
}
8+
9+
return something
10+
}

internal/slices/slices.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package slices
2+
3+
// All accepts a slice and a predicate function. It will return true if the predicate is true for all slice elements.
4+
func All[T any](ts []T, pred func(T) bool) bool {
5+
for _, t := range ts {
6+
if !pred(t) {
7+
return false
8+
}
9+
}
10+
return true
11+
}

0 commit comments

Comments
 (0)