2021: Day 1
This commit is contained in:
parent
db18ed3281
commit
cadb8fd03e
3 changed files with 136 additions and 0 deletions
49
2021/day1/main.go
Normal file
49
2021/day1/main.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func part1(depths []int) int {
|
||||
inc := 0
|
||||
for i := 0; i < len(depths)-1; i++ {
|
||||
if depths[i+1] > depths[i] {
|
||||
inc++
|
||||
}
|
||||
}
|
||||
return inc
|
||||
}
|
||||
|
||||
func part2(depths []int) int {
|
||||
inc := 0
|
||||
for i := 0; i < len(depths)-3; i++ {
|
||||
if depths[i+3] > depths[i] {
|
||||
inc++
|
||||
}
|
||||
}
|
||||
return inc
|
||||
}
|
||||
|
||||
func main() {
|
||||
f, err := os.Open("./input/day1.txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
var depths []int
|
||||
for scanner.Scan() {
|
||||
i, err := strconv.Atoi(scanner.Text())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
depths = append(depths, i)
|
||||
}
|
||||
|
||||
fmt.Println(part1(depths))
|
||||
fmt.Println(part2(depths))
|
||||
}
|
||||
Loading…
Reference in a new issue