package main

import (
	"fmt"
	intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
	"time"
)

func part1(prog []int) {
	p := intcode.NewProgram(prog)

	go func() {
		var line []byte
		for {
			for !p.NeedsOutput() {
				time.Sleep(1)
			}
			out := byte(p.Output())
			fmt.Print(string(out))
			if out == '\n' {
				layout = append(layout, line)
				line = []byte{}
			} else {
				line = append(line, out)
			}
		}
	}()
	fmt.Println("Running")
	p.Run()
	fmt.Println("Done")

	var total int
	for y := range layout {
		for x := range layout[y] {
			if IsIntersection(x, y) {
				total = total + (x * y)
				fmt.Print("O")
			} else {
				fmt.Print(string(layout[y][x]))
			}
		}
		fmt.Println()
	}

	fmt.Println("# Part 1: ", total)
}
func IsIntersection(x, y int) bool {
	if y == 0 || y == len(layout)-2 {
		return false
	} else if x == 0 || x == len(layout[y])-2 {
		return false
	}
	if layout[y][x] != '#' {
		return false
	}
	if layout[y-1][x] != '#' {
		return false
	}
	if layout[y+1][x] != '#' {
		return false
	}
	if layout[y][x-1] != '#' {
		return false
	}
	if layout[y][x+1] != '#' {
		return false
	}
	return true
}
