adventofcode2021/1-1/main.go

46 lines
687 B
Go

package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
input, err := readInput("input")
if err != nil {
fmt.Errorf(err.Error())
return
}
counter := 0
for i := 0; i < len(input)-1; i++ {
if input[i+1] > input[i] {
counter++
}
}
fmt.Printf("It increased %d times", counter)
}
func readInput(filename string) ([]int, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
numbers := make([]int, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
number, err := strconv.Atoi(scanner.Text())
if err != nil {
return nil, err
}
numbers = append(numbers, number)
}
return numbers, nil
}