diff --git a/2022/day4-sets.py b/2022/day4-sets.py new file mode 100644 index 0000000..8ead7fe --- /dev/null +++ b/2022/day4-sets.py @@ -0,0 +1,14 @@ +from helpers import * + +with open('input/4', 'r') as file: + sets_per_line = [ + (set(range(min(n[0], n[1]), max(n[0], n[1])+1)), + set(range(min(n[2], n[3]), max(n[2], n[3])+1))) + for n in lines_to_numbers(file.readlines()) + ] + +count = sum((set1 <= set2 or set1 >= set2) for (set1, set2) in sets_per_line) +print(f'Part 1: {count} elves have no unique work in their pairing') # Part 1 + +count = sum((len(set1 & set2) > 0) for (set1, set2) in sets_per_line) +print(f'Part 2: {count} elf pairs have overlapping work in their pairing') # Part 2 diff --git a/2022/day5.py b/2022/day5.py index 0b4f0d0..29b34dd 100644 --- a/2022/day5.py +++ b/2022/day5.py @@ -1,13 +1,10 @@ -import re -numbers_pattern = re.compile(r'((?:(? 0} -order_numbers = [[int(x) for x in numbers_pattern.findall(line)] for line in input_orders.split('\n')] +starting_stack_strings = {int(l[0]):l[1:] for l in transpose_array_of_strings(input_stacks.split('\n'), reverse_y=True, strip=' []') if len(l) > 0} +order_numbers = lines_to_numbers(input_orders.split('\n')) stacks = {k:[c for c in v] for k,v in starting_stack_strings.items()} for (num, source, dest) in order_numbers: diff --git a/2022/helpers.py b/2022/helpers.py new file mode 100644 index 0000000..195735d --- /dev/null +++ b/2022/helpers.py @@ -0,0 +1,11 @@ +import re +numbers_pattern = re.compile(r'((?:(?