/
05.go
99 lines (93 loc) · 1.81 KB
/
05.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"strings"
)
func init() {
addSolutions(5, problem5)
}
func problem5(ctx *problemContext) {
var vents []vent
scanner := ctx.scanner()
for scanner.scan() {
parts := strings.Split(scanner.text(), " -> ")
c0 := strings.Split(parts[0], ",")
x0 := parseInt(c0[0], 10, 64)
y0 := parseInt(c0[1], 10, 64)
c1 := strings.Split(parts[1], ",")
x1 := parseInt(c1[0], 10, 64)
y1 := parseInt(c1[1], 10, 64)
vents = append(vents, vent{vec2{x0, y0}, vec2{x1, y1}})
}
ctx.reportLoad()
ventLines1 := make(map[vec2]int)
for _, vent := range vents {
v0, v1 := vent.v0, vent.v1
switch {
case v0.x == v1.x:
if v0.y > v1.y {
v0, v1 = v1, v0
}
for y := v0.y; y <= v1.y; y++ {
ventLines1[vec2{v0.x, y}]++
}
case v0.y == v1.y:
if v0.x > v1.x {
v0, v1 = v1, v0
}
for x := v0.x; x <= v1.x; x++ {
ventLines1[vec2{x, v0.y}]++
}
}
}
var overlaps int
for _, n := range ventLines1 {
if n > 1 {
overlaps++
}
}
ctx.reportPart1(overlaps)
ventLines2 := make(map[vec2]int)
for _, vent := range vents {
v0, v1 := vent.v0, vent.v1
switch {
case v0.x == v1.x:
if v0.y > v1.y {
v0, v1 = v1, v0
}
for y := v0.y; y <= v1.y; y++ {
ventLines2[vec2{v0.x, y}]++
}
case v0.y == v1.y:
if v0.x > v1.x {
v0, v1 = v1, v0
}
for x := v0.x; x <= v1.x; x++ {
ventLines2[vec2{x, v0.y}]++
}
default:
if v0.y > v1.y {
v0, v1 = v1, v0
}
if v0.x < v1.x {
for x, y := v0.x, v0.y; x <= v1.x && y <= v1.y; x, y = x+1, y+1 {
ventLines2[vec2{x, y}]++
}
} else {
for x, y := v0.x, v0.y; x >= v1.x && y <= v1.y; x, y = x-1, y+1 {
ventLines2[vec2{x, y}]++
}
}
}
}
overlaps = 0
for _, n := range ventLines2 {
if n > 1 {
overlaps++
}
}
ctx.reportPart2(overlaps)
}
type vent struct {
v0 vec2
v1 vec2
}