def solve(inp): ops = { 'acc': acc, 'jmp': jmp, 'nop': nop, } accumulator = 0 index = 0 rules = stacked2list(inp, str) rules = [ru.rstrip() for ru in rules] used_rules = {} print(rules) while True: if used_rules.get(index): print("about to repeat, breaking instead") break rule = rules[index] used_rules[index] = rule instruction, delta = rule.split(' ') delta = int(delta) accumulator, index = ops.get(instruction)( rules, delta, accumulator, index) print('instruction and delta', instruction, delta) print('index is', accumulator, index) print('final values for accumulator and index', accumulator, index)
def find_match_of_3(inp): num_list = stacked2list(inp) for i in num_list: for j in num_list: for k in num_list: if i + j + k == 2020: return i * j * k
def solve(inp, preamble_length): rules = stacked2list(inp, int) for ru in rules: ru = ru # print(ru) pa_len = preamble_length i = pa_len m = 0 n = i # print(rules[m:n]) # print('perm', perm) while n < len(rules): # print(rules[n]) sub_rules = rules[m:n] combos = combinations(sub_rules, 2) next_value = rules[n] print("next value is", next_value) match = None for [x, y] in combos: sum = x + y if sum == next_value: match = [x, y] break if match == None: print("the problem number is ", next_value) return next_value i = i + 1 m = m + 1 n = n + 1
def find_match_of_2(inp): num_list = stacked2list(inp) for i in num_list: res = check_for_buddy(i, num_list) if res[0] + res[1] > 0: break return res[0] * res[1]
def solve(inp): rules = stacked2list(inp, str) rules = strip_newlines(rules) # for ru in rules: # print(ru) part1(rules) part2(rules)
def solve(inp): rules = stacked2list(inp, str) rules = [r.strip() for r in rules] earliest = int(rules[0]) busids = rules[1].split(',') p1 = solvep1(earliest, busids) print('p1 answer', p1) p2 = solvep2(busids) print('p1 answer', p2)
def solve(inp): rules = stacked2list(inp, str) rules = [r.strip() for r in rules] rules = [{'action': r[0], 'value': int(r[1:])} for r in rules ] final_pos = make_trail(rules) # part 1 answer1 = abs(final_pos[0]) + abs(final_pos[1]) print('answer for part 1 is ', answer1) final_pos = make_trail_part2(rules) # part 1 answer2 = abs(final_pos[0]) + abs(final_pos[1]) print('answer for part 2 is ', int(answer2))
def solve2(inp, invalid_number): rules = stacked2list(inp, int) for ru in rules: ru = ru # print(ru) i = 0 while i < len(rules): total = 0 span = 2 while total < invalid_number: sub_rules = rules[i:i+span] total = sum(sub_rules) if total == invalid_number: print("eureka found numbers", sub_rules) # sub_rules[0] + sub_rules[-1]) src = sub_rules.copy() src.sort() return src[0] + src[-1] span = span + 1 i = i + 1
def solve2(inp): ops = { 'acc': acc, 'jmp': jmp, 'nop': nop, } accumulator = 0 index = 0 rules = stacked2list(inp, str) rules = [ru.rstrip() for ru in rules] orig_rules = rules.copy() print("orig rules", orig_rules[0]) used_rules = {} # print(rules) changed_index = 0 while True: if index >= len(rules): print("it terminated!!! made it! ", accumulator, index) break if used_rules.get(index): print("rules were", rules) rules = change_rules(orig_rules.copy(), changed_index) print("rules are now", rules) used_rules = {} accumulator = 0 index = 0 changed_index = changed_index + 1 print("about to repeat, breaking instead", changed_index, len(rules)) continue rule = rules[index] used_rules[index] = rule instruction, delta = rule.split(' ') delta = int(delta) accumulator, index = ops.get(instruction)( rules, delta, accumulator, index) print('instruction and delta', instruction, delta) print('index is', accumulator, index)
def solve(inp): i = 0 diffs = [] kolts = stacked2list(inp, int) kolts = sorted(kolts) kolts = [0] + kolts + [kolts[-1] + 3] while i < (len(kolts) - 1): diff = kolts[i + 1] - kolts[i] diffs.append(diff) i = i + 1 # part 1 stuff dict_diffs = {} for _, val in enumerate(diffs): try: dict_diffs[val] = dict_diffs[val] + 1 except KeyError: dict_diffs[val] = 1 # part 2 stuff diffs = ''.join([str(n) for n in diffs]) # diffs = ['13113111311113111113111111'] diffs = diffs.split('3') # diffs = ['1', '11', '111', '1111', '11111', '111111'] # collect some multiples that we can multiply together to get the answer of possible combos multis = [] for big_pile in diffs: if big_pile != '': multi = get_multi_from_shit(big_pile) multis.append(len(multi)) # multiply all of the numbers from the '1111' and '11' prod = numpy.prod(multis) return (dict_diffs.get(3) * dict_diffs.get(1), prod)
def solve(inp): rules = stacked2list(inp, str) rule_book = build_book(rules) # print("rule book", rule_book) query = 'shiny_gold' # queries_found = 0 count = 0 # part 1 for rule in rule_book: bags_for_rule = get_bags_for_rule(rule_book, rule) res = explode_bag(rule_book, rule, query, rule, bags_for_rule) # print("matches were", res) if res: count = count + 1 # part 2 shiny_gold_contained_bags = rule_book.get('shiny_gold') total = tabulate_bags(rule_book, shiny_gold_contained_bags, 1) print("p2: bags inside", query, "were", total) return (count, total - 1)
def find_seats(inp): seats = map(lambda s: s.strip(), stacked2list(inp, str)) highest_seat_id = 0 seat_list = [] for s in seats: row_num = s[:7].replace('F', '0').replace('B', '1') row_num = (int(row_num, 2)) col_num = s[7:].replace('L', '0').replace('R', '1') col_num = (int(col_num, 2)) seat_id = calc_id(row_num, col_num) # part 1 highest_seat_id = seat_id if seat_id > highest_seat_id else highest_seat_id seat_list.append(seat_id) # print("row is ", row_num, " col is ", col_num, " seat id ", seat_id) seat_list = sorted(seat_list) # part 2 # note i manually select 48 to save time, it could be written more dynamically but it saved me 20 minutes so i did it the lazy way # my seat num is assumed to be length 1 my_seat_num = list(set([*range(48, 819)]) - set(seat_list)).pop() return (highest_seat_id, my_seat_num)
def solve(inp): rules = stacked2list(inp, str) for ru in rules: print(ru)
def find_num_valid_passwords_by_count(passwords): num_valids = 0 for line in passwords: rule, letter, pw = line.strip().split(" ") rule = rule.split('-') letter = letter[0] if (password_is_valid_by_count(rule, letter, pw)): num_valids = num_valids + 1 return num_valids def find_num_valid_passwords_by_pos(passwords): num_valids = 0 for line in passwords: rule, letter, pw = line.strip().split(" ") rule = rule.split('-') letter = letter[0] if (password_is_valid_by_pos(rule, letter, pw)): num_valids = num_valids + 1 return num_valids if __name__ == '__main__': passwords = stacked2list(fileinput.input(), str) # part1 print(find_num_valid_passwords_by_count(passwords)) # part2 print(find_num_valid_passwords_by_pos(passwords)) # print(find_match_of_3(fileinput.input()))