Skip to content

Instantly share code, notes, and snippets.

@okaq
Created April 20, 2013 19:16
Show Gist options
  • Save okaq/5427049 to your computer and use it in GitHub Desktop.
Save okaq/5427049 to your computer and use it in GitHub Desktop.
/*
* Google Code Jam 2013 Qualification Round
* Problem D. Treasure
*/
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
"strings"
"sync"
)
var (
inpath *string
outpath *string
infile *os.File
outfile *os.File
reader *bufio.Reader
writer *bufio.Writer
tests int
wg sync.WaitGroup
results []string
Treasures []*Treasure
)
type Treasure struct {
Case int // id number
Hands int // number of keys in hand at start
Chests int // number of chests
Hand []int // types of keys in hand
ChestList []*Chest // chests slice
CC map[int] int // chest counter
KC map[int] int // key counter
}
type Chest struct {
Id int // id number
Type int // type of Key to open
Keys int // number of keys inside
Key []int // keys slice
}
func (t0 *Treasure) Count() {
if t0.CC == nil {
t0.CC = make(map[int] int)
}
if t0.KC == nil {
t0.KC = make(map[int] int)
}
for c0 := range t0.ChestList {
t0.CC[t0.ChestList[c0].Type] = t0.CC[t0.ChestList[c0].Type] + 1
for k0 := range t0.ChestList[c0].Key {
t0.KC[t0.ChestList[c0].Key[k0]] = t0.KC[t0.ChestList[c0].Key[k0]] + 1
}
}
for k0 := range t0.Hand {
t0.KC[t0.Hand[k0]] = t0.KC[t0.Hand[k0]] + 1
}
}
func (t0 *Treasure) Possible() bool {
for k0, _ := range t0.CC {
if t0.CC[k0] < t0.KC[k0] {
return false
}
}
return true
}
func flags() {
inpath = flag.String("i", "D-small-practice.in", "Input filepath for the problem.")
outpath = flag.String("o", "D-small-practice.out", "Output filepath for the problem.")
}
func files() {
var err error
infile, err = os.Open(*inpath)
if err != nil {
fmt.Println(err)
}
outfile, err = os.Create(*outpath)
if err != nil {
fmt.Println(err)
}
reader = bufio.NewReader(infile)
writer = bufio.NewWriter(outfile)
}
func cleanup() {
var err error
err = infile.Close()
if err != nil {
fmt.Println(err)
}
err = outfile.Close()
if err != nil {
fmt.Println(err)
}
}
func parse() {
l0, p0, e0 := reader.ReadLine()
if e0 != nil {
fmt.Println(e0, p0)
}
tests, _ = strconv.Atoi(string(l0))
results = make([]string, tests)
Treasures = make([]*Treasure, tests)
for i := 0; i < tests; i++ {
l1, p1, e1 := reader.ReadLine()
if e1 != nil {
fmt.Println(e1, p1)
}
t0 := new(Treasure)
t0.Case = i
s0 := string(l1)
a0 := strings.Split(s0, " ")
t0.Hands, _ = strconv.Atoi(a0[0])
t0.Chests, _ = strconv.Atoi(a0[1])
l2, p2, e2 := reader.ReadLine()
if e2 != nil {
fmt.Println(e2, p2)
}
s1 := string(l2)
a1 := strings.Split(s1, " ")
t0.Hand = atoi(a1)
t0.ChestList = make([]*Chest, t0.Chests)
for j := 0; j < t0.Chests; j++ {
c0 := new(Chest)
c0.Id = j
l3, p3, e3:= reader.ReadLine()
if e3 != nil {
fmt.Println(e2, p3)
}
s2 := string(l3)
a2 := strings.Split(s2, " ")
c0.Type, _ = strconv.Atoi(a2[0])
c0.Keys, _ = strconv.Atoi(a2[1])
c0.Key = make([]int, c0.Keys)
copy(c0.Key, atoi(a2[2:len(a2)]))
t0.ChestList[j] = c0
}
Treasures[i] = t0
Treasures[i].Count()
fmt.Printf("%d: %t\n", i, Treasures[i].Possible())
}
}
func atoi(s0 []string) []int {
i0 := make([]int, len(s0))
for i := range s0 {
i0[i], _ = strconv.Atoi(s0[i])
}
return i0
}
func main() {
flags()
files()
defer cleanup()
parse()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment