This commit is contained in:
2021-12-05 18:52:06 +00:00
parent 7c828a4104
commit 73c49553cc
2 changed files with 28 additions and 15 deletions
+21 -9
View File
@@ -28,13 +28,12 @@ func createLine(s []string) *Line {
return l
}
func (l *Line) Walk() []Point {
var p []Point
func (l *Line) Walk() (p []Point, isDiagonal bool) {
xstart, xend := minMax(l.start.x, l.end.x)
ystart, yend := minMax(l.start.y, l.end.y)
// diagonal line
if xend-xstart == yend-ystart {
isDiagonal = true
for x, y := l.start.x, l.start.y; ; {
p = append(p, Point{x: x, y: y})
if x == l.end.x {
@@ -62,7 +61,7 @@ func (l *Line) Walk() []Point {
p = append(p, Point{x: l.start.x, y: y})
}
}
return p
return
}
func minMax(a int, b int) (min int, max int) {
@@ -87,23 +86,36 @@ func main() {
r := regexp.MustCompile("[0-9]+")
floorMap := make(map[Point]int)
var overlapCount int
diags := make([]Point, 0)
for scanner.Scan() {
text := scanner.Text()
coords := r.FindAllString(text, -1)
line := createLine(coords)
points := line.Walk()
points, isDiagonal := line.Walk()
if isDiagonal {
diags = append(diags, points...)
continue
}
for _, p := range points {
if floorMap[p] == 1 {
overlapCount++
}
floorMap[p]++
}
}
var overlapCount int
for _, v := range floorMap {
if v > 1 {
fmt.Println("Part one: ", overlapCount)
for _, p := range diags {
if floorMap[p] == 1 {
overlapCount++
}
floorMap[p]++
}
fmt.Println(overlapCount)
fmt.Println("Part two: ", overlapCount)
}