|
## ========================================== |
|
## Challenge #199 - Bank Number Banners Pt. 1 |
|
## ========================================== |
|
## http://www.reddit.com/r/dailyprogrammer/comments/2tr6yn/2015126_challenge_199_bank_number_banners_pt_1/ |
|
## |
|
## Description |
|
## ----------- |
|
## You work for a bank, which has recently purchased an ingenious machine to |
|
## assist in reading letters and faxes sent in by branch offices. The machine |
|
## scans the paper documents, and produces a file with a number of entries |
|
## which each look like this: |
|
## |
|
## :: |
|
## _ _ _ _ _ _ _ _ |
|
## | _| _||_||_ |_ ||_||_|| | |
|
## ||_ _| | _||_| ||_| _||_| |
|
## |
|
## Each entry is 4 lines long, and each line has 27 characters. The first 3 |
|
## lines of each entry contain an account number written using pipes and |
|
## underscores, and the fourth line is blank. Each account number should have |
|
## 9 digits, all of which should be in the range 0-9. |
|
## |
|
## Right now you're working in the print shop and you have to take account |
|
## numbers and produce those paper documents. |
|
## |
|
## Input |
|
## ----- |
|
## You'll be given a series of numbers and you have to parse them into the |
|
## previously mentioned banner format. This input... |
|
## |
|
## :: |
|
## 000000000 |
|
## 111111111 |
|
## 490067715 |
|
## |
|
import parseopt2 |
|
const |
|
font: array[0..9, int] = |
|
[111, 9, 94, 91, 57, 115, 119, 73, 127, 123] |
|
|
|
proc getBit(n: int, b: int): bool = |
|
((n and (1 shl b)) shr b) == 1 |
|
|
|
proc toBanner(text): string = |
|
if text.len != 9: |
|
raise newException(ValueError, "The string must be exactly 9 characters long.") |
|
var top, middle, bottom: string = "" |
|
for character in text: |
|
if not (character in {'0'..'9'}): |
|
raise newException(ValueError, "The string can contain only digits (0-9)") |
|
let value = ord(character) - ord('0') |
|
top &= (if font[value].getBit(6): " _ " else: " ") |
|
for i in countdown(5, 3): |
|
if font[value].getBit(i): |
|
middle &= (if i mod 2 == 0: "_" else: "|") |
|
else: |
|
middle &= " " |
|
for i in countdown(2, 0): |
|
if font[value].getBit(i): |
|
bottom &= (if i mod 2 == 0: "|" else: "_") |
|
else: |
|
bottom &= " " |
|
result = top & "\n" & middle & "\n" & bottom |
|
|
|
proc writeHelp() = |
|
echo("Usage: bankbanners FILENAME [-h|--help]") |
|
|
|
when isMainModule: |
|
var |
|
numbers: File |
|
line: string = "" |
|
filename: string = "" |
|
for kind, key, val in getopt(): |
|
case kind |
|
of cmdArgument: filename = key |
|
of cmdLongOption, cmdShortOption: |
|
case key |
|
of "help", "h": writeHelp() |
|
of cmdEnd: assert(false) |
|
if filename == "": |
|
writeHelp() |
|
else: |
|
if numbers.open(filename): |
|
while numbers.readLine(line): |
|
echo(toBanner(line)) |