Last active
October 6, 2022 16:14
-
-
Save michaelskyba/54c9f48cc38e4ad7992a5560e38e1860 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://classroom.google.com/c/NTU0OTE4MjA4NzMw/p/NTAyNDg0NzYyNjQ2/details | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func printError(msg string) { | |
fmt.Fprintln(os.Stderr, msg) | |
os.Exit(1) | |
} | |
func hdl(err error, msg string) { | |
if err != nil { | |
printError(msg) | |
} | |
} | |
// Parse the "T N" header to return T, the number of days Willow spends playing | |
// per box. N is useless since we can already look at the length of the input. | |
func parseHeader(header string) int { | |
split := strings.Split(header, " ") | |
T, err := strconv.Atoi(split[0]) | |
hdl(err, "Invalid input.") | |
return T | |
} | |
// Return Willow's playing day buffer overlap | |
func getDayOverlap(s *bufio.Scanner, playsPerBox int) int { | |
playBuffer := 0 | |
for s.Scan() { | |
entry := s.Text() | |
if entry == "B" { | |
playBuffer += playsPerBox | |
} else if entry != "E" { | |
printError("Invalid input. Use one file per chain of days.") | |
} | |
if playBuffer > 0 { | |
playBuffer-- | |
} | |
} | |
return playBuffer | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
printError("Usage: willow input.txt.") | |
} | |
f, err := os.Open(os.Args[1]) | |
hdl(err, "Couldn't open input.") | |
s := bufio.NewScanner(f) | |
s.Scan() | |
header := s.Text() | |
playsPerBox := parseHeader(header) | |
overlap := getDayOverlap(s, playsPerBox) | |
fmt.Println(overlap) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment