def test_cart_equality() -> None: cart1 = Cart(Point(4, 6), '^') cart2 = Cart(Point(4, 6), '>') cart3 = Cart(Point(5, 6), '^') assert cart1 == cart2 assert cart1 != cart3 assert cart2 != cart3
def get_overlapping_claim_cells(claim1: Claim, claim2: Claim) -> Set[Point]: claim1_cells = set( Point(x, y) for x in range(claim1.xmin, claim1.xmax) for y in range(claim1.ymin, claim1.ymax)) claim2_cells = set( Point(x, y) for x in range(claim2.xmin, claim2.xmax) for y in range(claim2.ymin, claim2.ymax)) return claim1_cells.intersection(claim2_cells)
def test_cart_move_straight() -> None: cart1 = Cart(Point(5, 6), '>') cart1.move('-') assert cart1.location == (6, 6) assert cart1.direction == EAST cart2 = Cart(Point(18, 7), 'v') cart2.move('|') assert cart2.x == 18 assert cart2.y == 8 assert cart2.direction == SOUTH
def test_cart_move_collision_detection() -> None: cart1 = Cart(Point(3, 4), '>') cart2 = Cart(Point(3, 5), 'v') cart3 = Cart(Point(4, 4), '^') cart4 = Cart(Point(4, 5), '<') others = [cart2, cart3, cart4] cart1.move('-', others) assert cart1.crashed assert cart3.crashed assert not cart2.crashed assert not cart4.crashed
def test_cart_inequality() -> None: cart1 = Cart(Point(3, 7), '>') # after move == 3,6 cart2 = Cart(Point(2, 7), 'v') # after move == 3,7 cart3 = Cart(Point(1, 8), '^') # after move == 0,8 assert cart1 > cart2 assert cart2 < cart1 assert cart1 < cart3 assert cart2 < cart3 carts = sorted([cart1, cart2, cart3]) assert carts == [cart2, cart1, cart3] for cart in carts: cart.move('+') carts.sort() assert carts == [cart1, cart2, cart3]
def test_cart_move_corner() -> None: cart1 = Cart(Point(1, 1), '^') cart1.move('/') assert cart1.location == (2, 1) assert cart1.direction == EAST cart1.move('\\') assert cart1.location == (2, 2) assert cart1.direction == SOUTH cart2 = Cart(Point(1, 1), '^') cart2.move('\\') assert cart2.location == (0, 1) assert cart2.direction == WEST cart2.move('/') assert cart2.location == (0, 2) assert cart2.direction == SOUTH
def __init__(self, seeds: List[IDPoint], padding: int = 0) -> None: self.seeds: List[IDPoint] = seeds seeds_x = [p.x for p in seeds] seeds_y = [p.y for p in seeds] self.x_min: int = min(seeds_x) - padding self.x_max: int = max(seeds_x) + padding self.y_min: int = min(seeds_y) - padding self.y_max: int = max(seeds_y) + padding self.grid: List[PointType] = [ Point(x, y) for x in range(self.x_min, self.x_max + 1) for y in range(self.y_min, self.y_max + 1) ]
def test_cart_move_intersection() -> None: cart = Cart(Point(5, 5), '>') cart.move('+') assert cart.location == (5, 4) assert cart.direction == NORTH cart.move('+') assert cart.location == (5, 3) assert cart.direction == NORTH cart.move('+') assert cart.location == (6, 3) assert cart.direction == EAST cart.move('+') assert cart.location == (6, 2) assert cart.direction == NORTH
def run(data: str) -> Solution: track_map = {} cart_descriptors = [] for y, line in enumerate(data.split('\n')): for x, char in enumerate(line): location = Point(x, y) if char in CART_SYMBOLS: cart_descriptors.append((location, char)) if char in '^v': track_map[location] = STRAIGHT_NS else: track_map[location] = STRAIGHT_EW else: track_map[location] = char return shared_solution(track_map, cart_descriptors)
def location(self) -> Point: return Point(self.x, self.y)
''' Solution for day 13 of the 2018 Advent of Code calendar. Run it with the command `python -m adventofcode run_solution -y 2018 13` from the project root. ''' from adventofcode.types import Solution, Point from typing import Any, Dict, Iterable, List, Tuple from collections import deque TrackMap = Dict[Point, str] CartDescriptor = Tuple[Point, str] LEFT = -1 STRAIGHT = 0 RIGHT = 1 NORTH = Point(0, -1) EAST = Point(1, 0) SOUTH = Point(0, 1) WEST = Point(-1, 0) STRAIGHT_NS = '|' STRAIGHT_EW = '-' CORNERS = '/\\' INTERSECTION = '+' DIRECTION_ORDER = [WEST, SOUTH, EAST, NORTH] TURN_ORDER = [RIGHT, STRAIGHT, LEFT] CART_SYMBOLS = {'^': NORTH, '>': EAST, 'v': SOUTH, '<': WEST} class Cart:
def test_cart() -> None: cart = Cart(Point(0, 0), '^') assert cart.location == (0, 0) assert cart.direction == NORTH