59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"strings"
|
||
|
|
"strconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
func check(e error) {
|
||
|
|
if e != nil {
|
||
|
|
panic(e)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
// read in file as lines
|
||
|
|
dat, err := os.ReadFile("data.txt")
|
||
|
|
check(err)
|
||
|
|
lines := strings.Split(string(dat), "\n")
|
||
|
|
|
||
|
|
// valid reports
|
||
|
|
valid := 0
|
||
|
|
|
||
|
|
for i := range len(lines) - 1 {
|
||
|
|
str_levels := strings.Split(lines[i], " ")
|
||
|
|
|
||
|
|
// convert to integer
|
||
|
|
levels := make([]int, len(str_levels))
|
||
|
|
for j := range str_levels {
|
||
|
|
levels[j], err = strconv.Atoi(str_levels[j])
|
||
|
|
check(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// check for validity
|
||
|
|
if levels[0] > levels[1] {
|
||
|
|
// decreasing sequence
|
||
|
|
valid += 1
|
||
|
|
for j := range len(levels) - 1 {
|
||
|
|
if levels[j] - levels[j+1] <= 0 || levels[j] - levels[j+1] > 3 {
|
||
|
|
valid -= 1
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if levels[0] < levels[1] {
|
||
|
|
// increasing sequence
|
||
|
|
valid += 1
|
||
|
|
for j := range len(levels) - 1 {
|
||
|
|
if levels[j+1] - levels[j] <= 0 || levels[j+1] - levels[j] > 3 {
|
||
|
|
valid -= 1
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fmt.Println(valid)
|
||
|
|
}
|