aoc2023/src/aoc2.lisp
2023-12-14 23:22:47 +01:00

80 lines
3.1 KiB
Common Lisp

;; #!/bin/sbcl --script
(with-open-file (file "aoc2input.txt")
(let((result 0) (game 1)
(validEntry t)
;(firstMatch nil)(lastMatch nil)
(lineLen 0)
(manipulatedLine "")
(showings'())
(num "") (color "") (copy "")
(max (list (cons "red" 12) (cons "green" 13) (cons "blue" 14) ))
)
(loop for line = (read-line file nil)
while line do
(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)
(subseq manipulatedLine 0 (position #\; manipulatedLine :test #'equalp))
(setf showing ""))
until (string= showing "")
do
(push showing showings)
;(print showing)
;(if (position #\; manipulatedLine :test #'equalp)
(if(position #\; manipulatedLine :test #'equalp)
(setf manipulatedLine (subseq manipulatedLine (+ 2 (position #\; manipulatedLine :test #'equalp)) (length manipulatedLine) ))
)
;(setf manipulatedLine "")
;(print manipulatedLine)
finally
(push manipulatedLine showings)
(dolist (item showings)
(format T "~%\"~a\"" item))
)
(dolist (item showings)
(setf copy item)
(loop for showing = (if(position #\, copy :test #'equalp)
(subseq copy 0 (position #\, copy :test #'equalp))
(setf showing ""))
until (string= showing "")
do
(format T "~%~a ~a~%" "Starting loop with:" copy)
;(print (subseq copy 0 (position #\, copy :test #'equalp)))
;(print showing)
(setf num (subseq showing 0 (position #\Space showing :test #'equalp)))
(setf color (subseq showing (+ 1 (position #\Space showing :test #'equalp)) (length showing) ))
(format T "~a ~a\+~a~%" "Segmentation:" num color)
(if (> (parse-integer num) (parse-integer(write-to-string(cdr(find color max :key 'car :test #'equal)))))
(setf validEntry nil))
(setf copy (subseq copy (+ 2 (position #\, copy :test #'equalp)) (length copy) ))
;; (if(position #\, showing :test #'equalp)
;; (setf copy (subseq copy (+ 2 (position #\, copy :test #'equalp)) (length copy) ))
;; )
(format T "~%~a ~a~%" "New copy:" copy)
finally
(setf num (subseq copy 0 (position #\Space copy :test #'equalp)))
(setf color (subseq copy (+ 1 (position #\Space copy :test #'equalp)) (length copy)))
(if (> (parse-integer num) (parse-integer(write-to-string(cdr(find color max :key 'car :test #'equal)))))
(setf validEntry nil))
(format T "~%~a~a\-~a\-~a~%" "Final:" copy num color)
)
)
(format T "~%~a~%" "finit")
;(push "finit" showings)
(if validEntry
(setf result (+ result game)))
(incf game)
(setf showings '())
(setf validEntry t)
;(setf manipulatedLine "")
)
(format T "~%~a ~a~%" "Result:" result)
)
)