-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.go
109 lines (88 loc) · 2.64 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
104
105
106
107
108
109
/*
Copyright © 2022 Jacob Saporito
*/
package cmd
import (
"fmt"
"time"
"github.com/japorito/merry/libxmas/toybag"
"github.com/japorito/merry/libxmas/xmas"
"github.com/spf13/cobra"
)
func getWinningMovePoints(opponentPts int32) int32 {
return (opponentPts % 3) + 1 // 1 "Rock" beats 3 "Scissors"
}
func roShamBo(player1, player2 int32) (int32, int32) {
const DrawPoints, WinPoints, LosePoints int32 = 3, 6, 0
switch player1 {
case player2:
return DrawPoints + player1, DrawPoints + player2
case getWinningMovePoints(player2):
return WinPoints + player1, LosePoints + player2
default:
return LosePoints + player1, WinPoints + player2
}
}
func movePointCalculator(move string, oneVal rune) int32 {
zeroVal := oneVal - 1
return []rune(move)[0] - zeroVal
}
func misunderstoodCipherToPoints(cipher [][]string) int32 {
pts := makePointsSlice(len(cipher))
for idx, game := range cipher {
pts[0][idx], pts[1][idx] = roShamBo(movePointCalculator(game[0], 'A'), movePointCalculator(game[1], 'X'))
}
return sumInt32(pts[1])
}
func cipherToPoints(cipher [][]string) int32 {
const Lose, Draw, Win string = "X", "Y", "Z"
roundScores := make([]int32, len(cipher))
for i, game := range cipher {
switch game[1] {
case Draw:
roundScores[i] = movePointCalculator(game[0], 'A') + 3
case Win:
roundScores[i] = getWinningMovePoints(movePointCalculator(game[0], 'A')) + 6
case Lose:
score := movePointCalculator(game[0], 'A') - 1
if score == 0 {
score = 3 // 1 "Rock" beats 3 "Scissors"
}
roundScores[i] = score
}
}
return sumInt32(roundScores)
}
func sumInt32(input []int32) int32 {
sum := int32(0)
for _, addend := range input {
sum += addend
}
return sum
}
func makePointsSlice(length int) [][]int32 {
return [][]int32{make([]int32, length), make([]int32, length)}
}
// day2Cmd represents the day2 command
var day2Cmd = &cobra.Command{
Use: "day2 path/to/input/file",
Short: "Advent of Code Day 2",
Long: `Advent of Code Day 2: Rock Paper Scissors`,
Run: func(cmd *cobra.Command, args []string) {
defer xmas.PrintHolidayMessage(time.Now())
if input := toybag.ReadAsTokenizedStringSlice(args...); input != nil {
fmt.Printf("%d games of rock, paper, scissors in the cipher.\n", len(input))
if Parts.Has(1) {
fmt.Println("Part 1 running...")
fmt.Printf("My rock, paper, scissors score following the cipher would be **%d**.\n", misunderstoodCipherToPoints(input))
}
if Parts.Has(2) {
fmt.Println("Part 2 running...")
fmt.Printf("My rock, paper, scissor score when I actually understand the cipher would be **%d**.\n", cipherToPoints(input))
}
}
},
}
func init() {
rootCmd.AddCommand(day2Cmd)
}