Started 1-2

This commit is contained in:
DutchEllie 2021-12-18 11:39:13 +01:00
parent 9f935989d5
commit 048854eaf0
3 changed files with 2048 additions and 0 deletions

3
1-2/go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.home.dutchellie.nl/DutchEllie/adventofcode2021
go 1.17

2000
1-2/input Normal file

File diff suppressed because it is too large Load Diff

45
1-2/main.go Normal file
View File

@ -0,0 +1,45 @@
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
}