day 20 part 2, yay
This commit is contained in:
parent
974cc4993d
commit
281a0aebf4
|
@ -187,11 +187,7 @@
|
||||||
|
|
||||||
;; 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)
|
||||||
|
@ -207,13 +203,7 @@
|
||||||
(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 (mixing-array mixed) 1000)
|
|
||||||
(get-ugh-nth (mixing-array mixed) 2000)
|
|
||||||
(get-ugh-nth (mixing-array mixed) 3000))))
|
|
||||||
|
|
||||||
(part-1-ans "day20-test.txt")
|
(part-1-ans "day20-test.txt")
|
||||||
(part-1-ans "day20-input.txt")
|
(part-1-ans "day20-input.txt")
|
||||||
|
@ -239,3 +229,29 @@
|
||||||
;; what should i do about the duplicates?
|
;; what should i do about the duplicates?
|
||||||
;; 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?
|
||||||
|
;; #'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))
|
||||||
|
|
||||||
|
(input-arr "day20-test.txt")
|
||||||
|
|
||||||
|
(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?
|
||||||
|
|
36
day20.lisp
36
day20.lisp
|
@ -92,21 +92,45 @@
|
||||||
))
|
))
|
||||||
|
|
||||||
;; we know the element value, but not it's place
|
;; we know the element value, but not it's place
|
||||||
(defun move-elem-by-itself (array element)
|
(defun move-elem-by-itself (array initial-index)
|
||||||
(declare (optimize (debug 3)))
|
(declare (optimize (debug 3)))
|
||||||
(let ((i (position element array)))
|
(let ((i (position initial-index array :test (lambda (searched-index zipped)
|
||||||
(move-item array i element)))
|
(= searched-index (car zipped))))))
|
||||||
|
(move-item array i (second (aref array i)))))
|
||||||
|
|
||||||
(defun mixing-array (arr)
|
(defun mixing-array (arr)
|
||||||
(let ((to-be-modified (alexandria:copy-array arr)))
|
(let ((to-be-modified (alexandria:copy-array arr)))
|
||||||
(loop
|
(loop
|
||||||
for elem across arr
|
for initial-index from 0 below (length arr)
|
||||||
do (progn (move-elem-by-itself to-be-modified elem)
|
;; for elem across arr
|
||||||
(format t "after moving ~a, arr: ~a~%" elem to-be-modified)
|
do (progn (move-elem-by-itself to-be-modified initial-index)
|
||||||
|
;; (format t "after moving ~a, arr: ~a~%" elem to-be-modified)
|
||||||
))
|
))
|
||||||
to-be-modified))
|
to-be-modified))
|
||||||
|
|
||||||
|
(defun zip-with-index (ls)
|
||||||
|
(loop for v in ls
|
||||||
|
for i from 0
|
||||||
|
collect (list i v)))
|
||||||
|
|
||||||
|
(defun input-arr (filename)
|
||||||
|
(let ((nums (mapcar #'parse-integer (uiop:read-file-lines filename))))
|
||||||
|
(make-array (length nums) :initial-contents (zip-with-index nums))))
|
||||||
|
|
||||||
|
(defun get-ugh-nth (arr n)
|
||||||
|
;; need to find 0 by value in the (index, value) array
|
||||||
|
(let* ((zero-ind (position 0 arr :test (lambda (searched-value zipped)
|
||||||
|
(= searched-value (second zipped)))))
|
||||||
|
(unsafe-index (+ zero-ind n))
|
||||||
|
(safe-n (mod unsafe-index (length arr))))
|
||||||
|
(second (aref arr safe-n))))
|
||||||
|
|
||||||
|
(defun part-1-ans (filename)
|
||||||
|
(let* ((input-arr (input-arr filename))
|
||||||
|
(mixed (mixing-array input-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))))
|
||||||
|
|
||||||
(5am:run! 'day20-tests)
|
(5am:run! 'day20-tests)
|
||||||
|
|
Loading…
Reference in New Issue