speed up day 5 with preallocated slices, not a map

This commit is contained in:
David 2021-12-05 13:24:59 -05:00
parent c5dbac0e9c
commit 019b87c43c

View File

@ -42,12 +42,15 @@ type point struct {
}
type grid struct {
m map[point]int
m [][]int
}
func (g *grid) AddLine(start, end point, includeHorizontal bool) {
for _, p := range LineFromPoints(start, end, includeHorizontal) {
g.m[p]++
if g.m[p.x] == nil {
g.m[p.x] = make([]int, 1000)
}
g.m[p.x][p.y]++
}
}
@ -109,7 +112,7 @@ func partOne() {
scanner := makeScanner(false)
grid := &grid{
m: map[point]int{},
m: make([][]int, 1000),
}
for scanner.Scan() {
line := scanner.Text()
@ -118,8 +121,10 @@ func partOne() {
}
var danger int
for i := range grid.m {
if grid.m[i] > 1 {
danger++
for j := range grid.m[i] {
if grid.m[i][j] > 1 {
danger++
}
}
}
fmt.Println(danger)
@ -129,7 +134,7 @@ func partTwo() {
scanner := makeScanner(false)
grid := &grid{
m: map[point]int{},
m: make([][]int, 1000),
}
for scanner.Scan() {
line := scanner.Text()
@ -138,8 +143,10 @@ func partTwo() {
}
var danger int
for i := range grid.m {
if grid.m[i] > 1 {
danger++
for j := range grid.m[i] {
if grid.m[i][j] > 1 {
danger++
}
}
}
fmt.Println(danger)