You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.6 KiB
123 lines
3.6 KiB
import os
|
|
|
|
dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
with open(os.path.join(dir, './input.txt'), 'r') as input_file:
|
|
|
|
lines = input_file.readlines()
|
|
|
|
total = 0
|
|
|
|
test = False
|
|
|
|
for line in lines:
|
|
input_segments = ""
|
|
output_segments = ""
|
|
|
|
if test == True:
|
|
if '|' in line:
|
|
input_segments = line.strip()
|
|
else:
|
|
output_segments = line.strip()
|
|
else:
|
|
parts = line.split('|')
|
|
input_segments = parts[0].strip()
|
|
output_segments = parts[1].strip()
|
|
|
|
if output_segments == "":
|
|
continue
|
|
|
|
output_parts = output_segments.split()
|
|
input_parts = input_segments.split()
|
|
one = ""
|
|
two = ""
|
|
three = ""
|
|
four = ""
|
|
five = ""
|
|
six = ""
|
|
seven = ""
|
|
eights = ""
|
|
nine = ""
|
|
zero = ""
|
|
for input in input_parts:
|
|
num_signals = len(input)
|
|
if num_signals == 2:
|
|
one = input
|
|
elif num_signals == 3:
|
|
seven = input
|
|
elif num_signals == 4:
|
|
four = input
|
|
elif num_signals == 7:
|
|
eight = input
|
|
|
|
input_parts.remove(one)
|
|
input_parts.remove(seven)
|
|
input_parts.remove(four)
|
|
input_parts.remove(eight)
|
|
|
|
segment_one = set(seven) - set(one)
|
|
|
|
for input in input_parts:
|
|
num_signals = len(input)
|
|
if num_signals == 6:
|
|
if one[0] in set(input) and one[1] in set(input):
|
|
continue
|
|
six = input
|
|
|
|
input_parts.remove(six)
|
|
|
|
segment_three = set(eight) - set(six)
|
|
segment_six = set(one) - segment_three
|
|
|
|
for input in input_parts:
|
|
num_signals = len(input)
|
|
if num_signals == 5:
|
|
if len(segment_six.intersection(set(input))) == 0:
|
|
two = input
|
|
|
|
input_parts.remove(two)
|
|
|
|
segment_two = set(six) - set(two) - segment_six
|
|
segment_four = set(four) - set(one) - segment_two
|
|
|
|
for input in input_parts:
|
|
num_signals = len(input)
|
|
if num_signals == 6:
|
|
if len(segment_four.intersection(set(input))) == 0:
|
|
zero = input
|
|
else:
|
|
nine = input
|
|
elif num_signals == 5:
|
|
if len(segment_two.intersection(set(input))) == 0:
|
|
three = input
|
|
else:
|
|
five = input
|
|
|
|
display_string = ""
|
|
for output in output_parts:
|
|
candidate = set(output)
|
|
if set(one) == candidate:
|
|
display_string = display_string + "1"
|
|
elif set(two) == candidate:
|
|
display_string = display_string + "2"
|
|
elif set(three) == candidate:
|
|
display_string = display_string + "3"
|
|
elif set(four) == candidate:
|
|
display_string = display_string + "4"
|
|
elif set(five) == candidate:
|
|
display_string = display_string + "5"
|
|
elif set(six) == candidate:
|
|
display_string = display_string + "6"
|
|
elif set(seven) == candidate:
|
|
display_string = display_string + "7"
|
|
elif set(eight) == candidate:
|
|
display_string = display_string + "8"
|
|
elif set(nine) == candidate:
|
|
display_string = display_string + "9"
|
|
elif set(zero) == candidate:
|
|
display_string = display_string + "0"
|
|
|
|
total = total + int(display_string)
|
|
|
|
print(total)
|
|
|
|
|