-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (74 loc) · 1.33 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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
func main() {
step1("input.txt")
step2("input.txt")
}
func step1(filename string) {
// elem := [3]string{"rock", "paper", "scissor"}
dataAsString := load(filename)
score := 0
for _, line := range strings.Split(dataAsString, "\n") {
them := int(line[0]) // 65
us := int(line[2]) // 88
score += us - 87
switch (them - 65) - (us - 88) {
case 0: // draw
score += 3
case 1, -2: // lose
score += 0
case -1, 2: // win
score += 6
}
}
fmt.Println("step1 -->", score)
}
func step2(filename string) {
dataAsString := load(filename)
score := 0
for _, line := range strings.Split(dataAsString, "\n") {
them := int(line[0]) // 65
outcome := int(line[2]) // 88
us := 0
switch outcome - 88 {
case 0: // lose
us = them - 65 - 1
if us == -1 {
us = 2
}
score += 0
case 1: // draw
us = them - 65
score += 3
case 2: // win
us = them - 65 + 1
if us == 3 {
us = 0
}
score += 6
}
score += us + 1
}
fmt.Println("step2 -->", score)
}
func load(filename string) string {
text, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Print(err)
}
return string(text)
}
func atoi(line string) int {
number, err := strconv.Atoi(line)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return number
}