package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	inp := StdinToStringSlice()
	fmt.Println("# Part 1")
	Pt1Combat(inp)

	fmt.Println("# Part 2")
  Pt2Combat(inp)
}

func Pt1Combat(input []string) int {
	cave := NewCave(input, defaultPower)
	for i := 1; true; i++ {
		hp, combat := cave.Status()
		if !combat {
      res := (i - 1) * hp
      fmt.Printf("Result: %d\n", res)
			return res
		}
		if cleanRound, _ := cave.Tick(false); !cleanRound {
			i--
		}
	}
	return -1
}

func Pt2Combat(input []string) int {
	for power := defaultPower; true; power++ {
		cave := NewCave(input, power)
		for i := 1; true; i++ {
			hp, combat := cave.Status()
			if !combat {
        res := (i - 1) * hp
        fmt.Printf("Result: %d; Attack Power: %d\n", res, power)
				return (i - 1) * hp
			}
			if cleanRound, elfDied := cave.Tick(true); elfDied {
				break
			} else if !cleanRound {
				i--
			}
		}
	}
	return -1
}

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