This commit is contained in:
Jake Pullen
2023-12-03 09:15:45 +00:00
5 changed files with 1165 additions and 8 deletions
+17 -2
View File
@@ -1,4 +1,4 @@
with open(r'advent_of_code\2022\day_3\input.txt', 'r') as file:
with open(r'D:\repos\advent_of_code\2022\day_3\input.txt', 'r') as file:
input = file.read()
test_input = '''vJrwpWtwJgWrhcsFMMfFFhFp
@@ -19,4 +19,19 @@ def task_1(file):
ascii_start = 96 if common_item.islower() else 38
priority += ord(common_item) - ascii_start
print("Task 1 result: " + str(priority))
task_1(input)
task_1(input)
def task_2(file):
# group 3 lines together, find the common item between all 3 lines
# using ASCII
priority = 0
#take the input and split it into groups of 3 lines
lines = file.split('\n')
grouped_lines = [lines[i:i + 3] for i in range(0, len(lines), 3)]
for group in grouped_lines:
common_items_set = set(group[0]).intersection(group[1], group[2])
common_item = common_items_set.pop()
ascii_start = 96 if common_item.islower() else 38
priority += ord(common_item) - ascii_start
print("Task 2 result: " + str(priority))
task_2(input)
+1000 -1
View File
File diff suppressed because it is too large Load Diff
+44 -2
View File
@@ -1,6 +1,48 @@
import os
with open(r'advent_of_code\2022\day_4\input.txt', 'r') as file:
with open(r'd:\repos\advent_of_code\2022\day_4\input.txt', 'r') as file:
input = file.read()
print(input)
# print(input)
test_input = '''2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8'''
lines = input.split('\n')
contained_pairs = 0
for line in lines:
print(line)
line = line.split(',')
left_pair = line[0]
right_pair = line[1]
left_pair = left_pair.split('-')
right_pair = right_pair.split('-')
#print(left_pair)
#print(right_pair)
left_range = int(left_pair[1]) - int(left_pair[0]) +1
right_range = int(right_pair[1]) - int(right_pair[0]) +1
#print(left_range)
#print(right_range)
if (left_range == right_range) and (left_pair[1] == right_pair[1]):
#print('valid')
contained_pairs += 1
elif (left_range > right_range):
# check to see if the right range starts and finishes within the left range
if int(right_pair[0]) >= int(left_pair[0]) and int(right_pair[1]) <= int(left_pair[1]):
contained_pairs += 1
else:
contained_pairs += 0
elif (right_range > left_range):
# check to see if the left range starts and finishes within the right range
if int(left_pair[0]) >= int(right_pair[0]) and int(left_pair[1]) <= int(right_pair[1]):
contained_pairs += 1
else:
contained_pairs += 0
else:
contained_pairs += 0
print(contained_pairs)
+47 -2
View File
@@ -1,6 +1,51 @@
import os
with open(r'advent_of_code\2022\day_4\input.txt', 'r') as file:
with open(r'd:\repos\advent_of_code\2022\day_4\input.txt', 'r') as file:
input = file.read()
print(input)
# print(input)
test_input = '''29-73,2-5
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8'''
lines = input.split('\n')
contained_pairs = 0
for line in lines:
#print(line)
line = line.split(',')
left_pair = line[0]
right_pair = line[1]
left_pair = left_pair.split('-')
right_pair = right_pair.split('-')
#print(left_pair)
#print(right_pair)
# right pair starts inbetween left pair
if int(left_pair[0]) <= int(right_pair[0]) <= int(left_pair[1]):
print('1',line)
contained_pairs += 1
continue
# right pair ends inbetween left pair
if int(left_pair[1]) >= int(right_pair[1]) >= int(left_pair[0]):
print('2',line)
contained_pairs += 1
continue
# left pair starts inbetween right pair
if int(right_pair[0]) <= int(left_pair[0]) <= int(right_pair[1]):
print('3',line)
contained_pairs += 1
continue
# left pair ends inbetween right pair
if int(right_pair[1]) >= int(left_pair[1]) >= int(right_pair[0]):
print('4',line)
contained_pairs += 1
continue
else:
contained_pairs += 0
print(contained_pairs)
+57 -1
View File
@@ -1 +1,57 @@
# Day 4 Puzzle Text.
# Day 4 Puzzle Text.
--- Day 4: Camp Cleanup ---
Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a range of section IDs.
However, as some of the Elves compare their section assignments with each other, they've noticed that many of the assignments overlap. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a big list of the section assignments for each pair (your puzzle input).
For example, consider the following list of section assignment pairs:
```
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
```
For the first few pairs, this list means:
Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf was assigned sections 6-8 (sections 6, 7, 8).
The Elves in the second pair were each assigned two sections.
The Elves in the third pair were each assigned three sections: one got sections 5, 6, and 7, while the other also got 7, plus 8 and 9.
This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this:
```
.234..... 2-4
.....678. 6-8
.23...... 2-3
...45.... 4-5
....567.. 5-7
......789 7-9
.2345678. 2-8
..34567.. 3-7
.....6... 6-6
...456... 4-6
.23456... 2-6
...45678. 4-8
```
Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are 2 such pairs.
In how many assignment pairs does one range fully contain the other?
--- Part Two ---
It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all.
In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:
5-7,7-9 overlaps in a single section, 7.
2-8,3-7 overlaps all of the sections 3 through 7.
6-6,4-6 overlaps in a single section, 6.
2-6,4-8 overlaps in sections 4, 5, and 6.
So, in this example, the number of overlapping assignment pairs is 4.
In how many assignment pairs do the ranges overlap?