2016-12-06 - Signals and Noise
(original .ipynb)from collections import defaultdict puzzle_input_str = open("puzzle_input/day6.txt").read() test_input_str = """eedadn drvtee eandsr raavrd atevrs tsrnev sdttsa rasrtv nssdts ntnada svetve tesnvt vntsnd vrdear dvrsen enarar""" def error_correct(signals, sort_descending=True): positions = [] for n in range(len(signals[0])): positions.append(defaultdict(lambda:0)) for signal in signals: for i, c in enumerate(signal): positions[i][c] += 1 word = [] for position in positions: word.append(sorted(position, key=lambda x:position[x], reverse=sort_descending)[0]) return "".join(word) def part_one(input_str): lines = input_str.split("\n") return error_correct(lines) assert "easter" == part_one(test_input_str) print("part one:" , part_one(puzzle_input_str))
part one: bjosfbce
def part_two(input_str): lines = input_str.split("\n") return error_correct(lines, False) assert "advent" == part_two(test_input_str) print("part two:", part_two(puzzle_input_str))
part two: veqfxzfx