def Day_7_Part_1(filepath): rules = txt_to_array(filepath, "\n") array_shiny_gold_bags = [] bag = 0 array_bags = [] num_bags = len(array_bags) while bag <= num_bags+1: print("bag", bag) if num_bags == 0: bag_name = "shiny gold" else: bag_name = array_bags[bag-1] for i in range(0, len(rules)): if (bag_name in rules[i]): array_shiny_gold_bags.append(rules[i].split(" bag",1)[0]) for i in range(0,len(array_shiny_gold_bags)): if (array_shiny_gold_bags[i] not in array_bags) and (array_shiny_gold_bags[i] != "shiny gold"): array_bags.append(array_shiny_gold_bags[i]) num_bags = len(array_bags) bag = bag +1 if bag > num_bags: break return num_bags
def Day_8_Part_1(filepath): array_original = txt_to_array(filepath, "\n") array = [] for i in range(0,len(array_original)): x = re.split(" ", array_original[i])[0],int(re.split(" ", array_original[i])[1]) array.append(x) num_acc = 0 start = 0 step = start while 1 > 0 : point = array[step] if len(point)<3: if point[0] == "nop": array[step] = (point[0],point[1],"Done") step = step + 1 elif point[0] == "acc": num_acc = num_acc+point[1] array[step] = (point[0], point[1], "Done") step = step + 1 elif point[0] == "jmp": array[step] = (point[0], point[1], "Done") step = step + point[1] else: break return num_acc
def day_5_part_1(filepath): array = txt_to_array(filepath,"\n") result = [0] for i in range(0, len(array)): val = seat_ID(array[i]) if val > result[0]: result[0]=val return result[0]
def day_2_part_2(filepath): list_of_passwords = txt_to_array(filepath, "\n") num_of_valid_passwords = 0 for i in range(0, len(list_of_passwords)): valid = check_string_2_2(list_of_passwords[i]) if valid == True: num_of_valid_passwords = num_of_valid_passwords + 1 return num_of_valid_passwords
def Day_7_Part_2(filepath): rules_original = txt_to_array(filepath, "\n") rules_split = [] for i in range(0, len(rules_original)): x = re.split("contain|bags|bag|,",rules_original[i]) rules_split.append(x) rules = [] for i in range(0,len(rules_split)): rules_layer_1 = [] for j in range(0,len(rules_split[i])): if (rules_split[i][j] == " ") or(rules_split[i][j] == ".") or (rules_split[i][j] == ""): None else: rules_layer_1.append(" ".join(rules_split[i][j].split())) rules.append(rules_layer_1) index_no_other = [] for i in range(0,len(rules)): if rules[i][1] == "no other": index_no_other.append(i) dict_bags = {} for i in range(0,len(index_no_other)): bag = rules[index_no_other[i]][0] dict_bags[bag] = 1 valid = 1 while valid == 1: y = 0 for i in range(0,len(rules)): x = 0 for j in range(1,len(rules[i])): for bag_checked in dict_bags: if bag_checked in rules[i][j]: x = x+1 if ((x == (len(rules[i]))-1)) and (rules[i][0] not in dict_bags): dict_bags[rules[i][0]] = 0 for j in range(1, len(rules[i])): dict_bags["Keep"] = dict_bags[rules[i][0]] bag_val = rules[i][j].split(" ", 1)[1] val = int(rules[i][j].split(" ", 1)[0]) dict_bags[rules[i][0]] = dict_bags["Keep"]+val * dict_bags[bag_val] y = 1 if rules[i][0] != "shiny gold": dict_bags["Keep"] = dict_bags[rules[i][0]] dict_bags[rules[i][0]] = dict_bags["Keep"]+1 dict_bags["Keep"] = 0 if y == 0: valid = 0 return dict_bags["shiny gold"]
def day_3_part_1(filepath): array = txt_to_array(filepath, "\n") expanded_array = [] for i in range(0,len(array)): expanded_array.append(array[i]*100) a=0 b = 0 tree = 0 for i in range(0, len(expanded_array)-1): a = a+1 b = b+3 if expanded_array[a][b] == "#": tree = tree+1 return tree
def day_4_part_1(filepath): array_original = txt_to_array(filepath, "\n\n") array_to_sort = [] for i in range(0, len(array_original)): array_to_add = re.split('\n| ', array_original[i]) array_to_sort.append(array_to_add) array = sorted(array_to_sort, key=len) valid = 0 for i in range(len(array), 0, -1): if len(array[i - 1]) < 7: break elif len(array[i - 1]) == 7: add_valid = 1 for j in range(0, len(array[i - 1])): if "cid" in array[i - 1][j]: add_valid = 0 valid = valid + add_valid elif len(array[i - 1]) == 8: valid = valid + 1 return valid
def day_3_part_2(filepath): array = txt_to_array(filepath, "\n") expanded_array = [] for i in range(0,len(array)): expanded_array.append(array[i]*len(array)) trees = [] list_to_check = [[1,1],[1,3],[1,5],[1,7],[2,1]] for i in range(0, len(list_to_check)): a = 0 b = 0 tree = 0 for j in range(0, len(expanded_array)-list_to_check[i][0],list_to_check[i][0]): a = a + list_to_check[i][0] b = b+list_to_check[i][1] if expanded_array[a][b] == "#": tree = tree +1 trees.append(tree) product = 1 for k in range(0,len(trees)): product = product*trees[k] return product
def Day_6_Part_1(filepath): array_original = txt_to_array(filepath, "\n\n") array = [] for i in range(0, len(array_original)): array_to_add = re.split('\n| ', array_original[i]) array.append(array_to_add) char = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ] counter = 0 for i in range(0, len(array)): for character in char: found = 0 for j in range(0, len(array[i])): if array[i][j].find(character) >= 0: found = 1 if found == 1: counter = counter + 1 return counter
def day_4_part_2(filepath): array_original = txt_to_array(filepath, "\n\n") array_to_sort = [] for i in range(0, len(array_original)): array_to_add = re.split('\n| ', array_original[i]) array_to_sort.append(array_to_add) array = sorted(array_to_sort, key=len) valid = 0 for i in range(0, len(array)): if len(array[i]) < 7: add_valid = 0 elif len(array[i]) >= 7: add_valid = 1 check_val = 1 for j in range(0, len(array[i])): if ("cid" in array[i][j]) and (len(array[i]) == 7): add_valid = 0 check_val = 0 if check_val == 1: add_valid = check_validation_4_2(array[i]) valid = valid + add_valid return valid
def Day_9_Part_2(filepath, step_size): array = txt_to_array(filepath, "\n") step = step_size go = 1 number = 0 while go > 0: array_to_check = array[(step - step_size):step] sums = int(array[step]) check = step for i in range(0, len(array_to_check)): for j in range(0, len(array_to_check)): val_1 = int(array_to_check[i]) val_2 = int(array_to_check[j]) if (val_1 != val_2) and ((val_1 + val_2) == sums): step = step + 1 break if (val_1 != val_2) and ((val_1 + val_2) == sums): break if check == step: number = sums go = 0 index_goal = step array_sliced = array[:index_goal] for i in range(0, len(array_sliced)): total = 0 array_val = [] for j in range(i, len(array_sliced)): total = total + int(array_sliced[j]) array_val.append(int(array_sliced[j])) if total > number: break elif total == number: value = min(array_val) + max(array_val) return value
def Day_9_Part_1(filepath, step_size): array = txt_to_array(filepath, "\n") step = step_size go = 1 number = 0 while go > 0: array_to_check = array[(step - step_size):step] sums = int(array[step]) check = step for i in range(0, len(array_to_check)): for j in range(0, len(array_to_check)): val_1 = int(array_to_check[i]) val_2 = int(array_to_check[j]) if (val_1 != val_2) and ((val_1 + val_2) == sums): step = step + 1 break if (val_1 != val_2) and ((val_1 + val_2) == sums): break if check == step: number = sums go = 0 return number
def Day_8_Part_2(filepath): array_original = txt_to_array(filepath, "\n") array = [] for i in range(0,len(array_original)): x = re.split(" ", array_original[i])[0],int(re.split(" ", array_original[i])[1]) array.append([x[0],x[1]]) index_jmp = [] index_nop = [] print(array) print(array[0]) for i in range(0,len(array)): if array[i][0] == "jmp": index_jmp.append(i) elif array[i][0] == "nop": index_nop.append(i) step = 0 finished = 0 while step < len(array): for i in range(0, len(index_jmp)): array_copy = copy.deepcopy(array) array_copy[index_jmp[i]][0] = "nop" num_acc = 0 start = 0 step = start con = 1 while con > 0: point = array_copy[step] if len(point) < 3: if point[0] == "nop": array_copy[step] = (point[0], point[1], "Done") step = step + 1 elif point[0] == "acc": num_acc = num_acc + point[1] array_copy[step] = (point[0], point[1], "Done") step = step + 1 elif point[0] == "jmp": array_copy[step] = (point[0], point[1], "Done") step = step + point[1] if step == len(array_copy): con = 0 finished = 1 i = len(array) else: con = 0 break if finished == 1: break if finished == 0: for i in range(0, len(index_nop)): array_copy = copy.deepcopy(array) array_copy[index_jmp[i]][0] = "jmp" num_acc = 0 start = 0 step = start con = 1 while con > 0: point = array_copy[step] if len(point) < 3: if point[0] == "nop": array_copy[step] = (point[0], point[1], "Done") step = step + 1 elif point[0] == "acc": num_acc = num_acc + point[1] array_copy[step] = (point[0], point[1], "Done") step = step + 1 elif point[0] == "jmp": array_copy[step] = (point[0], point[1], "Done") step = step + point[1] if step == len(array_copy): finished = 1 i = len(array) else: con = 0 if finished == 1: i = len(array) return num_acc