-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.go
101 lines (93 loc) · 1.8 KB
/
day5.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
100
101
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
)
type point struct {
x int64
y int64
}
func main() {
f, _ := os.Open("day5_input")
defer f.Close()
s := bufio.NewScanner(f)
inputs := make([][]string, 0)
for s.Scan() {
inputs = append(inputs, strings.Split(s.Text(), " -> "))
}
res := 0
// Part1
plan := make(map[point]int64)
for _, v := range inputs {
from := strings.Split(v[0], ",")
to := strings.Split(v[1], ",")
if from[0] == to[0] || from[1] == to[1] {
xf, _ := strconv.ParseInt(from[0], 10, 0)
yf, _ := strconv.ParseInt(from[1], 10, 0)
xt, _ := strconv.ParseInt(to[0], 10, 0)
yt, _ := strconv.ParseInt(to[1], 10, 0)
if xf == xt {
for i := intMin(yf, yt); i <= intMax(yf, yt); i++ {
p := point{x: xf, y: i}
plan[p]++
}
} else {
for i := intMin(xf, xt); i <= intMax(xf, xt); i++ {
p := point{x: i, y: yf}
plan[p]++
}
}
}
}
for _, v := range plan {
if v > 1 {
res++
}
}
fmt.Println(res)
// Part2
for _, v := range inputs {
from := strings.Split(v[0], ",")
to := strings.Split(v[1], ",")
if from[0] != to[0] && from[1] != to[1] {
xf, _ := strconv.ParseInt(from[0], 10, 0)
yf, _ := strconv.ParseInt(from[1], 10, 0)
xt, _ := strconv.ParseInt(to[0], 10, 0)
yt, _ := strconv.ParseInt(to[1], 10, 0)
x, y := xf, yf
for x != xt && y != yt {
p := point{x: x, y: y}
plan[p]++
if xf > xt {
x--
} else {
x++
}
if yf > yt {
y--
} else {
y++
}
}
p := point{x: xt, y: yt}
plan[p]++
}
}
res = 0
for _, v := range plan {
if v > 1 {
res++
}
}
fmt.Println(res)
}
func intMax(a int64, b int64) int64 {
return int64(math.Max(float64(a), float64(b)))
}
func intMin(a int64, b int64) int64 {
return int64(math.Min(float64(a), float64(b)))
}