Skip to content

Commit

Permalink
chore(2024): improve performance of day1
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Dec 1, 2024
1 parent 923404b commit 65f8afc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 50 deletions.
8 changes: 7 additions & 1 deletion go/2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day

| Day | #1 | #2 | Improvement\* |
| --- | -----------: | -----------: | ------------- |
| 1 | 120513 ns/op | 415683 ns/op | |
| 1 | 120513 ns/op | 155479 ns/op | - / `62.6%` |

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.

### Previous solutions

| Day | #1 | #2 | Improvement | Link |
| --: | --: | -----------: | ----------: | ------------------------------------------------------------------------------------------------------------------------------ |
| 1 | - | 415683 ns/op | | [Link](https://github.com/believer/advent-of-code/blob/47447cc17fffe6994d4b54c4cb815e698b3f5605/go/2024/puzzles/day01/main.go) |

## Running

Run a day
Expand Down
81 changes: 33 additions & 48 deletions go/2024/puzzles/day01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"slices"
"strconv"
"strings"

"github.com/believer/aoc-2024/utils"
Expand All @@ -17,33 +16,13 @@ func main() {
}

func part1(name string) int {
lines := files.ReadLines(name)
left := []int{}
right := []int{}
total := 0

for _, line := range lines {
row := strings.Split(line, " ")

l, err := strconv.Atoi(row[0])

if err != nil {
panic(err)
}

r, err := strconv.Atoi(row[1])

if err != nil {
panic(err)
}

left = append(left, l)
right = append(right, r)
}
left, right := historicallySignificantLocations(name)

slices.Sort(left)
slices.Sort(right)

total := 0

for i, l := range left {
total += utils.Abs(l - right[i])
}
Expand All @@ -52,40 +31,46 @@ func part1(name string) int {
}

func part2(name string) int {
lines := files.ReadLines(name)
left := []int{}
right := []int{}
total := 0
left, right := historicallySignificantLocations(name)

for _, line := range lines {
row := strings.Split(line, " ")
total := 0
appears := map[int]int{}

l, err := strconv.Atoi(row[0])
for _, r := range right {
_, ok := appears[r]

if err != nil {
panic(err)
if ok {
appears[r] += 1
} else {
appears[r] = 1
}
}

r, err := strconv.Atoi(row[1])
for _, l := range left {
v, ok := appears[l]

if err != nil {
panic(err)
if ok {
total += l * v
}

left = append(left, l)
right = append(right, r)
}

for _, l := range left {
appears := 0
for _, r := range right {
if l == r {
appears += 1
}
}
return total
}

func historicallySignificantLocations(name string) ([]int, []int) {
lines := files.ReadLines(name)
left := make([]int, len(lines))
right := make([]int, len(lines))

for _, line := range lines {
row := strings.Fields(line)

total += l * appears
l := utils.MustIntFromString(row[0])
r := utils.MustIntFromString(row[1])

left = append(left, l)
right = append(right, r)
}

return total
return left, right
}
2 changes: 1 addition & 1 deletion go/2024/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Sum(nums []int) int {
return total
}

func FromString(s string) int {
func MustIntFromString(s string) int {
v, err := strconv.Atoi(s)

if err != nil {
Expand Down

0 comments on commit 65f8afc

Please sign in to comment.