adventofcode2021/day08/part1.nim

49 lines
1.4 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 strutils
const UniqueDigitSegments: array[4, int] = [
2, # one
4, # four
3, # seven
7, # eight
]
proc countUniqueOutputs(): int =
var line = ""
while readLine(stdin, line):
let parts = line.split('|', 1)
if parts.len() != 2:
raise newException(ValueError, "invalid line")
let outputs = parts[1].splitWhitespace()
if outputs.len() != 4:
raise newException(ValueError, "invalid output values")
for output in outputs:
if output.len() in UniqueDigitSegments:
result.inc()
proc main(): int =
try:
echo countUniqueOutputs()
except Exception as error:
echo error.msg
result = -1
when isMainModule:
quit(main())