use a single slice instead of two

This commit is contained in:
David 2021-12-05 14:45:08 -05:00
parent 019b87c43c
commit d2699dfd24

View File

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