diff --git a/src/aoc2.lisp b/src/aoc2.lisp index ccc582f..9acc48b 100644 --- a/src/aoc2.lisp +++ b/src/aoc2.lisp @@ -15,7 +15,7 @@ (setf lineLen (length line)) ;(setf manipulatedLine (remove #\: (substitute #\: #\Space (subseq line (position #\: line :test #'equalp) lineLen) ) )) (setf manipulatedLine (subseq line (+ 2 (position #\: line :test #'equalp)) lineLen )) - (loop for showing = (if(position #\; manipulatedLine :test #'equalp) + (loop for showing = (if(position #\; manipulatedLine :test #'equalp)l (subseq manipulatedLine 0 (position #\; manipulatedLine :test #'equalp)) (setf showing "")) until (string= showing "") diff --git a/src/aoc5.lisp b/src/aoc5.lisp index b56f4e6..66e0a33 100644 --- a/src/aoc5.lisp +++ b/src/aoc5.lisp @@ -1,75 +1,72 @@ ;; #!/bin/sbcl --script -(with-open-file (file "aoc4input.txt") - (let((result 0) (playerNumbersLen 0) (repetitions '()) - (lineLen 0) (manipulatedLine "") (winnerLine "") (playerLine "") (currentNumber "") (playerNumbers '())) +(defun seed-list (seed-string) + (let ((temp-string "") (return-list '())) + (loop for c across seed-string + do + (if(char= c #\Space) + (progn + (push (parse-integer temp-string) return-list) + (setf temp-string "")) + (setf temp-string (format nil "~a~c" temp-string c))) + finally + (push (parse-integer temp-string) return-list)) + return-list)) + +(defun parse-map (seed source dest range) + (format T "~%~d-~d-~d-~d" seed source dest range) + (if(and(>= seed source) (<= seed (+ source range))) + (progn + (+ dest (- seed source))) + seed)) + +(with-open-file (file "aoc5input.txt") + (let( (digit-line nil) (temp-first-line "") (temp-string "") (manipulated-line "") (code '()) (numbers '()) (translated '())) + ;;Prep work + (setf temp-first-line (read-line file nil)) + (setf manipulated-line (subseq temp-first-line (+ 2 (position #\: temp-first-line :test #'equalp)) (length temp-first-line) )) + (setf numbers (seed-list manipulated-line)) + (dolist (item numbers) (push nil translated)) + (print numbers) + (print translated) + (read-line file nil) + (loop for line = (read-line file nil) - while line do - (setf lineLen (length line)) - (setf manipulatedLine (subseq line (+ 2 (position #\: line :test #'equalp)) lineLen )) - (setf winnerLine (subseq manipulatedLine 0 (- (position #\| manipulatedLine :test #'equalp) 1)) ) - (setf playerLine (subseq manipulatedLine (+ 2 (position #\| manipulatedLine :test #'equalp)) (length manipulatedLine) )) - (print winnerLine) - ;;(print manipulatedLine) - (print playerLine) - (loop for c across playerLine - with currentIdx = 0 - do - (if(not(char= c #\Space)) - (progn - (setf currentNumber (format nil "~a~c" currentNumber c)) - (if(equal (+ currentIdx 1) (length playerLine)) + while line + do + (when (equalp 0 (length line)) + (let((current-index 0)) + (print "hola") + (dolist (item translated) + (setf (nth current-index translated) nil) + (incf current-index)))) + (loop for c across line + do + (if(digit-char-p c) + (progn + (setf digit-line t) + (setf temp-string (format nil "~a~c" temp-string c))) + (if digit-line (progn - (push (parse-integer(format nil "~%~a" currentNumber)) playerNumbers ) - (setf currentNumber "")))) - (progn - ;;(format T "~%~d|~d" (+ currentIdx 1) (length playerLine)) - (if(not(string= currentNumber "")) - (progn - (push (parse-integer(format nil "~%~a" currentNumber)) playerNumbers ) - (setf currentNumber ""))))) - (incf currentIdx)) - (format T "~%~a ~a" "playerNumbersLen:" playerNumbersLen) - (if(equal 0 playerNumbersLen) - (progn - (setf playerNumbersLen (list-length playerNumbers)) - (loop with idx = 0 - while (< idx playerNumbersLen) - do - (push 1 repetitions) - (incf idx)) - (print repetitions))) - (let((numOfInstances 0) (nextToClone 0)) - (setf numOfInstances (first repetitions)) - (setf result (+ result numOfInstances)) - ;;(format T "~%~a ~a" "Number of cards won:" result) - (pop repetitions) - (setf repetitions (append repetitions '(1))) - (print repetitions) - (loop for c across winnerLine - with currentIdx = 0 - do - (if(not(char= c #\Space)) - (progn - (setf currentNumber (format nil "~a~c" currentNumber c)) - (if(equal (+ currentIdx 1) (length winnerLine)) - (progn - (dolist (item playerNumbers) - (if (equal (parse-integer currentNumber) item) - (progn - (setf (nth nextToClone repetitions) (+ (nth nextToClone repetitions) numOfInstances)) - (incf nextToClone) - (print nextToClone)))) - (setf currentNumber "")))) - (progn - (if(not(string= currentNumber "")) - (progn - (dolist (item playerNumbers) - (if (equal (parse-integer currentNumber) item) - (progn - (setf (nth nextToClone repetitions) (+ (nth nextToClone repetitions) numOfInstances)) - (incf nextToClone) - (print nextToClone)))) - (setf currentNumber ""))))) - (incf currentIdx))) - (setf playerNumbers '()) - (format T "~%~a ~a" "Number of cards won:" result)))) + (push (parse-integer temp-string) code) + (setf temp-string "")))) + finally + (when digit-line + (push (parse-integer temp-string) code) + (print code) + (let((current-index 0) (translation 0)) + (dolist (item numbers) + (when (not (nth current-index translated)) + (setf translation (parse-map item (nth 1 code) (nth 0 (last code)) (first code))) + (when (not (equal item translation)) + (setf (nth current-index translated) t) + (setf (nth current-index numbers) translation) + (setf translation 0))) + (incf current-index))) + (setf temp-string "") + (setf digit-line nil) + (setf code '())) )) + (format T "~%~a ~A" "Result: " numbers) + (let( (lowest (first numbers))) + (dolist (item numbers) + (when (< item lowest) (setf lowest item))) + (print lowest))))