-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (59 loc) · 1.73 KB
/
main.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
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"strings"
)
func one(d []byte) int {
scanner := bufio.NewScanner(bytes.NewReader(d))
scores := map[string]int{"A": 1, "B": 2, "C": 3, "X": 1, "Y": 2, "Z": 3}
score := 0
for scanner.Scan() {
play := strings.Split(scanner.Text(), " ")
score = score + scores[play[1]] // score for the shape played
if scores[play[1]] == 1 && scores[play[0]] == 3 {
score = score + 6 // score for WIN
} else if scores[play[1]] == 3 && scores[play[0]] == 1 {
score = score + 0 // score for LOSS
} else if scores[play[1]] > scores[play[0]] {
score = score + 6 // score for WIN
} else if scores[play[1]] == scores[play[0]] {
score = score + 3 // score for DRAW
} else {
score = score + 0 // score for LOSS
}
}
return score
}
func two(d []byte) int {
scanner := bufio.NewScanner(bytes.NewReader(d))
scores := map[string]int{"A": 1, "B": 2, "C": 3, "X": 0, "Y": 3, "Z": 6}
score := 0
for scanner.Scan() {
play := strings.Split(scanner.Text(), " ")
score = score + scores[play[1]] // score for the outcome
if scores[play[1]] == 6 && scores[play[0]] == 3 {
score = score + 1 // score for WIN
} else if scores[play[1]] == 6 {
score = score + scores[play[0]] + 1 // score for WIN
} else if scores[play[1]] == 0 && scores[play[0]] == 1 {
score = score + 3 // score for LOSS
} else if scores[play[1]] == 0 {
score = score + scores[play[0]] - 1 // score for LOSS
} else {
score = score + scores[play[0]] // score for DRAW
}
}
return score
}
func main() {
data, err := os.ReadFile("input.txt") // read input from file
if err != nil {
log.Fatal(err)
}
fmt.Printf("Part 1: Total score : %d\n", one(data))
fmt.Printf("Part 2: Total score : %d\n", two(data))
}