Skip to content

Instantly share code, notes, and snippets.

@Nv7-GitHub
Last active December 9, 2021 08:21
Show Gist options
  • Save Nv7-GitHub/e1c2b3bd29cfd9a13d7f4e4bf88216bf to your computer and use it in GitHub Desktop.
Save Nv7-GitHub/e1c2b3bd29cfd9a13d7f4e4bf88216bf to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 9
package main
import (
_ "embed"
"fmt"
"strconv"
"strings"
)
//go:embed input.txt
var input string
func main() {
lines := strings.Split(input, "\n")
grid := make([][]int, len(lines))
for i, line := range lines {
grid[i] = make([]int, len(line))
for j, num := range line {
num, err := strconv.Atoi(string(num))
if err != nil {
panic(err)
}
grid[i][j] = num
}
}
// Find risk points
risk := 0
for r, row := range grid {
for c, val := range row {
// Go through neighbors
isLower := true
for roff := -1; roff <= 1; roff++ {
for coff := -1; coff <= 1; coff++ {
if !isLower {
continue
}
if !(roff == 0 && coff == 0) && (roff == 0 || coff == 0) {
// Check offset
lessZero := (c+coff < 0) || (r+roff < 0)
greaterZero := (c+coff >= len(row)) || (r+roff >= len(grid))
if !lessZero && !greaterZero {
if grid[r+roff][c+coff] <= val {
isLower = false
}
}
}
}
}
// Add risk
if isLower {
risk += val + 1
}
}
}
fmt.Println(risk)
}
Can't figure out how to solve :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment