Skip to content

Commit

Permalink
feat(2024): add day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Dec 3, 2024
1 parent 64a5ea7 commit 461c2dd
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
10 changes: 6 additions & 4 deletions go/2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
| ----------------------------------------------------------------------------------------------------------------- | --- | --------: | --- | --------: |
| [Day 1: Historian Hysteria](https://github.com/believer/advent-of-code/blob/master/go/2024/puzzles/day01/main.go) | 🌟 | 1666427 | 🌟 | 24316233 |
| [Day 2: Red-Nosed Reports](https://github.com/believer/advent-of-code/blob/master/go/2024/puzzles/day02/main.go) | 🌟 | 564 | 🌟 | 604 |
| [Day 3: Mull It Over](https://github.com/believer/advent-of-code/blob/master/go/2024/puzzles/day03/main.go) | 🌟 | 161085926 | 🌟 | 82045421 |

## Benchmarks

Using Go's built-in benchmarking with the [testing](https://pkg.go.dev/testing#hdr-Benchmarks) package. Computer is a 2021 MacBook Pro M1 Pro, 32 GB RAM.

| Day | #1 | #2 | Improvement\* |
| --- | -----------: | -----------: | ------------------ |
| 1 | 116264 ns/op | 131233 ns/op | `3,53%` / `68,43%` |
| 2 | 310935 ns/op | 723512 ns/op | |
| Day | #1 | #2 | Improvement\* |
| --- | -----------: | ------------: | ------------------ |
| 1 | 116264 ns/op | 131233 ns/op | `3,53%` / `68,43%` |
| 2 | 310935 ns/op | 723512 ns/op | |
| 3 | 336448 ns/op | 1246155 ns/op | |

\* compared to first solution

Expand Down
64 changes: 64 additions & 0 deletions go/2024/puzzles/day03/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"regexp"
"strings"

"github.com/believer/aoc-2024/utils"
"github.com/believer/aoc-2024/utils/files"
)

// Wasted a couple of minutes when I didn't see that the test input had
// changed for part two and was wondering why my regex didn't find do's and don'ts...

// There might be something clever we can do here instead of the regexes?
func main() {
fmt.Println("Part 1: ", part1("input.txt"))
fmt.Println("Part 2: ", part2("input.txt"))
}

func part1(name string) int {
program := files.Read(name)
re := regexp.MustCompile(`mul\(\d{1,3},\d{1,3}\)`)
matches := re.FindAllString(program, -1)
total := 0

for _, mul := range matches {
parts := strings.Split(mul, ",")
lhs := utils.MustIntFromString(parts[0][4:])
rhs := utils.MustIntFromString(parts[1][:len(parts[1])-1])

total += lhs * rhs
}

return total
}

func part2(name string) int {
program := files.Read(name)
re := regexp.MustCompile(`(do\(\))|(don't\(\))|(mul\(\d{1,3},\d{1,3}\))`)
matches := re.FindAllString(program, -1)
total := 0
enabled := true

for _, m := range matches {
if strings.HasPrefix(m, "mul") && enabled {
parts := strings.Split(m, ",")
lhs := utils.MustIntFromString(parts[0][4:])
rhs := utils.MustIntFromString(parts[1][:len(parts[1])-1])

total += lhs * rhs
}

if strings.HasPrefix(m, "do(") {
enabled = true
}

if strings.HasPrefix(m, "don") {
enabled = false
}
}

return total
}
35 changes: 35 additions & 0 deletions go/2024/puzzles/day03/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPart1(t *testing.T) {
t.Run("Part 1", func(t *testing.T) {
expected := 161
actual := part1("test-input.txt")
assert.Equal(t, expected, actual)
})
}

func TestPart2(t *testing.T) {
t.Run("Part 2", func(t *testing.T) {
expected := 48
actual := part2("test-input-part2.txt")
assert.Equal(t, expected, actual)
})
}

func BenchmarkPart1(b *testing.B) {
for i := 0; i < b.N; i++ {
part1("input.txt")
}
}

func BenchmarkPart2(b *testing.B) {
for i := 0; i < b.N; i++ {
part2("input.txt")
}
}
1 change: 1 addition & 0 deletions go/2024/puzzles/day03/test-input-part2.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))
1 change: 1 addition & 0 deletions go/2024/puzzles/day03/test-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))

0 comments on commit 461c2dd

Please sign in to comment.