Skip to content

Commit

Permalink
feat: day 02 part 2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
agrmohit committed Dec 2, 2024
1 parent d68d82f commit 166b6fa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
74 changes: 52 additions & 22 deletions 2024/02/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
_ "embed"
"fmt"
"slices"

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

// checkIsSafe takes an int row and returns whether it is safe
func checkIsSafe(row []int) bool {
isIncreasing := row[1] > row[0]

for i := 1; i < len(row); i++ {
// If 2 consecutive numbers are same, it's unsafe
if row[i] == row[i-1] {
return false
}

// If `isIncreasing` flips anytime during execution, it's unsafe
if isIncreasing != (row[i] > row[i-1]) {
return false
}

// If absolute difference between consecutive numbers is not 1, 2 or 3
if mathinternal.Abs(row[i]-row[i-1]) > 3 {
return false
}
}

return true
}

func solvePart1(input string) int {
rows, err := inputs.ExtractIntRows(input)
if err != nil {
Expand All @@ -20,31 +45,34 @@ func solvePart1(input string) int {
safeCount := 0

for _, row := range rows {
isSafe := true
isIncreasing := row[1] > row[0]

for i := 1; i < len(row); i++ {
// If 2 consecutive numbers are same, it's unsafe
if row[i] == row[i-1] {
isSafe = false
break
}
if checkIsSafe(row) {
safeCount++
}
}

// If `isIncreasing` flips anytime during execution, it's unsafe
if isIncreasing != (row[i] > row[i-1]) {
isSafe = false
break
}
return safeCount
}

// If absolute difference between consecutive numbers is not 1, 2 or 3
if mathinternal.Abs(row[i]-row[i-1]) > 3 {
isSafe = false
break
}
}
func solvePart2(input string) int {
rows, err := inputs.ExtractIntRows(input)
if err != nil {
panic(err)
}

safeCount := 0

if isSafe {
for _, row := range rows {
// Check whether it is safe without removing any element
if checkIsSafe(row) {
safeCount++
} else {
// Remove each element one by one and check whether it is safe
for i := range row {
if checkIsSafe(slices.Concat(row[:i], row[i+1:])) {
safeCount++
break
}
}
}
}

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

func main() {
part1Solution := solvePart1(input)
part2Solution := solvePart2(input)

fmt.Println("Day 02 Part 1 solution:", part1Solution)
fmt.Println("Day 01 Part 1 solution:", part1Solution)
fmt.Println("Day 01 Part 2 solution:", part2Solution)
}
9 changes: 9 additions & 0 deletions 2024/02/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ func TestSolution(t *testing.T) {
t.Errorf("Incorrect solution, got %d want %d", got, want)
}
})

t.Run("Day 02 part 2", func(t *testing.T) {
want := 4
got := solvePart2(test1)

if got != want {
t.Errorf("Incorrect solution, got %d want %d", got, want)
}
})
}

0 comments on commit 166b6fa

Please sign in to comment.