add day 2 part 1
This commit is contained in:
58
day2/test_reports.go
Normal file
58
day2/test_reports.go
Normal file
@@ -0,0 +1,58 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user