Advent-of-Code/2021/day1/util.go

73 lines
1.2 KiB
Go
Raw Normal View History

2023-11-15 13:28:22 +01:00
package main
import (
"bufio"
"os"
"strconv"
)
func LoadData(path string) []DataEntry {
readFile, err := os.Open(path)
checkErr(err)
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
var fileLines []string
for fileScanner.Scan() {
fileLines = append(fileLines, fileScanner.Text())
}
err = readFile.Close()
checkErr(err)
var data []DataEntry
lastData := 0
for _, line := range fileLines {
toInt, err := strconv.Atoi(line)
status := Nothing
checkErr(err)
switch {
case lastData == 0:
status = Nothing
case lastData < toInt:
status = Increased
case lastData > toInt:
status = Decreased
case lastData == toInt:
status = Equal
}
lastData = toInt
data = append(data, DataEntry{
Value: toInt,
Status: status,
})
}
return data
}
type Status int
const (
Nothing Status = iota
Decreased
Increased
Equal
)
func (s Status) String() string {
switch s {
case Nothing:
return "N/A - no previous measurement"
case Increased:
return "increased"
case Decreased:
return "decreased"
case Equal:
return "equal"
default:
return ""
}
}
type DataEntry struct {
Value int
Status Status
}