day 20 part 2, yay

This commit is contained in:
efim 2022-12-25 09:56:18 +00:00
parent 281a0aebf4
commit e062633074

View File

@ -187,7 +187,11 @@
;; and now it works
;; 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*) 2000)
@ -203,10 +207,16 @@
(get-ugh-nth (mixing-array mixed) 3000)))
;; 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-input.txt")
;; (part-1-ans "day20-test.txt")
;; (print (part-1-ans "day20-input.txt"))
;; well, do we have duplicates?
@ -230,28 +240,25 @@
;; 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
;; so, how'd i do that?
;; #'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)
;; yay, at least previous solution was still incorrect. yayyayay
(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"))
;; and it works, now.
;;
;; 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?
(print (part-2-ans "day20-test.txt"))
(print (part-2-ans "day20-input.txt"))