-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.go
103 lines (85 loc) · 1.58 KB
/
day2.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
package main
import (
_ "embed"
"strings"
)
//go:embed input/day2
var d2 string
func day2(input string) (int, int) {
lines := strings.Split(input, "\n")
points1 := 0
points2 := 0
for _, line := range lines {
chars := strings.Split(line, " ")
if len(chars) == 1 {
continue
}
points1 += calculatePointOfMyChoice(chars[1])
points1 += calculateRound(chars[0], chars[1])
switch chars[1] {
case "Y":
chars[1] = chars[0]
case "X":
chars[1] = convertToLoose(chars[0])
case "Z":
chars[1] = convertToWin(chars[0])
}
points2 += calculatePointOfMyChoice(chars[1])
points2 += calculateRound(chars[0], chars[1])
}
return points1, points2
}
func convertToWin(ch string) string {
if ch == "A" {
return "Y"
}
if ch == "B" {
return "Z"
}
if ch == "C" {
return "X"
}
return ""
}
func convertToLoose(ch string) string {
if ch == "A" {
return "Z"
}
if ch == "B" {
return "X"
}
if ch == "C" {
return "Y"
}
return ""
}
func calculateRound(ch1 string, ch2 string) int {
if isSissor(ch1) && isRock(ch2) || isRock(ch1) && isPaper(ch2) || isPaper(ch1) && isSissor(ch2) {
return 6
}
if isRock(ch1) && isRock(ch2) || isPaper(ch1) && isPaper(ch2) || isSissor(ch1) && isSissor(ch2) {
return 3
}
return 0
}
func calculatePointOfMyChoice(ch string) int {
if isRock(ch) {
return 1
}
if isPaper(ch) {
return 2
}
if isSissor(ch) {
return 3
}
return 0
}
func isRock(ch string) bool {
return ch == "X" || ch == "A"
}
func isPaper(ch string) bool {
return ch == "Y" || ch == "B"
}
func isSissor(ch string) bool {
return ch == "Z" || ch == "C"
}