Problem Solving with Go course

Reading InputInput, os.Stdin and the Input Panel

Problem Solving with Go

Display

Theme

Reading Input · Output exercise

Input, os.Stdin and the Input Panel

Welcome to Problem Solving with Go. This course doesn't teach Go syntax from scratch (that's the Go course's job). Here we practise what happens before and around the code: reading input, choosing a data structure, and spotting the patterns that keep showing up in puzzles.

Sites like Advent of Code give every participant a text file with their puzzle input. Sites like LeetCode call your function with test data. Both share a habit: data arrives as text, and your first job is to turn that text into values you can compute with.

Standard input

A Go program reads input through os.Stdin, the standard input. The simplest way is to read all of it at once:

data, err := io.ReadAll(os.Stdin) // []byte with the whole input
input := string(data)

From there the input is just a string. Almost every puzzle input is made of lines, so the next step is usually to split it:

input = strings.TrimRight(input, "\n") // drop the empty line at the end of the file
lines := strings.Split(input, "\n")

TrimRight matters: a text file nearly always ends with a newline, and without it Split leaves one empty string at the end of the slice.

The Input panel in Nibble

Next to the editor there's an Input tab. Whatever is in it reaches the program as os.Stdin:

  • Run uses whatever is in the Input panel. Paste your puzzle input there and press Run.
  • Submit always uses the lesson's own input, so every learner is graded the same way.
  • Reset input brings the lesson's own input back.

The panel's input is kept on this device only, like your code draft.

Big input? Run it on your computer

The Go runtime in the browser is an interpreter: roughly 10 to 100 times slower than real Go. With a lesson's small input you won't notice. A real puzzle input can be thousands of lines and take millions of steps, so for that, run the program with Go on your own computer:

go run main.go < input.txt

The < sends the contents of input.txt to os.Stdin, exactly like the Input panel. The same code runs in both places without changes. Nibble's limits: 10 seconds per run in a lesson, 30 seconds in the playground, and at most 1 MB of input.

Task

This lesson's input lists places in a harbour town, one per line, each ending in open or closed. The starter code already reads the input and splits it into lines. Complete the program so it prints:

lines: <number of lines>
open: <number of lines ending in " open">

Use len(lines) and strings.HasSuffix(line, " open"). Try changing the Input panel too, then press Run to see the numbers change.

main.go
package main

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

func main() {
	// Read everything the program receives on standard input.
	data, err := io.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println("could not read input:", err)
		return
	}
	input := strings.TrimRight(string(data), "\n")
	lines := strings.Split(input, "\n")

	// TODO: count the lines, and the lines that end with " open"
	_ = lines
	fmt.Println("lines: 0")
	fmt.Println("open: 0")
}
Go runtime not loaded yet

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