full aoc3
This commit is contained in:
parent
f5d075e328
commit
b43d25edef
2 changed files with 90 additions and 1 deletions
89
src/aoc3-2.lisp
Normal file
89
src/aoc3-2.lisp
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
;; #!/bin/sbcl --script
|
||||||
|
(defun numbers-in-line (index line-length line)
|
||||||
|
(let( (temp-str "") (numbers-found '() ) )
|
||||||
|
(setf temp-str (format nil "~a~c" temp-str (char line index)))
|
||||||
|
(loop while (digit-char-p c)
|
||||||
|
with c = #\0
|
||||||
|
with current-index = index
|
||||||
|
do
|
||||||
|
(setf c (if(zerop current-index) #\. (char line (- current-index 1))))
|
||||||
|
(if (digit-char-p c)
|
||||||
|
(progn
|
||||||
|
(setf temp-str (format nil "~c~a" c temp-str))
|
||||||
|
(decf current-index)
|
||||||
|
)))
|
||||||
|
(loop while (digit-char-p c)
|
||||||
|
with c = #\0
|
||||||
|
with current-index = index
|
||||||
|
do
|
||||||
|
(setf c (if(= line-length (+ current-index 1)) #\. (char line (+ current-index 1))))
|
||||||
|
(if (digit-char-p c)
|
||||||
|
(progn
|
||||||
|
(setf temp-str (format nil "~a~c" temp-str c))
|
||||||
|
(incf current-index)
|
||||||
|
)))
|
||||||
|
|
||||||
|
(let( (validNumber nil) (number-str ""))
|
||||||
|
(loop for c across temp-str
|
||||||
|
do
|
||||||
|
(if(digit-char-p c)
|
||||||
|
(progn
|
||||||
|
(when (not validNumber)
|
||||||
|
(setf validNumber t))
|
||||||
|
(setf number-str (format nil "~a~c" number-str c)))
|
||||||
|
(progn
|
||||||
|
(when validNumber
|
||||||
|
(progn
|
||||||
|
(push (parse-integer number-str) numbers-found)
|
||||||
|
(setf validNumber nil)
|
||||||
|
(setf number-str "")))))
|
||||||
|
finally
|
||||||
|
(when validNumber
|
||||||
|
(progn
|
||||||
|
(push (parse-integer number-str) numbers-found)
|
||||||
|
(setf validNumber nil)))))
|
||||||
|
|
||||||
|
;;DEBUG
|
||||||
|
;;(dolist (item numbers-found)
|
||||||
|
;;(format T "~%~d" item))
|
||||||
|
numbers-found
|
||||||
|
))
|
||||||
|
|
||||||
|
(with-open-file (file "aoc3input.txt")
|
||||||
|
(let((result 0) (thirdLine "") (previousLine "") (currentLine "") (lineLen 0) (numbers-in-gear '()))
|
||||||
|
|
||||||
|
(loop for line = (read-line file nil)
|
||||||
|
while line
|
||||||
|
do
|
||||||
|
(setf currentLine line)
|
||||||
|
(format T "~%~a ~a~%~a ~a~%~a ~a" "Third line: " thirdLine "Previous Line:" previousLine "Current line: " currentLine)
|
||||||
|
(if(string= previousLine "")
|
||||||
|
(progn
|
||||||
|
(setf lineLen (length line)) )
|
||||||
|
(progn
|
||||||
|
(setf currentLine line)
|
||||||
|
(format T "~%")
|
||||||
|
(loop for c across previousLine
|
||||||
|
with currentIdx = 0
|
||||||
|
do
|
||||||
|
(when (char= c #\*) (progn
|
||||||
|
(format T "~%~a ~d" "\* found in:" currentIdx)
|
||||||
|
(setf numbers-in-gear (append numbers-in-gear (numbers-in-line currentIdx lineLen (if(string= "" thirdLine) "." thirdLine))))
|
||||||
|
(setf numbers-in-gear (append numbers-in-gear (numbers-in-line currentIdx lineLen previousLine)))
|
||||||
|
(setf numbers-in-gear (append numbers-in-gear (numbers-in-line currentIdx lineLen currentLine)))
|
||||||
|
(print numbers-in-gear)
|
||||||
|
(when(> (list-length numbers-in-gear) 1)
|
||||||
|
(let((gear-value 1))
|
||||||
|
(dolist (item numbers-in-gear)
|
||||||
|
(setf gear-value (* gear-value item)))
|
||||||
|
(setf result (+ result gear-value))
|
||||||
|
))
|
||||||
|
(setf numbers-in-gear '())
|
||||||
|
))
|
||||||
|
(incf currentIdx)
|
||||||
|
)))
|
||||||
|
(setf thirdLine previousLine)
|
||||||
|
(setf previousLine currentLine)
|
||||||
|
)
|
||||||
|
(print result)
|
||||||
|
))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue