package main
import (
"bufio"
"fmt"
"os"
"sort"
)
const (
elf = 'E'
goblin = 'G'
wall = '#'
open = '.'
)
type Point struct {
x, y int
}
type Unit struct {
position Point
hp int
power int
race rune
}
type ByReadingOrder []Point
func (a ByReadingOrder) Len() int { return len(a) }
func (a ByReadingOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByReadingOrder) Less(i, j int) bool {
if a[i].y == a[j].y {
return a[i].x < a[j].x
}
return a[i].y < a[j].y
}
func readInput(filename string) ([][]rune, []*Unit) {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
var units []*Unit
scanner := bufio.NewScanner(file)
var area [][]rune
y := 0
for scanner.Scan() {
line := scanner.Text()
row := make([]rune, len(line))
for x, char := range line {
switch char {
case elf, goblin:
units = append(units, &Unit{Point{x, y}, 200, 3, char})
}
row[x] = char
}
area = append(area, row)
y++
}
if err := scanner.Err(); err != nil {
panic(err)
}
return area, units
}
func isAdjacent(a, b Point) bool {
return (a.x == b.x && (a.y == b.y+1 || a.y == b.y-1)) || (a.y == b.y && (a.x == b.x+1 || a.x == b.x-1))
}
func findTargets(unit *Unit, units []*Unit) []*Unit {
var targets []*Unit
for _, u := range units {
if u.race != unit.race && u.hp > 0 {
targets = append(targets, u)
}
}
return targets
}
func findReachable(area [][]rune, from Point, targets []*Unit) []Point {
inRange := make(map[Point]bool)
for _, target := range targets {
adjacent := []Point{
{target.position.x, target.position.y - 1},
{target.position.x - 1, target.position.y},
{target.position.x + 1, target.position.y},
{target.position.x, target.position.y + 1},
}
for _, p := range adjacent {
if area[p.y][p.x] == open {
inRange[p] = true
}
}
}
var reachable []Point
visited := make(map[Point]bool)
queue := []Point{from}
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
if visited[current] {
continue
}
visited[current] = true
if inRange[current] {
reachable = append(reachable, current)
}
adjacent := []Point{
{current.x, current.y - 1},
{current.x - 1, current.y},
{current.x + 1, current.y},
{current.x, current.y + 1},
}
for _, p := range adjacent {
if area[p.y][p.x] == open && !visited[p] {
queue = append(queue, p)
}
}
}
return reachable
}
func findNearest(area [][]rune, from Point, targets []Point) Point {
distances := make(map[Point]int)
queue := []Point{from}
distances[from] = 0
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
adjacent := []Point{
{current.x, current.y - 1},
{current.x - 1, current.y},
{current.x + 1, current.y},
{current.x, current.y + 1},
}
for _, p := range adjacent {
if _, ok := distances[p]; !ok && area[p.y][p.x] == open {
distances[p] = distances[current] + 1
queue = append(queue, p)
}
}
}
var nearest Point
minDistance := int(^uint(0) >> 1) // Max int value
for _, target := range targets {
if distance, ok := distances[target]; ok {
if distance < minDistance {
minDistance = distance
nearest = target
} else if distance == minDistance && ByReadingOrder{target, nearest}.Less(0, 1) {
nearest = target
}
}
}
return nearest
}
func moveUnit(area [][]rune, unit *Unit, destination Point) {
distances := make(map[Point]int)
prev := make(map[Point]Point)
queue := []Point{unit.position}
distances[unit.position] = 0
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
adjacent := []Point{
{current.x, current.y - 1},
{current.x - 1, current.y},
{current.x + 1, current.y},
{current.x, current.y + 1},
}
for _, p := range adjacent {
if _, ok := distances[p]; !ok && area[p.y][p.x] == open {
distances[p] = distances[current] + 1
prev[p] = current
queue = append(queue, p)
}
}
}
current := destination
for prev[current] != unit.position {
current = prev[current]
}
area[unit.position.y][unit.position.x] = open
unit.position = current
area[unit.position.y][unit.position.x] = unit.race
}
func attackUnit(unit *Unit, targets []*Unit) *Unit {
var target *Unit
for _, t := range targets {
if isAdjacent(unit.position, t.position) {
if target == nil || t.hp < target.hp || (t.hp == target.hp && ByReadingOrder{t.position, target.position}.Less(0, 1)) {
target = t
}
}
}
if target != nil {
target.hp -= unit.power
}
return target
}
func simulateCombat(area [][]rune, units []*Unit) (int, int) {
rounds := 0
for {
sort.Slice(units, func(i, j int) bool {
return ByReadingOrder{units[i].position, units[j].position}.Less(0, 1)
})
for i, unit := range units {
if unit.hp <= 0 {
continue
}
targets := findTargets(unit, units)
if len(targets) == 0 {
return rounds, sumHitPoints(units)
}
inRange := findReachable(area, unit.position, targets)
if len(inRange) > 0 && !isAdjacentToTarget(unit.position, targets) {
nearest := findNearest(area, unit.position, inRange)
moveUnit(area, unit, nearest)
}
target := attackUnit(unit, targets)
if target != nil && target.hp <= 0 {
area[target.position.y][target.position.x] = open
}
}
units = filterAlive(units)
if len(units) == 0 || len(findTargets(units[0], units)) == 0 {
break
}
rounds++
}
}
func isAdjacentToTarget(position Point, targets []*Unit) bool {
for _, target := range targets {
if isAdjacent(position, target.position) {
return true
}
}
return false
}
func sumHitPoints(units []*Unit) int {
sum := 0
for _, unit := range units {
if unit.hp > 0 {
sum += unit.hp
}
}
return sum
}
func filterAlive(units []*Unit) []*Unit {
var alive []*Unit
for _, unit := range units {
if unit.hp > 0 {
alive = append(alive, unit)
}
}
return alive
}
func main() {
area, units := readInput("input.txt")
rounds, hpSum := simulateCombat(area, units)
outcome := rounds * hpSum
fmt.Printf("Outcome of the battle: %d\n", outcome)
}