summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandre Jesus <adbjesus@gmail.com>2024-01-24 16:16:01 +0000
committerAlexandre Jesus <adbjesus@gmail.com>2024-01-24 16:16:01 +0000
commitf6351b48135cb886ba6bfc4e859b5382b9bcc607 (patch)
treee2c8a218ae14d6318b1ba0482c34f17a9c54986d
parent4c79a9473347668ad10fdb243f0d51d71975a807 (diff)
downloadaoc2023-f6351b48135cb886ba6bfc4e859b5382b9bcc607.tar.gz
aoc2023-f6351b48135cb886ba6bfc4e859b5382b9bcc607.zip
day18
-rw-r--r--data/18/example.txt14
-rw-r--r--data/18/input.txt608
-rw-r--r--day18.lisp63
3 files changed, 685 insertions, 0 deletions
diff --git a/data/18/example.txt b/data/18/example.txt
new file mode 100644
index 0000000..fc7612e
--- /dev/null
+++ b/data/18/example.txt
@@ -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)
diff --git a/data/18/input.txt b/data/18/input.txt
new file mode 100644
index 0000000..abb32da
--- /dev/null
+++ b/data/18/input.txt
@@ -0,0 +1,608 @@
+R 7 (#32c140)
+U 2 (#253c23)
+R 5 (#1e70a0)
+U 4 (#584ec3)
+L 5 (#24a120)
+U 4 (#1d03a3)
+L 3 (#7454a0)
+U 2 (#388563)
+L 6 (#4f9170)
+U 4 (#300bf3)
+L 3 (#2f6e10)
+U 3 (#1c2613)
+R 3 (#656dd0)
+U 4 (#281983)
+R 7 (#287620)
+U 3 (#55f513)
+R 4 (#160ca0)
+D 7 (#1fce03)
+R 3 (#110940)
+U 4 (#67bd83)
+R 4 (#342180)
+U 2 (#1c6a21)
+R 3 (#0fd3c0)
+U 6 (#00b231)
+R 5 (#417e02)
+U 2 (#012b21)
+R 4 (#571920)
+D 6 (#2bc391)
+L 5 (#06b610)
+D 4 (#539271)
+R 5 (#5dcf32)
+D 3 (#11a7b1)
+R 4 (#417e00)
+D 5 (#057e31)
+R 5 (#10a4f0)
+D 7 (#07c651)
+R 2 (#0defe0)
+D 6 (#52d041)
+R 4 (#523fc0)
+U 4 (#3641c1)
+R 4 (#572670)
+U 2 (#0ed9c3)
+R 4 (#6f9270)
+U 3 (#481a43)
+R 4 (#18df40)
+U 5 (#321e03)
+R 3 (#19c400)
+U 6 (#240bb1)
+R 3 (#262fc0)
+D 8 (#213861)
+R 2 (#106700)
+D 4 (#6e7721)
+R 6 (#106702)
+D 4 (#1a0ca1)
+R 3 (#306cf0)
+D 4 (#4e72e3)
+R 6 (#2ddd00)
+U 5 (#0246f3)
+R 8 (#34c980)
+U 5 (#0246f1)
+R 5 (#468770)
+U 5 (#3bddb3)
+R 6 (#407df2)
+U 4 (#0d4723)
+R 2 (#232d72)
+U 3 (#0d4721)
+R 5 (#458292)
+U 6 (#460723)
+R 4 (#1099d2)
+U 7 (#021263)
+R 2 (#00ecd2)
+U 4 (#3609a3)
+R 4 (#5c4702)
+D 6 (#3609a1)
+R 2 (#34fdf2)
+D 5 (#48cd93)
+R 3 (#2aca52)
+U 7 (#1f0081)
+R 3 (#6fe682)
+U 4 (#1f0083)
+R 3 (#172802)
+D 4 (#52f873)
+R 4 (#0b1672)
+D 4 (#396f83)
+R 5 (#341452)
+D 5 (#021783)
+R 3 (#5562e2)
+U 6 (#021781)
+R 2 (#02f832)
+U 6 (#2f0413)
+R 3 (#0f87f2)
+U 2 (#3a1313)
+R 4 (#4a2000)
+D 7 (#3bae21)
+R 4 (#2fd400)
+D 7 (#3bae23)
+R 5 (#220350)
+D 3 (#1657f3)
+L 5 (#344c22)
+D 6 (#0a57d3)
+L 5 (#371b92)
+D 4 (#07e0d1)
+L 4 (#44d3c2)
+U 4 (#6a8161)
+L 7 (#374952)
+D 5 (#016691)
+R 2 (#03a9f2)
+D 5 (#1f21b1)
+R 7 (#556402)
+D 7 (#314a01)
+R 4 (#227e70)
+D 4 (#5c5421)
+R 8 (#227e72)
+D 3 (#14e951)
+L 4 (#2ae662)
+D 3 (#191733)
+L 5 (#011812)
+D 4 (#0ff213)
+R 9 (#201fc0)
+D 5 (#5c2c53)
+R 6 (#201fc2)
+D 4 (#175033)
+R 3 (#011810)
+D 3 (#0ed613)
+R 5 (#4ff382)
+D 4 (#17b3e3)
+R 7 (#419812)
+D 3 (#235fd3)
+R 5 (#5bdb22)
+D 4 (#2200e3)
+L 3 (#257c12)
+D 4 (#2d0183)
+L 5 (#44ccf2)
+D 3 (#033a63)
+R 8 (#2231f0)
+D 3 (#1a6073)
+R 2 (#2fd740)
+D 5 (#694813)
+R 8 (#3cd170)
+D 3 (#160c33)
+R 6 (#4f1da0)
+D 3 (#1510d3)
+R 2 (#064c80)
+D 4 (#618e43)
+L 4 (#0fc960)
+D 4 (#00a603)
+L 4 (#653382)
+D 4 (#3d9143)
+R 2 (#2c5fc2)
+D 3 (#82c2a1)
+R 7 (#2df952)
+D 3 (#82c2a3)
+R 2 (#348192)
+D 4 (#1919f3)
+R 6 (#14a7f0)
+U 5 (#215203)
+R 5 (#304700)
+D 4 (#52ed03)
+R 5 (#4461a0)
+D 5 (#104b63)
+L 5 (#31ebc0)
+D 6 (#560291)
+R 8 (#014200)
+U 3 (#5b0f01)
+R 8 (#3f3940)
+U 2 (#0046d1)
+R 3 (#5de660)
+U 3 (#0df221)
+L 3 (#30b5a0)
+U 2 (#44d301)
+L 8 (#1c2c70)
+U 5 (#5c2051)
+R 3 (#4580f0)
+U 3 (#2baca1)
+R 7 (#293080)
+U 3 (#352e11)
+R 8 (#108020)
+U 6 (#596e43)
+R 5 (#347900)
+U 4 (#35b053)
+R 5 (#2f9c80)
+U 4 (#5a9481)
+R 7 (#13e580)
+D 4 (#348a11)
+R 3 (#0d7c90)
+D 4 (#1397c3)
+R 5 (#5b5830)
+D 6 (#236813)
+R 3 (#1fcbb2)
+D 3 (#600003)
+R 6 (#2852a2)
+D 2 (#0e0633)
+R 3 (#4c9292)
+U 3 (#495b13)
+R 5 (#602c52)
+U 5 (#495b11)
+L 5 (#4c54d2)
+U 4 (#53acb3)
+R 4 (#3e3bc0)
+U 4 (#044403)
+R 7 (#363620)
+U 4 (#596a83)
+R 3 (#504600)
+U 7 (#4701f3)
+R 5 (#46a042)
+U 8 (#6f7593)
+R 5 (#09a5c2)
+U 5 (#096273)
+R 5 (#09d900)
+D 8 (#7096f1)
+R 4 (#509250)
+U 4 (#136681)
+R 2 (#2564d0)
+U 10 (#2cbcc1)
+R 3 (#2564d2)
+D 4 (#01cfc1)
+R 3 (#30de40)
+D 10 (#6afe81)
+R 3 (#417690)
+D 4 (#6b2053)
+L 7 (#4be090)
+D 4 (#2b4381)
+L 5 (#3027a0)
+U 4 (#485431)
+L 3 (#2f60d2)
+D 5 (#3f3751)
+L 3 (#2f60d0)
+D 6 (#0a1ac1)
+L 6 (#003d70)
+D 3 (#32b021)
+R 4 (#6dca90)
+D 5 (#507281)
+R 2 (#436f00)
+D 5 (#23c6a1)
+R 3 (#537db0)
+D 4 (#2aec01)
+R 7 (#4ec940)
+U 4 (#0d1c73)
+R 2 (#302510)
+U 3 (#1fac81)
+L 6 (#2bd470)
+U 5 (#1fac83)
+R 6 (#32a8a0)
+U 7 (#0d1c71)
+R 5 (#06d530)
+D 6 (#6bfe71)
+R 4 (#022af2)
+D 10 (#2999c1)
+R 2 (#147bf2)
+D 3 (#37c0e1)
+R 8 (#8349e2)
+D 4 (#37c0e3)
+R 2 (#27ea02)
+D 2 (#2999c3)
+R 9 (#0db3d2)
+D 4 (#43efc1)
+R 3 (#4ae5e2)
+D 3 (#342641)
+R 4 (#571b60)
+D 3 (#035e71)
+R 5 (#064d50)
+U 2 (#126b71)
+R 6 (#056180)
+U 8 (#51b3c1)
+R 5 (#5a9570)
+U 4 (#51b3c3)
+R 8 (#25a5e0)
+U 2 (#15c851)
+R 3 (#039590)
+D 6 (#55a361)
+R 6 (#33d960)
+D 4 (#21b371)
+R 2 (#74bb50)
+D 3 (#33a773)
+L 4 (#18e2e0)
+D 3 (#388313)
+L 8 (#282be0)
+D 2 (#5f3b13)
+L 2 (#5ad2c0)
+D 3 (#2b9d53)
+L 2 (#82fea2)
+D 10 (#035653)
+L 3 (#18e2e2)
+D 2 (#0f36f3)
+L 3 (#3c8d70)
+D 3 (#0e3a33)
+L 3 (#120a50)
+D 7 (#6f3323)
+L 3 (#31f8c0)
+D 5 (#309583)
+R 6 (#098b52)
+U 7 (#2997d3)
+R 7 (#284152)
+U 5 (#0cf161)
+R 5 (#442342)
+D 3 (#51c983)
+R 3 (#3fd5a2)
+D 4 (#51c981)
+R 2 (#071bc2)
+D 5 (#0cf163)
+R 5 (#096492)
+D 3 (#2997d1)
+L 3 (#08aa32)
+D 5 (#617173)
+L 4 (#159f80)
+D 4 (#1ad823)
+L 2 (#189870)
+D 2 (#49cd53)
+L 7 (#542880)
+D 4 (#64a571)
+L 3 (#4c8f90)
+D 3 (#4b9403)
+L 2 (#4776a0)
+D 7 (#115af3)
+L 5 (#31f4d0)
+U 4 (#5402b3)
+L 5 (#5a5820)
+D 7 (#18b4e1)
+L 7 (#204a50)
+U 7 (#618ac1)
+L 3 (#3d4fc0)
+D 4 (#6a84b1)
+L 5 (#1db0c0)
+D 6 (#152da1)
+L 2 (#02b390)
+D 8 (#41e331)
+L 2 (#7c9b10)
+D 7 (#072371)
+L 3 (#5e3210)
+D 3 (#2aec03)
+L 2 (#7f7280)
+D 9 (#3b0d41)
+L 4 (#141b02)
+U 3 (#5d9db1)
+L 3 (#400800)
+D 7 (#132521)
+L 5 (#544ec0)
+U 7 (#25f8c3)
+L 3 (#4f24c0)
+U 2 (#25f8c1)
+L 2 (#10fca0)
+U 3 (#132523)
+R 7 (#1299b0)
+U 2 (#2375b1)
+R 6 (#25bb92)
+U 4 (#2ceb11)
+L 4 (#569262)
+U 2 (#062d81)
+L 4 (#4f0eb2)
+U 9 (#062d83)
+L 5 (#3bb532)
+U 3 (#191f21)
+L 6 (#1754a2)
+D 3 (#44d1f1)
+L 3 (#722452)
+D 4 (#394421)
+R 6 (#27a0a2)
+D 8 (#7f3ae1)
+L 6 (#1a2952)
+D 7 (#402a41)
+L 6 (#1a2950)
+D 6 (#50f3c1)
+L 5 (#5479e2)
+U 4 (#4bd6b3)
+L 4 (#435e92)
+U 2 (#388f83)
+L 3 (#43caa0)
+U 5 (#43b4a3)
+L 7 (#43caa2)
+U 7 (#1c0eb3)
+L 7 (#435e90)
+U 3 (#0033b3)
+L 4 (#35a562)
+U 5 (#2bfbb3)
+L 2 (#4ab672)
+U 8 (#6bac11)
+L 3 (#029fe2)
+U 9 (#3e0f71)
+R 4 (#50a132)
+U 7 (#3698a1)
+R 2 (#15add2)
+U 3 (#7e3841)
+R 10 (#327a12)
+U 3 (#24b1b1)
+R 8 (#2b6592)
+U 3 (#3b80a1)
+R 6 (#106112)
+U 3 (#315b31)
+L 10 (#6510e2)
+D 3 (#1e5881)
+L 3 (#3a2e42)
+U 6 (#1e5883)
+L 4 (#175ed2)
+U 4 (#0bc683)
+L 2 (#443860)
+U 3 (#5b6113)
+L 6 (#443862)
+D 7 (#3fcbf3)
+L 6 (#427de2)
+U 8 (#04e613)
+L 3 (#3a06a2)
+D 4 (#3e7273)
+L 2 (#42fd42)
+D 10 (#2579c3)
+L 3 (#234762)
+D 3 (#7dc451)
+R 4 (#33cf70)
+D 8 (#121911)
+L 4 (#8121b0)
+D 3 (#1a2d31)
+L 6 (#1831c2)
+D 2 (#1ea1b1)
+L 4 (#0674f2)
+D 3 (#1679e1)
+L 10 (#5948b2)
+D 2 (#1679e3)
+L 2 (#2f4d52)
+D 4 (#0469b1)
+L 6 (#0db472)
+D 5 (#5a0e01)
+R 6 (#471a90)
+D 4 (#101dc1)
+R 2 (#5f57e0)
+D 3 (#432761)
+R 4 (#1e6d70)
+D 3 (#576d71)
+R 2 (#306cd0)
+D 2 (#0a9a11)
+R 7 (#41d3c0)
+U 2 (#2d1fd1)
+R 3 (#51e040)
+U 3 (#2d1fd3)
+R 3 (#13c380)
+D 2 (#0a9a13)
+R 5 (#6b9900)
+D 5 (#390be1)
+L 4 (#14f860)
+D 5 (#0e3a31)
+L 6 (#5f2292)
+D 4 (#498ae1)
+L 3 (#1b53a2)
+D 6 (#22b6b1)
+L 7 (#255972)
+D 3 (#7f2ad1)
+L 4 (#1badc0)
+D 4 (#2d96e1)
+L 8 (#083460)
+D 5 (#5aa4f1)
+L 2 (#083462)
+D 4 (#07c271)
+L 2 (#1badc2)
+D 9 (#19fe01)
+L 2 (#27c492)
+D 6 (#40be73)
+L 6 (#06eee2)
+U 2 (#231bd3)
+L 3 (#3b1db2)
+U 10 (#184ae1)
+L 2 (#3cea12)
+U 6 (#4b8f61)
+L 5 (#244e82)
+U 4 (#5576d1)
+R 5 (#2e7552)
+U 6 (#372b41)
+L 5 (#0cabb0)
+D 2 (#34bde1)
+L 3 (#3288b0)
+D 5 (#82d1c3)
+L 2 (#2911e0)
+D 3 (#82d1c1)
+L 6 (#3b28a0)
+D 4 (#34bde3)
+L 3 (#561020)
+D 6 (#568d21)
+L 5 (#5f06b2)
+U 6 (#106891)
+L 6 (#0c22c2)
+U 7 (#4941b1)
+L 3 (#603862)
+U 3 (#181fe1)
+L 6 (#195962)
+U 3 (#461091)
+L 4 (#257782)
+U 5 (#7fed61)
+L 6 (#2cf8d2)
+U 6 (#042361)
+L 4 (#527050)
+U 5 (#4a5051)
+R 6 (#195960)
+U 5 (#4107a1)
+R 2 (#319d92)
+U 7 (#3f0e61)
+L 7 (#17a5c2)
+U 4 (#429851)
+L 5 (#37f9a2)
+U 4 (#436d11)
+L 5 (#25c582)
+U 3 (#436d13)
+L 3 (#3815d2)
+U 5 (#042663)
+R 4 (#00be52)
+U 3 (#5e13a3)
+R 4 (#028f62)
+D 5 (#004c43)
+R 7 (#52e262)
+U 5 (#4826b3)
+R 5 (#04ba12)
+U 3 (#4826b1)
+L 8 (#509a02)
+U 6 (#254c03)
+R 8 (#11bfc2)
+U 4 (#1fba13)
+R 5 (#5e3280)
+U 5 (#56c073)
+R 3 (#08d9c0)
+U 3 (#75e7a3)
+R 4 (#380520)
+U 4 (#651443)
+L 6 (#1d7430)
+U 7 (#15f733)
+L 2 (#4d63a2)
+U 5 (#1a8ba3)
+L 5 (#39b4c2)
+U 4 (#372fc3)
+L 2 (#11a2a2)
+U 4 (#372fc1)
+L 7 (#440a92)
+D 4 (#2e18b3)
+L 4 (#2361b2)
+D 4 (#25f413)
+L 4 (#490582)
+U 4 (#5ea151)
+L 6 (#37b702)
+U 3 (#5ea153)
+L 3 (#1e0e42)
+U 3 (#3e17d3)
+L 6 (#4b6ec0)
+U 4 (#058e11)
+L 9 (#3c5900)
+U 2 (#058e13)
+L 3 (#3a64b0)
+U 6 (#362123)
+L 4 (#7a3390)
+U 5 (#0c9e53)
+L 7 (#06a9f0)
+U 7 (#036323)
+L 5 (#0e8470)
+U 3 (#0ad513)
+L 2 (#3331c2)
+U 7 (#12fed3)
+L 5 (#3c4a52)
+U 6 (#55aa33)
+L 4 (#18e540)
+D 6 (#2984c3)
+L 4 (#18e542)
+U 3 (#49aa53)
+L 3 (#0879e2)
+U 5 (#381dc3)
+L 3 (#3c9e92)
+U 6 (#471eb1)
+L 4 (#643aa2)
+U 6 (#471eb3)
+L 4 (#148512)
+U 9 (#228b33)
+L 2 (#084e12)
+U 5 (#228f33)
+L 3 (#60a742)
+D 7 (#44a213)
+L 2 (#094262)
+D 7 (#04aee3)
+L 4 (#46a752)
+U 5 (#0dd833)
+L 3 (#533952)
+U 9 (#447da3)
+L 2 (#5e0780)
+U 3 (#2d1783)
+L 4 (#275f90)
+U 6 (#35bc53)
+L 3 (#3c9da2)
+U 4 (#22fd93)
+L 4 (#48c972)
+D 5 (#1feb43)
+L 2 (#36bf42)
+D 7 (#405583)
+L 3 (#41af42)
+U 9 (#469c81)
+L 2 (#393e22)
+U 3 (#3cd9f1)
+L 6 (#3bb442)
+D 7 (#025261)
+L 7 (#5b7290)
+U 7 (#5b1311)
+L 4 (#197fd0)
+U 2 (#49b3e1)
+L 4 (#6d2722)
+U 4 (#0dd831)
+L 4 (#4b7072)
+D 3 (#4f78c3)
+L 3 (#125ab2)
+D 6 (#4dd6b3)
+L 4 (#14b212)
+U 2 (#3db733)
+L 2 (#5bb722)
+U 7 (#519d13)
+L 3 (#2baf82)
+D 4 (#5394b3)
+L 4 (#6e6f42)
+U 8 (#316c33)
diff --git a/day18.lisp b/day18.lisp
new file mode 100644
index 0000000..0b35af2
--- /dev/null
+++ b/day18.lisp
@@ -0,0 +1,63 @@
+(defun read-lines (filespec)
+ (with-open-file (stream filespec)
+ (loop for line = (read-line stream nil)
+ while line
+ collect line)))
+
+(defun split-by-char (char string)
+ (loop for i = 0 then (1+ j)
+ as j = (position char string :start i)
+ collect (subseq string i j)
+ while j))
+
+(defun compute-area (moves)
+ (let* ((coords '((0 . 0)))
+ (interior 0)
+ (bound 0 ))
+ (loop for m in moves
+ as dir = (car m)
+ as len = (cdr m)
+ as prv = (car coords)
+ as prvi = (car prv)
+ as prvj = (cdr prv)
+ as curi = (cond ((equal dir #\U) (- prvi len))
+ ((equal dir #\D) (+ prvi len))
+ (t prvi))
+ as curj = (cond ((equal dir #\L) (- prvj len))
+ ((equal dir #\R) (+ prvj len))
+ (t prvj))
+ as cur = (cons curi curj)
+ do (setf coords (cons cur coords)
+ interior (+ interior (- (* prvi curj) (* prvj curi)))
+ bound (+ bound (abs (+ (- curi prvi) (- curj prvj))))))
+ (1+ (ash (+ (abs interior) bound) -1))))
+
+(defun get-move (line)
+ (let ((aux (split-by-char #\Space line)))
+ (cons (aref (car aux) 0) (parse-integer (cadr aux)))))
+
+(defun get-moves (lines)
+ (mapcar #'get-move lines))
+
+(defun solve1 (filespec)
+ (let* ((lines (read-lines filespec))
+ (moves (get-moves lines)))
+ (compute-area moves)))
+
+(defun get-swapped-move (line)
+ (let* ((aux (caddr (split-by-char #\Space line)))
+ (aux (parse-integer aux :start 2 :radix 16 :junk-allowed t)))
+ (cons (aref "RDLU" (mod aux 16)) (ash aux -4))))
+
+(defun get-swapped-moves (lines)
+ (mapcar #'get-swapped-move lines))
+
+(defun solve2 (filespec)
+ (let* ((lines (read-lines filespec))
+ (moves (get-swapped-moves lines)))
+ (compute-area moves)))
+
+(print (solve1 "data/18/example.txt")) ;; 62
+(print (solve1 "data/18/input.txt")) ;; 34329
+(print (solve2 "data/18/example.txt")) ;; 952408144115
+(print (solve2 "data/18/input.txt")) ;; 42617947302920