You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
936 B
55 lines
936 B
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
func allzeros(ints []int) bool {
|
|
for i := range ints {
|
|
if ints[i] != 0 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func nextOasis(previous int, start []int) int {
|
|
if allzeros(start) {
|
|
return 0
|
|
}
|
|
|
|
length := len(start) - 1
|
|
levelBelow := make([]int, 0, length)
|
|
for i := 0; i < length; i++ {
|
|
levelBelow = append(levelBelow, start[i+1]-start[i])
|
|
}
|
|
|
|
current := start[length]
|
|
return current + nextOasis(current, levelBelow)
|
|
}
|
|
|
|
type day09 struct{}
|
|
|
|
func (d day09) solve1(r io.Reader) string {
|
|
scanner := bufio.NewScanner(r)
|
|
total := 0
|
|
|
|
for scanner.Scan() {
|
|
total += nextOasis(0, scanints(scanner.Text()))
|
|
}
|
|
return fmt.Sprintf("%d", total)
|
|
}
|
|
|
|
func (d day09) solve2(r io.Reader) string {
|
|
scanner := bufio.NewScanner(r)
|
|
total := 0
|
|
|
|
for scanner.Scan() {
|
|
ints := scanints(scanner.Text())
|
|
reverseSlice(ints)
|
|
total += nextOasis(0, ints)
|
|
}
|
|
return fmt.Sprintf("%d", total)
|
|
}
|
|
|