Last active
December 9, 2021 08:21
-
-
Save Nv7-GitHub/e1c2b3bd29cfd9a13d7f4e4bf88216bf to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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