diff --git a/src/aoc11-2.lisp b/src/aoc11-2.lisp new file mode 100644 index 0000000..4310c6e --- /dev/null +++ b/src/aoc11-2.lisp @@ -0,0 +1,81 @@ +(defun input-parsing (path) + (with-open-file (file path) + (let( (galaxies (make-array 0 :adjustable t)) ) + (loop for line = (read-line file nil) + for current-line from 0 + with galaxy-columns = '() + with galaxies-copy = (make-array 0 :adjustable t) + with data-line = nil + with key = "" + with left-value = "" + with right-value = "" + while line + do + (when(zerop current-line) + (dotimes (i (length line)) + (push nil galaxy-columns))) + + (loop for c across line + with char-position = 0 + do + (when (char= c #\#) + (let( (size (+ 1 (length galaxies))) ) + (adjust-array galaxies size) (adjust-array galaxies-copy size) (decf size) + (setf (aref galaxies size) (cons current-line char-position)) + (setf (aref galaxies-copy size) (cons current-line char-position))) + ;;(format T "~% ~d ~A" char-position galaxy-columns) + (setf (nth char-position galaxy-columns) t) + ;;(setf (nth i parsed) nil) + (setf data-line t)) + (incf char-position) + finally + (if (not data-line) + (setf current-line (+ current-line *replace-value*))) + ;;(incf current-line) + (setf data-line nil)) + finally + ;;(print galaxy-columns) + ;;(setf galaxies-copy galaxies) + (loop for occupied-column in galaxy-columns + for x-index from 0 + do + (when (not occupied-column) + (loop for index from 0 + while (< index (array-total-size galaxies-copy)) + ;;for galaxy in galaxies + do + ;;(print x-index) + ;;(print index) + (when (> (cdr (aref galaxies-copy index)) x-index) + ;;(print (aref galaxies-copy index)) + (setf (cdr (aref galaxies index)) (+ (cdr (aref galaxies index)) *replace-value*))))))) + ;;(y . x) + (print galaxies) + galaxies))) + +(defun calculate-pairs (galaxies) + (loop for base from 0 + with result = 0 + while (< base (array-total-size galaxies)) + do + (loop for comparee from (+ base 1) + with x-movement = 0 + with y-movement = 0 + while (< comparee (array-total-size galaxies)) + do + ;;(print (aref galaxies base)) + ;;(print (aref galaxies comparee)) + (setf y-movement (if(<= (car(aref galaxies base)) (car(aref galaxies comparee))) + (- (car(aref galaxies comparee)) (car(aref galaxies base))) + (- (car(aref galaxies base)) (car(aref galaxies comparee))))) + (setf x-movement (if(<= (cdr(aref galaxies base)) (cdr(aref galaxies comparee))) + (- (cdr(aref galaxies comparee)) (cdr(aref galaxies base))) + (- (cdr(aref galaxies base)) (cdr(aref galaxies comparee))))) + ;;(print y-movement) + ;;(print x-movement) + (setf result (+ result (+ x-movement y-movement)))) + finally + (return result))) + +(defparameter *replace-value* 999999) +(print (calculate-pairs (input-parsing "aoc11input.txt"))) diff --git a/src/aoc11.lisp b/src/aoc11.lisp index 33d2e98..c2b6d4f 100644 --- a/src/aoc11.lisp +++ b/src/aoc11.lisp @@ -1,22 +1,10 @@ - (defun input-parsing (path) (with-open-file (file path) - (let( (galaxy-columns '()) (data-line nil) (galaxies (make-array 0 :adjustable t)) (current-line 0) ) - ;;Prep work - ;;(defparameter keys (make-array 0 :adjustable t)) - ;;(defparameter parsed '()) - ;;(setf direction-line (read-line file nil)) - ;;(setf direction-line (subseq temp-first-line (+ 2 (position #\: temp-first-line :test #'equalp)) (length temp-first-line) )) - ;;(setf yet-to-translate (seed-list manipulated-line)) - ;;(print yet-to-translate) - ;;(print translated) - ;;(print "----") - ;(read-line file nil) - ;; (let( (size (+ 1 (length keys))) ) - ;; (adjust-array keys size) (decf size) - ;; (setf (aref keys size) key) )) - + (let( (galaxies (make-array 0 :adjustable t)) ) (loop for line = (read-line file nil) + for current-line from 0 + with galaxy-columns = '() + with data-line = nil with key = "" with left-value = "" with right-value = "" @@ -24,15 +12,16 @@ do (when(zerop current-line) (dotimes (i (length line)) - (push nil galaxy-columns))) + (push nil galaxy-columns))) + (loop for c across line with char-position = 0 do (when (char= c #\#) (let( (size (+ 1 (length galaxies))) ) (adjust-array galaxies size) (decf size) - (setf (aref galaxies size) (cons current-line char-position)) ) - (format T "~% ~d ~A" char-position galaxy-columns) + (setf (aref galaxies size) (cons current-line char-position )) ) + ;;(format T "~% ~d ~A" char-position galaxy-columns) (setf (nth char-position galaxy-columns) t) ;;(setf (nth i parsed) nil) (setf data-line t)) @@ -40,18 +29,52 @@ finally (if (not data-line) (incf current-line)) - (incf current-line) + ;;(incf current-line) (setf data-line nil)) - ) + finally + (loop for occupied-column in galaxy-columns + for x-index from 0 + do + (when (not occupied-column) + (loop for index from 0 + while (< index (array-total-size galaxies)) + ;;for galaxy in galaxies + do + (when (> (cdr (aref galaxies index)) x-index) + (setf (cdr (aref galaxies index)) (+ (cdr (aref galaxies index)) 1))))))) galaxies))) -(print (input-parsing "aoc11test.txt")) +(defun calculate-pairs (galaxies) + (loop for base from 0 + with result = 0 + while (< base (array-total-size galaxies)) + do + (loop for comparee from (+ base 1) + with x-movement = 0 + with y-movement = 0 + while (< comparee (array-total-size galaxies)) + do + ;;(print (aref galaxies base)) + ;;(print (aref galaxies comparee)) + (setf y-movement (if(<= (car(aref galaxies base)) (car(aref galaxies comparee))) + (- (car(aref galaxies comparee)) (car(aref galaxies base))) + (- (car(aref galaxies base)) (car(aref galaxies comparee))))) + (setf x-movement (if(<= (cdr(aref galaxies base)) (cdr(aref galaxies comparee))) + (- (cdr(aref galaxies comparee)) (cdr(aref galaxies base))) + (- (cdr(aref galaxies base)) (cdr(aref galaxies comparee))))) + ;;(print y-movement) + ;;(print x-movement) + (setf result (+ result (+ x-movement y-movement)))) + finally + result)) + +(print (calculate-pairs (input-parsing "aoc11input.txt"))) ;;(print "Hola") ;;(print (aref keys 0)) ;; (incf (nth 0 steps-per-key)) ;; (if(char= c #\L) - ;; (setf (aref keys 0) (car(gethash (aref keys 0) route-hash))) + ;; (setf (aref keys 0) (cdr(gethash (aref keys 0) route-hash))) ;; (setf (aref keys 0) (cdr(gethash (aref keys 0) route-hash)))) ;; ;;(print (aref keys 0)) diff --git a/src/aoc8-2.lisp b/src/aoc8-2.lisp index 37a11e9..c861017 100644 --- a/src/aoc8-2.lisp +++ b/src/aoc8-2.lisp @@ -6,7 +6,7 @@ (print (sb-thread:list-all-threads)) (with-open-file (file "aoc8input.txt") - (let( (goal-to-be-reached nil) (direction-line "") (route-hash (make-hash-table :test #'equalp :size 800)) ) + (let( (goal-to-be-reached nil) (direction-line "") (route-hash (make-hash-table :test #'equalp :size 800))) ;;Prep work (defparameter keys (make-array 0 :adjustable t)) (defparameter parsed '()) @@ -31,11 +31,7 @@ (when(char= #\A (char key 2)) (let( (size (+ 1 (length keys))) ) (adjust-array keys size) (decf size) - (setf (aref keys size) key) )) - ) - - ;;(print keys) - ;;(print parsed) + (setf (aref keys size) key) ))) (dotimes (i (length keys)) (push 0 steps-per-key) (push t parsed)) @@ -59,16 +55,9 @@ (when(char= (char (aref keys 0) 2) #\Z) ;;(format T "~%~a ~d" "Ayo on key 0 found at: " (nth 0 steps-per-key)) (dotimes (i (length keys)) - ;;(print (+ i 1)) - ;;(print (length keys)) - ;;(when (< (+ i 1) (length keys)) (when (> i 0) - ;;WTF - (sb-thread:make-thread (lambda (x hashmap dir strlen steps) (hashmap-parse hashmap dir strlen x steps) ) :arguments (list i route-hash direction-line (length direction-line) (nth i steps-per-key) ) ) - ;;(format T "~%~d" i) - ) - ) - ;;(print parsed) + (sb-thread:make-thread (lambda (x hashmap dir strlen steps) (hashmap-parse hashmap dir strlen x steps) ) :arguments (list i route-hash direction-line (length direction-line) (nth i steps-per-key))))) + (loop while (not (every #'null parsed)) do (print parsed) @@ -105,9 +94,7 @@ (when(char= (char (aref keys key-position) 2) #\Z) (setf z-found t) (setf (nth key-position parsed) nil) - (setf (nth key-position steps-per-key) steps)) - ) - )) + (setf (nth key-position steps-per-key) steps))))) ;; (defvar *directions*) ;; (defvar *ndirections*)