package main

import (
	"fmt"

	h "git.bullercodeworks.com/brian/adventofcode/helpers"
)

func main() {
	fmt.Println("vim-go")
	inp := h.StdinToStringSlice()
	field := h.StringSliceToCoordByteMap(inp)
	part1(field)
}

func part1(f h.CoordByteMap) {
	// Run until no cucumbers move
	var steps int
	var done bool
	for !done {
		east := f.FindAll('>')
		eastMovers := make(map[h.Coordinate]h.Coordinate)
		for i := range east {
			var check h.Coordinate
			if east[i].X == f.BRX {
				check = h.Coordinate{
					X: f.TLX,
					Y: east[i].Y,
				}
			} else {
				check = east[i].East()
			}
			if f.Get(check) == '.' {
				eastMovers[east[i]] = check
			}
		}
		if len(eastMovers) == 0 {
			done = true
		} else {
			for k, v := range eastMovers {
				f.Put(k, '.')
				f.Put(v, '>')
			}
		}

		south := f.FindAll('v')
		southMovers := make(map[h.Coordinate]h.Coordinate)
		for i := range south {
			var check h.Coordinate
			if south[i].Y == f.BRY {
				check = h.Coordinate{
					X: south[i].X,
					Y: f.TLY,
				}
			} else {
				check = south[i].South()
			}
			if f.Get(check) == '.' {
				southMovers[south[i]] = check
			}
		}
		if len(southMovers) == 0 {
			done = done && true
		} else {
			for k, v := range southMovers {
				f.Put(k, '.')
				f.Put(v, 'v')
			}
		}
		steps++
	}
	fmt.Println("# Part 1")
	fmt.Println("Stable after", steps, "steps")
}
