-
-
Save okaq/720a7fa9c7a182862283 to your computer and use it in GitHub Desktop.
Google Code Jam 2015 * Qualification Round * Problem D. Ominous Omino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* Google Code Jam | |
* 2015 Qualification Round | |
* Problem D. Ominous Omino | |
* AQ <[email protected]> | |
*/ | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
"sync" | |
"time" | |
) | |
const ( | |
IN = "D-large-practice.in" | |
OUT = "D-large-practice.out" | |
) | |
var ( | |
I *os.File | |
O *os.File | |
S *bufio.Scanner | |
W *bufio.Writer | |
T int | |
Omi []*Omino | |
WG sync.WaitGroup | |
) | |
type Omino struct { | |
X int | |
R int | |
C int | |
W string | |
} | |
func NewOmino() *Omino { | |
return &Omino{} | |
} | |
func (o *Omino) Solve() { | |
o.W = "GABRIEL" | |
if o.Richard() { | |
o.W = "RICHARD" | |
} | |
WG.Done() | |
} | |
// case-by-case test | |
// for richard win conditions | |
func (o *Omino) Richard() bool { | |
s := o.Min() | |
l := o.Max() | |
if ((s * l) % o.X) != 0 { | |
return true | |
} | |
if (o.X == 3) && (s == 1) { | |
return true | |
} | |
if (o.X == 4) && (s <= 2) { | |
return true | |
} | |
if (o.X == 5) && ((s <= 2) || ((s == 3) && (l == 5))) { | |
return true | |
} | |
if (o.X == 6) && (s <= 3) { | |
return true | |
} | |
if o.X >= 7 { | |
return true | |
} | |
return false | |
} | |
func (o *Omino) Min() int { | |
if o.R < o.C { | |
return o.R | |
} else { | |
return o.C | |
} | |
} | |
func (o *Omino) Max() int { | |
if o.R > o.C { | |
return o.R | |
} else { | |
return o.C | |
} | |
} | |
func Load() { | |
var err error | |
I, err = os.Open(IN) | |
if err != nil { | |
fmt.Println(err) | |
} | |
S = bufio.NewScanner(I) | |
O, err = os.Create(OUT) | |
if err != nil { | |
fmt.Println(err) | |
} | |
W = bufio.NewWriter(O) | |
} | |
func Cases() { | |
var err error | |
S.Scan() | |
T, err = strconv.Atoi(S.Text()) | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Printf("%d test cases.\n", T) | |
} | |
func Split() { | |
var err error | |
Omi = make([]*Omino, T) | |
for i := 0; i < T; i++ { | |
o := NewOmino() | |
S.Scan() | |
a := strings.Split(S.Text(), " ") | |
o.X, err = strconv.Atoi(a[0]) | |
o.R, err = strconv.Atoi(a[1]) | |
o.C, err = strconv.Atoi(a[2]) | |
if err != nil { | |
fmt.Println(err) | |
} | |
// fmt.Println(o) | |
Omi[i] = o | |
WG.Add(1) | |
go Omi[i].Solve() | |
} | |
} | |
func Finish() { | |
defer I.Close() | |
defer O.Close() | |
for i := 0; i < T; i++ { | |
s0 := fmt.Sprintf("Case #%d: %s\n", i+1, Omi[i].W) | |
W.WriteString(s0) | |
} | |
W.Flush() | |
} | |
func main() { | |
begin := time.Now() | |
fmt.Println("ok omino!") | |
Load() | |
Cases() | |
Split() | |
WG.Wait() | |
Finish() | |
end := time.Now() | |
fmt.Printf("total run time: %v.\n", end.Sub(begin)) | |
} | |
// rules based truth table from analysis | |
// total run time: 2ms large input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment