From 2cee91c8062dfaae6ad4179265290c646c95207a Mon Sep 17 00:00:00 2001 From: Felix Neumann Date: Wed, 18 Dec 2024 17:58:14 +0100 Subject: [PATCH] day18: solved part two --- day18/main.go | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/day18/main.go b/day18/main.go index 2e144b3..c774fd5 100644 --- a/day18/main.go +++ b/day18/main.go @@ -19,16 +19,19 @@ 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() @@ -36,26 +39,43 @@ func printSpace(space [][]int) { 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 { @@ -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 + } + } }