Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4ecb07e

Browse filesBrowse files
committed
Day 02 Part 02
1 parent f835cce commit 4ecb07e
Copy full SHA for 4ecb07e

File tree

2 files changed

+96
-13
lines changed
Filter options

2 files changed

+96
-13
lines changed

‎day02/main.go

Copy file name to clipboardExpand all lines: day02/main.go
+90-12Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,63 @@ type move struct {
1515
oppo string
1616
my string
1717
}
18+
type move2 struct {
19+
oppo string
20+
outcome string
21+
}
22+
23+
func (m move2) score() int {
24+
return m.scoreOutcome() + m.scoreMyMove()
25+
}
26+
func (m move2) scoreOutcome() int {
27+
if m.outcome == "X" {
28+
return 0
29+
} else if m.outcome == "Y" {
30+
return 3
31+
} else if m.outcome == "Z" {
32+
return 6
33+
}
34+
panic(m.outcome)
35+
}
36+
func (m move2) scoreMyMove() int {
37+
var myShape string
38+
if m.outcome == "Y" {
39+
myShape = m.oppo
40+
} else if m.outcome == "X" {
41+
if m.oppo == "A" {
42+
myShape = "C"
43+
} else if m.oppo == "B" {
44+
myShape = "A"
45+
} else {
46+
myShape = "B"
47+
}
48+
} else {
49+
if m.oppo == "A" {
50+
myShape = "B"
51+
} else if m.oppo == "B" {
52+
myShape = "C"
53+
} else {
54+
myShape = "A"
55+
}
56+
}
57+
58+
return scoreMyShape(myShape)
59+
}
1860

1961
func (m move) score() int {
20-
return m.scoreMove() + m.scoreMyShape()
62+
return m.scoreMove() + scoreMyShape(m.my)
2163
}
22-
func (m move) scoreMyShape() int {
23-
if m.my == "X" {
64+
func scoreMyShape(shape string) int {
65+
if shape == "X" || shape == "A" {
2466
return 1
25-
} else if m.my == "Y" {
67+
} else if shape == "Y" || shape == "B" {
2668
return 2
27-
} else if m.my == "Z" {
69+
} else if shape == "Z" || shape == "C" {
2870
return 3
2971
}
30-
panic(m.my)
72+
panic(shape)
3173
}
74+
3275
func (m move) scoreMove() int {
3376
if (m.oppo == "A" && m.my == "X") ||
3477
(m.oppo == "B" && m.my == "Y") ||
@@ -43,7 +86,16 @@ func (m move) scoreMove() int {
4386
}
4487
}
4588

46-
func scoreMoves(moves []move) []int {
89+
func scoreMoves1(moves []move) []int {
90+
scores := make([]int, len(moves))
91+
92+
for i, m := range moves {
93+
scores[i] = m.score()
94+
}
95+
96+
return scores
97+
}
98+
func scoreMoves2(moves []move2) []int {
4799
scores := make([]int, len(moves))
48100

49101
for i, m := range moves {
@@ -53,7 +105,7 @@ func scoreMoves(moves []move) []int {
53105
return scores
54106
}
55107

56-
func parseInput(input string) []move {
108+
func parseInput1(input string) []move {
57109
lines := strings.Split(input, "\n")
58110
var moves []move
59111

@@ -69,15 +121,41 @@ func parseInput(input string) []move {
69121

70122
return moves
71123
}
124+
func parseInput2(input string) []move2 {
125+
lines := strings.Split(input, "\n")
126+
var moves []move2
127+
128+
for _, line := range lines {
129+
if len(line) < 3 {
130+
break
131+
}
132+
133+
oppoMove := string(line[0])
134+
outcome := string(line[2])
135+
moves = append(moves, move2{oppoMove, outcome})
136+
}
137+
138+
return moves
139+
}
72140

73141
func partOne(input string) int {
74-
moves := parseInput(input)
75-
scores := scoreMoves(moves)
142+
moves := parseInput1(input)
143+
scores := scoreMoves1(moves)
144+
// fmt.Printf("%v\n", scores)
145+
sum := util.SumIntSlice(scores)
146+
return sum
147+
}
148+
func partTwo(input string) int {
149+
moves := parseInput2(input)
150+
scores := scoreMoves2(moves)
76151
// fmt.Printf("%v\n", scores)
77152
sum := util.SumIntSlice(scores)
78153
return sum
79154
}
80155
func main() {
81-
score := partOne(input2)
82-
fmt.Printf("%d\n", score)
156+
score1 := partOne(input2)
157+
fmt.Printf("%d\n", score1)
158+
159+
score2 := partTwo(input2)
160+
fmt.Printf("%d\n", score2)
83161
}

‎day02/main_test.go

Copy file name to clipboardExpand all lines: day02/main_test.go
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import (
1111
var input1 string
1212

1313
func TestOne(t *testing.T) {
14-
assert.Equal(t, 3, len(parseInput(input1)), "they should be equal")
14+
assert.Equal(t, 3, len(parseInput1(input1)), "they should be equal")
1515
assert.Equal(t, 15, partOne(input1), "they should be equal")
1616
// assert.Equal(t, 66616, partOne(input2), "they should be equal")
1717
}
18+
func TestTwo(t *testing.T) {
19+
assert.Equal(t, 3, len(parseInput2(input1)), "they should be equal")
20+
assert.Equal(t, 12, partTwo(input1), "they should be equal")
21+
// assert.Equal(t, 66616, partOne(input2), "they should be equal")
22+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.