Compare commits

...

7 Commits

Author SHA1 Message Date
efim
61abf66381 giving up for rest of AoC 2022 2022-12-31 15:33:45 +00:00
efim
f2c18830c2 yay, day 22. part 1 2022-12-31 14:41:30 +00:00
efim
5630acd513 day 25 part 1
unfortunately part 2 is closed until all other stars are collected.
oh, sad
2022-12-26 07:54:44 +00:00
efim
3abfbb53ca day 21, part 2 backward computation. that's hot 2022-12-25 14:54:34 +00:00
efim
f6799dd691 lets skip day 21 part 2! 2022-12-25 10:58:24 +00:00
efim
c2acd2f75a day 21 part 1, fun 2022-12-25 10:43:57 +00:00
efim
e062633074 day 20 part 2, yay 2022-12-25 09:56:18 +00:00
15 changed files with 4150 additions and 26 deletions

View File

@@ -187,7 +187,11 @@
;; and now it works ;; and now it works
;; god i also need to take 1000th value with overbound. ugh. ;; god i also need to take 1000th value with overbound. ugh.
(defun get-ugh-nth (arr n)
(let* ((zero-ind (position 0 arr))
(unsafe-index (+ zero-ind n))
(safe-n (mod unsafe-index (length arr))))
(aref arr safe-n)))
(get-ugh-nth (mixing-array *test-array*) 1000) (get-ugh-nth (mixing-array *test-array*) 1000)
(get-ugh-nth (mixing-array *test-array*) 2000) (get-ugh-nth (mixing-array *test-array*) 2000)
@@ -203,10 +207,16 @@
(get-ugh-nth (mixing-array mixed) 3000))) (get-ugh-nth (mixing-array mixed) 3000)))
;; 1797 is too low, ;; 1797 is too low,
(defun part-1-ans (filename)
(let* ((nums (mapcar #'parse-integer (uiop:read-file-lines filename)))
(input-arr (make-array (length nums) :initial-contents nums))
(mixed (mixing-array input-arr)))
(+ (get-ugh-nth mixed 1000)
(get-ugh-nth mixed 2000)
(get-ugh-nth mixed 3000))))
;; (part-1-ans "day20-test.txt")
(part-1-ans "day20-test.txt") ;; (print (part-1-ans "day20-input.txt"))
(part-1-ans "day20-input.txt")
;; well, do we have duplicates? ;; well, do we have duplicates?
@@ -230,28 +240,25 @@
;; i'd need to store elements with their initial indexes i suppose ;; i'd need to store elements with their initial indexes i suppose
;; and then what? iterate not over initial collection, but just over "initial" indexes ;; and then what? iterate not over initial collection, but just over "initial" indexes
;; so, how'd i do that? ;; yay, at least previous solution was still incorrect. yayyayay
;; #'move-item works with index and move-by
;; so shouldn't depent on type of elements
;; so just #'move-elem-by-itself should take in "original index"
;; then find position in the array by the index.
;; and array would be (index value)
(zip-with-index '(2 14 1 3 5)) ;;; PART 2.
;; wowy.
;; 1. multiply all numbers by 811589153
;; 2. do 10 mixings
(map 'vector #'1+ (aops:linspace 0 9 10))
(input-arr "day20-test.txt") (defun part-2-ans (filename)
(let* ((data (map 'vector (lambda (zipped) (list (first zipped) (* 811589153 (second zipped))))
(input-arr filename)))
;; (mixed (mixing-array input-arr))
(mixed (do* ((arr data (mixing-array arr))
(iteration 0 (1+ iteration)))
((= 10 iteration) arr))))
(format t "getting part 1, mixed array: ~a~%" mixed)
(+ (get-ugh-nth mixed 1000)
(get-ugh-nth mixed 2000)
(get-ugh-nth mixed 3000))))
(mixing-array (input-arr "day20-test.txt")) (print (part-2-ans "day20-test.txt"))
;; and it works, now. (print (part-2-ans "day20-input.txt"))
;;
;; next - extract the values from the 1000th etc
;; wait what. why did i do the mixing again. ugh
;; was that the problem, not the duplicates? will revert after getting to the answer,
;; but yikes
;; oh, i need to find 0 by value in new array
(part-1-ans "day20-test.txt")
(part-1-ans "day20-input.txt")
;; and i get a gold star.
;; let's commit and try with revert?

2979
day21-input.txt Normal file

File diff suppressed because it is too large Load Diff

135
day21-scratch.lisp Normal file
View File

@@ -0,0 +1,135 @@
;; // https://adventofcode.com/2022/day/21
;; well, this seems like this could be done with (eval)
;; would it be possible for me to also translate
;; xxx + yyy into (call xxx) + (call yyy)
;; that would be neat
(in-package :day-21)
;; so. have mashmap from symbol name into operation
;; and have (my-eval)?
;; it will take symbol name, and either return number directly from the map
;; or will wrap the names into my-eval and run operation on them?
;; is there a way to have less code and reuse existing lisp things?
;; if there's direct number, i could just run (defparameter lfqf 4)
;; and then execute root directly, i think
;; or what, i'd need to also define all the intermediate symbols,
;; (setq a (+ c d)) ; complains that C is unbound
;; how could this be a lazy eval?
;; well. hm
;; i could define all of these as macroses?
(defmacro *test-monkey-hmdt* () 32)
(+ 3 (*test-monkey-hmdt*))
;; yup, i guess
(defparameter *test-monkey-sllz* 4)
(eval *test-monkey-sllz*)
(defparameter *test-monkey-ljgn* 2)
(defparameter *my-operation* '(+ *test-monkey-sllz* *test-monkey-ljgn*))
*my-operation*
(eval *my-operation*) ; well, that works ok
;; so, read all of these things as parameters.
;; "root: pppw + sjmn "
(mapcar #'intern (ppcre:split " " "root: pppw + sjmn "))
(mapcar #'parse-integer-or-symbol (ppcre:split " " "root: 44"))
(ppcre:regex-replace ":" "root: pppw + sjmn " "")
(line-to-quoted-operation "root: pppw + sjmn")
(eval (line-to-quoted-operation "dbpl: 5"))
;; well, i'd want to put quoted expression into parameter?
;; and now i can remove quoting of the defparameter?
;; and actually define all of the parameters?
(loop
for line in (uiop:read-file-lines "day21-test.txt")
for definition = (line-to-quoted-operation line)
do (eval definition))
(eval root)
;; (load-file-defs "day21-test.txt")
;; (eval root)
;; (load-file-defs "day21-input.txt")
;; (print (eval root))
;; now. things are different.
;; HUMN is my input
;; and ROOT is "comparison of two numbers"
;; and change in root can influence results in a significant way
;; so, how'd i do that?
;;
;; what could be done then?
;; i could potentially somehow modify the operations?
;; so that (eval humn) would return
;;
;; root: pppw + sjmn
;; so. root should mean
;; pppw == sjmn
;; don't know which has HUMN yet.
;; should i then just eval those that have HUMN into a operation?
;; and others into a number?
;; well, i suppose i'd need to do pass down somehow
;; eval both parts of the root.
;; and then what?
;;
;; let's skip for now?
;; or, alternatively. i could save HUMN num disregard
;; defparameter with HUMN to be used as HUMN =
;; and then what? root
;; but then i'd need to reverse all the other operations, hm
;; or, maybe i could join into flat expression?
;; and then what? i macroexpand?
;;
;; now. i had an idea. i could have additional symbols,
;; also pointing to quoted computations
;; but these would be `back-ptdq`
;; for line 'lgvd: ljgn * ptdq'
;; make two? for each
;;
;; ptdq: humn - dvpt
;; back-humn = ptdq + dvpt
;; but one of these wouldn't be possible to forward eval, so it would be back-ptdq + dvpt
;; and on
;; root: pppw + sjmn
;; two would be created back-pppw = - eval sjmn and vice versa
;; this should work?
;; sure, let's try?
;; so we'd do what?
;; same for forward eval?
;; and two for back-monkey
(back-symbol 'hello)
(intern (format nil "BACK-~a" 'yo))
;; and i need reverse of the operands?
(reverse-operation '+)
(line-to-quoted-operation-2 "dbpl: 5")
(line-to-quoted-operation-2 "pppw: cczh / lfqf")
(line-to-quoted-operation-2 "lgvd: ljgn * ptdq")
(line-to-quoted-operation-2 "drzm: hmdt - zczc")
(line-to-quoted-operation-2 "cczh: sllz + lgvd")
(line-to-quoted-operation-2 "root: pppw + sjmn")
;; i think it's done? let's try to eval all of this?
;; but i also need root back-computation.
;; for each operant that they are equal to forward calculation of another
;; (load-file-defs-2 "day21-test.txt")
;; (eval root)
;; (eval back-humn) ; 301. cool
;; (load-file-defs-2 "day21-input.txt")
;; (print (eval back-humn))
;; 3715799488132

15
day21-test.txt Normal file
View File

@@ -0,0 +1,15 @@
root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32

98
day21.lisp Normal file
View File

@@ -0,0 +1,98 @@
;; // https://adventofcode.com/2022/day/21
(defpackage :day-21
(:use :cl))
(in-package :day-21)
(ql:quickload 'cl-ppcre)
(defun parse-integer-or-symbol (str)
(let ((maybe-int (parse-integer str :junk-allowed t)))
(if maybe-int
maybe-int
(intern (string-upcase str)))))
(defun line-to-quoted-operation (line)
(let* ((words (ppcre:split " " (ppcre:regex-replace ":" line "")))
(symbols (mapcar #'parse-integer-or-symbol words)))
(cond
((= 4 (length symbols))
;; with operation
(destructuring-bind (name operand1 op operand2)
symbols
`(defparameter ,name '(,op (eval ,operand1) (eval ,operand2)))))
((= 2 (length symbols))
;; just number
(destructuring-bind (name value)
symbols
`(defparameter ,name ,value))))))
(defun load-file-defs (filename)
(loop
for line in (uiop:read-file-lines filename)
for definition = (line-to-quoted-operation line)
do (eval definition)))
(defun reverse-operation (op)
(case op
('* '/)
('/ '*)
('+ '-)
('- '+)))
(defun back-symbol (symb)
(intern (format nil "BACK-~a" symb)))
;; adding BACK-symbol computations.
(defun line-to-quoted-operation-2 (line)
(let* ((words (ppcre:split " " (ppcre:regex-replace ":" line "")))
(symbols (mapcar #'parse-integer-or-symbol words)))
(cond
((= 4 (length symbols))
;; with operation
(destructuring-bind (name operand1 op operand2)
symbols
(if (eq name 'ROOT)
`(progn (defparameter ,(back-symbol operand1) '(eval ,operand2))
(defparameter ,(back-symbol operand2) '(eval ,operand1)))
(let ((forward-calc
`(defparameter ,name '(,op (eval ,operand1) (eval ,operand2))))
(backward-calc
(case op
('+ `((defparameter
,(back-symbol operand1)
'(- (eval ,(back-symbol name)) (eval ,operand2)))
(defparameter
,(back-symbol operand2)
'(- (eval ,(back-symbol name)) (eval ,operand1)))))
('- `((defparameter
,(back-symbol operand1)
'(+ (eval ,(back-symbol name)) (eval ,operand2)))
(defparameter
,(back-symbol operand2)
'(- (eval ,operand1) (eval ,(back-symbol name))))))
(t `((defparameter
,(back-symbol operand1)
'(,(reverse-operation op) (eval ,(back-symbol name)) (eval ,operand2)))
(defparameter
,(back-symbol operand2)
'(,(reverse-operation op) (eval ,(back-symbol name)) (eval ,operand1)))))
)))
`(progn
,forward-calc
,@backward-calc
;; (defparameter ,(innern (format t "BACK~a" name) ))
)))))
((= 2 (length symbols))
;; just number
(destructuring-bind (name value)
symbols
`(defparameter ,name ,value))))))
(defun load-file-defs-2 (filename)
(loop
for line in (uiop:read-file-lines filename)
for definitions = (line-to-quoted-operation-2 line)
do (eval definitions)))

200
day22-map.txt Normal file
View File

@@ -0,0 +1,200 @@
......#....#.#........................................#.......##...........#.............#......#...
..............#..#........#......##........................#......#....#........................#...
.....................#.........#...#.#........#..............................#....#........#..#.....
.........#.........##................................#.....#.........#...........#...........#......
..........#.............#.....##.......#..#..........##......#.#...........#............##..........
......#.............##.........#.....#....#...............................................#.......#.
..#......#......#.........#..#.........#....#...................#...........#...#.....##.#..#...#..#
#.#.............#.............#.#......#.....#.....#.##.##.....#...#..#...............#.............
.#....#...##....#...................#..#............................................#.........#.....
....#..##....#..#....#..............#..#...#...#................#...........#..#..#............##...
..#.#....................................................................#.....##....##.....#..#....
.......#...#......#...##........#...#......................#.#.#..............#...#..#.....#........
.....#.#..#........#..............##......#........##...............#..................#.....#......
.......#...............#...#...................................#............#.......................
......#..##.....#.......#....#..................................................#.#.......#.........
................#..#..#....#.#.....................#................................................
.................#.#........#.....##...#...#.#..#....#....#.............#...#..................#..#.
.#...................#......#..#...............#.............#.........................#...#.....#..
...#.#...................#...#.........................#.......##.......................#.........#.
.#.............................#........##.........#.#.....##.....................#....#...........#
..........#.....#..............................#................##.....................#.......#....
.....................................#....##..........#...............#.......##.........#.#........
....#.###............#....#..........#..........#.......................#...............#....#.#....
.#...#...#...............#.........#........................#.................#..................#.#
.................#......#.....................#........#......#.....................#....#.##.......
##..#....................#.....................#....#....#.....#.............................#.....#
#...#.......................................................##.#...#.........#..#...................
............#......#.....#........#..#.#......#.#.............#....#......#.#...............#.......
..................................................#.#..........##.........##......#.#.#.........##..
..#..........................#.......#..#..#.......#.................###.......#...........#..#...##
..#.....#..#...#.....#.....#...#..#.................................#.....#.........#.............#.
............#.#..#..##..#.#.##....#...............#.......#........#............#........#..........
.#...........#...........................#............#.....#.#.........#.....#.......###...#.......
...#.......................#.........................#...##........#.....................#.......#..
...........#..#.............................#.................#..##...#.............................
..................#.#...#..#....#....#..........#................#..#.........#......#.......#......
...#...#....#.#................#.............#.............................#..#.....................
....................#.........#..#.#...#.#.##...#...#..............#.......#.........#.............#
...#.....#.............##.....#....#.#.......#......................................................
..........#..#...#.......................#.................##.....#........#...#......#.......#.....
........#......#...............#.#.....#...........................#.....................#.......#..
..................................#...........#......#...........#.............#....###.............
................#.......................#.#......#.##..........#......#............#................
.........................................#..........................................##...........#..
...#.......#....##.#....#...............#..#...#.............#...#....#.....#.#...........#.........
#........#...........#...........#.....................#........#..................#.....#......#...
........#.....##..#.........................#....#..........#.##..#.......#.#.................#...#.
..........................#.............................#........#............#.............#....#..
.............................#..................................................#...................
................#.............#..#..........##......#......#.........#...........##............##...
#...#.......#.............#............#..........
#................#...#....#..................#....
.................#.....#.#....#......#...#........
..................................#...............
.....#...............................#............
...............#............##..........#.......#.
#............#.....................###.#.........#
.#...............#...........#....#......#........
.#.............###.#................#........#....
....##............#..........#.#.........#.#.#....
..................#..#..............#......#......
....#.............................#...............
.#.#..........#...........#......##...............
..........#......#....#...#.#....#..#.............
................#...............#.................
..........#..#...#..............#...............#.
...#..............##..............#...............
..#...#.#....#.........#.........#.......#........
.................#..............#.........#.....#.
.......#........#......#............#......#......
..........#..............#....#..#............#...
.......#............#.............#...............
.....#.#..........#.#..........#..................
#...#...................#......#......#.....#.....
........#........##.#.......##................#...
..................#.........................##....
..#................#..............#.#....#.#.#...#
........#..........................#..........#...
..#.......#.#............#..........#.....#.......
....#....#...............................#........
..#............................................#..
....#.......#..............##.......#......##.....
.#..........#......#....#.#........#..............
................#.#.......##.............###.....#
...............#......##.#...#................#...
....#.......#....##...#..#..........#.........#...
.....#.........#...#...........#........#.........
.......#...............#..................#.......
...........#.....#.........#........#....#.......#
..#.....................................#...#.....
..................#..............#....#.#.........
........................#.......#.#......#........
...#......#..#..........#..#..#........#..........
.....#.......#.........#.............#.......#....
......#......#...............#.....#.........#....
.#.........................#.....#.............#..
...#......#.#.....##...#.#....#.#...............#.
.............#........#.#.#.....#.................
...#.........#.........#....#.................##..
.......#.......##...#....#..........#.#...........
..#......#....................#.........#........#.....................#............#.....#.........
.##...........##...................#.#..................................#...........................
....#............#..........#..........................#...#......................#....##...........
.#...........#........................#..............................#..#...................#......#
#................#.#........#...........................#....................#...........#..........
#.....................#.....#.......##....#......#..##...#...........#...................#..........
.........#..#......#....#......#....#.......#.....................................................##
..#...#......##.#..................#.......#.............#....#....#.........#..............#.....#.
...........#...#...#.#........#..................##...........#.....#...............................
.............................#....##..#..#..........#.............................#.................
...#.................#...#...#...#.................................................#..............##
......#........#........#.........##....#....#.....#.......#.............#...##.##.#................
..#.......................#....................#..#...#.................#......#..........#......#..
.........................#....#..##..............#.............#.#...#....#.......#...#....#.....#..
....#................#.#...........................#....#..#.......##.#.....#................#.#....
..#.......................#.....#...#..#..#...#...#.............................#............#......
#..............................................#.......................................##........#.#
..............#.#............##..........#.........................#..................#.#...........
.....#......................#...........#.....#.................#................#........#...#..#.#
......#.#....#......#.#..................#.#................#.#.#...........#............#.....#....
.#......#......##..............#.........#.#.#.#..................#...#..........#..................
...#...........#.............#....#.#...#....#.......................#.#.....#.....#..............#.
...#...#.#.........#...#............#....#.........#.....#......#..#...........#.....#......#.......
.#.....#..........#..............#..#........#..............#...#.............#.#.......#..#........
.....#........................#.....#......#...............##..#.#...#...#.#............#...........
.....#......#.........#....#.......##.......#.##..................#...............#......#........#.
.#..#..#..#.................................#......#.#............#..#....#...................#.....
.#..#...........................#....#............................................................#.
......#..............................#..#.......#.........##..........##...#........................
............#...............#..#..#..........#......................#...#.#............#............
...##..................#.....#.........#......#...........#.......#.....#.......................#...
..#..#..#......#......#.....#.......#.................##..#.........................................
............#...##...#..........#.................................##....#........##........#........
...##...#....#....#........................#.............##.........................##.......#......
.............##..................#...................##.......#........##.............#.#..........#
.......#.....#...##..............#..........................#..........#..........#..#.............#
............#...#........#........#...............#.....#.....#.......#..............#.....#......#.
...........#.....#..##.................#......#........#.#.....................#....................
.....#......#.........#..........#..#......................................................##.......
..........#.....#......#.#.........#.....#..................##.............................#........
##......#...#..........#...............#.............#........#..................#........#........#
..#.........#...........#.......#...........##...................#.................#.#.....#...#....
...#.........#..................................#.....#..#............#.............#.#.............
....#...........#..........#..............#..###........#..#.........#....................#.#.......
..#..........#..#...............................#...#.................#............#............#.#.
.....................#.............#....#..#....#......................#..#......#..................
........#.#.............#....#...#.#.....#..........#................#.......#........#....#.#.....#
##.........................##....#...............#..........#......................##...............
.........#..................................#........................#....#............#...#.#......
........#........................#..#...#..........................#................................
................#..#.......#..#...................
.............#........#...................#.......
..#....#.......................#..........#...#..#
..........#.....................#..............#.#
.........#...#...#..............#..#.........#....
...........#.....#................................
.......#..#..#.......#............#...............
.........................#..........#..#....#..#..
#...........#....#.....#....#.#..#.......#........
#.#.......#.....#........#....#...#...............
..............................................#...
#......#....................................#...#.
........##..#.....#.#......#.........#..##....#...
...............#.........#...#.......#......#..#..
..........#.#......#.....#........................
.....#.....#......................................
..#..#............#.......#.....#..#.......#...#..
...............##....#........#..#.............#.#
..............#..................#.....#......#...
#.......#.......#...#...............#.#....#......
#.........#........#..........#........#....#.....
.....###.#...........#...........##............#..
.........#..##.......#......................#.....
.....#.##......#.......#...#.............#........
##.................#.....#.............#....#...#.
........#..............#.#.........#.......#......
........#..#.#.....#.............#............#..#
.#.....#.#..#..#.##........#..........#.....##....
.....#....#.........#.............................
................#..#....#.....#....#........#..#..
#.#............#.....#.............#.........#....
...#....#..........#.#.................#.#..#...#.
........#................#...#......#......#...#.#
..#.............#....#.......#..............#.....
.....................................##.........#.
............#.#...................................
..#...........................#..........#........
...#.............#..............#..........#...#..
#......#...#...#...#...#..................#....#..
....#......#..#....#.#.#..........................
..#.......................#..............#..#.....
................#........#...#..........#.#.......
....#...#......#...................#.#.#...#....#.
............#..............#........#..#.....#....
..........#...............#.......#...............
#....................#.......#...........#........
.......#.........#..........#.#..#.#..............
..#.........##......................#...#.........
...#.................#.##............#........#...
....#......#..#.....................#.............

1
day22-path.txt Normal file

File diff suppressed because one or more lines are too long

229
day22-scratch.lisp Normal file
View File

@@ -0,0 +1,229 @@
;; https://adventofcode.com/2022/day/22
(in-package :day-22)
;; monkey map. so, i'd want to store the map.
;; and have functions that return "neighbors" maybe as alist?
;; neighbor is the right place to calculate wrapping around empty space
;; on top of neighbors get walkable directions
;; how to best represent coord?
;; next - moving
;; looking at the map of input. maybe somehow precompute where to jump on the map?
;; nah.
;; so, let's get map. with chars space, dot, hash
;; have line of spaces at the start, end, left and right?
;; that would shift coords by +1
;; and i'd also like what? i'd also like to store the path?
;; when exiting save that last orientation at the point?
;; and only treat space and hast as special, dot and arrows as walkable
;; or i could print "every snapshot" with setting and removing the walkout
;; i could split the input into files, that would simplify things
;; let's read the map into array?
(setq *ugh*
(let* ((lines (uiop:read-file-lines "day22-test-map.txt"))
(rows (length lines))
(cols (apply #'max (mapcar #'length lines)))
(arr (make-array (list (+ 2 rows) (+ 2 cols)) ; adding for padding row of empty space
:initial-element #\ )))
(loop for row from 0 below rows
for line = (coerce (nth row lines) 'array)
do (loop for col from 0 below cols
for char = (if (array-in-bounds-p line col) (aref line col) #\ )
do (setf (aref arr (1+ row) (1+ col)) char)))
arr))
(setq *test-arr-out-bounds* (make-array '(2 2) :initial-element #\. :adjustable t))
(print-map *ugh*)
;; yep, this is what i want
(print-map (read-map-to-array "day22-map.txt"))
(setq *ugh* (read-map-to-array "day22-test-map.txt"))
;; seems to work.
;; what are next steps?
;;
;; for some coords get neighboring to four sides
;; this should just return coords even with wrapping
;; let's first do function that returns coord or wrapped coord if value is space?
(alexandria:assoc-value *movements* 'left)
;; so from coord as list to updated coord
(move-coord-one-step '(1 1) 'right)
;; next should check the value of the moved. and if it's space -
;; calculate wrap
;; would also take the map array
;; if we're out of bounds, just skip this movement. shouldn't happen in my data
(opposite-movement 'left)
(opposite-movement 'right)
(opposite-movement 'down)
(opposite-movement 'up)
(apply #'aref *ugh* '(1 12))
;; now i need to check that. hm.
*ugh*
(print-map *ugh*)
'(4 1) ; begining of part
(move-with-possible-wrap '(5 1) 'left *ugh*)
(aref *ugh* 5 0)
(let ((coord '(5 1))
(map *ugh*)
(direction 'left))
(do
((mov-coord coord
(move-coord-one-step mov-coord (opposite-movement direction))))
((equal #\ (apply #'aref map mov-coord)) (move-coord-one-step mov-coord direction))))
;; now? um
(move-with-possible-wrap '(5 2) 'up *ugh*) ; now that seems ok
;; next is from 'move-with-possible-wrap
;; i guess i'd alreay want to display?
;; set X on that coord?
(display-coord '(5 2) *ugh*)
(display-coord (move-with-possible-wrap '(5 2) 'up *ugh*) *ugh*)
;; yeah, kind of ok.
;; what next? simulate movement?
(move 4 'right '(1 1) *ugh*)
(display-coord '(6 1) *ugh*)
(display-coord (move 4 'right '(6 1) *ugh*) *ugh*)
(display-coord '(6 8) *ugh*)
(display-coord (move-with-possible-wrap '(6 8) 'left *ugh*) *ugh*)
(display-coord (move 1 'left '(6 8) *ugh*) *ugh*)
(display-coord (move 2 'left '(6 8) *ugh*) *ugh*)
(display-coord (move 3 'left '(6 8) *ugh*) *ugh*)
(display-coord '(6 8) *ugh*)
(display-coord (move-with-possible-wrap '(6 8) 'right *ugh*) *ugh*)
(display-coord (move 1 'right '(6 8) *ugh*) *ugh*)
(display-coord (move 2 'right '(6 8) *ugh*) *ugh*)
(display-coord (move 3 'right '(6 8) *ugh*) *ugh*)
(display-coord '(6 2) *ugh*)
(display-coord (move-with-possible-wrap '(6 2) 'left *ugh*) *ugh*)
(display-coord (move 1 'left '(6 2) *ugh*) *ugh*)
(display-coord (move 2 'left '(6 2) *ugh*) *ugh*)
(display-coord (move 3 'left '(6 2) *ugh*) *ugh*)
(display-coord (move 4 'left '(6 2) *ugh*) *ugh*)
(display-coord (move 5 'left '(6 2) *ugh*) *ugh*)
(display-coord (move 6 'left '(6 2) *ugh*) *ugh*)
;; ok, i guess
;;
;; and now code the walk?
(defparameter *test-path* "10R5L5R10L4R5L5")
(ppcre:split "(L|R|U|D])" *test-path* :with-registers-p t )
;; somewhat of what i want, but also lrud into words
(mapcar #'parse-integer-or-symbol
(ppcre:split "(L|R)" *test-path* :with-registers-p t ))
;; initial number is "forward" from initial direction
;; oh, so the path is with turns Right turn or Left turn.
;; with initial Right
;; so, now i'd want a fuction that transformes direction
;; i guess i could what? make cyclic list? not quite i guess
(alexandria:circular-list 'up 'right 'down 'left)
(position 'up (alexandria:circular-list 'up 'right 'down 'left))
(nth (mod -1 4) (alexandria:circular-list 'up 'right 'down 'left))
;; yeah i guess
(new-direction 'UP 'L)
(new-direction 'LEFT 'L)
(new-direction 'down 'L)
(new-direction 'right 'L)
(new-direction 'UP 'R)
(new-direction 'LEFT 'R)
(new-direction 'down 'R)
(new-direction 'right 'R)
;; yay. that's kind of ok
;; now. ugh
;; yup, let's add that in code...
(append (read-path "day22-test-path.txt") (list 'L))
(read-path "day22-path.txt")
;; the path is "go N to your direction"
;; then L or R to change direction
;; so, now the main loop?
;; i guess i could add one more R or L to the end
;; and treat path as (n turn-direction)
;; oh, but the final direction is part of the answer
;; OK, LET'S add L to the end
(let ((padded-path (append (read-path "day22-test-path.txt") (list 'L)))
(direction 'right)
(coords '(1 1)) ; to be calculated
(map *ugh*)
)
(loop
for (n turn-direction) on padded-path by #'cddr
do (progn
(setq coords (move n direction coords map))
(setq direction (new-direction direction turn-direction)))
finally (return (list coords direction))))
;; hoho. the task requires indices starting from 1, cool
;; and i did 1 too many left turns, so let's turn right
;; UP to right, is RIGHT
;; and 0 for right, cool
;; yay! now let's clean it up.
;; one more thing. determining top leftmost point.
(get-topmost-left-coords *ugh*)
(print-map *ugh*)
(apply #'calc-password (walk-path "day22-test-path.txt" "day22-test-map.txt"))
(apply #'calc-password (walk-path "day22-path.txt" "day22-map.txt"))
;; 11464
;; and one gold star.
;;; and PART 2.
;; then neighbors is different
;; especially the wrap thing.
;; how'd i calc that? for the map? ugh
;; so, it's 6 parts. huh. ugh
;; oh, they're oblong, but they are all 4 by 4
;; XX
;; X
;; XX
;; X
;; outline of my input
;; how do i get freaking folding?
;; ugh.
;; maybe go for the next? um.
;; yeah. i'm giving up for now. let's go to rest for the new years. 19:33 of Dec 31st
;; fare well Common Lisp, I'll return to use in Q2 or Q3

12
day22-test-map.txt Normal file
View File

@@ -0,0 +1,12 @@
...#
.#..
#...
....
...#.......#
........#...
..#....#....
..........#.
...#....
.....#..
.#......
......#.

1
day22-test-path.txt Normal file
View File

@@ -0,0 +1 @@
10R5L5R10L4R5L5

141
day22.lisp Normal file
View File

@@ -0,0 +1,141 @@
;; https://adventofcode.com/2022/day/22
(defpackage :day-22
(:use :cl))
(in-package :day-22)
(ql:quickload 'alexandria)
(ql:quickload 'fiveam)
(ql:quickload 'cl-ppcre)
(5am:def-suite :day-22-test)
(defun read-map-to-array (filename)
(let* ((lines (uiop:read-file-lines filename))
(rows (length lines))
(cols (apply #'max (mapcar #'length lines)))
(arr (make-array (list (+ 2 rows) (+ 2 cols)) ; adding for padding row of empty space
:initial-element #\ )))
(loop for row from 0 below rows
for line = (coerce (nth row lines) 'array)
do (loop for col from 0 below cols
for char = (if (array-in-bounds-p line col) (aref line col) #\ )
do (setf (aref arr (1+ row) (1+ col)) char)))
arr))
(defun print-map (arr)
(loop for row from 0 below (array-dimension arr 0)
do (let ((line (make-array (array-dimension arr 1)
:displaced-to arr
:displaced-index-offset (* row (array-dimension arr 1)))))
(format t "~a~%" (coerce line 'string))
)))
(defconstant *movements*
'((left . (0 -1))
(right . (0 1))
(down . (1 0))
(up . (-1 0))))
(defun opposite-movement (movement)
(let ((alist '((left . right) (down . up))))
(or (alexandria:assoc-value alist movement)
(alexandria:rassoc-value alist movement))))
(defun move-coord-one-step (coord direction)
(mapcar #'+ coord (alexandria:assoc-value *movements* direction)))
(5am:def-test move-coord-left (:suite :day-22-test)
(5am:is (equalp (move-coord-one-step '(2 2) 'left)
'(2 1))))
(5am:def-test move-coord-right (:suite :day-22-test)
(5am:is (equalp (move-coord-one-step '(2 2) 'right)
'(2 3))))
(5am:def-test move-coord-up (:suite :day-22-test)
(5am:is (equalp (move-coord-one-step '(2 2) 'up)
'(1 2))))
(5am:def-test move-coord-down (:suite :day-22-test)
(5am:is (equalp (move-coord-one-step '(2 2) 'down)
'(3 2))))
(defun move-with-possible-wrap (coord direction map)
(let ((initial (move-coord-one-step coord direction)))
(if (not (equal #\ (apply #'aref map initial)))
;; when not wrapping
initial
;; when map on initial movement is empty - wrap
(do
((mov-coord coord
(move-coord-one-step mov-coord (opposite-movement direction))))
((equal #\ (apply #'aref map mov-coord)) (move-coord-one-step mov-coord direction))))))
(defun display-coord (coord map)
(let ((copied (alexandria:copy-array map)))
(setf (apply #'aref copied coord) #\X)
(print-map copied)))
(defun move (n direction coord map)
(do* ((prev-coord coord next-coord)
(next-coord (move-with-possible-wrap coord direction map) (move-with-possible-wrap next-coord direction map))
(count 0 (1+ count)))
((or (= count n)
(equal #\# (apply #'aref map next-coord)))
prev-coord)
;; (format t "before move, iter ~a~%" n)
;; (display-coord prev-coord map)
;; (format t "after move, iter ~a~%" n)
;; (display-coord next-coord map)
))
(defun new-direction (direction turn-to)
(let* ((directions-cycle '(UP RIGHT DOWN LEFT))
(turn-mod '((L . -1) (R . 1)))
(cur-dir-pos (position direction directions-cycle))
(new-pos (mod
(+ cur-dir-pos (alexandria:assoc-value turn-mod turn-to))
4)))
(nth new-pos directions-cycle)))
(defun parse-integer-or-symbol (str)
(let ((maybe-int (parse-integer str :junk-allowed t)))
(if maybe-int
maybe-int
(intern (string-upcase str)))))
(defun read-path (filename)
(mapcar #'parse-integer-or-symbol
(ppcre:split "(L|R)" (uiop:read-file-string filename) :with-registers-p t )))
(defun get-topmost-left-coords (map)
(loop for col from 0 below (array-dimension map 1)
do (when (equal #\. (aref map 1 col))
(return (list 1 col)))))
(defun walk-path (path-filename map-filename)
(let* ((padded-path (append (read-path path-filename)
(list 'L)))
(direction 'right)
; to be calculated
(map (read-map-to-array map-filename))
(coords (get-topmost-left-coords map))
(walk-result
(loop
for (n turn-direction)
on padded-path by #'cddr
do (progn
(setq coords (move n direction coords map))
(setq direction (new-direction direction turn-direction)))
finally (return (list coords direction)))))
(destructuring-bind ((row col) over-direction)
walk-result
(list row col (new-direction over-direction 'R)))))
(defun calc-password (row col direction)
(let* ((direction-scores '((right . 0) (down . 1) (left . 2) (up . 3)))
(direction-score (alexandria:assoc-value direction-scores direction)))
(+ (* 1000 row) (* 4 col) direction-score)))
(5am:run! ':day-22-test)

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