commit 3855366b30b3a82668cc2a45137e52921a2be5af from: Lukas Henkel date: Fri Dec 01 07:38:28 2023 UTC Optimize commit - 7f482c9b9a0cdd1130670ee18b70fbcbb45fca97 commit + 3855366b30b3a82668cc2a45137e52921a2be5af blob - 7d14b1f98774cb709c4b247977b243eec526eeb1 blob + 712d6e11bf177adc361961be7d6f244cee018063 --- build.lisp +++ build.lisp @@ -7,6 +7,10 @@ exec sbcl --script "build.lisp" (when (probe-file quicklisp-init) (load quicklisp-init))) (push (probe-file #P".") asdf:*central-registry*) + +(sb-ext:restrict-compiler-policy 'speed 3 3) +(push :release *features*) + (ql:quickload :aoc) (loop for day from 1 to 25 do (handler-case blob - 1a47b5fb4923b65990a33882cc6f9ef220c3e32c blob + bf3a7cff8c47f0bb352622993259f191e256b179 --- src/day-1.lisp +++ src/day-1.lisp @@ -3,16 +3,24 @@ (:export #:day-1)) (in-package #:aoc/day-1) +#+release (declaim (inline first-digit last-digit + alpha-digit-at reverse-alpha-digit-at + first-digit-alpha last-digit-alpha)) + +(declaim (ftype (function (simple-string) fixnum) first-digit)) (defun first-digit (line) - (loop for char across line - when (digit-char-p char) - do (return (char-number char)))) + (or (loop for char across line + when (digit-char-p char) + do (return (char-number char))) + 0)) +(declaim (ftype (function (simple-string) fixnum) last-digit)) (defun last-digit (line) - (loop for i from (1- (length line)) downto 0 - for char = (aref line i) - when (digit-char-p char) - do (return (char-number char)))) + (or (loop for i from (1- (length line)) downto 0 + for char = (aref line i) + when (digit-char-p char) + do (return (char-number char))) + 0)) (defparameter *alpha-digit-table* '(("one" . 1) ("two" . 2) @@ -24,8 +32,9 @@ ("eight" . 8) ("nine" . 9))) +(declaim (ftype (function (simple-string fixnum) (or fixnum null)) alpha-digit-at)) (defun alpha-digit-at (line start) - (loop for (compare . digit) in *alpha-digit-table* + (loop for (compare . digit) (simple-string . fixnum) in *alpha-digit-table* when (loop for i from start below (min (length line) (+ start (length compare))) for ci from 0 @@ -33,8 +42,9 @@ (aref compare ci))) do (return digit))) +(declaim (ftype (function (simple-string fixnum) (or fixnum null)) reverse-alpha-digit-at)) (defun reverse-alpha-digit-at (line end) - (loop for (compare . digit) in *alpha-digit-table* + (loop for (compare . digit) (simple-string . fixnum) in *alpha-digit-table* for compare-length = (1- (length compare)) when (loop for i from end downto (max (- end compare-length) 0) for ci downfrom compare-length @@ -42,25 +52,29 @@ (aref compare ci))) do (return digit))) +(declaim (ftype (function (simple-string) fixnum) first-digit-alpha)) (defun first-digit-alpha (line) - (loop for i from 0 below (length line) - for char = (aref line i) - when (digit-char-p char) - do (return (char-number char)) - thereis (alpha-digit-at line i))) + (or (loop for i from 0 below (length line) + for char = (aref line i) + when (digit-char-p char) + do (return (char-number char)) + thereis (alpha-digit-at line i)) + 0)) +(declaim (ftype (function (simple-string) fixnum) last-digit-alpha)) (defun last-digit-alpha (line) - (loop for i from (1- (length line)) downto 0 - for char = (aref line i) - when (digit-char-p char) - do (return (char-number char)) - thereis (reverse-alpha-digit-at line i))) + (or (loop for i from (1- (length line)) downto 0 + for char = (aref line i) + when (digit-char-p char) + do (return (char-number char)) + thereis (reverse-alpha-digit-at line i)) + 0)) (defun day-1 (input) (loop for line = (read-line input nil) while line - sum (+ (* (or (first-digit line) 0) 10) - (or (last-digit line) 0)) into task-1 - sum (+ (* (first-digit-alpha line) 10) - (last-digit-alpha line)) into task-2 + sum (+ (the fixnum (* (first-digit line) 10)) + (last-digit line)) into task-1 fixnum + sum (+ (the fixnum (* (first-digit-alpha line) 10)) + (last-digit-alpha line)) into task-2 fixnum finally (return (values task-1 task-2)))) blob - 2bf81d9d9e3aac98879b9a786c59d4e6152def24 blob + d7b39703b207ae470cf5d66d0d9ce72086b07b9a --- src/utils.lisp +++ src/utils.lisp @@ -66,6 +66,8 @@ groups))) +(declaim (ftype (function (character) fixnum) char-number) + (inline char-number)) (defun char-number (char) (- (char-int char) 48))