Skip to content

Commit 0907b7e

Browse files
committed
chore(aoc-2024): day2 part 1
Signed-off-by: Márk Kővári <kovarimarkofficial@gmail.com>
1 parent 0338591 commit 0907b7e

6 files changed

Lines changed: 1104 additions & 9 deletions

File tree

2024/cmd/01.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99
)
1010

11-
func readFile(path string) ([]int64, []int64, error) {
11+
func read01File(path string) ([]int64, []int64, error) {
1212
dat, err := os.ReadFile(path)
1313
if err != nil {
1414
return nil, nil, err

2024/cmd/01_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package main
22

33
import "testing"
44

5-
func TestExample(t *testing.T) {
6-
left, right, err := readFile("../data/01/example")
5+
func Test01Example(t *testing.T) {
6+
left, right, err := read01File("../data/01/example")
77
if err != nil {
88
t.Fatalf("Error reading file: %v", err)
99
}
@@ -15,8 +15,8 @@ func TestExample(t *testing.T) {
1515
}
1616
}
1717

18-
func TestExampleSecond(t *testing.T) {
19-
left, right, err := readFile("../data/01/example")
18+
func Test01ExampleSecond(t *testing.T) {
19+
left, right, err := read01File("../data/01/example")
2020
if err != nil {
2121
t.Fatalf("Error reading file: %v", err)
2222
}
@@ -27,8 +27,8 @@ func TestExampleSecond(t *testing.T) {
2727
}
2828
}
2929

30-
func TestInput(t *testing.T) {
31-
left, right, err := readFile("../data/01/input")
30+
func Test01Input(t *testing.T) {
31+
left, right, err := read01File("../data/01/input")
3232
if err != nil {
3333
t.Fatalf("Error reading file: %v", err)
3434
}
@@ -40,8 +40,8 @@ func TestInput(t *testing.T) {
4040
}
4141
}
4242

43-
func TestInputSecond(t *testing.T) {
44-
left, right, err := readFile("../data/01/input")
43+
func Test01InputSecond(t *testing.T) {
44+
left, right, err := read01File("../data/01/input")
4545
if err != nil {
4646
t.Fatalf("Error reading file: %v", err)
4747
}

2024/cmd/02.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"math"
5+
"os"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
func read02File(path string) ([][]int, error) {
11+
dat, err := os.ReadFile(path)
12+
if err != nil {
13+
return nil, err
14+
}
15+
lines := strings.Split(string(dat), "\n")
16+
matrix := make([][]int, 0)
17+
18+
for _, line := range lines {
19+
if line == "" {
20+
continue
21+
}
22+
parts := strings.Fields(line)
23+
row := make([]int, 0)
24+
for _, part := range parts {
25+
value, err := strconv.Atoi(part)
26+
if err != nil {
27+
continue
28+
}
29+
row = append(row, value)
30+
}
31+
matrix = append(matrix, row)
32+
}
33+
return matrix, nil
34+
}
35+
36+
func countSafeLevels(levels [][]int) int {
37+
count := 0
38+
for _, row := range levels {
39+
if isLevelSafe(row) {
40+
count++
41+
}
42+
}
43+
return count
44+
}
45+
46+
func isLevelSafe(row []int) bool {
47+
if len(row) < 3 {
48+
return false
49+
}
50+
increasing := row[0] < row[1]
51+
decreasing := !increasing
52+
for j := 0; j < len(row)-1; j++ {
53+
current, next := row[j], row[j+1]
54+
difference := int(math.Abs(float64(current) - float64(next)))
55+
if increasing && current > next {
56+
return false
57+
} else if decreasing && next > current {
58+
return false
59+
} else if difference > 3 || difference < 1 {
60+
return false
61+
}
62+
}
63+
return true
64+
}

2024/cmd/02_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test02Example(t *testing.T) {
6+
levels, err := read02File("../data/02/example")
7+
if err != nil {
8+
t.Fatalf("Error reading file: %v", err)
9+
}
10+
countSafe := countSafeLevels(levels)
11+
if countSafe != 2 {
12+
t.Fatalf("Expected 2 safe leves, got=%d", countSafe)
13+
}
14+
}
15+
16+
func Test02Input(t *testing.T) {
17+
levels, err := read02File("../data/02/input")
18+
if err != nil {
19+
t.Fatalf("Error reading file: %v", err)
20+
}
21+
countSafe := countSafeLevels(levels)
22+
if countSafe != 686 {
23+
t.Fatalf("Expected 686 safe leves, got=%d", countSafe)
24+
}
25+
}

2024/data/02/example

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

0 commit comments

Comments
 (0)