Skip to content

Commit 166b6fa

Browse files
committed
feat: day 02 part 2 solution
1 parent d68d82f commit 166b6fa

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

2024/02/main.go

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
_ "embed"
55
"fmt"
6+
"slices"
67

78
"github.com/agrmohit/advent-of-code/internal/inputs"
89
mathinternal "github.com/agrmohit/advent-of-code/internal/math"
@@ -11,6 +12,30 @@ import (
1112
//go:embed input.txt
1213
var input string
1314

15+
// checkIsSafe takes an int row and returns whether it is safe
16+
func checkIsSafe(row []int) bool {
17+
isIncreasing := row[1] > row[0]
18+
19+
for i := 1; i < len(row); i++ {
20+
// If 2 consecutive numbers are same, it's unsafe
21+
if row[i] == row[i-1] {
22+
return false
23+
}
24+
25+
// If `isIncreasing` flips anytime during execution, it's unsafe
26+
if isIncreasing != (row[i] > row[i-1]) {
27+
return false
28+
}
29+
30+
// If absolute difference between consecutive numbers is not 1, 2 or 3
31+
if mathinternal.Abs(row[i]-row[i-1]) > 3 {
32+
return false
33+
}
34+
}
35+
36+
return true
37+
}
38+
1439
func solvePart1(input string) int {
1540
rows, err := inputs.ExtractIntRows(input)
1641
if err != nil {
@@ -20,31 +45,34 @@ func solvePart1(input string) int {
2045
safeCount := 0
2146

2247
for _, row := range rows {
23-
isSafe := true
24-
isIncreasing := row[1] > row[0]
25-
26-
for i := 1; i < len(row); i++ {
27-
// If 2 consecutive numbers are same, it's unsafe
28-
if row[i] == row[i-1] {
29-
isSafe = false
30-
break
31-
}
48+
if checkIsSafe(row) {
49+
safeCount++
50+
}
51+
}
3252

33-
// If `isIncreasing` flips anytime during execution, it's unsafe
34-
if isIncreasing != (row[i] > row[i-1]) {
35-
isSafe = false
36-
break
37-
}
53+
return safeCount
54+
}
3855

39-
// If absolute difference between consecutive numbers is not 1, 2 or 3
40-
if mathinternal.Abs(row[i]-row[i-1]) > 3 {
41-
isSafe = false
42-
break
43-
}
44-
}
56+
func solvePart2(input string) int {
57+
rows, err := inputs.ExtractIntRows(input)
58+
if err != nil {
59+
panic(err)
60+
}
61+
62+
safeCount := 0
4563

46-
if isSafe {
64+
for _, row := range rows {
65+
// Check whether it is safe without removing any element
66+
if checkIsSafe(row) {
4767
safeCount++
68+
} else {
69+
// Remove each element one by one and check whether it is safe
70+
for i := range row {
71+
if checkIsSafe(slices.Concat(row[:i], row[i+1:])) {
72+
safeCount++
73+
break
74+
}
75+
}
4876
}
4977
}
5078

@@ -53,6 +81,8 @@ func solvePart1(input string) int {
5381

5482
func main() {
5583
part1Solution := solvePart1(input)
84+
part2Solution := solvePart2(input)
5685

57-
fmt.Println("Day 02 Part 1 solution:", part1Solution)
86+
fmt.Println("Day 01 Part 1 solution:", part1Solution)
87+
fmt.Println("Day 01 Part 2 solution:", part2Solution)
5888
}

2024/02/main_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@ func TestSolution(t *testing.T) {
1717
t.Errorf("Incorrect solution, got %d want %d", got, want)
1818
}
1919
})
20+
21+
t.Run("Day 02 part 2", func(t *testing.T) {
22+
want := 4
23+
got := solvePart2(test1)
24+
25+
if got != want {
26+
t.Errorf("Incorrect solution, got %d want %d", got, want)
27+
}
28+
})
2029
}

0 commit comments

Comments
 (0)