Created
December 9, 2021 16:59
Revisions
-
chadmiller created this gist
Dec 9, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ def get_wires(possibilities, of_length, after_removing, is_strict_subset=False): found = list(wires for wires in possibilities if len(wires - after_removing) == of_length) if is_strict_subset: found = list(wires for wires in found if len(after_removing - wires) == 0) possibilities.remove(found[0]) # remove from future consideration return found[0] def decipher_samples(samples): """ >>> sorted(decipher_samples("acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab").items()) [('ab', '1'), ('abcdef', '9'), ('abcdefg', '8'), ('abcdeg', '0'), ('abcdf', '3'), ('abd', '7'), ('abef', '4'), ('acdfg', '2'), ('bcdef', '5'), ('bcdefg', '6')] """ samples = set(frozenset(sample) for sample in samples.split()) wires = dict() wires['1'] = get_wires(samples, 2, set()) wires['7'] = get_wires(samples, 3, set()) wires['4'] = get_wires(samples, 4, set()) wires['8'] = get_wires(samples, 7, set()) wires['9'] = get_wires(samples, 2, wires['4'], True) wires['6'] = get_wires(samples, 5, wires['1']) wires['0'] = get_wires(samples, 6, set()) wires['2'] = get_wires(samples, 1, wires['9']) wires['3'] = get_wires(samples, 2, wires['7']) wires['5'] = get_wires(samples, 5, set()) return dict(("".join(sorted(w)), number) for number, w in wires.items()) s = 0 for line in actual_data.split("\n"): samples, puzzle = line.split(" | ") wires_to_num = decipher_samples(samples) #s += int("".join(wires_to_num["".join(sorted(word))] for word in puzzle.split())) s += int("".join(wires_to_num["".join(sorted(word))] for word in puzzle.split())) print(s)