full aoc11

This commit is contained in:
Hane 2023-12-31 01:12:22 +01:00
commit cebe6b645f
3 changed files with 132 additions and 41 deletions

81
src/aoc11-2.lisp Normal file
View file

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

View file

@ -1,22 +1,10 @@
(defun input-parsing (path) (defun input-parsing (path)
(with-open-file (file path) (with-open-file (file path)
(let( (galaxy-columns '()) (data-line nil) (galaxies (make-array 0 :adjustable t)) (current-line 0) ) (let( (galaxies (make-array 0 :adjustable t)) )
;;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) ))
(loop for line = (read-line file nil) (loop for line = (read-line file nil)
for current-line from 0
with galaxy-columns = '()
with data-line = nil
with key = "" with key = ""
with left-value = "" with left-value = ""
with right-value = "" with right-value = ""
@ -25,14 +13,15 @@
(when(zerop current-line) (when(zerop current-line)
(dotimes (i (length line)) (dotimes (i (length line))
(push nil galaxy-columns))) (push nil galaxy-columns)))
(loop for c across line (loop for c across line
with char-position = 0 with char-position = 0
do do
(when (char= c #\#) (when (char= c #\#)
(let( (size (+ 1 (length galaxies))) ) (let( (size (+ 1 (length galaxies))) )
(adjust-array galaxies size) (decf size) (adjust-array galaxies size) (decf size)
(setf (aref galaxies size) (cons current-line char-position)) ) (setf (aref galaxies size) (cons current-line char-position )) )
(format T "~% ~d ~A" char-position galaxy-columns) ;;(format T "~% ~d ~A" char-position galaxy-columns)
(setf (nth char-position galaxy-columns) t) (setf (nth char-position galaxy-columns) t)
;;(setf (nth i parsed) nil) ;;(setf (nth i parsed) nil)
(setf data-line t)) (setf data-line t))
@ -40,18 +29,52 @@
finally finally
(if (not data-line) (if (not data-line)
(incf current-line)) (incf current-line))
(incf current-line) ;;(incf current-line)
(setf data-line nil)) (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))) 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 "Hola")
;;(print (aref keys 0)) ;;(print (aref keys 0))
;; (incf (nth 0 steps-per-key)) ;; (incf (nth 0 steps-per-key))
;; (if(char= c #\L) ;; (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)))) ;; (setf (aref keys 0) (cdr(gethash (aref keys 0) route-hash))))
;; ;;(print (aref keys 0)) ;; ;;(print (aref keys 0))

View file

@ -6,7 +6,7 @@
(print (sb-thread:list-all-threads)) (print (sb-thread:list-all-threads))
(with-open-file (file "aoc8input.txt") (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 ;;Prep work
(defparameter keys (make-array 0 :adjustable t)) (defparameter keys (make-array 0 :adjustable t))
(defparameter parsed '()) (defparameter parsed '())
@ -31,11 +31,7 @@
(when(char= #\A (char key 2)) (when(char= #\A (char key 2))
(let( (size (+ 1 (length keys))) ) (let( (size (+ 1 (length keys))) )
(adjust-array keys size) (decf size) (adjust-array keys size) (decf size)
(setf (aref keys size) key) )) (setf (aref keys size) key) )))
)
;;(print keys)
;;(print parsed)
(dotimes (i (length keys)) (dotimes (i (length keys))
(push 0 steps-per-key) (push 0 steps-per-key)
(push t parsed)) (push t parsed))
@ -59,16 +55,9 @@
(when(char= (char (aref keys 0) 2) #\Z) (when(char= (char (aref keys 0) 2) #\Z)
;;(format T "~%~a ~d" "Ayo on key 0 found at: " (nth 0 steps-per-key)) ;;(format T "~%~a ~d" "Ayo on key 0 found at: " (nth 0 steps-per-key))
(dotimes (i (length keys)) (dotimes (i (length keys))
;;(print (+ i 1))
;;(print (length keys))
;;(when (< (+ i 1) (length keys))
(when (> i 0) (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)))))
(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)
(loop while (not (every #'null parsed)) (loop while (not (every #'null parsed))
do do
(print parsed) (print parsed)
@ -105,9 +94,7 @@
(when(char= (char (aref keys key-position) 2) #\Z) (when(char= (char (aref keys key-position) 2) #\Z)
(setf z-found t) (setf z-found t)
(setf (nth key-position parsed) nil) (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 *directions*)
;; (defvar *ndirections*) ;; (defvar *ndirections*)