-
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
Showing
7 changed files
with
115 additions
and
5 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,65 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
|
||
from utils import io | ||
from utils.grid import BOTTOM, LEFT, RIGHT, TOP | ||
from utils.math import poly_area | ||
|
||
instructions = { | ||
"R": RIGHT, | ||
"L": LEFT, | ||
"U": TOP, | ||
"D": BOTTOM, | ||
} | ||
|
||
|
||
def to_instr(i): | ||
if i == 0: | ||
return "R" | ||
elif i == 1: | ||
return "D" | ||
elif i == 2: | ||
return "L" | ||
elif i == 3: | ||
return "U" | ||
else: | ||
raise Exception("Unknown instruction", i) | ||
|
||
|
||
def get_outline(lines, part2=False): | ||
pos = (0, 0) | ||
corners = [pos] | ||
outline = 0 | ||
for line in lines: | ||
if not part2: | ||
instr = line.split("()")[0].strip().split(" ") | ||
dir = instr[0] | ||
length = int(instr[1]) | ||
else: | ||
instr = line.split("#")[1].strip()[:-1] | ||
length = int(instr[:-1], 16) | ||
dir = to_instr(int(instr[-1])) | ||
|
||
pos = (pos[0] + instructions[dir][0] * length, pos[1] + instructions[dir][1] * length) | ||
outline += length | ||
corners.append(pos) | ||
|
||
return corners, outline | ||
|
||
|
||
def solve(filename, part2=False): | ||
corners, outline = get_outline(io.get_lines(filename), part2) | ||
return int(poly_area(corners)) + outline // 2 + 1 | ||
|
||
|
||
def main(): | ||
assert solve("example.txt") == 62 | ||
print(solve("../input/2023/day18.txt")) | ||
|
||
assert solve("example.txt", True) == 952408144115 | ||
print(solve("../input/2023/day18.txt", True)) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
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,14 @@ | ||
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) |
Submodule input
updated
from 400c9f to 9759ad
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
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
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
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