Created
August 10, 2014 04:23
-
-
Save thedeemon/ffd1509a7b1ce77af8a4 to your computer and use it in GitHub Desktop.
Aug'14 FP(FP) contest entry of thedeemon, shorter version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module main; | |
import std.stdio, std.range, std.algorithm, std.string, std.typecons; | |
enum W = 3; | |
enum H = 5; | |
char[W][H][10] symbols = [["111", "101", "101", "101", "111"], ["110", "010", "010", "010", "111"], | |
["111", "001", "111", "100", "111"], ["111", "001", "111", "001", "111"], ["101", "101", "111", "001", "001"], | |
["111", "100", "111", "001", "111"], ["111", "100", "111", "101", "111"], ["111", "001", "011", "010", "010"], | |
["111", "101", "111", "101", "111"], ["111", "101", "111", "001", "111"]]; | |
class MatrixReader { // range of ranges of (line, pos, range of strings); holds just H lines in memory | |
File f; | |
string[H] lines; | |
int curline; | |
this(string fname) { | |
f = File(fname, "r"); | |
foreach(i; 0..H) lines[i] = f.readln.strip; | |
} | |
auto front() { return iota(lines[0].length-W+1).map!(x => tuple(curline, x, iota(H).map!(y => lines[y][x .. x+W]))); } | |
bool empty() { return lines[H-1].length==0; } | |
void popFront() { | |
delete lines[0]; | |
foreach(i; 0..H-1) lines[i] = lines[i+1]; | |
lines[H-1] = f.readln.strip; | |
curline++; | |
} | |
} | |
int whatDigit(R)(R r) { return symbols[].countUntil!((a,b) => equal(a[], b.save))(r); } | |
void main(string[] argv) { | |
foreach(t; new MatrixReader("matrix.txt").joiner.map!(t => tuple(t[0], t[1], whatDigit(t[2]))).filter!(t => t[2] >= 0)) | |
writeln("line: ", t[0], " col: ", t[1], " digit: ", t[2]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment