mirror of
https://github.com/chylex/Advent-of-Code.git
synced 2025-05-20 04:34:03 +02:00
Add 2022 - Day 4 - Part 1
This commit is contained in:
parent
c2b2da271b
commit
4304efd023
24
2022/.idea/runConfigurations/Day_04.xml
Normal file
24
2022/.idea/runConfigurations/Day_04.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Day 04" type="PythonConfigurationType" factoryName="Python">
|
||||
<module name="2022" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
<envs>
|
||||
<env name="PYTHONUNBUFFERED" value="1" />
|
||||
</envs>
|
||||
<option name="SDK_HOME" value="" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/04" />
|
||||
<option name="IS_MODULE_SDK" value="true" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
|
||||
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/04/main.py" />
|
||||
<option name="PARAMETERS" value="" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<option name="EMULATE_TERMINAL" value="false" />
|
||||
<option name="MODULE_MODE" value="false" />
|
||||
<option name="REDIRECT_INPUT" value="false" />
|
||||
<option name="INPUT_FILE" value="" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
1000
2022/04/input.txt
Normal file
1000
2022/04/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
42
2022/04/main.py
Normal file
42
2022/04/main.py
Normal file
@ -0,0 +1,42 @@
|
||||
from utils.input import read_input_lines
|
||||
|
||||
|
||||
class Assignment:
|
||||
def __init__(self, first_section: int, last_section: int):
|
||||
self.first_section = first_section
|
||||
self.last_section = last_section
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"{self.first_section}-{self.last_section}"
|
||||
|
||||
def contains(self, other: "Assignment") -> bool:
|
||||
return self.first_section <= other.first_section and self.last_section >= other.last_section
|
||||
|
||||
@staticmethod
|
||||
def from_str(line: str) -> "Assignment":
|
||||
(first_section, last_section) = line.split("-", maxsplit = 1)
|
||||
return Assignment(int(first_section), int(last_section))
|
||||
|
||||
|
||||
class Pair:
|
||||
def __init__(self, first: Assignment, second: Assignment):
|
||||
self.first = first
|
||||
self.second = second
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"{self.first}, {self.second}"
|
||||
|
||||
def has_overlap(self) -> bool:
|
||||
return self.first.contains(self.second) or self.second.contains(self.first)
|
||||
|
||||
@staticmethod
|
||||
def from_line(line: str) -> "Pair":
|
||||
(first, second) = line.split(",", maxsplit = 1)
|
||||
return Pair(Assignment.from_str(first), Assignment.from_str(second))
|
||||
|
||||
|
||||
lines = read_input_lines()
|
||||
pairs = map(lambda line: Pair.from_line(line), lines)
|
||||
|
||||
overlaps = sum(1 for pair in pairs if pair.has_overlap())
|
||||
print(f"Amount of assignments with overlap: {overlaps}")
|
@ -83,7 +83,7 @@ The versions should not matter, but I used Visual Studio 2019 with `MSVC v142 (1
|
||||
| 2021 | 01 | Kotlin | / | 2022 | 01 | Python |
|
||||
| 2021 | 02 | Kotlin | / | 2022 | 02 | Python |
|
||||
| 2021 | 03 | Kotlin | / | 2022 | 03 | Python |
|
||||
| 2021 | 04 | Kotlin | / | | | |
|
||||
| 2021 | 04 | Kotlin | / | 2022 | 04 | Python |
|
||||
| 2021 | 05 | Kotlin | / | | | |
|
||||
| 2021 | 06 | Kotlin | / | | | |
|
||||
| 2021 | 07 | Kotlin | / | | | |
|
||||
|
Loading…
Reference in New Issue
Block a user