package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {
	input := stdinToStringSlice()
	puzzleOne(input)
	puzzleTwo(input)
}

func puzzleOne(input []string) {
	fmt.Println("=== Puzzle One ===")
	var numGood int
	var sides []int
	for i := range input {
		pts := strings.Fields(input[i])
		sides = []int{atoi(pts[0]), atoi(pts[1]), atoi(pts[2])}
		if isValidTriangle(sides) {
			numGood++
		}
	}
	fmt.Println("Found Good: ", numGood)
}

func puzzleTwo(input []string) {
	fmt.Println("=== Puzzle Two ===")
	var numGood int
	var sides1, sides2, sides3 []int
	for i := range input {
		pts := strings.Fields(input[i])
		sides1 = append(sides1, atoi(pts[0]))
		sides2 = append(sides2, atoi(pts[1]))
		sides3 = append(sides3, atoi(pts[2]))
		if len(sides1) == 3 {
			if isValidTriangle(sides1) {
				numGood++
			}
			if isValidTriangle(sides2) {
				numGood++
			}
			if isValidTriangle(sides3) {
				numGood++
			}
			sides1 = []int{}
			sides2 = []int{}
			sides3 = []int{}
		}
	}
	fmt.Println("Found Good: ", numGood)
}

func isValidTriangle(sides []int) bool {
	sort.Ints(sides)
	if sides[2] < (sides[1] + sides[0]) {
		return true
	}
	return false
}

func itoa(i int) string {
	return strconv.Itoa(i)
}

func atoi(i string) int {
	var ret int
	var err error
	if ret, err = strconv.Atoi(i); err != nil {
		log.Fatal("Invalid Atoi")
	}
	return ret
}

func stdinToStringSlice() []string {
	var input []string
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		input = append(input, scanner.Text())
	}
	return input
}
