day 9a complete
This commit is contained in:
3
day9/day 9a/go.mod
Normal file
3
day9/day 9a/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module PWD
|
||||
|
||||
go 1.17
|
83
day9/day 9a/main.go
Normal file
83
day9/day 9a/main.go
Normal file
@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
content := returnContent("../input")
|
||||
//content := returnContent("testInput")
|
||||
answer := 0
|
||||
|
||||
for y, row := range *content {
|
||||
for x, value := range row {
|
||||
|
||||
low := true
|
||||
|
||||
for _, n := range *neighbours(x, y, len((*content)[0]), len(*content)) {
|
||||
//Check for each value if the current one we are looking
|
||||
//at is greater than any of its neighbours
|
||||
if value >= (*content)[n[0]][n[1]] {
|
||||
low = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if low {
|
||||
answer += (1 + value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(answer)
|
||||
}
|
||||
|
||||
func neighbours(x int, y int, lenX int, lenY int) *[][]int {
|
||||
var neighbours [][]int
|
||||
|
||||
if x > 0 {
|
||||
//Cell left
|
||||
neighbours = append(neighbours, []int{y, x - 1})
|
||||
}
|
||||
if x < lenX-1 {
|
||||
//Cell right
|
||||
neighbours = append(neighbours, []int{y, x + 1})
|
||||
}
|
||||
if y > 0 {
|
||||
//Cell Above
|
||||
neighbours = append(neighbours, []int{y - 1, x})
|
||||
}
|
||||
if y < lenY-1 {
|
||||
//Cell Below
|
||||
neighbours = append(neighbours, []int{y + 1, x})
|
||||
}
|
||||
return &neighbours
|
||||
}
|
||||
|
||||
func returnContent(path string) *[][]int {
|
||||
//read file and return it as an array of integers
|
||||
|
||||
file, err := os.Open(path)
|
||||
var content [][]int
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Unlucky, the file didn't open")
|
||||
return &content
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
var values []int
|
||||
for _, val := range strings.Split(scanner.Text(), "") {
|
||||
num, _ := strconv.Atoi(val)
|
||||
values = append(values, num)
|
||||
}
|
||||
content = append(content, values)
|
||||
}
|
||||
|
||||
return &content
|
||||
}
|
5
day9/day 9a/testInput
Normal file
5
day9/day 9a/testInput
Normal file
@ -0,0 +1,5 @@
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
Reference in New Issue
Block a user