Day 09 part 1
const
CAVEW = 100
CAVEH = 100
type
Heightmap = array[CAVEW, array[CAVEH, int]]
var
cave: Heightmap
proc readFile(fn: string) =
var j: int
for line in fn.lines:
for i in 0..<line.len:
cave[i][j] = ($line[i]).parseInt
j.inc
proc isLowPoint(hm: Heightmap, x, y: int): bool =
let me = hm[x][y]
if x>0 and hm[x-1][y] <= me:
return false
elif x+1<CAVEW and hm[x+1][y] <= me:
return false
elif y>0 and hm[x][y-1] <= me:
return false
elif y+1<CAVEH and hm[x][y+1] <= me:
return false
return true
proc `$`(x: Heightmap): string =
for j in 0..<CAVEH:
for i in 0..<CAVEW:
if x.isLowPoint(i,j):
result.add("X")
else:
result.add($(x[i][j]))
result.add('\n')
proc countScore(x: Heightmap): int =
for j in 0..<CAVEH:
for i in 0..<CAVEW:
if x.isLowPoint(i,j):
result.inc(x[i][j] + 1)
readFile("input.txt")
Answer is: 600
import strutils
import strformat
import nimib
const
DAY = 9
PART = 1
DOCNAME = fmt"d{DAY:02}p{PART}"
nbInit
nb.title = DOCNAME
nbText: fmt"""
# Day {DAY:02} part {PART}
"""
nbCode:
const
CAVEW = 100
CAVEH = 100
type
Heightmap = array[CAVEW, array[CAVEH, int]]
var
cave: Heightmap
proc readFile(fn: string) =
var j: int
for line in fn.lines:
for i in 0..<line.len:
cave[i][j] = ($line[i]).parseInt
j.inc
proc isLowPoint(hm: Heightmap, x, y: int): bool =
let me = hm[x][y]
if x>0 and hm[x-1][y] <= me:
return false
elif x+1<CAVEW and hm[x+1][y] <= me:
return false
elif y>0 and hm[x][y-1] <= me:
return false
elif y+1<CAVEH and hm[x][y+1] <= me:
return false
return true
proc `$`(x: Heightmap): string =
for j in 0..<CAVEH:
for i in 0..<CAVEW:
if x.isLowPoint(i,j):
result.add("X")
else:
result.add($(x[i][j]))
result.add('\n')
proc countScore(x: Heightmap): int =
for j in 0..<CAVEH:
for i in 0..<CAVEW:
if x.isLowPoint(i,j):
result.inc(x[i][j] + 1)
readFile("input.txt")
nbText: fmt"""
Answer is: **{cave.countScore}**
"""
nb.filename = fmt"../../html/{DOCNAME}.html"
nbSave