aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-01-24 20:53:38 +0100
committerOscar Najera <hi@oscarnajera.com>2023-01-24 20:53:38 +0100
commitf23650fbae588817c22505f0efe91fed9bd4bc11 (patch)
tree5503cfa61767d82f3eff311913427bf985a393a5
parent10292271a2a46f13849f7e1e2a27c33e2384b2de (diff)
downloadscratch-f23650fbae588817c22505f0efe91fed9bd4bc11.tar.gz
scratch-f23650fbae588817c22505f0efe91fed9bd4bc11.tar.bz2
scratch-f23650fbae588817c22505f0efe91fed9bd4bc11.zip
Complete Advent of code day 25 lisp
-rw-r--r--AoC2022/25/eg-in13
-rw-r--r--AoC2022/25/input122
-rw-r--r--AoC2022/25/solver.lisp37
3 files changed, 172 insertions, 0 deletions
diff --git a/AoC2022/25/eg-in b/AoC2022/25/eg-in
new file mode 100644
index 0000000..027aeec
--- /dev/null
+++ b/AoC2022/25/eg-in
@@ -0,0 +1,13 @@
+1=-0-2
+12111
+2=0=
+21
+2=01
+111
+20012
+112
+1=-1=
+1-12
+12
+1=
+122
diff --git a/AoC2022/25/input b/AoC2022/25/input
new file mode 100644
index 0000000..3904a81
--- /dev/null
+++ b/AoC2022/25/input
@@ -0,0 +1,122 @@
+1-11=21=22-0=22
+12-0-2022-=1
+11101-0---0=-1
+2-
+20
+1-=2=-021=21211-1-01
+1==0122002210-00
+2=2122011=-=020-=0
+2--0
+1=1--=2
+1-0
+22==020-
+1=1=1
+221-2201=--
+2=
+1=21101=--1=1=-
+112=-1
+1---21-0-00==-=00=0
+1-022-0=1
+2=12=20
+1222-000=-01-02
+222=0
+1010=21-01-20
+1=1=2220210=11-210
+1--2=1=0=00--0=
+2102-1-==2-==-0
+211=-==00
+2=--=-1-=10=-2-21-
+10-2=1-1
+1=22-2=-0-1-101
+102-00=0-1=1=022
+11-=--
+1=222
+1==1--2210-=
+22-01-1=-1010120
+2==2-2=0-2--2
+101
+2-22-=20121
+20-0=22
+1-10020
+1-==01
+1-111-010=
+1-
+1022021=10210
+2=102===1-0112
+1--0-120=21-
+101-2-0102-=10==22
+11011=-12=1
+1=22=-11=-002=1
+1=-0=21102=0-=-
+1=1=2--=2
+21=12112
+1111
+2=2=-10201=-12
+10--1=2-=012010=0
+102=-=-1-
+21
+1-0-=-21221=11
+1=1-=-1112=2
+22
+1=
+1-=212-0
+12-=200--1=11
+10-=100=221=1-10=
+2-2-02--0-0-1-0-02
+10-011--20=0
+1--10211--=
+1--1=-=
+1-2=0012==1=2
+22-20=212
+1=100-
+12=001211
+222012
+2-21-=-01---212
+11=002=-0=2022020
+10==1=-=202
+1====0020=2000001=
+1=1--=0=2=2--2
+222=2-1-002
+2--====-00--2=02=0
+1-==21111-11-
+2211=1==0221
+2=0002--
+200-
+1-1=1=-110==0
+1-0-
+22001=-01=1-
+1--221===2-221212
+1=2-
+1=1
+1=2==-0=10=02211=-
+10=--0-212-
+1-0=
+11-=-=11=22=00=
+11---
+110201110-
+2-0=-
+2=12=12----121-0-
+1==120==012220=-
+1=00-0
+102-=-2
+122
+20020
+1=---1-1==0=1=
+22=1110-=02-
+120=20=020=0-=-11-
+1-2-0
+21=2==0-11==---2--0
+112=
+12222=20-000
+1=110
+2222=10-
+1-2
+111211-=
+2=2=1--2220==2=02
+111022=21100100121
+20--==2-21=1
+1000=-1=2202
+21==--==11-1==0-=
+1=0-2=21-1=2==2
+111
+21-0=11-2-220=-=
diff --git a/AoC2022/25/solver.lisp b/AoC2022/25/solver.lisp
new file mode 100644
index 0000000..630cd03
--- /dev/null
+++ b/AoC2022/25/solver.lisp
@@ -0,0 +1,37 @@
+(ql:quickload '(fiveam uiop))
+
+(defun encode-char (c)
+ (ecase c
+ (2 #\2)
+ (1 #\1)
+ (0 #\0)
+ (-1 #\-)
+ (-2 #\=)))
+
+(defun decode-char (c)
+ (ecase c
+ (#\2 2)
+ (#\1 1)
+ (#\0 0)
+ (#\- -1)
+ (#\= -2)))
+
+(defun parse-snafu (num)
+ (loop for a across (reverse num)
+ for pow = 1 then (* pow 5)
+ sum (* pow (decode-char a))))
+
+(defun write-snafu (num &optional coeffs)
+ (if (zerop num)
+ (concatenate 'string coeffs)
+ (multiple-value-bind (div rest) (floor num 5)
+ (if (> rest 2)
+ (write-snafu (1+ div) (cons (encode-char (- rest 5)) coeffs))
+ (write-snafu div (cons (encode-char rest) coeffs))))))
+
+(defun solver (filename)
+ (write-snafu (apply #'+ (mapcar #'parse-snafu (uiop:read-file-lines filename)))))
+
+(fiveam:test solutions
+ (fiveam:is (string= "2=-1=0" (solver "eg-in")))
+ (fiveam:is (string= "2-212-2---=00-1--102" (solver "input"))))