Skip to content

Instantly share code, notes, and snippets.

@Valiev
Last active August 29, 2015 14:05
Show Gist options
  • Save Valiev/5fb8e28f8ecd5734bb69 to your computer and use it in GitHub Desktop.
Save Valiev/5fb8e28f8ecd5734bb69 to your computer and use it in GitHub Desktop.
from itertools import izip
symbols = {
0 : [[1, 1, 1],
[1, 0, 1],
[1, 0, 1],
[1, 0, 1],
[1, 1, 1]],
1 : [[1, 1, 0],
[0, 1, 0],
[0, 1, 0],
[0, 1, 0],
[1, 1, 1]],
2 : [[1, 1, 1],
[0, 0, 1],
[1, 1, 1],
[1, 0, 0],
[1, 1, 1]],
3 : [[1, 1, 1],
[0, 0, 1],
[1, 1, 1],
[0, 0, 1],
[1, 1, 1]],
4 : [[1, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 0, 1],
[0, 0, 1]],
5 : [[1, 1, 1],
[1, 0, 0],
[1, 1, 1],
[0, 0, 1],
[1, 1, 1]],
6 : [[1, 1, 1],
[1, 0, 0],
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]],
7 : [[1, 1, 1],
[0, 0, 1],
[0, 1, 1],
[0, 1, 0],
[0, 1, 0]],
8 : [[1, 1, 1],
[1, 0, 1],
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]],
9 : [[1, 1, 1],
[1, 0, 1],
[1, 1, 1],
[0, 0, 1],
[1, 1, 1]],
}
ROWS_NUM = len(symbols[0])
def process_line(line, line_number, processed_data):
""" Processed data is a list (start_x, start_y, num) """
for pos, (a, b, c) in enumerate(izip(line, line[1:], line[2:])):
# check whether given number continue matching in current row
for start_x, start_y, num in processed_data:
if start_x != pos:
continue
line_to_check = line_number - start_y
if [a, b, c] == symbols[num][line_to_check]:
yield (start_x, start_y, num)
# May be there is a new matching starts in this row
for num in symbols:
if [a, b, c] == symbols[num][0]:
yield (pos, line_number, num)
def find_numbers_in_file(fp):
results = []
for y, row in enumerate(fp):
line = map(int, row.strip())
res_collection = []
for start_x, start_y, num in process_line(line, y, results):
# Found the number
if y - start_y >= ROWS_NUM - 1:
yield start_x, start_y, num
# Continue processing
else:
res_collection.append((start_x, start_y, num))
results = res_collection
if __name__ == "__main__":
with open('input.txt') as fp:
for x, y, n in find_numbers_in_file(fp):
print n, '@', '(%d, %d)' % (x, y)
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
011101100111011101010111011101110111011100111011001110111010101110111011101110111001110110011101110101011101110111011101110
010100100001000101010100010000010101010100101001000010001010101000100000101010101001010010000100010101010001000001010101010
010100100111011101110111011100110111011100101001001110111011101110111001101110111001010010011101110111011101110011011101110
010100100100000100010001010100100101000100101001001000001000100010101001001010001001010010010000010001000101010010010100010
011101110111011100010111011100100111011100111011101110111000101110111001001110111001110111011101110001011101110010011101110
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
➜ python FP_2014August.py
0 @ (1, 1)
1 @ (5, 1)
2 @ (9, 1)
3 @ (13, 1)
4 @ (17, 1)
5 @ (21, 1)
6 @ (25, 1)
7 @ (29, 1)
8 @ (33, 1)
9 @ (37, 1)
0 @ (42, 1)
1 @ (46, 1)
2 @ (50, 1)
3 @ (54, 1)
4 @ (58, 1)
5 @ (62, 1)
6 @ (66, 1)
7 @ (70, 1)
8 @ (74, 1)
9 @ (78, 1)
0 @ (83, 1)
1 @ (87, 1)
2 @ (91, 1)
3 @ (95, 1)
4 @ (99, 1)
5 @ (103, 1)
6 @ (107, 1)
7 @ (111, 1)
8 @ (115, 1)
9 @ (119, 1)
0 @ (1, 7)
1 @ (5, 7)
2 @ (9, 7)
3 @ (13, 7)
4 @ (17, 7)
5 @ (21, 7)
6 @ (25, 7)
7 @ (29, 7)
8 @ (33, 7)
9 @ (37, 7)
0 @ (42, 7)
1 @ (46, 7)
2 @ (50, 7)
3 @ (54, 7)
4 @ (58, 7)
5 @ (62, 7)
6 @ (66, 7)
7 @ (70, 7)
8 @ (74, 7)
9 @ (78, 7)
0 @ (83, 7)
1 @ (87, 7)
2 @ (91, 7)
3 @ (95, 7)
4 @ (99, 7)
5 @ (103, 7)
6 @ (107, 7)
7 @ (111, 7)
8 @ (115, 7)
9 @ (119, 7)
0 @ (1, 13)
1 @ (5, 13)
2 @ (9, 13)
3 @ (13, 13)
4 @ (17, 13)
5 @ (21, 13)
6 @ (25, 13)
7 @ (29, 13)
8 @ (33, 13)
9 @ (37, 13)
0 @ (42, 13)
1 @ (46, 13)
2 @ (50, 13)
3 @ (54, 13)
4 @ (58, 13)
5 @ (62, 13)
6 @ (66, 13)
7 @ (70, 13)
8 @ (74, 13)
9 @ (78, 13)
0 @ (83, 13)
1 @ (87, 13)
2 @ (91, 13)
3 @ (95, 13)
4 @ (99, 13)
5 @ (103, 13)
6 @ (107, 13)
7 @ (111, 13)
8 @ (115, 13)
9 @ (119, 13)
0 @ (1, 19)
1 @ (5, 19)
2 @ (9, 19)
3 @ (13, 19)
4 @ (17, 19)
5 @ (21, 19)
6 @ (25, 19)
7 @ (29, 19)
8 @ (33, 19)
9 @ (37, 19)
0 @ (42, 19)
1 @ (46, 19)
2 @ (50, 19)
3 @ (54, 19)
4 @ (58, 19)
5 @ (62, 19)
6 @ (66, 19)
7 @ (70, 19)
8 @ (74, 19)
9 @ (78, 19)
0 @ (83, 19)
1 @ (87, 19)
2 @ (91, 19)
3 @ (95, 19)
4 @ (99, 19)
5 @ (103, 19)
6 @ (107, 19)
7 @ (111, 19)
8 @ (115, 19)
9 @ (119, 19)
0 @ (1, 25)
1 @ (5, 25)
2 @ (9, 25)
3 @ (13, 25)
4 @ (17, 25)
5 @ (21, 25)
6 @ (25, 25)
7 @ (29, 25)
8 @ (33, 25)
9 @ (37, 25)
0 @ (42, 25)
1 @ (46, 25)
2 @ (50, 25)
3 @ (54, 25)
4 @ (58, 25)
5 @ (62, 25)
6 @ (66, 25)
7 @ (70, 25)
8 @ (74, 25)
9 @ (78, 25)
0 @ (83, 25)
1 @ (87, 25)
2 @ (91, 25)
3 @ (95, 25)
4 @ (99, 25)
5 @ (103, 25)
6 @ (107, 25)
7 @ (111, 25)
8 @ (115, 25)
9 @ (119, 25)
0 @ (1, 31)
1 @ (5, 31)
2 @ (9, 31)
3 @ (13, 31)
4 @ (17, 31)
5 @ (21, 31)
6 @ (25, 31)
7 @ (29, 31)
8 @ (33, 31)
9 @ (37, 31)
0 @ (42, 31)
1 @ (46, 31)
2 @ (50, 31)
3 @ (54, 31)
4 @ (58, 31)
5 @ (62, 31)
6 @ (66, 31)
7 @ (70, 31)
8 @ (74, 31)
9 @ (78, 31)
0 @ (83, 31)
1 @ (87, 31)
2 @ (91, 31)
3 @ (95, 31)
4 @ (99, 31)
5 @ (103, 31)
6 @ (107, 31)
7 @ (111, 31)
8 @ (115, 31)
9 @ (119, 31)
0 @ (1, 37)
1 @ (5, 37)
2 @ (9, 37)
3 @ (13, 37)
4 @ (17, 37)
5 @ (21, 37)
6 @ (25, 37)
7 @ (29, 37)
8 @ (33, 37)
9 @ (37, 37)
0 @ (42, 37)
1 @ (46, 37)
2 @ (50, 37)
3 @ (54, 37)
4 @ (58, 37)
5 @ (62, 37)
6 @ (66, 37)
7 @ (70, 37)
8 @ (74, 37)
9 @ (78, 37)
0 @ (83, 37)
1 @ (87, 37)
2 @ (91, 37)
3 @ (95, 37)
4 @ (99, 37)
5 @ (103, 37)
6 @ (107, 37)
7 @ (111, 37)
8 @ (115, 37)
9 @ (119, 37)
0 @ (1, 43)
1 @ (5, 43)
2 @ (9, 43)
3 @ (13, 43)
4 @ (17, 43)
5 @ (21, 43)
6 @ (25, 43)
7 @ (29, 43)
8 @ (33, 43)
9 @ (37, 43)
0 @ (42, 43)
1 @ (46, 43)
2 @ (50, 43)
3 @ (54, 43)
4 @ (58, 43)
5 @ (62, 43)
6 @ (66, 43)
7 @ (70, 43)
8 @ (74, 43)
9 @ (78, 43)
0 @ (83, 43)
1 @ (87, 43)
2 @ (91, 43)
3 @ (95, 43)
4 @ (99, 43)
5 @ (103, 43)
6 @ (107, 43)
7 @ (111, 43)
8 @ (115, 43)
9 @ (119, 43)
0 @ (1, 49)
1 @ (5, 49)
2 @ (9, 49)
3 @ (13, 49)
4 @ (17, 49)
5 @ (21, 49)
6 @ (25, 49)
7 @ (29, 49)
8 @ (33, 49)
9 @ (37, 49)
0 @ (42, 49)
1 @ (46, 49)
2 @ (50, 49)
3 @ (54, 49)
4 @ (58, 49)
5 @ (62, 49)
6 @ (66, 49)
7 @ (70, 49)
8 @ (74, 49)
9 @ (78, 49)
0 @ (83, 49)
1 @ (87, 49)
2 @ (91, 49)
3 @ (95, 49)
4 @ (99, 49)
5 @ (103, 49)
6 @ (107, 49)
7 @ (111, 49)
8 @ (115, 49)
9 @ (119, 49)
0 @ (1, 55)
1 @ (5, 55)
2 @ (9, 55)
3 @ (13, 55)
4 @ (17, 55)
5 @ (21, 55)
6 @ (25, 55)
7 @ (29, 55)
8 @ (33, 55)
9 @ (37, 55)
0 @ (42, 55)
1 @ (46, 55)
2 @ (50, 55)
3 @ (54, 55)
4 @ (58, 55)
5 @ (62, 55)
6 @ (66, 55)
7 @ (70, 55)
8 @ (74, 55)
9 @ (78, 55)
0 @ (83, 55)
1 @ (87, 55)
2 @ (91, 55)
3 @ (95, 55)
4 @ (99, 55)
5 @ (103, 55)
6 @ (107, 55)
7 @ (111, 55)
8 @ (115, 55)
9 @ (119, 55)
@Valiev
Copy link
Author

Valiev commented Aug 9, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment