aoc5-1
This commit is contained in:
parent
b43d25edef
commit
5bab5d320e
2 changed files with 70 additions and 73 deletions
|
|
@ -15,7 +15,7 @@
|
||||||
(setf lineLen (length line))
|
(setf lineLen (length line))
|
||||||
;(setf manipulatedLine (remove #\: (substitute #\: #\Space (subseq line (position #\: line :test #'equalp) lineLen) ) ))
|
;(setf manipulatedLine (remove #\: (substitute #\: #\Space (subseq line (position #\: line :test #'equalp) lineLen) ) ))
|
||||||
(setf manipulatedLine (subseq line (+ 2 (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))
|
(subseq manipulatedLine 0 (position #\; manipulatedLine :test #'equalp))
|
||||||
(setf showing ""))
|
(setf showing ""))
|
||||||
until (string= showing "")
|
until (string= showing "")
|
||||||
|
|
|
||||||
141
src/aoc5.lisp
141
src/aoc5.lisp
|
|
@ -1,75 +1,72 @@
|
||||||
;; #!/bin/sbcl --script
|
;; #!/bin/sbcl --script
|
||||||
(with-open-file (file "aoc4input.txt")
|
(defun seed-list (seed-string)
|
||||||
(let((result 0) (playerNumbersLen 0) (repetitions '())
|
(let ((temp-string "") (return-list '()))
|
||||||
(lineLen 0) (manipulatedLine "") (winnerLine "") (playerLine "") (currentNumber "") (playerNumbers '()))
|
(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)
|
(loop for line = (read-line file nil)
|
||||||
while line do
|
while line
|
||||||
(setf lineLen (length line))
|
do
|
||||||
(setf manipulatedLine (subseq line (+ 2 (position #\: line :test #'equalp)) lineLen ))
|
(when (equalp 0 (length line))
|
||||||
(setf winnerLine (subseq manipulatedLine 0 (- (position #\| manipulatedLine :test #'equalp) 1)) )
|
(let((current-index 0))
|
||||||
(setf playerLine (subseq manipulatedLine (+ 2 (position #\| manipulatedLine :test #'equalp)) (length manipulatedLine) ))
|
(print "hola")
|
||||||
(print winnerLine)
|
(dolist (item translated)
|
||||||
;;(print manipulatedLine)
|
(setf (nth current-index translated) nil)
|
||||||
(print playerLine)
|
(incf current-index))))
|
||||||
(loop for c across playerLine
|
(loop for c across line
|
||||||
with currentIdx = 0
|
do
|
||||||
do
|
(if(digit-char-p c)
|
||||||
(if(not(char= c #\Space))
|
(progn
|
||||||
(progn
|
(setf digit-line t)
|
||||||
(setf currentNumber (format nil "~a~c" currentNumber c))
|
(setf temp-string (format nil "~a~c" temp-string c)))
|
||||||
(if(equal (+ currentIdx 1) (length playerLine))
|
(if digit-line
|
||||||
(progn
|
(progn
|
||||||
(push (parse-integer(format nil "~%~a" currentNumber)) playerNumbers )
|
(push (parse-integer temp-string) code)
|
||||||
(setf currentNumber ""))))
|
(setf temp-string ""))))
|
||||||
(progn
|
finally
|
||||||
;;(format T "~%~d|~d" (+ currentIdx 1) (length playerLine))
|
(when digit-line
|
||||||
(if(not(string= currentNumber ""))
|
(push (parse-integer temp-string) code)
|
||||||
(progn
|
(print code)
|
||||||
(push (parse-integer(format nil "~%~a" currentNumber)) playerNumbers )
|
(let((current-index 0) (translation 0))
|
||||||
(setf currentNumber "")))))
|
(dolist (item numbers)
|
||||||
(incf currentIdx))
|
(when (not (nth current-index translated))
|
||||||
(format T "~%~a ~a" "playerNumbersLen:" playerNumbersLen)
|
(setf translation (parse-map item (nth 1 code) (nth 0 (last code)) (first code)))
|
||||||
(if(equal 0 playerNumbersLen)
|
(when (not (equal item translation))
|
||||||
(progn
|
(setf (nth current-index translated) t)
|
||||||
(setf playerNumbersLen (list-length playerNumbers))
|
(setf (nth current-index numbers) translation)
|
||||||
(loop with idx = 0
|
(setf translation 0)))
|
||||||
while (< idx playerNumbersLen)
|
(incf current-index)))
|
||||||
do
|
(setf temp-string "")
|
||||||
(push 1 repetitions)
|
(setf digit-line nil)
|
||||||
(incf idx))
|
(setf code '())) ))
|
||||||
(print repetitions)))
|
(format T "~%~a ~A" "Result: " numbers)
|
||||||
(let((numOfInstances 0) (nextToClone 0))
|
(let( (lowest (first numbers)))
|
||||||
(setf numOfInstances (first repetitions))
|
(dolist (item numbers)
|
||||||
(setf result (+ result numOfInstances))
|
(when (< item lowest) (setf lowest item)))
|
||||||
;;(format T "~%~a ~a" "Number of cards won:" result)
|
(print lowest))))
|
||||||
(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))))
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue