Day 17 Work

This commit is contained in:
Brian Buller 2018-12-16 04:47:49 +00:00
parent 3b41f56cef
commit 5cbc4fdad0

View File

@ -11,9 +11,18 @@ import (
) )
const ( const (
MaxInt = int(^uint(0) >> 1) MAX_INT = int(^uint(0) >> 1)
MinInt = -MaxInt - 1 MIN_INT = -MAX_INT - 1
ClearScreen = "\033[H\033[2J" CLEAR_SCREEN = "\033[H\033[2J"
DEBUG = true
SLOW_IT_DOWN = true
FLOW_NO = iota
FLOW_D
FLOW_LR
FLOW_L
FLOW_R
FLOW_ERR
) )
func main() { func main() {
@ -22,9 +31,9 @@ func main() {
//part2(input) //part2(input)
} }
var scan [1000][1000]byte var scan [2000][2000]byte
var minX, maxX int = MaxInt, MinInt var minX, maxX int = MAX_INT, MIN_INT
var minY, maxY int = MaxInt, MinInt var minY, maxY int = MAX_INT, MIN_INT
func part1(input []string) { func part1(input []string) {
for _, v := range input { for _, v := range input {
@ -36,103 +45,109 @@ func part1(input []string) {
if fst[0] == "x" { if fst[0] == "x" {
x = Atoi(fst[1]) x = Atoi(fst[1])
for y = Atoi(scdRange[0]); y <= Atoi(scdRange[1]); y++ { for y = Atoi(scdRange[0]); y <= Atoi(scdRange[1]); y++ {
minmax(x, y) if y < 50 && x > 450 && x < 550 {
scan[x][y] = '#' minmax(x, y)
scan[x][y] = '#'
}
} }
} else if fst[0] == "y" { } else if fst[0] == "y" {
y = Atoi(fst[1]) y = Atoi(fst[1])
for x := Atoi(scdRange[0]); x <= Atoi(scdRange[1]); x++ { for x := Atoi(scdRange[0]); x <= Atoi(scdRange[1]); x++ {
minmax(x, y) if y < 50 && x > 450 && x < 550 {
scan[x][y] = '#' minmax(x, y)
scan[x][y] = '#'
}
} }
} }
} }
printScan()
// The water starts at 500, 0 // The water starts at 500, 0
fillDown(500, 0) for {
printScan()
if SLOW_IT_DOWN {
time.Sleep(time.Millisecond * 250)
}
if !fillSpot(500, 1) {
break
}
}
printScan()
} }
func part2(input []string) { func part2(input []string) {
} }
// The 'fill<dir>' functions return the x, y that was filled func fillSpot(x, y int) bool {
// or MinInt, MinInt, if x, y can't be filled if y > maxY {
return false
} else if !canFill(x, y) {
return false
}
// fillDown will check down, then left, then right if scan[x][y] == 0 {
func fillDown(x, y int) (int, int) { scan[x][y] = '|'
fx, fy := fillSpot(x, y) return true
if !gud(fx, fy) {
return fx, fy
} }
// Try to fill the spot below it
dx, dy := fillDown(x, y+1) if canFill(x, y+1) {
if dx == MinInt && dy == MinInt { return fillSpot(x, y+1)
// We couldn't go down } else if canFill(x-1, y) && canFill(x+1, y) {
lx, ly := fillLeft(x-1, y) // left? return fillLeft(x-1, y) || fillRight(x+1, y)
rx, ry := fillRight(x+1, y) // right? } else if canFill(x-1, y) {
if !gud(lx, ly) || !gud(rx, ry) { return fillLeft(x-1, y)
return MinInt, MinInt // Off to infinity } else if canFill(x+1, y) {
} return fillRight(x+1, y)
if ly == y && ry == y {
scan[lx][y] = '~'
scan[rx][y] = '~'
}
} }
return dx, dy
scan[x][y] = '~'
return true
} }
func fillLeft(x, y int) (int, int) { func fillLeft(x, y int) bool {
fx, fy := fillSpot(x, y) if !canFill(x, y) {
if !gud(fx, fy) { return false
return fx, fy
} }
// This spot can be filled, try to go down
rx, ry := fillDown(fx, fy+1) if scan[x][y] == 0 {
if gud(rx, ry) { scan[x][y] = '|'
return rx, ry return true
} }
// Couldn't go down, try to go left
rx, ry = fillLeft(fx-1, y) if canFill(x, y+1) {
if gud(rx, ry) { return fillSpot(x, y+1)
return rx, ry } else if canFill(x-1, y) {
return fillLeft(x-1, y)
} }
return x, y
scan[x][y] = '~'
return true
} }
func fillRight(x, y int) (int, int) { func fillRight(x, y int) bool {
fx, fy := fillSpot(x, y) if !canFill(x, y) {
if !gud(fx, fy) { return false
return fx, fy
} }
// This spot can be filled, try to go down
rx, ry := fillDown(fx, fy+1)
if gud(rx, ry) {
return rx, ry
}
// Couldn't go down, try to go left
rx, ry = fillRight(fx+1, y)
if gud(rx, ry) {
return rx, ry
}
return x, y
}
func fillSpot(x, y int) (int, int) { if scan[x][y] == 0 {
time.Sleep(time.Millisecond * 250) scan[x][y] = '|'
printScan() return true
if y > maxY || !canFill(x, y) {
return MinInt, MinInt
} }
scan[x][y] = '|'
return x, y if canFill(x, y+1) {
return fillSpot(x, y+1)
} else if canFill(x+1, y) {
return fillRight(x+1, y)
}
scan[x][y] = '~'
return true
} }
func gud(x, y int) bool { func gud(x, y int) bool {
return x != MinInt && y != MinInt return x != MIN_INT && y != MIN_INT
} }
func printScan() { func printScan() {
fmt.Print(ClearScreen) fmt.Print(CLEAR_SCREEN)
for y := minY; y <= maxY; y++ { for y := minY; y <= maxY; y++ {
for x := minX; x <= maxX; x++ { for x := minX; x <= maxX; x++ {
if scan[x][y] == 0 { if scan[x][y] == 0 {