mirror of
https://github.com/rjNemo/advent_of_code_2022
synced 2026-06-06 02:26:41 +00:00
day4 part 1
This commit is contained in:
parent
62a1897a35
commit
b7e189bb9e
8 changed files with 1107 additions and 11 deletions
0
common/__init__.py
Normal file
0
common/__init__.py
Normal file
3
common/file.py
Normal file
3
common/file.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
def read_data() -> list:
|
||||||
|
with open("./input.txt", "r") as f:
|
||||||
|
return [line.strip() for line in f.readlines()]
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
from common.file import read_data
|
||||||
|
|
||||||
|
|
||||||
def total_score_1(data: list[list[str]]) -> int:
|
def total_score_1(data: list[list[str]]) -> int:
|
||||||
points = {
|
points = {
|
||||||
"A": 1,
|
"A": 1,
|
||||||
|
|
@ -47,11 +50,6 @@ def total_score_2(data: list[list[str]]) -> int:
|
||||||
return score
|
return score
|
||||||
|
|
||||||
|
|
||||||
def read_data() -> list[list[str]]:
|
|
||||||
with open("./input.txt", "r") as f:
|
|
||||||
return [row.strip().split(" ") for row in f.readlines()]
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_data = [
|
test_data = [
|
||||||
("A", "Y"),
|
("A", "Y"),
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
from string import ascii_letters
|
from string import ascii_letters
|
||||||
|
|
||||||
|
from common.file import read_data
|
||||||
|
|
||||||
test_dataset = [
|
test_dataset = [
|
||||||
"vJrwpWtwJgWrhcsFMMfFFhFp",
|
"vJrwpWtwJgWrhcsFMMfFFhFp",
|
||||||
"jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL",
|
"jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL",
|
||||||
|
|
@ -17,18 +19,13 @@ def priority_shared_items_sum_1(data: list[str]) -> int:
|
||||||
def process(rucksack: str) -> int:
|
def process(rucksack: str) -> int:
|
||||||
size = len(rucksack)
|
size = len(rucksack)
|
||||||
a = rucksack[: size // 2]
|
a = rucksack[: size // 2]
|
||||||
b = rucksack[size // 2:]
|
b = rucksack[size // 2 :]
|
||||||
a = set(a)
|
a = set(a)
|
||||||
b = set(b)
|
b = set(b)
|
||||||
item = a.intersection(b)
|
item = a.intersection(b)
|
||||||
return ascii_letters.index(item.pop()) + 1
|
return ascii_letters.index(item.pop()) + 1
|
||||||
|
|
||||||
|
|
||||||
def read_data() -> list[str]:
|
|
||||||
with open("./input.txt", "r") as f:
|
|
||||||
return [line.strip() for line in f.readlines()]
|
|
||||||
|
|
||||||
|
|
||||||
def priority_shared_items_sum_2(data: list[str]) -> int:
|
def priority_shared_items_sum_2(data: list[str]) -> int:
|
||||||
badges = []
|
badges = []
|
||||||
i = 0
|
i = 0
|
||||||
|
|
|
||||||
61
day4/README.md
Normal file
61
day4/README.md
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
# 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 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 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?**
|
||||||
0
day4/__init__.py
Normal file
0
day4/__init__.py
Normal file
1000
day4/input.txt
Normal file
1000
day4/input.txt
Normal file
File diff suppressed because it is too large
Load diff
37
day4/main.py
Normal file
37
day4/main.py
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
from common.file import read_data
|
||||||
|
|
||||||
|
test_dataset = [
|
||||||
|
"2-4,6-8",
|
||||||
|
"2-3,4-5",
|
||||||
|
"5-7,7-9",
|
||||||
|
"2-8,3-7",
|
||||||
|
"6-6,4-6",
|
||||||
|
"2-6,4-8",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def find_overlapping_assignments_1(data: list[str]) -> int:
|
||||||
|
# iterate over the data, for each line
|
||||||
|
counter = 0
|
||||||
|
for line in data:
|
||||||
|
# split the ','
|
||||||
|
a, b = line.strip().split(",")
|
||||||
|
start_a, end_a = [int(x) for x in a.split("-")]
|
||||||
|
int_a = range(start_a, end_a + 1)
|
||||||
|
start_b, end_b = [int(x) for x in b.split("-")]
|
||||||
|
int_b = range(start_b, end_b + 1)
|
||||||
|
# if 1 interval in the other increment the counter
|
||||||
|
if all(x in int_b for x in int_a) or all(x in int_a for x in int_b):
|
||||||
|
counter += 1
|
||||||
|
# return the counter
|
||||||
|
return counter
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
dataset = read_data()
|
||||||
|
|
||||||
|
res = find_overlapping_assignments_1(dataset)
|
||||||
|
assert res == 2, f"{res} is not the right value"
|
||||||
|
#
|
||||||
|
# res = priority_shared_items_sum_2(dataset)
|
||||||
|
# assert res == 70, f"{res} is not the right value"
|
||||||
Loading…
Reference in a new issue