-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path170.go
64 lines (55 loc) · 1.29 KB
/
170.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
// UVa 170 - Clock Patience
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type card struct{ rank, suite byte }
var rankMap = map[byte]int{'A': 0, '2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12}
func solve(piles [][]card) (int, card) {
var last card
var exposed int
for idx := 12; len(piles[idx]) > 0; {
last = piles[idx][0]
piles[idx] = piles[idx][1:]
idx = rankMap[last.rank]
exposed++
}
return exposed, last
}
func deal(cards []card) [][]card {
piles := make([][]card, 13)
for i := len(cards) - 1; i >= 0; i-- {
idx := (len(cards) - 1 - i) % 13
piles[idx] = append([]card{{cards[i].rank, cards[i].suite}}, piles[idx]...)
}
return piles
}
func main() {
in, _ := os.Open("170.in")
defer in.Close()
out, _ := os.Create("170.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var line, token string
var cards []card
for s.Scan() {
if line = s.Text(); line == "#" {
break
}
for r := strings.NewReader(line); ; {
if _, err := fmt.Fscanf(r, "%s", &token); err != nil {
break
}
cards = append(cards, card{token[0], token[1]})
}
if len(cards) == 52 {
exposed, last := solve(deal(cards))
fmt.Fprintf(out, "%2d,%c%c\n", exposed, last.rank, last.suite)
cards = nil
}
}
}