Last active
August 29, 2015 14:16
-
-
Save panchiz/3180b7965894e9847b1f to your computer and use it in GitHub Desktop.
Problem A. Magic Trick https://code.google.com/codejam/contest/2974486/dashboard#s=p0
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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func usage() { | |
if len(os.Args) <= 1 { | |
fmt.Fprintf(os.Stderr, "usage: %s [inputfile]\n", os.Args[0]) | |
os.Exit(2) | |
} | |
} | |
func parseInt(s string) int { | |
i, _ := strconv.ParseInt(s, 10, 0) | |
return int(i) | |
} | |
func parseCards(scanner *bufio.Scanner) (cards [4][4]int) { | |
for i := 0; i < 4; i++ { | |
scanner.Scan() | |
line := scanner.Text() | |
for j, v := range strings.Split(line, " ") { | |
cards[i][j] = parseInt(v) | |
} | |
} | |
return cards | |
} | |
func main() { | |
usage() | |
file, err := os.Open(os.Args[1]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
scanner.Scan() | |
number_of_tests := parseInt(scanner.Text()) | |
//fmt.Println("Test: ", scanner.Text()) | |
for test_id := 1; test_id <= int(number_of_tests); test_id++ { | |
scanner.Scan() | |
chosen1 := parseInt(scanner.Text()) | |
cards1 := parseCards(scanner) | |
scanner.Scan() | |
chosen2 := parseInt(scanner.Text()) | |
cards2 := parseCards(scanner) | |
//fmt.Printf("chosen %d: %d\n", test_id, chosen1) | |
//fmt.Println(cards1) | |
//fmt.Printf("chosen %d: %d\n", test_id, chosen2) | |
//fmt.Println(cards2) | |
matches, matched := findMatches(cards1, cards2, chosen1, chosen2) | |
output(test_id, matches, matched) | |
} | |
} | |
func findMatches(cards1, cards2 [4][4]int, chosen1, chosen2 int) (matches, matched int) { | |
for _, v1 := range cards1[chosen1-1] { | |
for _, v2 := range cards2[chosen2-1] { | |
if v1 == v2 { | |
matches += 1 | |
matched = v1 | |
} | |
} | |
} | |
return matches, matched | |
} | |
func output(case_id, matches, matched int) { | |
var answer string | |
switch matches { | |
case 0: | |
answer = "Volunteer cheated!" | |
case 1: | |
answer = strconv.Itoa(matched) | |
case 2, 3, 4: | |
answer = "Bad magician!" | |
} | |
fmt.Printf("Case #%d: %s\n", case_id, answer) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment