aoc3-1
This commit is contained in:
parent
919c46d25e
commit
f5d075e328
6 changed files with 739 additions and 0 deletions
75
src/aoc5.lisp
Normal file
75
src/aoc5.lisp
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
;; #!/bin/sbcl --script
|
||||
(with-open-file (file "aoc4input.txt")
|
||||
(let((result 0) (playerNumbersLen 0) (repetitions '())
|
||||
(lineLen 0) (manipulatedLine "") (winnerLine "") (playerLine "") (currentNumber "") (playerNumbers '()))
|
||||
(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))
|
||||
(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))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue