Skip to content

Commit ec24973

Browse files
committed
Refactor input parsing
1 parent 314dac2 commit ec24973

File tree

7 files changed

+80
-91
lines changed

7 files changed

+80
-91
lines changed

cmd/day1/main.go

+35-50
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,85 @@
11
package main
22

33
import (
4-
"bufio"
54
"fmt"
5+
"github.com/gdejong/advent-of-code-2024/internal/input"
66
"github.com/gdejong/advent-of-code-2024/internal/must"
7-
"io"
8-
"os"
97
"slices"
108
)
119

1210
func main() {
13-
f := must.NoError(os.Open("cmd/day1/real_input.txt"))
11+
lines := input.Content("cmd/day1/real_input.txt")
1412

15-
part1answer := part1(f)
16-
fmt.Println(part1answer)
13+
fmt.Println(part1(lines))
1714

18-
// Reset the file so we can it again for part 2
19-
must.NoError(f.Seek(0, io.SeekStart))
20-
21-
part2answer := part2(f)
22-
fmt.Println(part2answer)
15+
fmt.Println(part2(lines))
2316
}
2417

25-
func part2(f *os.File) int {
26-
s := bufio.NewScanner(f)
27-
18+
func part1(lines []string) int {
2819
var leftList []int
2920
var rightList []int
3021

31-
for s.Scan() {
32-
line := s.Text()
33-
22+
for _, line := range lines {
3423
var leftNumber int
3524
var rightNumber int
3625
must.NoError(fmt.Sscanf(line, "%d %d", &leftNumber, &rightNumber))
3726
leftList = append(leftList, leftNumber)
3827
rightList = append(rightList, rightNumber)
3928
}
4029

41-
counterMap := make(map[int]int)
30+
slices.Sort(leftList)
31+
slices.Sort(rightList)
4232

43-
for _, value := range rightList {
44-
if _, ok := counterMap[value]; ok {
45-
counterMap[value]++
46-
} else {
47-
counterMap[value] = 1
48-
}
33+
if len(leftList) != len(rightList) {
34+
panic("expected same length")
4935
}
5036

51-
similarityScore := 0
52-
for _, value := range leftList {
53-
counterMapValue, ok := counterMap[value]
54-
if !ok {
55-
continue
37+
distanceSum := 0
38+
for i := 0; i < len(leftList); i++ {
39+
distance := leftList[i] - rightList[i]
40+
41+
// Take the absolute value
42+
if distance < 0 {
43+
distance = distance * -1
5644
}
5745

58-
similarityScore += value * counterMapValue
46+
distanceSum += distance
5947
}
6048

61-
return similarityScore
49+
return distanceSum
6250
}
6351

64-
func part1(f *os.File) int {
65-
s := bufio.NewScanner(f)
66-
52+
func part2(lines []string) int {
6753
var leftList []int
6854
var rightList []int
6955

70-
for s.Scan() {
71-
line := s.Text()
72-
56+
for _, line := range lines {
7357
var leftNumber int
7458
var rightNumber int
7559
must.NoError(fmt.Sscanf(line, "%d %d", &leftNumber, &rightNumber))
7660
leftList = append(leftList, leftNumber)
7761
rightList = append(rightList, rightNumber)
7862
}
7963

80-
slices.Sort(leftList)
81-
slices.Sort(rightList)
64+
counterMap := make(map[int]int)
8265

83-
if len(leftList) != len(rightList) {
84-
panic("expected same length")
66+
for _, value := range rightList {
67+
if _, ok := counterMap[value]; ok {
68+
counterMap[value]++
69+
} else {
70+
counterMap[value] = 1
71+
}
8572
}
8673

87-
distanceSum := 0
88-
for i := 0; i < len(leftList); i++ {
89-
distance := leftList[i] - rightList[i]
90-
91-
// Take the absolute value
92-
if distance < 0 {
93-
distance = distance * -1
74+
similarityScore := 0
75+
for _, value := range leftList {
76+
counterMapValue, ok := counterMap[value]
77+
if !ok {
78+
continue
9479
}
9580

96-
distanceSum += distance
81+
similarityScore += value * counterMapValue
9782
}
9883

99-
return distanceSum
84+
return similarityScore
10085
}

cmd/day1/main_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package main
22

33
import (
4-
"github.com/gdejong/advent-of-code-2024/internal/must"
5-
"os"
4+
"github.com/gdejong/advent-of-code-2024/internal/input"
65
"testing"
76
)
87

98
func TestPart1(t *testing.T) {
10-
f := must.NoError(os.Open("test_input.txt"))
9+
lines := input.Content("test_input.txt")
1110

12-
answer := part1(f)
11+
answer := part1(lines)
1312

1413
if answer != 11 {
1514
t.Errorf("wrong answer, got: %d", answer)
1615
}
1716
}
1817

1918
func TestPart2(t *testing.T) {
20-
f := must.NoError(os.Open("test_input.txt"))
19+
lines := input.Content("test_input.txt")
2120

22-
answer := part2(f)
21+
answer := part2(lines)
2322

2423
if answer != 31 {
2524
t.Errorf("wrong answer, got: %d", answer)

cmd/day2/main.go

+8-29
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11
package main
22

33
import (
4-
"bufio"
54
"fmt"
65
"github.com/gdejong/advent-of-code-2024/internal/input"
76
"github.com/gdejong/advent-of-code-2024/internal/math"
8-
"github.com/gdejong/advent-of-code-2024/internal/must"
97
"github.com/gdejong/advent-of-code-2024/internal/slices"
10-
"io"
11-
"os"
128
"strings"
139
)
1410

1511
func main() {
16-
f := must.NoError(os.Open("cmd/day2/real_input.txt"))
12+
lines := input.Content("cmd/day2/real_input.txt")
1713

18-
fmt.Println(part1(f))
14+
fmt.Println(part1(lines))
1915

20-
// Reset the file so we can it again for part 2
21-
must.NoError(f.Seek(0, io.SeekStart))
22-
23-
fmt.Println(part2(f))
16+
fmt.Println(part2(lines))
2417
}
2518

26-
func part1(f *os.File) int {
27-
s := bufio.NewScanner(f)
28-
19+
func part1(lines []string) int {
2920
safeCounter := 0
30-
for s.Scan() {
31-
line := s.Text()
32-
21+
for _, line := range lines {
3322
report := input.ToIntegers(strings.Fields(line))
3423

3524
if isSafeReport(report) {
@@ -40,20 +29,16 @@ func part1(f *os.File) int {
4029
return safeCounter
4130
}
4231

43-
func part2(f *os.File) int {
44-
s := bufio.NewScanner(f)
45-
32+
func part2(lines []string) int {
4633
safeCounter := 0
47-
for s.Scan() {
48-
line := s.Text()
49-
34+
for _, line := range lines {
5035
report := input.ToIntegers(strings.Fields(line))
5136

5237
// Create all possible options by continually removing one level
5338
var reportsToCheck [][]int
5439
reportsToCheck = append(reportsToCheck, report)
5540
for index := range len(report) {
56-
reportsToCheck = append(reportsToCheck, remove(report, index))
41+
reportsToCheck = append(reportsToCheck, slices.CopyAndRemoveIndex(report, index))
5742
}
5843

5944
for _, r := range reportsToCheck {
@@ -67,12 +52,6 @@ func part2(f *os.File) int {
6752
return safeCounter
6853
}
6954

70-
func remove(slice []int, s int) []int {
71-
s2 := make([]int, len(slice))
72-
copy(s2, slice)
73-
return append(s2[:s], s2[s+1:]...)
74-
}
75-
7655
func isSafeReport(levels []int) bool {
7756
// Calculate the differences
7857
diffs := make([]int, len(levels)-1)

cmd/day2/main_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package main
22

33
import (
4-
"github.com/gdejong/advent-of-code-2024/internal/must"
5-
"os"
4+
"github.com/gdejong/advent-of-code-2024/internal/input"
65
"testing"
76
)
87

98
func TestPart1(t *testing.T) {
10-
f := must.NoError(os.Open("test_input.txt"))
9+
lines := input.Content("test_input.txt")
1110

12-
answer := part1(f)
11+
answer := part1(lines)
1312

1413
if answer != 2 {
1514
t.Errorf("wrong answer, got: %d", answer)
1615
}
1716
}
1817

1918
func TestPart2(t *testing.T) {
20-
f := must.NoError(os.Open("test_input.txt"))
19+
lines := input.Content("test_input.txt")
2120

22-
answer := part2(f)
21+
answer := part2(lines)
2322

2423
if answer != 4 {
2524
t.Errorf("wrong answer, got: %d", answer)

internal/input/input.go

+18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
package input
22

33
import (
4+
"bufio"
45
"github.com/gdejong/advent-of-code-2024/internal/must"
6+
"os"
57
"strconv"
68
)
79

10+
func Content(file string) []string {
11+
f, err := os.Open(file)
12+
if err != nil {
13+
panic(err)
14+
}
15+
defer f.Close()
16+
s := bufio.NewScanner(f)
17+
18+
lines := make([]string, 0)
19+
for s.Scan() {
20+
lines = append(lines, s.Text())
21+
}
22+
23+
return lines
24+
}
25+
826
func ToIntegers(input []string) []int {
927
integers := make([]int, len(input))
1028

internal/math/math.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package math
22

3+
// Abs returns the absolute value of value.
34
func Abs(value int) int {
45
if value < 0 {
56
value = value * -1

internal/slices/slices.go

+8
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ func All[T any](ts []T, pred func(T) bool) bool {
99
}
1010
return true
1111
}
12+
13+
// CopyAndRemoveIndex returns a new slice with the given i element removed.
14+
func CopyAndRemoveIndex(slice []int, i int) []int {
15+
newSlice := make([]int, len(slice))
16+
copy(newSlice, slice) // make a copy so we do not alter the original slice
17+
18+
return append(newSlice[:i], newSlice[i+1:]...)
19+
}

0 commit comments

Comments
 (0)