Skip to content

Commit

Permalink
perf(2024): update day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Dec 4, 2024
1 parent a440d6d commit 4bab4d6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 132 deletions.
23 changes: 12 additions & 11 deletions go/2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day

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 | |
| 3 | 336448 ns/op | 785320 ns/op | - / `36.98%` |
| 4 | 2864606 ns/op | 294413 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 | 785320 ns/op | - / `36.98%` |
| 4 | 523315 ns/op | 294413 ns/op | `81.73%` / - |

\* compared to first solution

### Previous solutions

| Day | #1 | #2 | Improvement | Link |
| --: | -----------: | ------------: | -----------: | ------------------------------------------------------------------------------------------------------------------------------ |
| 1 | 120513 ns/op | 415683 ns/op | Baseline | [Link](https://github.com/believer/advent-of-code/blob/47447cc17fffe6994d4b54c4cb815e698b3f5605/go/2024/puzzles/day01/main.go) |
| 1 | 120513 ns/op | 155479 ns/op | - / `62,59%` | [Link](https://github.com/believer/advent-of-code/blob/ea42592462771b74de87eae6bea9c0ca892a4499/go/2024/puzzles/day01/main.go) |
| 3 | 336448 ns/op | 1246155 ns/op | Baseline | [Link](https://github.com/believer/advent-of-code/blob/461c2dd40039c27102aa1790c650decb79d4f549/go/2024/puzzles/day03/main.go) |
| Day | #1 | #2 | Improvement | Link |
| --: | ------------: | ------------: | -----------: | ------------------------------------------------------------------------------------------------------------------------------ |
| 1 | 120513 ns/op | 415683 ns/op | Baseline | [Link](https://github.com/believer/advent-of-code/blob/47447cc17fffe6994d4b54c4cb815e698b3f5605/go/2024/puzzles/day01/main.go) |
| 1 | 120513 ns/op | 155479 ns/op | - / `62,59%` | [Link](https://github.com/believer/advent-of-code/blob/ea42592462771b74de87eae6bea9c0ca892a4499/go/2024/puzzles/day01/main.go) |
| 3 | 336448 ns/op | 1246155 ns/op | Baseline | [Link](https://github.com/believer/advent-of-code/blob/461c2dd40039c27102aa1790c650decb79d4f549/go/2024/puzzles/day03/main.go) |
| 4 | 2864606 ns/op | 294413 ns/op | Baseline | [Link](https://github.com/believer/advent-of-code/blob/99909bb30f82cda079471134452d886a0eb6266f/go/2024/puzzles/day04/main.go) |

## Running

Expand Down
185 changes: 64 additions & 121 deletions go/2024/puzzles/day04/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package main

import (
"fmt"
"regexp"
"strings"

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

// Can be cleaned up a bunch, but it works!
// Changed part 1 to take a similar approach to what I did in part 2
func main() {
fmt.Println("Part 1: ", part1("input.txt"))
fmt.Println("Part 2: ", part2("input.txt"))
Expand All @@ -17,103 +16,45 @@ func main() {
func part1(name string) int {
lines := files.ReadLines(name)
xmas := 0
forwards := regexp.MustCompile(`XMAS`)
backwards := regexp.MustCompile(`SAMX`)

// Check horizontal, forwards and backwards
for _, l := range lines {
t := len(forwards.FindAllString(l, -1))
t2 := len(backwards.FindAllString(l, -1))

xmas += t + t2
}

rows := len(lines)
cols := len(lines[0])

// Check vertical
for col := 0; col < cols; col++ {
vertical := ""

for row := 0; row < rows; row++ {
// Getting from line by row/col becomes an ASCII rune. Convert to string.
vertical += string(lines[row][col])
}

t := len(forwards.FindAllString(vertical, -1))
t2 := len(backwards.FindAllString(vertical, -1))

xmas += t + t2
}

// Diagonal, top-left to bottom-right
for start := 0; start < rows; start++ {
diagonal := ""

for i, j := start, 0; i < rows && j < cols; i, j = i+1, j+1 {
diagonal += string(lines[i][j])
}

if len(diagonal) < 4 {
break
}

t := len(forwards.FindAllString(diagonal, -1))
t2 := len(backwards.FindAllString(diagonal, -1))

xmas += t + t2
}

for start := 1; start < cols; start++ {
diagonal := ""

for i, j := 0, start; i < rows && j < cols; i, j = i+1, j+1 {
diagonal += string(lines[i][j])
}

if len(diagonal) < 4 {
break
}

t := len(forwards.FindAllString(diagonal, -1))
t2 := len(backwards.FindAllString(diagonal, -1))

xmas += t + t2
}

// Diagonal, top-right to bottom-left
for start := 0; start < rows; start++ {
diagonal := ""

for i, j := start, cols-1; i < rows && j >= 0; i, j = i+1, j-1 {
diagonal += string(lines[i][j])
}
for r := range rows {
for c := range cols {
// Skip if not an X
if string(lines[r][c]) != "X" {
continue
}

if len(diagonal) < 4 {
break
}
// Check all directions
for _, dr := range []int{-1, 0, 1} {
for _, dc := range []int{-1, 0, 1} {
if dr == 0 && dc == 0 {
continue
}

t := len(forwards.FindAllString(diagonal, -1))
t2 := len(backwards.FindAllString(diagonal, -1))
// Check bounds
threeDown := r + 3*dr
threeForwards := c + 3*dc
hasSpaceDown := threeDown >= 0 && threeDown < rows
hasSpaceForwards := threeForwards >= 0 && threeForwards < cols

xmas += t + t2
}

for start := cols - 2; start >= 0; start-- {
diagonal := ""
if !hasSpaceDown || !hasSpaceForwards {
continue
}

for i, j := 0, start; i < rows && j >= 0; i, j = i+1, j-1 {
diagonal += string(lines[i][j])
}
// Check that the next three letters are MAS
nextIsM := string(lines[r+dr][c+dc]) == "M"
nextIsA := string(lines[r+2*dr][c+2*dc]) == "A"
nextIsS := string(lines[r+3*dr][c+3*dc]) == "S"

if len(diagonal) < 4 {
break
if nextIsM && nextIsA && nextIsS {
xmas++
}
}
}
}

t := len(forwards.FindAllString(diagonal, -1))
t2 := len(backwards.FindAllString(diagonal, -1))

xmas += t + t2
}

return xmas
Expand All @@ -129,41 +70,43 @@ func part2(name string) int {
for i, r := range lines {
for j := range strings.Split(r, "") {
// A's are always in the middle
if string(r[j]) == "A" {
// Check line above and below
if i-1 >= 0 && j-1 >= 0 && i+1 < rows && j+1 < cols {
diagonalTopLeft := string(lines[i-1][j-1])
diagonalTopRight := string(lines[i-1][j+1])
diagonalBottomLeft := string(lines[i+1][j-1])
diagonalBottomRight := string(lines[i+1][j+1])

// M.M
// .A.
// S.S
if diagonalTopLeft == "M" && diagonalBottomRight == "S" && diagonalTopRight == "M" && diagonalBottomLeft == "S" {
xmas++
}
if string(r[j]) != "A" {
continue
}

// M.S
// .A.
// M.S
if diagonalTopLeft == "M" && diagonalBottomRight == "S" && diagonalTopRight == "S" && diagonalBottomLeft == "M" {
xmas++
}
// Check line above and below
if i-1 >= 0 && j-1 >= 0 && i+1 < rows && j+1 < cols {
diagonalTopLeft := string(lines[i-1][j-1])
diagonalTopRight := string(lines[i-1][j+1])
diagonalBottomLeft := string(lines[i+1][j-1])
diagonalBottomRight := string(lines[i+1][j+1])

// M.M
// .A.
// S.S
if diagonalTopLeft == "M" && diagonalBottomRight == "S" && diagonalTopRight == "M" && diagonalBottomLeft == "S" {
xmas++
}

// S.S
// .A.
// M.M
if diagonalTopLeft == "S" && diagonalBottomRight == "M" && diagonalTopRight == "S" && diagonalBottomLeft == "M" {
xmas++
}
// M.S
// .A.
// M.S
if diagonalTopLeft == "M" && diagonalBottomRight == "S" && diagonalTopRight == "S" && diagonalBottomLeft == "M" {
xmas++
}

// S.M
// .A.
// S.M
if diagonalTopLeft == "S" && diagonalBottomRight == "M" && diagonalTopRight == "M" && diagonalBottomLeft == "S" {
xmas++
}
// S.S
// .A.
// M.M
if diagonalTopLeft == "S" && diagonalBottomRight == "M" && diagonalTopRight == "S" && diagonalBottomLeft == "M" {
xmas++
}

// S.M
// .A.
// S.M
if diagonalTopLeft == "S" && diagonalBottomRight == "M" && diagonalTopRight == "M" && diagonalBottomLeft == "S" {
xmas++
}
}
}
Expand Down

0 comments on commit 4bab4d6

Please sign in to comment.