aboutsummaryrefslogtreecommitdiff
path: root/2023/18
diff options
context:
space:
mode:
authorMarvin Borner2023-12-18 11:57:20 +0100
committerMarvin Borner2023-12-18 15:00:46 +0100
commit0b0b2a6878c569e36d91be4263d1a9ab1947cc54 (patch)
tree4ee2cd69ad60143717b745bcfd4f54afcad70175 /2023/18
parent997f1b75995ebac79982e5fdd8bf9cce2e590285 (diff)
<100ms ha!
Diffstat (limited to '2023/18')
-rw-r--r--2023/18/solve.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/2023/18/solve.py b/2023/18/solve.py
new file mode 100644
index 0000000..04bda99
--- /dev/null
+++ b/2023/18/solve.py
@@ -0,0 +1,44 @@
+L = [
+ (s := l.split(" ")) and (s[0], int(s[1]), s[2][2:-2])
+ for l in open("input").readlines()
+]
+
+
+# rosetta, shoelace, day 10
+def area(loop):
+ x, y = zip(*loop)
+ return abs(sum(i * j for i, j in zip(x, y[1:] + y[:1])) - sum(i * j for i, j in zip(x[1:] + x[:1], y))) // 2
+
+
+def part1():
+ mv = {"R": (0, 1), "D": (1, 0), "L": (0, -1), "U": (-1, 0)}
+
+ c = (0, 0)
+ mp = [c]
+ for d, n, _ in L:
+ nc = mv[d]
+ c = (c[0] + nc[0] * n, c[1] + nc[1] * n)
+ mp.append(c)
+
+ s = sum(l[1] for l in L)
+ print(area(mp) + s // 2 + 1)
+
+
+def part2():
+ mv = [(0, 1), (1, 0), (0, -1), (-1, 0)]
+
+ c = (0, 0)
+ mp = [c]
+ s = 0
+ for _, _, h in L:
+ n = int(h[:-1], 16)
+ nc = mv[int(h[-1])]
+ c = (c[0] + nc[0] * n, c[1] + nc[1] * n)
+ s += n
+ mp.append(c)
+
+ print(area(mp) + s // 2 + 1)
+
+
+part1()
+part2()