Problem Solving with Go course

Capstone PuzzlesGreenhouse, Part 2

Problem Solving with Go

Display

Theme

Capstone Puzzles · Test exercise

Greenhouse, Part 2

Your part 1 answer matched the timer on the keeper's wall, but her beds tell another story. The peat beds take far longer to soak than the soil ones.

The puzzle, continued

Soil gets wet one minute after its wet neighbour, as before. Peat (~) holds the water back: it gets wet 3 minutes after the neighbour the water comes from. Once a peat cell is wet, water spreads out of it as usual. Paving still stops everything.

S~.       0 3 4
~~.   →   3 6 5
...       4 5 6

Water can now arrive sooner by going around the peat than through it, so the order in which cells get wet is no longer "one ring at a time". The last cell in this bed is wet after 6 minutes instead of 4.

With the slow peat, how many minutes until every reachable cell of the greenhouse is wet, and how many cells stay dry?

Task

Your wetAfter from part 1 is already in the code. Complete wetAfterPeat(grid []string) (minutes, dry int): the same two numbers, with peat cells taking 3 minutes. The soakQueue type is ready for container/heap, which you'll need to import.

main.go
package main

import (
	"fmt"
	"io"
	"os"
	"strings"
)

// Point is a cell: Row grows downwards, Col grows to the right.
type Point struct {
	Row, Col int
}

// Up, down, left, right.
var dirs = []Point{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}

// wetAfter returns how many minutes pass until every cell the water can reach is wet,
// and how many soil or peat cells stay dry. Water starts on every 'S' at minute 0 and
// spreads up, down, left and right by one cell a minute; it doesn't cross '#'.
func wetAfter(grid []string) (minutes, dry int) {
	wetAt := map[Point]int{}
	var queue []Point
	for r, row := range grid {
		for c := 0; c < len(row); c++ {
			if row[c] == 'S' {
				p := Point{r, c}
				wetAt[p] = 0
				queue = append(queue, p)
			}
		}
	}
	for len(queue) > 0 {
		cur := queue[0]
		queue = queue[1:]
		minutes = max(minutes, wetAt[cur])
		for _, d := range dirs {
			next := Point{cur.Row + d.Row, cur.Col + d.Col}
			if next.Row < 0 || next.Row >= len(grid) || next.Col < 0 || next.Col >= len(grid[next.Row]) ||
				grid[next.Row][next.Col] == '#' {
				continue
			}
			if _, seen := wetAt[next]; !seen {
				wetAt[next] = wetAt[cur] + 1
				queue = append(queue, next)
			}
		}
	}
	for r, row := range grid {
		for c := 0; c < len(row); c++ {
			if _, wet := wetAt[Point{r, c}]; !wet && row[c] != '#' {
				dry++
			}
		}
	}
	return minutes, dry
}

// soak is a cell and the minute it gets wet.
type soak struct {
	at      Point
	minutes int
}

// soakQueue is a priority queue for container/heap: Pop gives the earliest minute.
type soakQueue []soak

func (q soakQueue) Len() int           { return len(q) }
func (q soakQueue) Less(i, j int) bool { return q[i].minutes < q[j].minutes }
func (q soakQueue) Swap(i, j int)      { q[i], q[j] = q[j], q[i] }
func (q *soakQueue) Push(x any)        { *q = append(*q, x.(soak)) }
func (q *soakQueue) Pop() any {
	old := *q
	s := old[len(old)-1]
	*q = old[:len(old)-1]
	return s
}

// wetAfterPeat is wetAfter where water needs 3 minutes to soak into a peat cell '~'
// instead of 1. Every other cell still takes 1 minute.
func wetAfterPeat(grid []string) (minutes, dry int) {
	// TODO: the same spread, but the earliest wet cell first: Dijkstra with soakQueue
	return 0, 0
}

func main() {
	data, err := io.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println("could not read input:", err)
		return
	}
	grid := strings.Split(strings.TrimSpace(strings.ReplaceAll(string(data), "\r\n", "\n")), "\n")
	minutes, dry := wetAfter(grid)
	fmt.Println("part 1:", minutes, "minutes,", dry, "cells stay dry")
	minutes, dry = wetAfterPeat(grid)
	fmt.Println("part 2:", minutes, "minutes,", dry, "cells stay dry")
}
Go runtime not loaded yet

8 lines · 136 B of the 1 MB limit. Submit always uses the lesson's own input.