Skip to content

Commit

Permalink
day18: solved part two
Browse files Browse the repository at this point in the history
  • Loading branch information
fxnn committed Dec 18, 2024
1 parent 238bf5c commit 2cee91c
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions day18/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,63 @@ func printSpace(space [][]int) {
var max = len(space) - 1
for y := range space {
for x := range space[y] {
var val string
switch space[y][x] {
case CORRUPTED:
fmt.Print(" C")
val = "C"
case UNIVSITED:
fmt.Print(" .")
val = "."
default:
fmt.Printf("%2d", space[y][x])
val = util.Itoa(space[y][x])
}
if max > 9 {
fmt.Print(" ")
fmt.Printf("%3s ", val)
} else {
fmt.Printf("%2s", val)
}
}
fmt.Println()
}
fmt.Println()
}

func readSpace(space [][]int, count int) {
func readPoints() []util.Point {
var scanner = bufio.NewScanner(os.Stdin)
var pattern = regexp.MustCompile(`^(\d+),(\d+)$`)

var fallen int
for scanner.Scan() && fallen < count {
var points = []util.Point{}
for scanner.Scan() {
var line = scanner.Text()
var matches = pattern.FindStringSubmatch(line)
var x, y int
if len(matches) > 0 {
x = util.Atoi(matches[1])
y = util.Atoi(matches[2])
space[y][x] = CORRUPTED
fallen++
points = append(points, util.Point{Y: y, X: x})
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}

return points
}

func resetSpace(space [][]int) {
for y := range space {
for x := range space[y] {
if space[y][x] > 0 {
space[y][x] = 0
}
}
}
}

func fillSpace(space [][]int, points []util.Point) {
for _, point := range points {
space[point.Y][point.X] = CORRUPTED
}
}

func makeSpace(size int) [][]int {
Expand Down Expand Up @@ -106,10 +126,20 @@ func main() {
var count = flag.Int("count", 1024, "number of bytes that have fallen")
flag.Parse()

var points = readPoints()
var space = makeSpace(*max)
readSpace(space, *count)
fillSpace(space, points[:*count])
printSpace(space)
var steps = searchPath(space) - 1
fmt.Printf("found exit in %d steps\n", steps)
printSpace(space)

for _, point := range points[*count:] {
resetSpace(space)
fillSpace(space, []util.Point{point})
if searchPath(space) == 0 {
fmt.Printf("no way out at coordinate %d,%d\n", point.X, point.Y)
break
}
}
}

0 comments on commit 2cee91c

Please sign in to comment.