Skip to content

Commit

Permalink
Indent AoC 2021 with 2 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
loociano committed Dec 4, 2024
1 parent 4ef4b65 commit a972b77
Show file tree
Hide file tree
Showing 8 changed files with 526 additions and 526 deletions.
58 changes: 29 additions & 29 deletions aoc2021/src/day01/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@


def part_one(depth_report: Sequence[int]) -> int:
"""AOC 2021 Day 1 Part 1.
"""AOC 2021 Day 1 Part 1.
Args:
depth_report: A sonar sweep report which consist of depth measurements.
Returns:
Number of times a depth measurement increases.
"""
count_depth_increases = 0
last_depth = None
for depth in depth_report:
count_depth_increases += 1 if (last_depth and depth > last_depth) else 0
last_depth = depth
return count_depth_increases
Args:
depth_report: A sonar sweep report which consist of depth measurements.
Returns:
Number of times a depth measurement increases.
"""
count_depth_increases = 0
last_depth = None
for depth in depth_report:
count_depth_increases += 1 if (last_depth and depth > last_depth) else 0
last_depth = depth
return count_depth_increases


def part_two(depths: Sequence[int]) -> int:
"""AOC 2021 Day 1 Part 2.
"""AOC 2021 Day 1 Part 2.
Args:
depths: A sonar sweep report which consist of depth measurements.
Returns:
Number of times a depth measurement increases in sums of a
three-measurement sliding window.
"""
count_depth_increases = 0
last_window_sum = None
for pos, depth in enumerate(depths):
if pos == 0 or pos == len(depths) - 1:
continue # Sliding window is out of bounds.
window_sum = depths[pos - 1] + depths[pos] + depths[pos + 1]
if last_window_sum and window_sum > last_window_sum:
count_depth_increases += 1
last_window_sum = window_sum
return count_depth_increases
Args:
depths: A sonar sweep report which consist of depth measurements.
Returns:
Number of times a depth measurement increases in sums of a
three-measurement sliding window.
"""
count_depth_increases = 0
last_window_sum = None
for pos, depth in enumerate(depths):
if pos == 0 or pos == len(depths) - 1:
continue # Sliding window is out of bounds.
window_sum = depths[pos - 1] + depths[pos] + depths[pos + 1]
if last_window_sum and window_sum > last_window_sum:
count_depth_increases += 1
last_window_sum = window_sum
return count_depth_increases
164 changes: 82 additions & 82 deletions aoc2021/src/day02/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,109 +17,109 @@


def _parse(instruction: str) -> Tuple[str, int]:
"""Parses an instruction.
Args:
instruction: A raw input instruction.
Returns:
command and command value.
"""
command, value = instruction.split(' ')
# TODO: handle incorrect instructions.
if not command:
raise ValueError('Missing command!')
if not value:
raise ValueError('Missing value!')
return command, int(value)
"""Parses an instruction.
Args:
instruction: A raw input instruction.
Returns:
command and command value.
"""
command, value = instruction.split(' ')
# TODO: handle incorrect instructions.
if not command:
raise ValueError('Missing command!')
if not value:
raise ValueError('Missing value!')
return command, int(value)


@dataclass
class Submarine(ABC):
"""Represents a generic submarine."""
horizontal_position: int = 0
depth: int = 0
"""Represents a generic submarine."""
horizontal_position: int = 0
depth: int = 0

@abstractmethod
def execute(self, instruction: str) -> None:
pass
@abstractmethod
def execute(self, instruction: str) -> None:
pass


@dataclass
class PartOneSubmarine(Submarine):
"""Submarine that follows specs from part two ."""

def execute(self, instruction: str) -> None:
"""Executes an instruction.
Args:
instruction: A raw input instruction.
"""
command, value = _parse(instruction=instruction)
if command == 'forward':
self.horizontal_position += value
elif command == 'down':
self.depth += value
elif command == 'up':
self.depth -= value
else:
raise ValueError(f'Unrecognized command {command}.')
"""Submarine that follows specs from part two ."""

def execute(self, instruction: str) -> None:
"""Executes an instruction.
Args:
instruction: A raw input instruction.
"""
command, value = _parse(instruction=instruction)
if command == 'forward':
self.horizontal_position += value
elif command == 'down':
self.depth += value
elif command == 'up':
self.depth -= value
else:
raise ValueError(f'Unrecognized command {command}.')


@dataclass
class PartTwoSubmarine(Submarine):
"""Submarine that follows specs from part one."""
aim: int = 0

def execute(self, instruction: str) -> None:
"""Executes an instruction.
Args:
instruction: A raw input instruction.
"""
command, value = _parse(instruction=instruction)
if command == 'forward':
self.horizontal_position += value
self.depth += self.aim * value
elif command == 'down':
self.aim += value
elif command == 'up':
self.aim -= value
else:
raise ValueError(f'Unrecognized command {command}.')
"""Submarine that follows specs from part one."""
aim: int = 0

def execute(self, instruction: str) -> None:
"""Executes an instruction.
Args:
instruction: A raw input instruction.
"""
command, value = _parse(instruction=instruction)
if command == 'forward':
self.horizontal_position += value
self.depth += self.aim * value
elif command == 'down':
self.aim += value
elif command == 'up':
self.aim -= value
else:
raise ValueError(f'Unrecognized command {command}.')


def _run(planned_course: Sequence[str], submarine: Submarine) -> int:
"""Runs the planned course and computes result.
"""Runs the planned course and computes result.
Returns:
Final horizontal position multiplied by final depth.
"""
for instruction in planned_course:
submarine.execute(instruction=instruction)
return submarine.horizontal_position * submarine.depth
Returns:
Final horizontal position multiplied by final depth.
"""
for instruction in planned_course:
submarine.execute(instruction=instruction)
return submarine.horizontal_position * submarine.depth


def part_one(planned_course: Sequence[str]) -> int:
"""AOC 2021 Day 2 Part 1.
"""AOC 2021 Day 2 Part 1.
Args:
planned_course: sequence of instructions:
- 'forward X' increases the horizontal position by X units.
- 'down X' increases the depth by X units.
- 'up X' decreases the depth by X units.
Returns:
Final horizontal position multiplied by final depth.
"""
return _run(planned_course=planned_course, submarine=PartOneSubmarine())
Args:
planned_course: sequence of instructions:
- 'forward X' increases the horizontal position by X units.
- 'down X' increases the depth by X units.
- 'up X' decreases the depth by X units.
Returns:
Final horizontal position multiplied by final depth.
"""
return _run(planned_course=planned_course, submarine=PartOneSubmarine())


def part_two(planned_course: Sequence[str]) -> int:
"""AOC 2021 Day 2 Part 2.
Args:
planned_course: sequence of instructions:
- 'forward X' increases horizontal position by X units and increases depth
by the aim multiplied by X.
- 'down X' increases aim by X units.
- 'up X' decreases aim by X units.
Returns:
Final horizontal position multiplied by final depth.
"""
return _run(planned_course=planned_course, submarine=PartTwoSubmarine())
"""AOC 2021 Day 2 Part 2.
Args:
planned_course: sequence of instructions:
- 'forward X' increases horizontal position by X units and increases depth
by the aim multiplied by X.
- 'down X' increases aim by X units.
- 'up X' decreases aim by X units.
Returns:
Final horizontal position multiplied by final depth.
"""
return _run(planned_course=planned_course, submarine=PartTwoSubmarine())
Loading

0 comments on commit a972b77

Please sign in to comment.