inline grid field to type alias
This commit is contained in:
parent
d2699dfd24
commit
056ac16cf1
22
05/main.go
22
05/main.go
@ -41,13 +41,11 @@ type point struct {
|
|||||||
y int
|
y int
|
||||||
}
|
}
|
||||||
|
|
||||||
type grid struct {
|
type grid []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.x+p.y*1000]++
|
(*g)[p.x+p.y*1000]++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,17 +106,15 @@ func parsePoints(line string) (point, point) {
|
|||||||
func partOne() {
|
func partOne() {
|
||||||
scanner := makeScanner(false)
|
scanner := makeScanner(false)
|
||||||
|
|
||||||
grid := &grid{
|
grid := make(grid, 1_000_000)
|
||||||
m: make([]int, 1_000_000),
|
|
||||||
}
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
start, end := parsePoints(line)
|
start, end := parsePoints(line)
|
||||||
grid.AddLine(start, end, false)
|
grid.AddLine(start, end, false)
|
||||||
}
|
}
|
||||||
var danger int
|
var danger int
|
||||||
for i := range grid.m {
|
for i := range grid {
|
||||||
if grid.m[i] > 1 {
|
if grid[i] > 1 {
|
||||||
danger++
|
danger++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,17 +124,15 @@ func partOne() {
|
|||||||
func partTwo() {
|
func partTwo() {
|
||||||
scanner := makeScanner(false)
|
scanner := makeScanner(false)
|
||||||
|
|
||||||
grid := &grid{
|
grid := make(grid, 1_000_000)
|
||||||
m: make([]int, 1_000_000),
|
|
||||||
}
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
start, end := parsePoints(line)
|
start, end := parsePoints(line)
|
||||||
grid.AddLine(start, end, true)
|
grid.AddLine(start, end, true)
|
||||||
}
|
}
|
||||||
var danger int
|
var danger int
|
||||||
for i := range grid.m {
|
for i := range grid {
|
||||||
if grid.m[i] > 1 {
|
if grid[i] > 1 {
|
||||||
danger++
|
danger++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user