day 25 part 1

unfortunately part 2 is closed until all other stars are collected.
oh, sad
This commit is contained in:
efim 2022-12-25 16:09:46 +00:00
parent 3abfbb53ca
commit 5630acd513
4 changed files with 306 additions and 0 deletions

129
day25-input.txt Normal file
View File

@ -0,0 +1,129 @@
1-012=0=2=--12
11=
1=--000111=01-0
201-12=0211--
110-=010-01--02
11021==---22-1
122001
1--0
2-=2
22=02==01=0-2-011
12-2-00-11
20=0-2102=-01
1102=
122--0-112221
11=00-2=201-22=-=
10-02==210--=0
2=220-02=1202
1=--0-
2-122==
10-=00
1=001-22==1
1=121022122-1--0
11-
2=0-1-0-1
1=-0221-==
1-==-0--1012
1--02=-01=020110=
2-0212==1--=2=
112
1=-=1=0012201
1==-
1=02-2=012-=2--=--
1=220-1=0--=1
10-=
1-=22-111=211
11--==21==202
20-
1=-1=02=0=1===0-210
1==-0=010
1=-2=-=2-01-102102=
110-==0=2=-==-2-10
12200=--21-
21-=1-=1-2-
111-==2=2
210=-0-02=-0=11
10-1
1-0=011
20=10=001-
2-0=0-=1121=---2-0
22-1=2=0202
21=2201020211=2
1-110=
21=22=0-=1==121
1==-=01
1-1=1012==1
1-01===1=--21
1==
2-=
200=202121--0122
1-02
1=21=-12-0-
2-=10
121=-20=0200=02==1
101=2-2102-0-02=
1===11
22==0
22-21==-2-1220=10
1==2120--1-=
1=11-2=-110100002200
2211=2=-=-=01-01
1==-010==-=2-=
2=0=2
11-100-21=
11=1=-1=0
2=2--1=2
1-0==1=2-211=1
1-2=-202011211
10=-==-00-1==01
1-=2122==
112=-012
12==-0=
1122-0=0
1=2=0
2===-0=-0-0
1212
202
1==1
2111=1=000221-=-2=-
210111=2=0-1==-
1===00=
22=22=-1-==2-==
102--1=-1=222
2=--=--0-2
11-02=201101=2
1=
12--112-=0=
10====0=220
100020002=-0=02-1-
101
1=1-112-=
2022-02
22201212
21221201010210-1-
1-=1=-121-0-221-10
1=212=01--10-==
12-0=2121=21-2
111-2-00
1=20=202--
2-==2=--2-2101002
111-12=00
1=0===2=
12=-2020=1=2012
2=
1-02---
221---2122212
10=-20002=20-22
2010-220
12
2=0-=221
10011=0
1-20--=1=1-=1
1=1
1=0202-2-1=20-2-
101=--0-=-010-=
1=12=--
2=2111=
1=0-2=2120002=0
10-1=0---10=-20=010
20-121===--=2-=111

96
day25-scratch.lisp Normal file
View File

@ -0,0 +1,96 @@
;; https://adventofcode.com/2022/day/25
(in-package :day-25)
;; so. from decimal to SNAFU and back...
;; or maybe direct sum in snafu?
;;
;; at least 16 digits in my input
(parse-integer (format nil "~a" '1)) ; kek
;; would rather have hashmap then? or array, i guess
;; no? list? alist is list of cons cells, no?
;; from SNAFU should be easier
;; 1=-0-2
(defparameter *test-snafu*
'((5 . 1) (4 . -2) (3 . -1) (2 . 0) (1 . -1) (0 . 2)))
(assoc 4 *test-snafu*)
(alexandria:assoc-value *test-snafu* 4)
(loop
for (pow . mul) in *test-snafu*
sum (* mul (expt 5 pow)))
(exp 2) ; e^2
(expt 5 2) ; 5^2
;; now i want to parse snafu number, right?
;; reverse, start with power 0 yeah, i guess
;; 1-12
(mul-to-char 1)
(mul-to-char -1)
(mul-to-char -2)
(char-to-mul #\=)
(coerce )
(let ((str "1-12"))
(loop
for char across (reverse (coerce str 'array))
for pow from 0
collect (cons pow (char-to-mul char))))
(snafu-to-dec (read-snafu "1-12"))
;; but last thing i want is decimal to snafu.
;; and that's ugh.
;; i could decode into 5 base system, maybe even automatically,
;; then parse into powers of 5 written in 0 - 5
(format t "print in base/radix 5 - ~5R ~5R ~5R ~5R ~5R " 4 5 6 7 8 9)
;; i think this works.
;; now i could parse this into amounts of powers of 5.
;; but then i'd still need to go from 0..5 to -2..2
;;
;; if in pow_k we have >2
;; we can +1 to higher power pow_k+1
;; and to balance -5 in pow_k
(decimal-to-pows-5 1747) ; yeah, maybe good, yes
(- (char-code #\1) (char-code #\0))
(- (char-code #\4) (char-code #\0))
(- (char-code #\6) (char-code #\0))
(- (char-code #\7) (char-code #\0))
(- (char-code #\9) (char-code #\0))
;; and now - modify multipliers of the powers.
;; loop from 0 to (1- length) over all powers.
;;
(defparameter *test-snafu*
'((5 . 1) (4 . -2) (3 . -1) (2 . 0) (1 . -1) (0 . 2)))
(setf (cdr (assoc 4 *test-snafu*)) 6)
(setf (cdr (assoc 6 *test-snafu*)) 6)
(setf (assoc 6 *test-snafu*) '(6 . 2))
(alexandria:assoc-value *test-snafu* 4)
(print (decimal-to-pows-5 1747))
;; ((0 . 2) (1 . 4) (2 . 4) (3 . 3) (4 . 2))
(print (pows-5-to-snafu (decimal-to-pows-5 1747)))
;; ((0 . 2) (1 . -1) (2 . 0) (3 . -1) (4 . -2) (5 . 1))
;; ((0 . 2) (1 . 4) (2 . 4) (3 . 3) (4 . 2))
;; ((0 . -3) (1 . 0) (2 . 5) (3 . 3) (4 . 2))
(coerce (list #\a #\1 #\- #\e) 'string)
(snafu-to-dec (pows-5-to-snafu (decimal-to-pows-5 1747)))
(snafu-pows-print (pows-5-to-snafu (decimal-to-pows-5 1747)))
;; yeah
(loop
for line in (uiop:read-file-lines "day25-test.txt")
for dec = (snafu-to-dec (read-snafu line))
summing dec into the-sum
finally (return (snafu-pows-print (decimal-to-snafu the-sum))))
;; (part-1-calc "day25-test.txt")
;; (print (part-1-calc "day25-input.txt"))

13
day25-test.txt Normal file
View File

@ -0,0 +1,13 @@
1=-0-2
12111
2=0=
21
2=01
111
20012
112
1=-1=
1-12
12
1=
122

68
day25.lisp Normal file
View File

@ -0,0 +1,68 @@
;; https://adventofcode.com/2022/day/25
(defpackage :day-25
(:use :cl))
(in-package :day-25)
(ql:quickload 'alexandria)
(defun snafu-to-dec (snafu-alist)
(loop
for (pow . mul) in snafu-alist
sum (* mul (expt 5 pow))))
;; let's do that as alist as well?
(defparameter *snafu-char-to-mul*
'((#\2 . 2)
(#\1 . 1)
(#\0 . 0)
(#\- . -1)
(#\= . -2)))
(defun char-to-mul (char)
(alexandria:assoc-value *snafu-char-to-mul* char))
(defun mul-to-char (n)
(alexandria:rassoc-value *snafu-char-to-mul* n))
;; into the alist power representation
(defun read-snafu (str)
(loop
for char across (reverse (coerce str 'array))
for pow from 0
collect (cons pow (char-to-mul char))))
(defun decimal-to-pows-5 (num)
(let ((str (format nil "~5R" num)))
(loop
for char across (reverse (coerce str 'array))
for pow from 0
;; do (print char)
collect (cons pow (- (char-code char) (char-code #\0))))))
(defun pows-5-to-snafu (pows-5)
(let ((copied-list (copy-alist pows-5)))
(loop
for pow from 0 below (length pows-5)
when (> (alexandria:assoc-value copied-list pow) 2)
do (progn
(incf (cdr (assoc pow copied-list)) -5)
(when (not (assoc (1+ pow) copied-list))
(push (cons (1+ pow) 0) copied-list))
(incf (cdr (assoc (1+ pow) copied-list)))))
copied-list))
(defun snafu-pows-print (snafu-alist)
(coerce (loop
for pow from (1- (length snafu-alist)) downto 0
collect (mul-to-char (alexandria:assoc-value snafu-alist pow))
)
'string))
(defun decimal-to-snafu (num)
(pows-5-to-snafu (decimal-to-pows-5 num)))
(defun part-1-calc (filename)
(loop
for line in (uiop:read-file-lines filename)
for dec = (snafu-to-dec (read-snafu line))
summing dec into the-sum
finally (return (snafu-pows-print (decimal-to-snafu the-sum)))) )