-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd9p1.go
More file actions
58 lines (53 loc) · 906 Bytes
/
d9p1.go
File metadata and controls
58 lines (53 loc) · 906 Bytes
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
package main
import (
"io"
"math"
"os"
"strconv"
"strings"
)
type Point struct {
X, Y int
}
func main() {
f, _ := os.Open("input.txt")
b, _ := io.ReadAll(f)
s := string(b)
tail := &Point{200, 0}
head := &Point{200, 0}
visited := make(map[Point]bool)
visited[*tail] = true
for _, line := range strings.Split(s, "\n") {
if line == "" {
continue
}
split := strings.Split(line, " ")
times, _ := strconv.Atoi(split[1])
movement := split[0]
for i := 0; i < times; i++ {
prev := &Point{head.X, head.Y}
switch movement {
case "U":
head.X--
case "D":
head.X++
case "L":
head.Y--
case "R":
head.Y++
}
if math.Abs(float64(tail.X)-float64(head.X)) <= 1 && math.Abs(float64(tail.Y)-float64(head.Y)) <= 1 {
continue
}
tail = prev
visited[*tail] = true
}
}
r := 0
for _, v := range visited {
if v {
r++
}
}
println(r)
}