def setUp(self) -> None: self.test_data_1 = [ int(i) for i in get_input(__file__, "input_test_1") ] self.test_data_2 = [ int(i) for i in get_input(__file__, "input_test_2") ]
from typing import List from itertools import product from enum import Enum from shared.helper import get_input # Setup @dataclass() class Seat: row: int column: int id: int data = get_input(__file__) class Partition(Enum): upper = 1 lower = 2 def binary_search(partitions: List[Partition], seats: List[int]) -> int: if len(seats) > 1: mid = len(seats) // 2 next_partition, remaining_partitions = partitions[0], partitions[1:] upper, lower = seats[mid:], seats[:mid]
import math from typing import List from shared.helper import get_input data: List[str] = get_input(__file__) # Problem 1 class SkiSlope: ground = "." tree = "#" def __init__(self, data: List[str]) -> None: self.slope = [self.__convert_line(line) for line in data] def __convert_line(self, line: str): """ Convert trees & ground to 0s and 1s """ return [ 0 if c == self.ground else 1 for c in line if c in [self.tree, self.ground] ] def count_trees(self, x: int, y: int) -> int: """ Returns number of trees for a given path down the slope. """
def setUp(self) -> None: self.test_data = get_input(__file__, file_name="input_test") self.invalid_data = get_input(__file__, file_name="input_test_invalid") self.valid_data = get_input(__file__, file_name="input_test_valid")
# Setup from typing import Generator, List, Tuple from itertools import product from shared.helper import get_input data = [int(i) for i in get_input(__file__)] Chunk = List[int] NextNumber = int def create_chunks( nums: List[int], preamble_size: int) -> Generator[Tuple[Chunk, NextNumber], None, None]: """ Returns a generator for getting a slice of the provided nums based on the preamble size paired with the next number. Example item: ([35, 20, 15, 25, 47], 40) """ return ((nums[x:x + preamble_size], x + preamble_size) for x in range(0, len(nums)) if (x + preamble_size) < len(nums)) def contains_valid_pair(current_num: int, chunk: List[int]): """ Checks whether there is a pair of numbers in the provided chunk that add up to the current_num. """ return any((current_num - x) in chunk for x in chunk)
def setUp(self) -> None: self.test_data = get_input(__file__, "input_test") self.test_data_2 = get_input(__file__, "input_test_2")
def setUp(self) -> None: self.test_data = [int(i) for i in get_input(__file__, "input_test")]
from enum import Enum from typing import List from itertools import product from shared.helper import get_input data = [list(s.strip()) for s in get_input(__file__)] class SeatStatus(Enum): Occupied = "#" Empty = "L" Floor = "." AllSeats = List[List[SeatStatus]] # Problem 1 class FerryProblem1(): def __init__(self, data: List[List[str]]) -> None: seats: AllSeats = [] for row in data: parsed_row: List[SeatStatus] = [] for seat in row: parsed_row.append(SeatStatus(seat)) seats.append(parsed_row)
import math from shared.helper import get_input from typing import List file_1 = get_input(__file__) file_data = [int(x) for x in file_1] # Problem 1 def get_entries(data: List[int], expected_sum: int) -> List[int]: for num in data: if (expected_sum - num) in data: return [num, expected_sum - num] return [0, 0] # default if not found def problem_1(): return math.prod(get_entries(file_data, 2020)) # Problem 2 def get_three_entries(data: List[int], expected_sum: int) -> List[int]: for num in data: remaining = expected_sum - num
def setUp(self) -> None: self.test_data = get_input(__file__, file_name="input_test")
def setUp(self) -> None: self.test_data = [ list(s.strip()) for s in get_input(__file__, "input_test") ]