Skip to content

Commit 5ad8951

Browse files
committed
update advent-of-code day1
1 parent 1b8be56 commit 5ad8951

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

advent-of-code/2024/day1/day1_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111
)
1212

13-
func TestDay1(t *testing.T) {
13+
func TestPart1(t *testing.T) {
1414
file, err := os.Open("input")
1515
if err != nil {
1616
t.Fatal("can't open input file")
@@ -61,3 +61,51 @@ func TestDay1(t *testing.T) {
6161

6262
fmt.Println(sum)
6363
}
64+
65+
func TestPart2(t *testing.T) {
66+
file, err := os.Open("input")
67+
if err != nil {
68+
t.Fatal("can't open input file")
69+
}
70+
defer file.Close()
71+
72+
var column1 []int
73+
var column2 []int
74+
75+
scanner := bufio.NewScanner(file)
76+
for scanner.Scan() {
77+
// ex: 31594 93577
78+
line := scanner.Text()
79+
80+
lineStrSlice := strings.Split(line, " ")
81+
lineIntVar1, err := strconv.Atoi(lineStrSlice[0])
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
86+
lineIntVar2, err := strconv.Atoi(lineStrSlice[1])
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
91+
column1 = append(column1, lineIntVar1)
92+
column2 = append(column2, lineIntVar2)
93+
}
94+
95+
if scanner.Err() != nil {
96+
t.Fatal("scanner err")
97+
}
98+
99+
var score int
100+
for _, v1 := range column1 {
101+
var sum int
102+
for _, v2 := range column2 {
103+
if v1 == v2 {
104+
sum += v1
105+
}
106+
}
107+
score += sum
108+
}
109+
110+
fmt.Println(score)
111+
}
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

0 commit comments

Comments
 (0)