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++ {
if y < 50 && x > 450 && x < 550 {
minmax(x, y) minmax(x, y)
scan[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++ {
if y < 50 && x > 450 && x < 550 {
minmax(x, y) minmax(x, y)
scan[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) {
fx, fy := fillSpot(x, y)
if !gud(fx, fy) {
return fx, fy
}
// Try to fill the spot below it
dx, dy := fillDown(x, y+1)
if dx == MinInt && dy == MinInt {
// We couldn't go down
lx, ly := fillLeft(x-1, y) // left?
rx, ry := fillRight(x+1, y) // right?
if !gud(lx, ly) || !gud(rx, ry) {
return MinInt, MinInt // Off to infinity
}
if ly == y && ry == y {
scan[lx][y] = '~'
scan[rx][y] = '~'
}
}
return dx, dy
}
func fillLeft(x, y int) (int, int) {
fx, fy := fillSpot(x, y)
if !gud(fx, fy) {
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 = fillLeft(fx-1, y)
if gud(rx, ry) {
return rx, ry
}
return x, y
}
func fillRight(x, y int) (int, int) {
fx, fy := fillSpot(x, y)
if !gud(fx, fy) {
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) {
time.Sleep(time.Millisecond * 250)
printScan()
if y > maxY || !canFill(x, y) {
return MinInt, MinInt
}
scan[x][y] = '|' scan[x][y] = '|'
return x, y return true
}
if canFill(x, y+1) {
return fillSpot(x, y+1)
} else if canFill(x-1, y) && canFill(x+1, y) {
return fillLeft(x-1, y) || fillRight(x+1, y)
} else if canFill(x-1, y) {
return fillLeft(x-1, y)
} else if canFill(x+1, y) {
return fillRight(x+1, y)
}
scan[x][y] = '~'
return true
}
func fillLeft(x, y int) bool {
if !canFill(x, y) {
return false
}
if scan[x][y] == 0 {
scan[x][y] = '|'
return true
}
if canFill(x, y+1) {
return fillSpot(x, y+1)
} else if canFill(x-1, y) {
return fillLeft(x-1, y)
}
scan[x][y] = '~'
return true
}
func fillRight(x, y int) bool {
if !canFill(x, y) {
return false
}
if scan[x][y] == 0 {
scan[x][y] = '|'
return true
}
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 {