55 lines
1.9 KiB
Nim
55 lines
1.9 KiB
Nim
# Copyright 2021 Christian Ulrich
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# usage: ./part1 < input
|
|
|
|
import algorithm
|
|
import strutils
|
|
|
|
proc countLowPoints(prevLine, curLine, nextLine: string): int =
|
|
if curLine[0] < prevLine[0] and curLine[0] < nextLine[0] and
|
|
curLine[0] < curLine[1]:
|
|
result += int(curLine[0]) - 47
|
|
for i in 1 .. curLine.len() - 2:
|
|
if curLine[i] < curLine[i - 1] and curLine[i] < prevLine[i] and
|
|
curLine[i] < nextLine[i] and curLine[i] < curLine[i + 1]:
|
|
result += int(curLine[i]) - 47
|
|
if curLine[^1] < prevLine[^1] and curLine[^1] < nextLine[^1] and
|
|
curLine[^1] < curLine[^2]:
|
|
result += int(curLine[^1]) - 47
|
|
|
|
proc riskLevels(): int =
|
|
var lines: array[3, string]
|
|
if not readLine(stdin, lines[1]):
|
|
raise newException(ValueError, "too little input")
|
|
lines[0] = repeat('9', lines[1].len())
|
|
while readLine(stdin, lines[2]):
|
|
if lines[2].len() < 3 or lines[2].len() != lines[1].len():
|
|
raise newException(ValueError, "invalid line")
|
|
result += countLowPoints(lines[0], lines[1], lines[2])
|
|
lines.rotateLeft(1)
|
|
lines[2] = repeat('9', lines[1].len())
|
|
result += countLowPoints(lines[0], lines[1], lines[2])
|
|
|
|
proc main(): int =
|
|
try:
|
|
echo riskLevels()
|
|
except Exception as error:
|
|
echo error.msg
|
|
result = -1
|
|
|
|
when isMainModule:
|
|
quit(main())
|