Solved 1-1

This commit is contained in:
DutchEllie 2021-12-18 11:35:36 +01:00
parent 4e31b2b57c
commit 9f935989d5
1 changed files with 40 additions and 2 deletions

View File

@ -1,7 +1,45 @@
package main
import "fmt"
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
fmt.Println("Hello World")
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
}