def run_part_1(): data = advent_tools.read_whole_input() result = '' in_parentheses = False in_repeat_section = False to_repeat = '' repeat_inst = '' len_repeat = 0 times_repeat = 0 for char in data: if in_parentheses: if char == ')': len_repeat, times_repeat = (int(num) for num in repeat_inst.split('x')) in_parentheses = False in_repeat_section = True to_repeat = '' else: repeat_inst = repeat_inst + char elif in_repeat_section: to_repeat = to_repeat + char if len(to_repeat) == len_repeat: result = result + to_repeat * times_repeat in_repeat_section = False elif char == '(': in_parentheses = True repeat_inst = '' else: result = result + char print(len(result))
def run_part_1(): instructions = advent_tools.read_whole_input().split(',') letters = list(ascii_lowercase[:16]) # instructions = ['s1', 'x3/4', 'pe/b'] # letters = list('abcde')/ letters = do_dance(instructions, letters) return ''.join(letters)
def __init__(self): self.state = 'A' self.position = 0 self.tape = collections.defaultdict(bool) program = advent_tools.read_whole_input() program = program + '\n\n' flags = re.MULTILINE | re.DOTALL state_pat = r'(In state ([A-Z]):.*?\n\n)' state_descs = re.findall(state_pat, program, flags) self.cases = {} cur_pat = re.compile( r'If the current value is ([0-1]):\n\s*' r'- Write the value ([0-1]).\n\s*' r'- Move one slot to the ([a-z]*).\n\s*' r'- Continue with state ([A-Z]).', flags) for subtext, state in state_descs: cur_descs = re.findall(cur_pat, subtext) for cur_val, write_val, direction, new_state in cur_descs: if direction == 'right': increment = 1 else: increment = -1 self.cases[(state, bool(int(cur_val)))] = (bool(int(write_val)), increment, new_state)
def run_part_2(): x = 0 y = 0 max_steps = 813 for line in advent_tools.read_whole_input().split(','): x, y = move_dir(x, y, line) num_steps = abs(x) + max(0, (abs(y) - abs(x)) / 2) if num_steps > max_steps: max_steps = num_steps return max_steps
def run_part_2(): instructions = advent_tools.read_whole_input() letters = ascii_lowercase[:16] init_letters = letters period = False for i in range(1000000000): if i % 10000000 == 0: print(i) letters = do_dance(instructions, letters) if letters == init_letters and not period: period = True print('Period: ', i) return ''.join(letters)
def run_part_2(): lengths = (convert_ascii(advent_tools.read_whole_input()) + [17, 31, 73, 47, 23]) pos = 0 skip = 0 result = list(range(256)) for i in range(64): result, pos, skip = run_operations(lengths, result, pos, skip) dense = [] for chunk in advent_tools.chunk_iterable(result, 16): hexval = hex(functools.reduce(xor, chunk, 0)) if len(hexval) == 4: dense.append(hexval[2:]) else: dense.append('0' + hexval[2:]) return ''.join(dense)
def run_part_1(): seen = set() banks = [int(num) for num in advent_tools.read_whole_input().split()] # banks = [0, 2, 7, 0] count = 0 while True: count = count + 1 maxval = max(banks) ind = banks.index(maxval) banks[ind] = 0 for m in range(maxval): add_index = (ind + m + 1) % len(banks) banks[add_index] = banks[add_index] + 1 # print(banks) if tuple(banks) in seen: break seen.add(tuple(banks)) print(count)
def run_part_1(): lengths = [int(num) for num in advent_tools.read_whole_input().split(',')] result, _, _ = run_operations(lengths, list(range(256))) return (result[0] * result[1])
def run_part_1(): x = 0 y = 0 for line in advent_tools.read_whole_input().split(','): x, y = move_dir(x, y, line) return abs(x) + max(0, (abs(y) - abs(x)) / 2)
def run_both_parts(): data = advent_tools.read_whole_input() data = clean_ex(data) data, count = remove_garbage(data) print('Part 1:', total_score(data)) print('Part 2:', count)
def run_part_2(): print(get_uncomp_length(advent_tools.read_whole_input()))
def main(): data = advent_tools.read_whole_input() print('Part 1:', run_part_1(data)) print('Part 2:', run_part_2(data))
def run_part_1(): the_sum = 0 data = [int(char) for char in advent_tools.read_whole_input()] print(sum(first for first, second in zip(data, data[1:] + [data[0]]) if first==second))
def run_part_2(): data = [int(char) for char in advent_tools.read_whole_input()] jump = len(data) // 2 print(sum(first for first, second in zip(data, data[jump:] + data[:jump]) if first == second))