full aoc1
This commit is contained in:
parent
4c57ab57f3
commit
cbe01ffb4b
3 changed files with 131 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
*.fasl
|
||||
122
src/aoc1-2.lisp
Normal file
122
src/aoc1-2.lisp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
;; #!/bin/sbcl --script
|
||||
|
||||
(import 'ppcre)
|
||||
|
||||
(with-open-file (file "aoc1input.txt")
|
||||
(let((num 0)
|
||||
(firstMatch nil)(lastMatch nil)
|
||||
(translation (list "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
|
||||
)
|
||||
(loop for line = (read-line file nil)
|
||||
while line do
|
||||
(loop for c across line
|
||||
with currentIdx = 0
|
||||
do
|
||||
(if (parse-integer (format nil "~c" c) :junk-allowed t)
|
||||
(if firstMatch
|
||||
(setq lastMatch c)
|
||||
(setq firstMatch c)
|
||||
)
|
||||
|
||||
(loop for numInList in translation
|
||||
with listIdx = 0
|
||||
do
|
||||
;;debug
|
||||
;;(if (< (+ currentIdx 2) (length line)) (print (subseq line currentIdx (+ currentIdx 3))) )
|
||||
;;
|
||||
(if(and (< (+ currentIdx 2) (length line)) (string= (subseq line currentIdx (+ currentIdx 3)) numInList) )
|
||||
(if firstMatch
|
||||
(setq lastMatch (digit-char (+ listIdx 1)))
|
||||
(setq firstMatch (digit-char (+ listIdx 1)) )
|
||||
))
|
||||
(if (and (< (+ currentIdx 3) (length line)) (string= (subseq line currentIdx (+ currentIdx 4)) numInList) )
|
||||
(if firstMatch
|
||||
(setq lastMatch (digit-char (+ listIdx 1)))
|
||||
(setq firstMatch (digit-char (+ listIdx 1)))
|
||||
))
|
||||
(if (and (< (+ currentIdx 4) (length line)) (string= (subseq line currentIdx (+ currentIdx 5)) numInList) )
|
||||
(if firstMatch
|
||||
(setq lastMatch (digit-char (+ listIdx 1)))
|
||||
(setq firstMatch (digit-char (+ listIdx 1)))
|
||||
))
|
||||
(incf listIdx)
|
||||
)
|
||||
)
|
||||
(incf currentIdx)
|
||||
)
|
||||
(print firstMatch)
|
||||
(print lastMatch)
|
||||
(if (not lastMatch) (setq lastMatch firstMatch))
|
||||
(setq num (+ num (parse-integer (format nil "~c~c" firstMatch lastMatch))))
|
||||
(print num)
|
||||
|
||||
(print "---")
|
||||
(setq firstMatch nil)
|
||||
(setq lastMatch nil)
|
||||
)
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
|
||||
;; (setq finding (ppcre:all-matches-as-strings "\\d|one|two|three|four|five|six|seven|eight|nine" line :sharedp t ))
|
||||
;; (print finding)
|
||||
;; ;(print (length(nth 0 (last finding))) )
|
||||
;; (if (> (length(first finding)) 1 )
|
||||
;; (loop for numInList in translation
|
||||
;; for currentIdx upfrom 1
|
||||
;; when (string= (first finding) numInList)
|
||||
;; do ;(print currentIdx)
|
||||
;; (setq lineNum (concatenate 'string lineNum (write-to-string currentIdx)) )
|
||||
;; (return currentIdx)
|
||||
;; )
|
||||
;; (setq lineNum (concatenate 'string lineNum (first finding))) )
|
||||
|
||||
|
||||
;; (if (> (length(nth 0 (last finding))) 1 )
|
||||
;; (loop for numInList in translation
|
||||
;; for currentIdx upfrom 1
|
||||
;; when (string= (nth 0 (last finding)) numInList)
|
||||
;; do ;(print currentIdx)
|
||||
;; (setq lineNum (concatenate 'string lineNum (write-to-string currentIdx)) )
|
||||
;; (return currentIdx)
|
||||
;; )
|
||||
;; (setq lineNum (concatenate 'string lineNum (nth 0 (last finding) )) )
|
||||
;; )
|
||||
|
||||
|
||||
;; (defun find-downstep-index (list)
|
||||
;; (if (endp (rest list))
|
||||
;; (length list)
|
||||
;; (loop :for current-element :in list
|
||||
;; :for next-element :in (rest list)
|
||||
;; :for current-index :upfrom 0
|
||||
;; :when (> current-element next-element)
|
||||
;; :do (return-from find-downstep-index current-index)
|
||||
;; :finally (return (+ current-index 2)))))
|
||||
|
||||
;(setq lineNum (parse-integer (concatenate 'string (first finding) (nth 0 (last finding)) )))
|
||||
;(setq num (+ num lineNum))
|
||||
;(setq lineNum 0)
|
||||
;)
|
||||
;(print num)
|
||||
|
||||
;; (with-open-file (file "aoc1test.txt")
|
||||
;; (let((num 0) (lineNum 0) (finding nil))
|
||||
;; (loop for line = (read-line file nil)
|
||||
;; while line do ;(
|
||||
;; (setq finding (map 'list #'parse-integer (ppcre:all-matches-as-strings "||d" line )))
|
||||
;; (setq lineNum (+ (nth 0 (last finding)) (first finding)))
|
||||
;; (setq num (+ num lineNum))
|
||||
;; (setq lineNum 0)
|
||||
;; ;)
|
||||
;; (print num)
|
||||
;; ))
|
||||
|
||||
;; ;; format t "~a~%" line))
|
||||
;; )
|
||||
|
||||
;; (let ((in (open "/some/file/name.txt" :if-does-not-exist nil)))
|
||||
;; (when in
|
||||
;; (close in))
|
||||
;; )
|
||||
8
src/aoc1-2test.txt
Normal file
8
src/aoc1-2test.txt
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
8
|
||||
Loading…
Add table
Add a link
Reference in a new issue