-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
63 lines (52 loc) · 822 Bytes
/
common.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
package main
import (
"bufio"
"os"
)
type P struct{ X, Y int }
func popToken(input string) (string, string) {
first := string(input[0])
if first == "w" || first == "e" {
return first, input[1:]
}
return input[:2], input[2:]
}
func parse() map[P]bool {
floor := map[P]bool{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input := scanner.Text()
x, y := 0, 0
var token string
for len(input) > 0 {
token, input = popToken(input)
switch token {
case "ne":
y++
case "se":
y--
x++
case "nw":
y++
x--
case "sw":
y--
case "w":
x--
case "e":
x++
}
}
floor[P{x, y}] = !floor[P{x, y}]
}
return floor
}
func countBlacks(floor map[P]bool) int {
count := 0
for _, v := range floor {
if v {
count++
}
}
return count
}