Solution to problem 8 in Python
This commit is contained in:
parent
b00127fa6d
commit
d98dac2c05
@ -106,6 +106,8 @@
|
||||
|
||||
# In the output values, how many times do digits 1, 4, 7, or 8 appear?
|
||||
|
||||
from ast import Num
|
||||
|
||||
with open("files/P8.txt") as f:
|
||||
output = [line for line in f.read().strip().split("\n")]
|
||||
|
||||
@ -124,5 +126,118 @@ def part_1() -> None:
|
||||
print(f"There are {easy_digits} ocurrences in the output values")
|
||||
|
||||
|
||||
# --- Part Two ---
|
||||
|
||||
# Through a little deduction, you should now be able to determine the remaining
|
||||
# digits. Consider again the first example above:
|
||||
|
||||
# acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab |
|
||||
# cdfeb fcadb cdfeb cdbaf
|
||||
|
||||
# After some careful analysis, the mapping between signal wires and segments
|
||||
# only make sense in the following configuration:
|
||||
|
||||
# dddd
|
||||
# e a
|
||||
# e a
|
||||
# ffff
|
||||
# g b
|
||||
# g b
|
||||
# cccc
|
||||
|
||||
# So, the unique signal patterns would correspond to the following digits:
|
||||
|
||||
# acedgfb: 8
|
||||
# cdfbe: 5
|
||||
# gcdfa: 2
|
||||
# fbcad: 3
|
||||
# dab: 7
|
||||
# cefabd: 9
|
||||
# cdfgeb: 6
|
||||
# eafb: 4
|
||||
# cagedb: 0
|
||||
# ab: 1
|
||||
|
||||
# Then, the four digits of the output value can be decoded:
|
||||
|
||||
# cdfeb: 5
|
||||
# fcadb: 3
|
||||
# cdfeb: 5
|
||||
# cdbaf: 3
|
||||
|
||||
# Therefore, the output value for this entry is 5353.
|
||||
|
||||
# Following this same process for each entry in the second, larger example
|
||||
# above, the output value of each entry can be determined:
|
||||
|
||||
# fdgacbe cefdb cefbgd gcbe: 8394
|
||||
# fcgedb cgb dgebacf gc: 9781
|
||||
# cg cg fdcagb cbg: 1197
|
||||
# efabcd cedba gadfec cb: 9361
|
||||
# gecf egdcabf bgf bfgea: 4873
|
||||
# gebdcfa ecba ca fadegcb: 8418
|
||||
# cefg dcbef fcge gbcadfe: 4548
|
||||
# ed bcgafe cdgba cbgef: 1625
|
||||
# gbdfcae bgc cg cgb: 8717
|
||||
# fgae cfgab fg bagce: 4315
|
||||
|
||||
# Adding all of the output values in this larger example produces 61229.
|
||||
|
||||
# For each entry, determine all of the wire/segment connections and decode the
|
||||
# four-digit output values. What do you get if you add up all of the output
|
||||
# values?
|
||||
|
||||
|
||||
def solve_line(numbers: str) -> dict[str, list[str]]:
|
||||
numbers_dic = {}
|
||||
for num in numbers.split(" "):
|
||||
# first populate with easy numbers
|
||||
if len(num) == 2:
|
||||
numbers_dic["1"] = sorted(num)
|
||||
elif len(num) == 3:
|
||||
numbers_dic["7"] = sorted(num)
|
||||
elif len(num) == 4:
|
||||
numbers_dic["4"] = sorted(num)
|
||||
elif len(num) == 7:
|
||||
numbers_dic["8"] = sorted(num)
|
||||
# populate with subsets of easy numbers
|
||||
for num in numbers.split(" "):
|
||||
if len(num) == 6:
|
||||
if set(numbers_dic["4"]).issubset(set(num)):
|
||||
numbers_dic["9"] = sorted(num)
|
||||
elif set(numbers_dic["1"]).issubset(set(num)):
|
||||
numbers_dic["0"] = sorted(num)
|
||||
else:
|
||||
numbers_dic["6"] = sorted(num)
|
||||
# missing numbers of length 5
|
||||
for num in numbers.split(" "):
|
||||
if len(num) == 5:
|
||||
if set(num).issubset(set(numbers_dic["6"])):
|
||||
numbers_dic["5"] = sorted(num)
|
||||
elif set(numbers_dic["1"]).issubset(set(num)):
|
||||
numbers_dic["3"] = sorted(num)
|
||||
else:
|
||||
numbers_dic["2"] = sorted(num)
|
||||
|
||||
return numbers_dic
|
||||
|
||||
|
||||
def part_2() -> None:
|
||||
decoded_output = 0
|
||||
for line in output:
|
||||
patterns, out_val = line.split(" | ")
|
||||
_numbers_dic = solve_line(patterns)
|
||||
entry_value = []
|
||||
for out in out_val.split(" "):
|
||||
for k, v in _numbers_dic.items():
|
||||
if sorted(out) == v:
|
||||
entry_value.append(k)
|
||||
|
||||
decoded_output += int("".join(entry_value))
|
||||
|
||||
print(f"All the output values sum {decoded_output}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part_1()
|
||||
part_2()
|
||||
|
Loading…
x
Reference in New Issue
Block a user