Advent-of-Code/day25-scratch.lisp

97 lines
2.7 KiB
Common Lisp

;; 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"))