-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
81 lines (68 loc) · 1.48 KB
/
solution.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
package d02
import (
"fmt"
"io"
"github.com/Mike-Burton/advent-of-code/helpers"
)
const (
ROCK = 1
PAPER = 2
SCISSORS = 3
LOOSE = 0
DRAW = 3
WIN = 6
)
var scoreMap = map[string]int{
"A X": ROCK + DRAW,
"A Y": PAPER + WIN,
"A Z": SCISSORS + LOOSE,
"B X": ROCK + LOOSE,
"B Y": PAPER + DRAW,
"B Z": SCISSORS + WIN,
"C X": ROCK + WIN,
"C Y": PAPER + LOOSE,
"C Z": SCISSORS + DRAW,
}
var riggedMap = map[string]int{
"A X": LOOSE + SCISSORS,
"A Y": DRAW + ROCK,
"A Z": WIN + PAPER,
"B X": LOOSE + ROCK,
"B Y": DRAW + PAPER,
"B Z": WIN + SCISSORS,
"C X": LOOSE + PAPER,
"C Y": DRAW + SCISSORS,
"C Z": WIN + ROCK,
}
// PartOne solves the first problem of day 2 of Advent of Code 2022.
func PartOne(r io.Reader, w io.Writer) error {
lines, err := helpers.LinesFromReader(r)
if err != nil {
return fmt.Errorf("could not read input: %w", err)
}
score := 0
for _, l := range lines {
score += scoreMap[l]
}
_, err = fmt.Fprintf(w, "%d", score)
if err != nil {
return fmt.Errorf("could not write answer: %w", err)
}
return nil
}
// PartTwo solves the second problem of day 2 of Advent of Code 2022.
func PartTwo(r io.Reader, w io.Writer) error {
lines, err := helpers.LinesFromReader(r)
if err != nil {
return fmt.Errorf("could not read input: %w", err)
}
score := 0
for _, l := range lines {
score += riggedMap[l]
}
_, err = fmt.Fprintf(w, "%d", score)
if err != nil {
return fmt.Errorf("could not write answer: %w", err)
}
return nil
}