-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ted Cassirer
committed
Jan 9, 2024
1 parent
6c276ec
commit 1e467d2
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from aoc_cas.common import Coordinate, DIR_RIGHT, DIR_DOWN, DIR_UP, DIR_LEFT, Direction | ||
|
||
directions: dict[str, Direction] = { | ||
"U": DIR_UP, | ||
"D": DIR_DOWN, | ||
"R": DIR_RIGHT, | ||
"L": DIR_LEFT, | ||
} | ||
directions_hexa = [ | ||
DIR_RIGHT, | ||
DIR_DOWN, | ||
DIR_LEFT, | ||
DIR_UP, | ||
] | ||
|
||
|
||
def parse_dig_coords(data: str, use_encoded: bool = False) -> tuple[list[Coordinate], int]: | ||
coords: list[Coordinate] = [Coordinate.create(0, 0)] | ||
total_distance = 0 | ||
for line in data.splitlines(): | ||
direction, k, encoded = line.split(" ") | ||
if use_encoded: | ||
distance_str = encoded[2:7] | ||
dir_str = encoded[7] | ||
dir = directions_hexa[int(dir_str, 16)] | ||
distance = int(distance_str, 16) | ||
else: | ||
dir = directions[direction] | ||
distance = int(k) | ||
coords.append((coords[-1].move(dir, distance))) | ||
total_distance += distance - 1 | ||
# for _ in range(int(distance)): | ||
# coords.append(coords[-1].move(dir)) | ||
return coords[:-1], total_distance - 1 | ||
|
||
|
||
def calculate_area(coords: list[Coordinate]) -> int: | ||
shifted = coords[1:] + [coords[0]] | ||
s1 = sum((c1.y + c2.y) * (c1.x - c2.x) for c1, c2 in zip(coords, shifted)) | ||
return s1 // 2 + len(coords) // 2 + 1 | ||
|
||
|
||
def part_a(data: str) -> int: | ||
dig_coords, length = parse_dig_coords(data) | ||
area = calculate_area(dig_coords) | ||
return area + length // 2 + 1 | ||
|
||
|
||
def part_b(data: str) -> int: | ||
dig_coords, length = parse_dig_coords(data, use_encoded=True) | ||
area = calculate_area(dig_coords) | ||
return area + length // 2 + 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
from aoc_cas.util import solve_with_examples | ||
|
||
solve_with_examples(year=2023, day=18) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
R 6 (#70c710) | ||
D 5 (#0dc571) | ||
L 2 (#5713f0) | ||
D 2 (#d2c081) | ||
R 2 (#59c680) | ||
D 2 (#411b91) | ||
L 5 (#8ceee2) | ||
U 2 (#caa173) | ||
L 1 (#1b58a2) | ||
U 2 (#caa171) | ||
R 2 (#7807d2) | ||
U 3 (#a77fa3) | ||
L 2 (#015232) | ||
U 2 (#7a21e3) | ||
62 | ||
952408144115 |