advent-of-code-2023

My solutions to AoC 2023
git clone git://git.entf.net/advent-of-code-2023
Log | Files | Refs

commit 3855366b30b3a82668cc2a45137e52921a2be5af
parent 7f482c9b9a0cdd1130670ee18b70fbcbb45fca97
Author: Lukas Henkel <lh@entf.net>
Date:   Fri,  1 Dec 2023 08:38:28 +0100

Optimize

Diffstat:
Mbuild.lisp | 4++++
Msrc/day-1.lisp | 60+++++++++++++++++++++++++++++++++++++-----------------------
Msrc/utils.lisp | 2++
3 files changed, 43 insertions(+), 23 deletions(-)

diff --git a/build.lisp b/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 diff --git a/src/day-1.lisp b/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)))) diff --git a/src/utils.lisp b/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))