adventofcode/2015/day16/main.go

71 lines
1.2 KiB
Go
Raw Normal View History

2016-12-16 22:21:15 +00:00
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
var req map[string]int
func main() {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
req = make(map[string]int)
req["children"] = 3
req["cats"] = 7
req["samoyeds"] = 2
req["pomeranians"] = 3
req["akitas"] = 0
req["vizslas"] = 0
req["goldfish"] = 5
req["trees"] = 3
req["cars"] = 2
req["perfumes"] = 1
for i := range input {
if checkString(input[i]) {
fmt.Println(input[i])
break
}
}
}
func checkString(s string) bool {
split := strings.SplitN(s, ": ", 2)
items := strings.Split(split[1], ", ")
for i := range items {
tst := strings.Split(items[i], ": ")
if tst[0] == "trees" || tst[0] == "cats" {
if req[tst[0]] > mustAtoi(tst[1]) {
return false
}
} else if tst[0] == "pomeranians" || tst[0] == "goldfish" {
if req[tst[0]] < mustAtoi(tst[1]) {
return false
}
} else {
if req[tst[0]] != mustAtoi(tst[1]) {
return false
}
}
}
return true
}
func mustAtoi(s string) int {
var i int
var err error
if i, err = strconv.Atoi(s); err != nil {
fmt.Println("Tried to atoi " + s)
os.Exit(1)
}
return i
}