speed up day 5 with preallocated slices, not a map
This commit is contained in:
parent
c5dbac0e9c
commit
019b87c43c
23
05/main.go
23
05/main.go
@ -42,12 +42,15 @@ type point struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type grid struct {
|
type grid struct {
|
||||||
m map[point]int
|
m [][]int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *grid) AddLine(start, end point, includeHorizontal bool) {
|
func (g *grid) AddLine(start, end point, includeHorizontal bool) {
|
||||||
for _, p := range LineFromPoints(start, end, includeHorizontal) {
|
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)
|
scanner := makeScanner(false)
|
||||||
|
|
||||||
grid := &grid{
|
grid := &grid{
|
||||||
m: map[point]int{},
|
m: make([][]int, 1000),
|
||||||
}
|
}
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
@ -118,8 +121,10 @@ func partOne() {
|
|||||||
}
|
}
|
||||||
var danger int
|
var danger int
|
||||||
for i := range grid.m {
|
for i := range grid.m {
|
||||||
if grid.m[i] > 1 {
|
for j := range grid.m[i] {
|
||||||
danger++
|
if grid.m[i][j] > 1 {
|
||||||
|
danger++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(danger)
|
fmt.Println(danger)
|
||||||
@ -129,7 +134,7 @@ func partTwo() {
|
|||||||
scanner := makeScanner(false)
|
scanner := makeScanner(false)
|
||||||
|
|
||||||
grid := &grid{
|
grid := &grid{
|
||||||
m: map[point]int{},
|
m: make([][]int, 1000),
|
||||||
}
|
}
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
@ -138,8 +143,10 @@ func partTwo() {
|
|||||||
}
|
}
|
||||||
var danger int
|
var danger int
|
||||||
for i := range grid.m {
|
for i := range grid.m {
|
||||||
if grid.m[i] > 1 {
|
for j := range grid.m[i] {
|
||||||
danger++
|
if grid.m[i][j] > 1 {
|
||||||
|
danger++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(danger)
|
fmt.Println(danger)
|
||||||
|
Loading…
Reference in New Issue
Block a user