Skip to content

Commit 9f6d20b

Browse files
committed
[2024] Solution for Day 2
1 parent ced7c13 commit 9f6d20b

File tree

3 files changed

+156
-1
lines changed

3 files changed

+156
-1
lines changed

2024/day02/main.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"slices"
8+
"strings"
9+
10+
"github.com/kfarnung/advent-of-code/2024/lib"
11+
)
12+
13+
func part1(input string) int64 {
14+
reports, err := parseInput(input)
15+
if err != nil {
16+
log.Fatal(err)
17+
}
18+
19+
count := int64(0)
20+
for _, report := range reports {
21+
if isSafeReport(report) {
22+
count++
23+
}
24+
25+
slices.Reverse(report)
26+
27+
if isSafeReport(report) {
28+
count++
29+
}
30+
}
31+
32+
return count
33+
}
34+
35+
func part2(input string) int64 {
36+
reports, err := parseInput(input)
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
count := int64(0)
42+
for _, report := range reports {
43+
if isSafeReportAllowOneSkip(report) {
44+
count++
45+
}
46+
}
47+
48+
return count
49+
}
50+
51+
func isSafeReportAllowOneSkip(report []int64) bool {
52+
for i := 0; i < len(report); i++ {
53+
backup := report[i]
54+
report = slices.Delete(report, i, i+1)
55+
if isSafeReport(report) {
56+
return true
57+
}
58+
59+
slices.Reverse(report)
60+
if isSafeReport(report) {
61+
return true
62+
}
63+
64+
slices.Reverse(report)
65+
report = slices.Insert(report, i, backup)
66+
}
67+
68+
return false
69+
}
70+
71+
func isSafeReport(report []int64) bool {
72+
for i := 1; i < len(report); i++ {
73+
if report[i] <= report[i-1] || report[i]-report[i-1] > 3 {
74+
return false
75+
}
76+
}
77+
78+
return true
79+
}
80+
81+
func parseInput(input string) ([][]int64, error) {
82+
var reports [][]int64
83+
84+
for _, line := range lib.SplitLines(input) {
85+
if line == "" {
86+
continue
87+
}
88+
89+
var levels []int64
90+
numbers := strings.Split(line, " ")
91+
for _, number := range numbers {
92+
num, err := lib.ParseInt[int64](number)
93+
if err != nil {
94+
return nil, err
95+
}
96+
97+
levels = append(levels, num)
98+
}
99+
100+
reports = append(reports, levels)
101+
}
102+
103+
return reports, nil
104+
}
105+
106+
func main() {
107+
name := os.Args[1]
108+
content, err := lib.LoadFileContent(name)
109+
if err != nil {
110+
log.Fatal(err)
111+
}
112+
113+
fmt.Printf("Part 1: %d\n", part1(content))
114+
fmt.Printf("Part 2: %d\n", part2(content))
115+
}

2024/day02/main_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/kfarnung/advent-of-code/2024/lib"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var input = strings.Join([]string{
12+
"7 6 4 2 1",
13+
"1 2 7 8 9",
14+
"9 7 6 2 1",
15+
"1 3 2 4 5",
16+
"8 6 4 4 1",
17+
"1 3 6 7 9",
18+
}, "\n")
19+
20+
func TestPart1(t *testing.T) {
21+
assert.Equal(t, int64(2), part1(input))
22+
23+
inputContent, err := lib.GetInputContent()
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
assert.Equal(t, int64(411), part1(inputContent))
29+
}
30+
31+
func TestPart2(t *testing.T) {
32+
assert.Equal(t, int64(4), part2(input))
33+
34+
inputContent, err := lib.GetInputContent()
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
39+
assert.Equal(t, int64(465), part2(inputContent))
40+
}

private

0 commit comments

Comments
 (0)