inline grid field to type alias

This commit is contained in:
David 2021-12-05 14:46:48 -05:00
parent d2699dfd24
commit 056ac16cf1

View File

@ -41,13 +41,11 @@ type point struct {
y int
}
type grid struct {
m []int
}
type grid []int
func (g *grid) AddLine(start, end point, includeHorizontal bool) {
for _, p := range LineFromPoints(start, end, includeHorizontal) {
g.m[p.x+p.y*1000]++
(*g)[p.x+p.y*1000]++
}
}
@ -108,17 +106,15 @@ func parsePoints(line string) (point, point) {
func partOne() {
scanner := makeScanner(false)
grid := &grid{
m: make([]int, 1_000_000),
}
grid := make(grid, 1_000_000)
for scanner.Scan() {
line := scanner.Text()
start, end := parsePoints(line)
grid.AddLine(start, end, false)
}
var danger int
for i := range grid.m {
if grid.m[i] > 1 {
for i := range grid {
if grid[i] > 1 {
danger++
}
}
@ -128,17 +124,15 @@ func partOne() {
func partTwo() {
scanner := makeScanner(false)
grid := &grid{
m: make([]int, 1_000_000),
}
grid := make(grid, 1_000_000)
for scanner.Scan() {
line := scanner.Text()
start, end := parsePoints(line)
grid.AddLine(start, end, true)
}
var danger int
for i := range grid.m {
if grid.m[i] > 1 {
for i := range grid {
if grid[i] > 1 {
danger++
}
}