-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.go
115 lines (105 loc) · 1.51 KB
/
day09.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type V struct {
X int
Y int
}
type N struct {
V V
Prev *N
Next *N
}
// Don't call on Head.
func (n *N) Follow() {
dx := n.V.X - n.Prev.V.X
dx2 := dx * dx
dy := n.V.Y - n.Prev.V.Y
dy2 := dy * dy
if dx2 <= 1 && dy2 <= 1 {
// Touching!
if n.Next != nil {
n.Next.Follow()
}
return
}
if dx > 0 {
n.V.X--
} else if dx < 0 {
n.V.X++
}
if dy > 0 {
n.V.Y--
} else if dy < 0 {
n.V.Y++
}
if n.Next != nil {
n.Next.Follow()
}
}
type L struct {
Head *N
Tail *N
}
func (l *L) Move(dir string) {
switch dir {
case "U":
l.Head.V.Y++
case "R":
l.Head.V.X++
case "D":
l.Head.V.Y--
case "L":
l.Head.V.X--
default:
panic("not a dir")
}
l.Head.Next.Follow()
}
func MakeL(size int) *L {
pos := V{0, 0}
head := &N{V: pos}
prev := head
for i := 1; i < size; i++ {
cur := &N{V: pos, Prev: prev}
prev.Next = cur
prev = cur
}
return &L{
Head: head,
Tail: prev,
}
}
func main() {
l1 := MakeL(2)
seen1 := map[V]bool{
l1.Tail.V: true,
}
l2 := MakeL(10)
seen2 := map[V]bool{
l2.Tail.V: true,
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
cmd := strings.Split(scanner.Text(), " ")
dir := cmd[0]
num, err := strconv.Atoi(cmd[1])
if err != nil {
log.Fatalf("%v", err)
}
for i := 0; i < num; i++ {
l1.Move(dir)
seen1[l1.Tail.V] = true
l2.Move(dir)
seen2[l2.Tail.V] = true
}
}
fmt.Printf("Part 1: %d\n", len(seen1))
fmt.Printf("Part 2: %d\n", len(seen2))
}