From d2699dfd2437d2bce8432f2cd4e5e6551053b4b3 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Sun, 5 Dec 2021 14:45:08 -0500 Subject: [PATCH] use a single slice instead of two --- 05/main.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/05/main.go b/05/main.go index 51d04fa..138fd80 100644 --- a/05/main.go +++ b/05/main.go @@ -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)