Problem Solving with Go course

Capstone PuzzlesStage Build, Part 1

Problem Solving with Go

Display

Theme

Capstone Puzzles · Test exercise

Stage Build, Part 1

The puzzle

A village is putting up an open-air stage for its summer concert. The crew lead has written every task on a card: its name, how many hours it takes, and which tasks must be finished before it can start.

footings 3:
scaffold 2: footings
roof 5: scaffold
power 1:
lights 3: roof power

Plenty of volunteers have turned up, so any task whose earlier tasks are all finished can start straight away, alongside any others. Work starts at hour 0. In the small plan above, lights can't start before hour 10 (it waits for roof, which ends at 3 + 2 + 5), even though power is done at hour 1, so the whole plan is ready at hour 13.

How many hours until the whole stage, this lesson's input, is ready?

The cards were written in a hurry. If a card waits for a task that has no card, or some tasks end up waiting for each other in a circle, the plan can't be built, and your program should say so.

Task

  1. Complete parseTask(line string) (Task, error): read a card such as lights 3: roof power. Before the colon there must be exactly a name and a whole number of hours (1 or more); after it come the tasks it waits for, possibly none.
  2. Complete earliestEnds(tasks map[string]Task) (map[string]int, error): the earliest hour each task can be finished. Return an error for a task that waits for an unknown task, and for tasks that wait in a circle.
  3. Complete finish(tasks map[string]Task) (int, error): the earliest hour the whole stage is ready.
main.go
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// Task is one job in building the stage: how many hours it takes, and the tasks
// that must be finished before it can start.
type Task struct {
	Name  string
	Hours int
	After []string
}

// parseTask reads a line such as "lights 3: roof power": the name, the hours (1 or
// more), a colon, then the tasks it waits for (possibly none).
func parseTask(line string) (Task, error) {
	// TODO
	return Task{}, nil
}

// earliestEnds returns, for every task, the earliest hour it can be finished when
// there are enough people to work on every task that is ready. Work starts at hour 0.
// A task that waits for a missing task, or a circle of tasks, is an error.
func earliestEnds(tasks map[string]Task) (map[string]int, error) {
	// TODO
	return map[string]int{}, nil
}

// finish returns the earliest hour the whole stage can be finished.
func finish(tasks map[string]Task) (int, error) {
	// TODO
	return 0, nil
}

func main() {
	tasks := map[string]Task{}
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		if strings.TrimSpace(scanner.Text()) == "" {
			continue
		}
		t, err := parseTask(scanner.Text())
		if err != nil {
			fmt.Println(err)
			return
		}
		tasks[t.Name] = t
	}
	hours, err := finish(tasks)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("part 1: the stage can be ready after", hours, "hours")
}
Go runtime not loaded yet

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