adventofcode/2018/day15/day15.go

62 lines
1.1 KiB
Go
Raw Permalink Normal View History

2018-12-16 01:57:03 +00:00
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
2019-11-07 15:36:24 +00:00
inp := StdinToStringSlice()
fmt.Println("# Part 1")
Pt1Combat(inp)
2018-12-16 01:57:03 +00:00
2019-11-07 15:36:24 +00:00
fmt.Println("# Part 2")
Pt2Combat(inp)
2018-12-16 01:57:03 +00:00
}
2019-11-07 15:36:24 +00:00
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
2018-12-16 01:57:03 +00:00
}
2019-11-07 15:36:24 +00:00
if cleanRound, _ := cave.Tick(false); !cleanRound {
i--
2018-12-19 16:24:50 +00:00
}
}
2019-11-07 15:36:24 +00:00
return -1
2018-12-19 16:24:50 +00:00
}
2019-11-07 15:36:24 +00:00
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
2018-12-19 16:24:50 +00:00
}
2019-11-07 15:36:24 +00:00
if cleanRound, elfDied := cave.Tick(true); elfDied {
break
} else if !cleanRound {
i--
2018-12-16 01:57:03 +00:00
}
}
}
2019-11-07 15:36:24 +00:00
return -1
2018-12-16 01:57:03 +00:00
}
2019-11-07 15:36:24 +00:00
func StdinToStringSlice() []string {
var input []string
2018-12-16 01:57:03 +00:00
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
2019-11-07 15:36:24 +00:00
input = append(input, scanner.Text())
2018-12-16 01:57:03 +00:00
}
2019-11-07 15:36:24 +00:00
return input
2018-12-16 01:57:03 +00:00
}