-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (75 loc) · 1.55 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
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2020", "22", "input.txt"))
if err != nil {
panic(err)
}
return input
}
func round(player1Deck, player2Deck []int) ([]int, []int) {
player1Card := player1Deck[0]
player2Card := player2Deck[0]
player1Deck = player1Deck[1:]
player2Deck = player2Deck[1:]
if player1Card > player2Card {
player1Deck = append(player1Deck, player1Card, player2Card)
} else {
player2Deck = append(player2Deck, player2Card, player1Card)
}
return player1Deck, player2Deck
}
func solve(r io.Reader) {
scanner := bufio.NewScanner(r)
var player1Deck []int
var player2Deck []int
zone := 0
for scanner.Scan() {
row := scanner.Text()
if row == "" {
zone += 1
continue
}
if strings.Index(row, "Player") == 0 {
continue
}
val, err := strconv.Atoi(row)
if err != nil {
panic(err)
}
switch zone {
case 0:
player1Deck = append(player1Deck, val)
case 1:
player2Deck = append(player2Deck, val)
default:
panic(zone)
}
}
if scanner.Err() != nil {
panic(scanner.Err())
}
for len(player1Deck) != 0 && len(player2Deck) != 0 {
player1Deck, player2Deck = round(player1Deck, player2Deck)
}
fmt.Println(score(player1Deck) + score(player2Deck))
}
func score(deck []int) int {
score := 0
for index, card := range deck {
score += (len(deck) - index) * card
}
return score
}
func main() {
solve(strings.NewReader("Player 1:\n9\n2\n6\n3\n1\n\nPlayer 2:\n5\n8\n4\n7\n10"))
solve(input())
}