Skip to content

Commit 7f587a8

Browse files
committed
2022 Day 02 Part 2
1 parent d77a683 commit 7f587a8

File tree

1 file changed

+75
-51
lines changed

1 file changed

+75
-51
lines changed

2022/day-02/main.go

Lines changed: 75 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,101 @@ import (
88

99
func main() {
1010
var score int
11+
//test := []string{
12+
// "A Y",
13+
// "B X",
14+
// "C Z",
15+
//}
16+
1117
for _, round := range loadInput() {
18+
1219
shapes := strings.Split(round, " ")
13-
me := getValue(shapes[1])
14-
15-
if (shapes[0] == "A" && shapes[1] == "X") ||
16-
(shapes[0] == "B" && shapes[1] == "Y") ||
17-
(shapes[0] == "C" && shapes[1] == "Z") {
18-
// draw = 3
19-
log.Printf("[DRAW] score(%v) + draw(3) + shape(%v)", score, me)
20-
score = score + DRAW + me
21-
} else if (shapes[0] == "A" && shapes[1] == "Y") ||
22-
(shapes[0] == "B" && shapes[1] == "Z") ||
23-
(shapes[0] == "C" && shapes[1] == "X") {
24-
// win = 6
25-
log.Printf("[WIN] score(%v) + win(6) + shape(%v)", score, me)
26-
score = score + WIN + me
27-
} else if (shapes[0] == "A" && shapes[1] == "Z") ||
28-
(shapes[0] == "B" && shapes[1] == "X") ||
29-
(shapes[0] == "C" && shapes[1] == "Y") {
30-
// lose = 0
31-
log.Printf("[LOSE] score(%v) + lose(0) + shape(%v)", score, me)
32-
score = score + LOSE + me
20+
21+
elfKey := shapes[0]
22+
outcomeKey := shapes[1]
23+
if elfKey == "A" && outcomeKey == DRAW {
24+
// ROCK(A) vs DRAW(Y=3) = ROCK(A=1)
25+
score = score + drawInt + 1
26+
}
27+
28+
if elfKey == "B" && outcomeKey == LOSE {
29+
// PAPER(B) vs LOSE(X=0) = ROCK(A=1)
30+
score = score + loseInt + 1
31+
}
32+
33+
if elfKey == "C" && outcomeKey == WIN {
34+
// SCISSORS(C) vs WIN(Z=6) = ROCK(A=1)
35+
score = score + winInt + 1
36+
}
37+
38+
if elfKey == "B" && outcomeKey == DRAW {
39+
// PAPER(B) vs DRAW(Y=3) = PAPER(B=2)
40+
score = score + drawInt + 2
41+
}
42+
43+
if elfKey == "C" && outcomeKey == LOSE {
44+
// SCISSORS(C) vs LOSE(X=0) = PAPER(B=2)
45+
score = score + loseInt + 2
46+
}
47+
48+
if elfKey == "A" && outcomeKey == WIN {
49+
// ROCK(A) vs WIN(Z=6) = PAPER(B=2)
50+
score = score + winInt + 2
51+
}
52+
53+
if elfKey == "C" && outcomeKey == DRAW {
54+
// SCISSORS(C) vs DRAW(Y=3) = SCISSORS(C=3)
55+
score = score + drawInt + 3
3356
}
57+
58+
if elfKey == "A" && outcomeKey == LOSE {
59+
// ROCK(A) vs LOSE(X=0) = SCISSORS(C=3)
60+
score = score + loseInt + 3
61+
}
62+
63+
if elfKey == "B" && outcomeKey == WIN {
64+
// PAPER(B) vs WIN(Z=6) = SCISSORS(C=3)
65+
score = score + winInt + 3
66+
}
67+
3468
}
3569
log.Println("score:", score) // 15632
3670
}
3771

3872
func getValue(shape string) int {
3973
switch shape {
40-
case "A", "X":
74+
case "A":
4175
return 1
42-
case "B", "Y":
76+
case "B":
4377
return 2
44-
case "C", "Z":
78+
case "C":
4579
return 3
80+
// WIN = 6 // Z
81+
// DRAW = 3 // Y
82+
// LOSE = 0 // X
4683
default:
4784
log.Fatalln("[ERROR] unknown shape:", shape)
4885
return 0
4986
}
5087
}
5188

52-
// rock vs rock = draw
53-
// rock vs paper = lose
54-
// rock vs scissors = win
55-
56-
// paper vs paper = draw
57-
// paper vs scissors = lose
58-
// paper vs rock = win
59-
60-
// scissors vs scissors = draw
61-
// scissors vs rock = lose
62-
// scissors vs paper = win
63-
64-
/////////////////////////////////
65-
66-
// A vs X = draw
67-
// B vs Y = draw
68-
// C vs Z = draw
69-
70-
// A vs Y = win
71-
// B vs Z = win
72-
// C vs X = win
73-
74-
// A vs Z = lose
75-
// B vs X = lose
76-
// C vs Y = lose
89+
///////////////////////////////////
7790

7891
const (
79-
WIN = 6
80-
DRAW = 3
81-
LOSE = 0
92+
WIN = "Z" // 6 // Z
93+
DRAW = "Y" // 3 // Y
94+
LOSE = "X" // 0 // X
95+
winInt = 6 // Z
96+
drawInt = 3 // Y
97+
loseInt = 0 // X
98+
)
99+
100+
var (
101+
outcomes = map[string]int{
102+
"Z": 6,
103+
"Y": 3,
104+
"X": 0,
105+
}
82106
)
83107

84108
////////////////////

0 commit comments

Comments
 (0)