-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
137 lines (122 loc) · 1.86 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bufio"
"io"
"strconv"
)
func fs1(input io.Reader) (int, error) {
scanner := bufio.NewScanner(input)
var instructions []string
for scanner.Scan() {
instructions = append(instructions, scanner.Text())
}
x := 0
y := 0
res := 0
for _, instruction := range instructions {
runes := []rune(instruction)
for _, r := range runes {
switch r {
case 'U':
y = move(y - 1)
case 'D':
y = move(y + 1)
case 'L':
x = move(x - 1)
case 'R':
x = move(x + 1)
}
}
res = res*10 + getButton(x, y)
}
return res, nil
}
func move(v int) int {
if v == -2 {
return -1
}
if v == 2 {
return 1
}
return v
}
func getButton(x, y int) int {
return (y+1)*3 + (x + 1) + 1
}
func fs2(input io.Reader) (string, error) {
scanner := bufio.NewScanner(input)
var instructions []string
for scanner.Scan() {
instructions = append(instructions, scanner.Text())
}
p := &Position{x: -2, y: 0}
res := ""
for _, instruction := range instructions {
runes := []rune(instruction)
for _, r := range runes {
switch r {
case 'U':
p.move(0, -1)
case 'D':
p.move(0, 1)
case 'L':
p.move(-1, 0)
case 'R':
p.move(1, 0)
}
}
res += getButton2(p)
}
return res, nil
}
type Position struct {
x int
y int
}
func (p *Position) move(dx, dy int) {
x := p.x + dx
y := p.y + dy
if y == -2 || y == 2 {
if x != 0 {
return
}
} else if y == -1 || y == 1 {
if x < -1 || x > 1 {
return
}
} else if y == 0 {
if x < -2 || x > 2 {
return
}
} else {
return
}
p.x = x
p.y = y
}
func getButton2(pos *Position) string {
x := pos.x
y := pos.y
if y == -2 {
return "1"
}
if y == -1 {
v := x + 3
return strconv.Itoa(v)
}
if y == 0 {
v := x + 7
return strconv.Itoa(v)
}
if y == 1 {
switch x {
case -1:
return "A"
case 0:
return "B"
case 1:
return "C"
}
}
return "D"
}