Skip to content

Instantly share code, notes, and snippets.

@chadmiller
Created December 9, 2021 16:59
Show Gist options
  • Save chadmiller/b4c64bf20983af20eb81967b6dfcb4e7 to your computer and use it in GitHub Desktop.
Save chadmiller/b4c64bf20983af20eb81967b6dfcb4e7 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment