Skip to content

Commit

Permalink
feat: day 03 part 2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
agrmohit committed Dec 3, 2024
1 parent 2b70739 commit d2da5de
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 2024/03/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,37 @@ func solvePart1(input string) int {
return result
}

func solvePart2(input string) int {
rexp := regexp.MustCompile(`mul\((\d{1,3})\,(\d{1,3})\)|do\(\)|don't\(\)`)
matches := rexp.FindAllStringSubmatch(input, -1)

result := 0
enabled := true

for _, match := range matches {
switch match[0] {
case "do()":
enabled = true
case "don't()":
enabled = false
default:
// Ignore the error since regex already does the type matching
num1, _ := strconv.Atoi(match[1])
num2, _ := strconv.Atoi(match[2])

if enabled {
result += num1 * num2
}
}
}

return result
}

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

fmt.Println("Day 03 Part 1 solution:", part1Solution)
fmt.Println("Day 03 Part 2 solution:", part2Solution)
}
12 changes: 12 additions & 0 deletions 2024/03/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
//go:embed test1.txt
var test1 string

//go:embed test2.txt
var test2 string

func TestSolution(t *testing.T) {
t.Run("Day 03 part 1", func(t *testing.T) {
want := 161
Expand All @@ -17,4 +20,13 @@ func TestSolution(t *testing.T) {
t.Errorf("Incorrect solution, got %d want %d", got, want)
}
})

t.Run("Day 03 part 2", func(t *testing.T) {
want := 48
got := solvePart2(test2)

if got != want {
t.Errorf("Incorrect solution, got %d want %d", got, want)
}
})
}
1 change: 1 addition & 0 deletions 2024/03/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

0 comments on commit d2da5de

Please sign in to comment.