Skip to content

Instantly share code, notes, and snippets.

@skoop
Created November 25, 2017 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skoop/e9f6cbb61aceacb8fcacade69d390567 to your computer and use it in GitHub Desktop.
Save skoop/e9f6cbb61aceacb8fcacade69d390567 to your computer and use it in GitHub Desktop.
Kata 2: parse old print files
_ _ _ _ _ _ _
| _| _||_||_ |_ ||_||_|
||_ _| | _||_| ||_| _|
_ _ _ _ _ _ _ _ _
| || || || || || || || || |
|_||_||_||_||_||_||_||_||_|
_ _ _ _ _ _ _ _ _
_| _| _| _| _| _| _| _| _|
_| _| _| _| _| _| _| _| _|
| | | | | | | | |
| | | | | | | | |
_ _ _ _ _ _ _ _ _
|_||_||_||_||_||_||_||_||_|
_| _| _| _| _| _| _| _| _|
<?php
/**
* parsing "old-school" numbers into actual numbers
* _ _ _ _ _ _ _
* | _| _||_||_ |_ ||_||_|
* ||_ _| | _||_| ||_| _|
*/
$characterMapping = [
' | |' => 1,
' _ _||_ ' => 2,
' _ _| _|' => 3,
' |_| |' => 4,
' _ |_ _|' => 5,
' _ |_ |_|' => 6,
' _ | |' => 7,
' _ |_||_|' => 8,
' _ |_| _|' => 9,
' _ | ||_|' => 0,
];
$content = file('./file1.txt');
$lines = [];
$i = 1;
$line = [];
foreach ($content as $singleLine) {
if ($i <= 3) {
$line[] = $singleLine;
$i++;
} else {
$lines[] = $line;
$line = [];
$i = 1;
}
}
$lines[] = $line;
$parsedLines = [];
// parse the line into individual characters
foreach ($lines as $lineNumber => $line) {
$amountOfChars = strlen($line[0])/3;
// parse each character in the line
$chars = [];
for ($i = 0;$i<$amountOfChars;$i++) {
$char = '';
foreach ($line as $individualLine) {
$start = $i * 3;
$char .= str_replace("\n", "", substr($individualLine, $start, 3));
}
$parsedLines[$lineNumber][] = $char;
}
}
// echo content in actual numbers;
foreach($parsedLines as $lineNumber => $characters) {
echo $lineNumber . ': ';
foreach($characters as $character) {
if (strlen($character) === 9) {
echo $characterMapping[$character];
}
}
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment