Skip to content

Commit 74ad2ad

Browse files
committed
day2: solution for second part
1 parent 50f1ec9 commit 74ad2ad

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

day2/day2.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
const (
11-
rock = iota
11+
rock = iota + 1
1212
paper
1313
scissor
1414
)
@@ -53,19 +53,53 @@ func main() {
5353
}
5454

5555
opponent := parseToken(tokens[0])
56-
me := parseToken(tokens[1])
5756

58-
totalScore += me + 1
57+
if false {
58+
me := parseToken(tokens[1])
59+
// part 1
60+
totalScore += me
5961

60-
if me == opponent {
61-
// draw
62-
totalScore += 3
63-
}
62+
if me == opponent {
63+
// draw
64+
totalScore += 3
65+
}
66+
67+
if ((me == rock) && (opponent == scissor)) ||
68+
((me == paper) && (opponent == rock)) ||
69+
((me == scissor) && (opponent == paper)) {
70+
// win
71+
totalScore += 6
72+
}
73+
} else {
74+
// part 2
75+
me := tokens[1]
6476

65-
if ((me == rock) && (opponent == scissor)) ||
66-
((me == paper) && (opponent == rock)) ||
67-
((me == scissor) && (opponent == paper)) {
68-
totalScore += 6
77+
if me == "X" {
78+
// need to lose
79+
switch opponent {
80+
case rock:
81+
totalScore += scissor
82+
case paper:
83+
totalScore += rock
84+
case scissor:
85+
totalScore += paper
86+
}
87+
} else if me == "Y" {
88+
// draw
89+
totalScore += opponent
90+
totalScore += 3
91+
} else if me == "Z" {
92+
// need to win
93+
totalScore += 6
94+
switch opponent {
95+
case rock:
96+
totalScore += paper
97+
case paper:
98+
totalScore += scissor
99+
case scissor:
100+
totalScore += rock
101+
}
102+
}
69103
}
70104
}
71105

0 commit comments

Comments
 (0)