full aoc6

This commit is contained in:
Hane 2023-12-21 22:50:23 +01:00
commit 243347ce10
5 changed files with 192 additions and 0 deletions

91
src/aoc5-2.lisp Normal file
View file

@ -0,0 +1,91 @@
;; #!/bin/sbcl --script
(defun seed-list (seed-string)
(let ((temp-string "") (source-parsed nil) (temp-source 0) (temp-range 0) (index 0) (return-list '()))
(loop for c across seed-string
do
(if(char= c #\Space)
(cond ((not source-parsed)
(setf temp-source (parse-integer temp-string) )
(setf temp-string "")
(setf source-parsed t))
(t (setf temp-range (parse-integer temp-string))
(setf temp-string "")
;(setf index temp-range)
(loop while (< index temp-range)
do
(push (+ temp-source index) return-list)
(incf index)
)
(setf index 0)
(setf source-parsed nil)))
(setf temp-string (format nil "~a~c" temp-string c)))
finally
(setf temp-range (parse-integer temp-string))
(loop while (< index temp-range)
do
(push (+ temp-source index) return-list)
(incf index)))
(format T "~%~A" 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))
(defun parse-file (file
(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)
while line
do
(when (equalp 0 (length line))
(let((current-index 0))
(print "hola")
(dolist (item translated)
(setf (nth current-index translated) nil)
(incf current-index))))
(loop for c across line
do
(if(digit-char-p c)
(progn
(setf digit-line t)
(setf temp-string (format nil "~a~c" temp-string c)))
(if digit-line
(progn
(push (parse-integer temp-string) code)
(setf temp-string ""))))
finally
(when digit-line
(push (parse-integer temp-string) code)
(print code)
(let((current-index 0) (translation 0))
(dolist (item numbers)
(when (not (nth current-index translated))
(setf translation (parse-map item (nth 1 code) (nth 0 (last code)) (first code)))
(when (not (equal item translation))
(setf (nth current-index translated) t)
(setf (nth current-index numbers) translation)
(setf translation 0)))
(incf current-index)))
(setf temp-string "")
(setf digit-line nil)
(setf code '())) ))
(format T "~%~a ~A" "Result: " numbers)
(let( (lowest (first numbers)))
(dolist (item numbers)
(when (< item lowest) (setf lowest item)))
(print lowest))))

45
src/aoc6-2.lisp Normal file
View file

@ -0,0 +1,45 @@
;; #!/bin/sbcl --script
(defun number-string-to-list (seed-string)
(let ((temp-string "") (parsing nil) (return-list '()))
(loop for c across seed-string
do
(if(not (char= c #\Space) )
(setf temp-string (format nil "~a~c" temp-string c)))
finally
(push (parse-integer temp-string) return-list))
return-list))
(defun num-ways-to-win (time distance)
(format T "~%~d-~d" time distance)
(let( (ways-to-win 0) )
(loop with hold = 1
with distance-travelled = 0
while (< hold time)
do
(setf distance-travelled (* hold (- time hold)))
;(format T "~%~a ~d ~a ~d ~a ~d" "Hold:" hold "Travelled:" distance-travelled "Param dist:" distance)
(when (> distance-travelled distance)
(incf ways-to-win))
(incf hold))
ways-to-win))
(with-open-file (file "aoc6input.txt")
(let( (file-lines '()) (time-list '()) (distance-list '()) (result 1) )
(loop for line = (read-line file nil)
while line
do
(push (string-trim " " (string-left-trim " " (subseq line (+ 2 (position #\: line :test #'equalp)) (length line) ))) file-lines) )
;(print file-lines)
(setf time-list (number-string-to-list (nth 1 file-lines)))
(setf distance-list (number-string-to-list (nth 0 file-lines)))
;(format T "~%~A~%~A~%" time-list distance-list)
(loop with index = 0
while (< index (list-length time-list))
do
;(print (num-ways-to-win (nth index time-list) (nth index distance-list)))
(setf result (* result (num-ways-to-win (nth index time-list) (nth index distance-list))))
(incf index)
)
(print result)
))

52
src/aoc6.lisp Normal file
View file

@ -0,0 +1,52 @@
;; #!/bin/sbcl --script
(defun number-string-to-list (seed-string)
(let ((temp-string "") (parsing nil) (return-list '()))
(loop for c across seed-string
do
(if(and (char= c #\Space) parsing)
(progn
(push (parse-integer temp-string) return-list)
(setf temp-string "")
(setf parsing nil))
(progn
(when (not(char= c #\Space))
(setf temp-string (format nil "~a~c" temp-string c))
(setf parsing t))))
finally
(push (parse-integer temp-string) return-list))
return-list))
(defun num-ways-to-win (time distance)
(format T "~%~d-~d" time distance)
(let( (ways-to-win 0) )
(loop with hold = 1
with distance-travelled = 0
while (< hold time)
do
(setf distance-travelled (* hold (- time hold)))
;(format T "~%~a ~d ~a ~d ~a ~d" "Hold:" hold "Travelled:" distance-travelled "Param dist:" distance)
(when (> distance-travelled distance)
(incf ways-to-win))
(incf hold))
ways-to-win))
(with-open-file (file "aoc6input.txt")
(let( (file-lines '()) (time-list '()) (distance-list '()) (result 1) )
(loop for line = (read-line file nil)
while line
do
(push (string-left-trim " " (subseq line (+ 2 (position #\: line :test #'equalp)) (length line) )) file-lines) )
;(print file-lines)
(setf time-list (number-string-to-list (nth 1 file-lines)))
(setf distance-list (number-string-to-list (nth 0 file-lines)))
(format T "~%~A~%~A~%" time-list distance-list)
(loop with index = 0
while (< index (list-length time-list))
do
(print (num-ways-to-win (nth index time-list) (nth index distance-list)))
(setf result (* result (num-ways-to-win (nth index time-list) (nth index distance-list))))
(incf index)
)
(print result)
))

2
src/aoc6input.txt Normal file
View file

@ -0,0 +1,2 @@
Time: 40 81 77 72
Distance: 219 1012 1365 1089

2
src/aoc6test.txt Normal file
View file

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200