id
int64 0
45.1k
| file_name
stringlengths 4
68
| file_path
stringlengths 14
193
| content
stringlengths 32
9.62M
| size
int64 32
9.62M
| language
stringclasses 1
value | extension
stringclasses 6
values | total_lines
int64 1
136k
| avg_line_length
float64 3
903k
| max_line_length
int64 3
4.51M
| alphanum_fraction
float64 0
1
| repo_name
stringclasses 779
values | repo_stars
int64 0
882
| repo_forks
int64 0
108
| repo_open_issues
int64 0
90
| repo_license
stringclasses 8
values | repo_extraction_date
stringclasses 146
values | sha
stringlengths 64
64
| __index_level_0__
int64 0
45.1k
| exdup_ids_cmlisp_stkv2
sequencelengths 1
47
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
41,914 | utility.lisp | staudtlex_calcal-lisp/utility.lisp | ;;;; -*- mode: Lisp; indent-tabs-mode: nil; -*-
;;;; Copyright (C) 2022 Alexander Staudt
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;;;
;;;; This file provides utility functions for easier conversion from ISO 8601
;;;; dates to absolute (fixed) dates, and conversion from absolute dates to
;;;; various calendars
(in-package :calcal)
;;;
;;; Helper functions
;;;
(defun mdy-from-ymd (iso-8601-date)
;; Convert a YMD (ISO 8601) date to a MDY date
(list (nth 1 iso-8601-date) (nth 2 iso-8601-date) (nth 0 iso-8601-date)))
(defun seq (from to)
;; Generate a sequence of consecutive integers
(let ((length (1+ (- to from))))
(loop :for n :below length :collect (+ from n))))
;;;
;;; Data and wrapper functions for easier date conversion
;;;
(defparameter calendars
;; List of calendar names
'("gregorian"
"iso"
"julian"
"islamic"
"hebrew"
"mayan-long-count"
"mayan-haab"
"mayan-tzolkin"
"french"
"old-hindu-solar"
"old-hindu-lunar"))
(defstruct date
;; A structure with date components and calendar information.
(calendar nil)
(components nil))
(defun get-function-with-name (string)
(do-symbols (s 'calcal)
(if (and (search string (symbol-name s) :test #'string-equal) (fboundp s))
(return s))))
(defun make-function-map (calendar-list)
(let* ((function-name-strings
(mapcar #'(lambda (c) (concatenate 'string c "-from-absolute"))
calendar-list))
(function-names
(mapcar #'get-function-with-name
function-name-strings)))
(reverse (pairlis calendar-list function-names))))
(defparameter *function-map* (make-function-map calendars))
(defun from-absolute (absolute-date to-calendar)
;; Convert an absolute (fixed) date to a date representation specified by to-calendar.
(let ((f (cdr (assoc to-calendar *function-map* :test #'string-equal))))
(if (not (equal f nil))
(make-date :calendar to-calendar
:components (funcall f absolute-date))
(make-date))))
(defun absolute-from-ymd (iso-8601-date)
;; Get the absolute (fixed) date from an ISO 8601 date (of type (list year month day)).
(absolute-from-gregorian (mdy-from-ymd iso-8601-date)))
(defun compute-dates (iso-8601-date calendar-list)
;; Compute all available calendar representations of a given absolute (fixed) date.
(let ((absolute-date (absolute-from-ymd iso-8601-date)))
(mapcar #'(lambda (cal) (from-absolute absolute-date cal)) calendar-list)))
;;;
;;; Data and functions for date formatting
;;;
(defparameter gregorian-months
(reverse (pairlis
(seq 1 12)
(list
"January" "February" "March"
"April" "May" "June"
"July" "August" "September"
"October" "November" "December"))))
(defparameter islamic-months
(reverse (pairlis
(seq 1 12)
(list
"Muharram" "Safar" "Rabi I"
"Rabi II" "Jumada I" "Jumada II"
"Rajab" "Sha' Ban" "Ramadan"
"Shawwal" "Dhu al-Qada" "Dhu al-Hijjah"))))
(defparameter hebrew-months
(reverse (pairlis
(seq 1 12)
(list
"Nisan" "Iyyar" "Sivan"
"Tammuz" "Av" "Elul"
"Tishri" "Heshvan" "Kislev"
"Teveth" "Shevat" "Adar"))))
(defparameter haab-months
(reverse (pairlis
(seq 1 18)
(list
"Pop" "Uo" "Zip"
"Zotz" "Tzec" "Xul"
"Yaxkin" "Mol" "Chen"
"Yax" "Zac" "Ceh"
"Mac" "Kankin" "Muan"
"Pax" "Kayab" "Cumku"))))
(defparameter tzolkin-names
(reverse (pairlis
(seq 1 20)
(list
"Imix" "Ik" "Akbal" "Kan"
"Chiccan" "Cimi" "Manik" "Lamat"
"Muluc" "Oc" "Chuen" "Eb"
"Ben" "Ix" "Men" "Cib"
"Caban" "Etznab" "Cauac" "Ahau"))))
(defparameter french-months
(reverse (pairlis
(seq 1 13)
(list
"Vendémiaire" "Brumaire" "Frimaire"
"Nivôse" "Pluviôse" "Ventôse"
"Germinal" "Floréal" "Prairial"
"Messidor" "Thermidor" "Fructidor"
"Sansculottides"))))
(defparameter old-hindu-solar-months
(reverse (pairlis
(seq 1 12)
(list
"Mesha" "Vrshabha" "Mithuna"
"Karka" "Simha" "Kanya"
"Tula" "Vrischika" "Dhanus"
"Makara" "Kumbha" "Mina"))))
(defparameter old-hindu-lunar-months
(reverse (pairlis
(seq 1 12)
(list
"Chaitra" "Vaisakha" "Jyaishtha"
"Ashadha" "Sravana" "Bhadrapada"
"Asvina" "Kartika" "Margasira"
"Pausha" "Magha" "Phalguna"))))
(defun format-gregorian (date)
(let* ((components (date-components date))
(day (nth 1 components))
(month (nth 0 components))
(month-name (cdr (assoc month gregorian-months)))
(year (nth 2 components)))
(format nil "~d ~a ~d" day month-name year)))
(defun format-julian (date)
(format-gregorian date))
(defun format-iso (date)
(let* ((components (date-components date))
(day-of-week (nth 1 components))
(week (nth 0 components))
(year (nth 2 components)))
(format nil "~d-W~2,'0d-~d" year week day-of-week)))
(defun format-islamic (date)
(let* ((components (date-components date))
(day (nth 1 components))
(month (nth 0 components))
(month-name (cdr (assoc month islamic-months)))
(year (nth 2 components)))
(format nil "~d ~a ~d" day month-name year)))
(defun format-hebrew (date)
(let* ((components (date-components date))
(day (nth 1 components))
(month (nth 0 components))
(year (nth 2 components))
(month-name
(cond
((and (equalp month 12) (hebrew-leap-year year)) "Adar I")
((and (equalp month 13) (hebrew-leap-year year)) "Adar II")
(t (cdr (assoc month hebrew-months))))))
(format nil "~d ~a ~d" day month-name year)))
(defun format-mayan-long-count (date)
(let* ((components (date-components date))
(baktun (nth 0 components))
(katun (nth 1 components))
(tun (nth 2 components))
(uinal (nth 3 components))
(kin (nth 4 components)))
(format nil "~d.~d.~d.~d.~d" baktun katun tun uinal kin)))
(defun format-mayan-haab (date)
(let* ((components (date-components date))
(day (nth 0 components))
(month (nth 1 components))
(month-name (cdr (assoc month haab-months))))
(format nil "~d ~a" day month-name)))
(defun format-mayan-tzolkin (date)
(let* ((components (date-components date))
(day (nth 0 components))
(tzolkin-name (nth 1 components))
(name (cdr (assoc tzolkin-name tzolkin-names))))
(format nil "~d ~a" day name)))
(defun format-french (date)
(let* ((components (date-components date))
(day (nth 1 components))
(month (nth 0 components))
(month-name (cdr (assoc month french-months)))
(year (nth 2 components)))
(format nil "~d ~a an ~d" day month-name year)))
(defun format-old-hindu-solar (date)
(let* ((components (date-components date))
(day (nth 1 components))
(month (nth 0 components))
(month-name (cdr (assoc month old-hindu-solar-months)))
(year (nth 2 components)))
(format nil "~d ~a ~d" day month-name year)))
(defun format-old-hindu-lunar (date)
(let* ((components (date-components date))
(day (nth 2 components))
(month (nth 0 components))
(month-name (cdr (assoc month old-hindu-lunar-months)))
(year (nth 3 components)))
(format nil "~d ~a ~d" day month-name year)))
(defun format-date (date)
(let ((f (get-function-with-name
(concatenate 'string "format-" (date-calendar date)))))
(funcall f date)))
| 8,670 | Common Lisp | .lisp | 226 | 31.10177 | 89 | 0.60446 | staudtlex/calcal-lisp | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | 1888a3d3342f496555ff15c74a9edc76ac04d24c8e05a5c9345e20091845109e | 41,914 | [
-1
] |
41,915 | calcal.asd | staudtlex_calcal-lisp/calcal.asd | ;;;; -*- mode: Lisp; indent-tabs-mode: nil; -*-
;;;; Copyright (C) 2022 Alexander Staudt
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;;;
(defsystem "calcal"
:description "calcal: Compute and convert dates from 11 calendars on the command line."
:version "0.1.0"
:author "Alexander Staudt"
:licence "GPLv3+"
:components ((:file "package")
(:file "calendar" :depends-on ("package"))
(:file "utility" :depends-on ("calendar"))
(:file "cli" :depends-on ("utility")))
:build-operation "program-op"
:build-pathname "calcal"
:entry-point "calcal::toplevel-main"
:depends-on (#:asdf))
| 1,277 | Common Lisp | .asd | 29 | 40.758621 | 89 | 0.682692 | staudtlex/calcal-lisp | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | eec7eef3a15acd7f46fb3e4d7f98d75eb0acb6f933ded3496852b981103f0812 | 41,915 | [
-1
] |
41,919 | Makefile | staudtlex_calcal-lisp/Makefile | # This is the Makefile of calcal-lisp
LISP=sbcl --no-userinit --no-sysinit
all: asd
asd: calcal.asd calendar.lisp
@LISP_BIN=`echo $(LISP) | \
sed -E 's/(.*\/)?(.*)/\2/g' | sed -E 's/( -+.*)//g'`; \
if [ "$$LISP_BIN" = "clisp" ]; then \
$(LISP) \
-x "(require 'asdf)" \
-x "(setf asdf:*central-registry* (list (uiop:getcwd)))" \
-x "(if (asdf:make :calcal) (quit))"; \
else \
$(LISP) \
--eval "(require 'asdf)" \
--eval "(setf asdf:*central-registry* (list (uiop:getcwd)))" \
--eval "(if (asdf:make :calcal) (quit))"; \
fi
calendar.lisp: calendar.l modify.patch
cp calendar.l calendar.lisp && \
patch calendar.lisp < modify.patch
test: calcal
./$<
clean-sources:
rm -rf calendar.lisp
clean-binary:
rm -rf calcal
clean: clean-sources clean-binary | 784 | Common Lisp | .l | 27 | 26.592593 | 65 | 0.621838 | staudtlex/calcal-lisp | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | 39941c61be21f0fb5f44ffd3a5f2907de4b0d0bd72ead3877981ef1221cf824d | 41,919 | [
-1
] |
41,921 | calendar.l | staudtlex_calcal-lisp/calendar.l | ;; The following Lisp code is from ``Calendrical
;; Calculations'' by Nachum Dershowitz and Edward
;; M. Reingold, Software---Practice & Experience, vol. 20,
;; no. 9 (September, 1990), pp. 899--928 and from
;; ``Calendrical Calculations, II: Three Historical
;; Calendars'' by Edward M. Reingold, Nachum Dershowitz,
;; and Stewart M. Clamen, Software---Practice & Experience,
;; vol. 23, no. 4 (April, 1993), pp. 383--404.
;; This code is in the public domain, but any use of it
;; should publically acknowledge its source.
(defun quotient (m n)
(floor (/ m n)))
(defun extract-month (date)
;; Month field of $date$ = (month day year).
(first date))
(defun extract-day (date)
;; Day field of $date$ = (month day year).
(second date))
(defun extract-year (date)
;; Year field of $date$ = (month day year).
(third date))
(defmacro sum (expression index initial condition)
;; Sum $expression$ for $index$ = $initial$ and successive integers,
;; as long as $condition$ holds.
(let* ((temp (gensym)))
`(do ((,temp 0 (+ ,temp ,expression))
(,index ,initial (1+ ,index)))
((not ,condition) ,temp))))
(defun last-day-of-gregorian-month (month year)
;; Last day in Gregorian $month$ during $year$.
(if ;; February in a leap year
(and (= month 2)
(= (mod year 4) 0)
(not (member (mod year 400) (list 100 200 300))))
;; Then return
29
;; Else return
(nth (1- month)
(list 31 28 31 30 31 30 31 31 30 31 30 31))))
(defun absolute-from-gregorian (date)
;; Absolute date equivalent to the Gregorian $date$.
(let* ((month (extract-month date))
(year (extract-year date)))
;; Return
(+ (extract-day date) ;; Days so far this month.
(sum ;; Days in prior months this year.
(last-day-of-gregorian-month m year) m 1 (< m month))
(* 365 (1- year)) ;; Days in prior years.
(quotient (1- year) 4);; Julian leap days in prior years...
(- ;; ...minus prior century years...
(quotient (1- year) 100))
(quotient ;; ...plus prior years divisible...
(1- year) 400)))) ;; ...by 400.
(defun gregorian-from-absolute (date)
;; Gregorian (month day year) corresponding absolute $date$.
(let* ((approx (quotient date 366));; Approximation from below.
(year ;; Search forward from the approximation.
(+ approx
(sum 1 y approx
(>= date
(absolute-from-gregorian
(list 1 1 (1+ y)))))))
(month ;; Search forward from January.
(1+ (sum 1 m 1
(> date
(absolute-from-gregorian
(list m
(last-day-of-gregorian-month m year)
year))))))
(day ;; Calculate the day by subtraction.
(- date (1- (absolute-from-gregorian
(list month 1 year))))))
;; Return
(list month day year)))
(defun Kday-on-or-before (date k)
;; Absolute date of the $k$day on or before $date$.
;; $k=0$ means Sunday, $k=1$ means Monday, and so on.
(- date (mod (- date k) 7)))
(defun absolute-from-iso (date)
;; Absolute date equivalent to ISO $date$ = (week day year).
(let* ((week (first date))
(day (second date))
(year (third date)))
;; Return
(+ (Kday-on-or-before
(absolute-from-gregorian (list 1 4 year))
1) ;; Days in prior years.
(* 7 (1- week)) ;; Days in prior weeks this year.
(1- day)))) ;; Prior days this week.
(defun iso-from-absolute (date)
;; ISO (week day year) corresponding to the absolute $date$.
(let* ((approx
(extract-year (gregorian-from-absolute (- date 3))))
(year (if (>= date
(absolute-from-iso (list 1 1 (1+ approx))))
;; Then
(1+ approx)
;; Else
approx))
(week (1+ (quotient
(- date (absolute-from-iso (list 1 1 year)))
7)))
(day (if (= 0 (mod date 7))
;; Then
7
;; Else
(mod date 7))))
;; Return
(list week day year)))
(defun last-day-of-julian-month (month year)
;; Last day in Julian $month$ during $year$.
(if ;; February in a leap year
(and (= month 2) (= (mod year 4) 0))
;; Then return
29
;; Else return
(nth (1- month) (list 31 28 31 30 31 30 31 31 30 31 30 31))))
(defun absolute-from-julian (date)
;; Absolute date equivalent to Julian $date$.
(let* ((month (extract-month date))
(year (extract-year date)))
;; Return
(+ (extract-day date) ;; Days so far this month.
(sum ;; Days in prior months this year.
(last-day-of-julian-month m year) m 1 (< m month))
(* 365 (1- year)) ;; Days in prior years.
(quotient (1- year) 4);; Leap days in prior years.
-2))) ;; Days elapsed before absolute date 1.
(defun julian-from-absolute (date)
;; Julian (month day year) corresponding to absolute $date$.
(let*
((approx ;; Approximation from below.
(quotient (+ date 2) 366))
(year ;; Search forward from the approximation.
(+ approx
(sum 1 y approx
(>= date
(absolute-from-julian (list 1 1 (1+ y)))))))
(month ;; Search forward from January.
(1+ (sum 1 m 1
(> date
(absolute-from-julian
(list m
(last-day-of-julian-month m year)
year))))))
(day ;; Calculate the day by subtraction.
(- date (1- (absolute-from-julian (list month 1 year))))))
;; Return
(list month day year)))
(defun islamic-leap-year (year)
;; True if $year$ is an Islamic leap year.
(< (mod (+ 14 (* 11 year)) 30) 11))
(defun last-day-of-islamic-month (month year)
;; Last day in $month$ during $year$ on the Islamic calendar.
(if (or (oddp month)
(and (= month 12) (islamic-leap-year year)))
;; Then return
30
;; Else return
29))
(defun absolute-from-islamic (date)
;; Absolute date equivalent to Islamic $date$.
(let* ((month (extract-month date))
(year (extract-year date)))
(+ (extract-day date) ;; Days so far this month.
(* 29 (1- month)) ;; Days so far...
(quotient month 2) ;; ...this year.
(* (1- year) 354) ;; Non-leap days in prior years.
(quotient ;; Leap days in prior years.
(+ 3 (* 11 year)) 30)
227014))) ;; Days before start of calendar.
(defun islamic-from-absolute (date)
;; Islamic date (month day year) corresponding to absolute $date$.
(if ;; Pre-Islamic date.
(<= date 227014)
;; Then return
(list 0 0 0)
;; Else
(let* ((approx ;; Approximation from below.
(quotient (- date 227014) 355))
(year ;; Search forward from the approximation.
(+ approx
(sum 1 y approx
(>= date
(absolute-from-islamic
(list 1 1 (1+ y)))))))
(month ;; Search forward from Muharram.
(1+ (sum 1 m 1
(> date
(absolute-from-islamic
(list m
(last-day-of-islamic-month m year)
year))))))
(day ;; Calculate the day by subtraction.
(- date (1- (absolute-from-islamic
(list month 1 year))))))
;; Return
(list month day year))))
(defun hebrew-leap-year (year)
;; True if $year$ is a leap year.
(< (mod (1+ (* 7 year)) 19) 7))
(defun last-month-of-hebrew-year (year)
;; Last month of Hebrew $year$.
(if (hebrew-leap-year year)
;; Then return
13
;; Else return
12))
(defun last-day-of-hebrew-month (month year)
;; Last day of $month$ in Hebrew $year$.
(if (or (member month (list 2 4 6 10 13))
(and (= month 12) (not (hebrew-leap-year year)))
(and (= month 8) (not (long-heshvan year)))
(and (= month 9) (short-kislev year)))
;; Then return
29
;; Else return
30))
(defun hebrew-calendar-elapsed-days (year)
;; Number of days elapsed from the Sunday prior to the start of the
;; Hebrew calendar to the mean conjunction of Tishri of Hebrew $year$.
(let*
((months-elapsed
(+
(* 235 ;; Months in complete cycles so far.
(quotient (1- year) 19))
(* 12 ;; Regular months in this cycle.
(mod (1- year) 19))
(quotient ;; Leap months this cycle
(1+ (* 7 (mod (1- year) 19)))
19)))
;; (parts-elapsed (+ 5604 (* 13753 months-elapsed)))
;; (day ;; Conjunction day
;; (+ 1 (* 29 months-elapsed) (quotient parts-elapsed 25920)))
;; (parts (mod parts-elapsed 25920)) ;; Conjunction parts
;;
;; The above lines of code are correct, but can have intermediate
;; values that are too large for a 32-bit machine. The following
;; lines of code that replace them are equivalent, but avoid the
;; problem.
;;
(parts-elapsed
(+ 204
(* 793 (mod months-elapsed 1080))))
(hours-elapsed
(+ 5
(* 12 months-elapsed)
(* 793 (quotient months-elapsed 1080))
(quotient parts-elapsed 1080)))
(day ;; Conjunction day
(+ 1
(* 29 months-elapsed)
(quotient hours-elapsed 24)))
(parts ;; Conjunction parts
(+ (* 1080 (mod hours-elapsed 24))
(mod parts-elapsed 1080)))
(alternative-day
(if (or
(>= parts 19440) ;; If new moon is at or after midday,
(and
(= (mod day 7) 2);; ...or is on a Tuesday...
(>= parts 9924) ;; at 9 hours, 204 parts or later...
(not (hebrew-leap-year year)));; of a common year,
(and
(= (mod day 7) 1);; ...or is on a Monday at...
(>= parts 16789) ;; 15 hours, 589 parts or later...
(hebrew-leap-year;; at the end of a leap year
(1- year))))
;; Then postpone Rosh HaShanah one day
(1+ day)
;; Else
day)))
(if ;; If Rosh HaShanah would occur on Sunday, Wednesday,
;; or Friday
(member (mod alternative-day 7) (list 0 3 5))
;; Then postpone it one (more) day and return
(1+ alternative-day)
;; Else return
alternative-day)))
(defun days-in-hebrew-year (year)
;; Number of days in Hebrew $year$.
(- (hebrew-calendar-elapsed-days (1+ year))
(hebrew-calendar-elapsed-days year)))
(defun long-heshvan (year)
;; True if Heshvan is long in Hebrew $year$.
(= (mod (days-in-hebrew-year year) 10) 5))
(defun short-kislev (year)
;; True if Kislev is short in Hebrew $year$.
(= (mod (days-in-hebrew-year year) 10) 3))
(defun absolute-from-hebrew (date)
;; Absolute date of Hebrew $date$.
(let* ((month (extract-month date))
(day (extract-day date))
(year (extract-year date)))
;; Return
(+ day ;; Days so far this month.
(if ;; before Tishri
(< month 7)
;; Then add days in prior months this year before and
;; after Nisan.
(+ (sum (last-day-of-hebrew-month m year)
m 7 (<= m (last-month-of-hebrew-year year)))
(sum (last-day-of-hebrew-month m year)
m 1 (< m month)))
;; Else add days in prior months this year
(sum (last-day-of-hebrew-month m year) m 7 (< m month)))
(hebrew-calendar-elapsed-days year);; Days in prior years.
-1373429))) ;; Days elapsed before absolute date 1.
(defun hebrew-from-absolute (date)
;; Hebrew (month day year) corresponding to absolute $date$.
(let* ((approx ;; Approximation from below.
(quotient (+ date 1373429) 366))
(year ;; Search forward from the approximation.
(+ approx (sum 1 y approx
(>= date
(absolute-from-hebrew
(list 7 1 (1+ y)))))))
(start ;; Starting month for search for month.
(if (< date (absolute-from-hebrew (list 1 1 year)))
;; Then start at Tishri
7
;; Else start at Nisan
1))
(month ;; Search forward from either Tishri or Nisan.
(+ start
(sum 1 m start
(> date
(absolute-from-hebrew
(list m
(last-day-of-hebrew-month m year)
year))))))
(day ;; Calculate the day by subtraction.
(- date (1- (absolute-from-hebrew (list month 1 year))))))
;; Return
(list month day year)))
(defun independence-day (year)
;; Absolute date of American Independence Day in Gregorian $year$.
(absolute-from-gregorian (list 7 4 year)))
(defun Nth-Kday (n k month year)
;; Absolute date of the $n$th $k$day in Gregorian $month$, $year$.
;; If $n$<0, the $n$th $k$day from the end of month is returned
;; (that is, -1 is the last $k$day, -2 is the penultimate $k$day,
;; and so on). $k=0$ means Sunday, $k=1$ means Monday, and so on.
(if (> n 0)
;; Then return
(+ (Kday-on-or-before ;; First $k$day in month.
(absolute-from-gregorian
(list month 7 year)) k)
(* 7 (1- n))) ;; Advance $n-1$ $k$days.
;; Else return
(+ (Kday-on-or-before ;; Last $k$day in month.
(absolute-from-gregorian
(list month
(last-day-of-gregorian-month month year)
year))
k)
(* 7 (1+ n))))) ;; Go back $-n-1$ $k$days.
(defun labor-day (year)
;; Absolute date of American Labor Day in Gregorian $year$.
(Nth-Kday 1 1 9 year));; First Monday in September.
(defun memorial-day (year)
;; Absolute date of American Memorial Day in Gregorian $year$.
(Nth-Kday -1 1 5 year));; Last Monday in May.
(defun daylight-savings-start (year)
;; Absolute date of the start of American daylight savings time
;; in Gregorian $year$.
(Nth-Kday 1 0 4 year));; First Sunday in April.
(defun daylight-savings-end (year)
;; Absolute date of the end of American daylight savings time
;; in Gregorian $year$.
(Nth-Kday -1 0 10 year));; Last Sunday in October.
(defun christmas (year)
;; Absolute date of Christmas in Gregorian $year$.
(absolute-from-gregorian (list 12 25 year)))
(defun advent (year)
;; Absolute date of Advent in Gregorian $year$.
(Kday-on-or-before (absolute-from-gregorian (list 12 3 year)) 0))
(defun epiphany (year)
;; Absolute date of Epiphany in Gregorian $year$.
(+ 12 (christmas year)))
(defun eastern-orthodox-christmas (year)
;; List of zero or one absolute dates of Eastern Orthodox
;; Christmas in Gregorian $year$.
(let* ((jan1 (absolute-from-gregorian (list 1 1 year)))
(dec31 (absolute-from-gregorian (list 12 31 year)))
(y (extract-year (julian-from-absolute jan1)))
(c1 (absolute-from-julian (list 12 25 y)))
(c2 (absolute-from-julian (list 12 25 (1+ y)))))
(append
(if ;; c1 occurs in current year
(<= jan1 c1 dec31)
;; Then that date; otherwise, none
(list c1) nil)
(if ;; c2 occurs in current year
(<= jan1 c2 dec31)
;; Then that date; otherwise, none
(list c2) nil))))
(defun nicaean-rule-easter (year)
;; Absolute date of Easter in Julian $year$, according to the rule
;; of the Council of Nicaea.
(let* ((shifted-epact ;; Age of moon for April 5.
(mod (+ 14
(* 11 (mod year 19)))
30))
(paschal-moon ;; Day after full moon on or after March 21.
(- (absolute-from-julian (list 4 19 year))
shifted-epact)))
;; Return the Sunday following the Paschal moon
(Kday-on-or-before (+ paschal-moon 7) 0)))
(defun easter (year)
;; Absolute date of Easter in Gregorian $year$.
(let* ((century (1+ (quotient year 100)))
(shifted-epact ;; Age of moon for April 5...
(mod
(+ 14 (* 11 (mod year 19));; ...by Nicaean rule
(- ;; ...corrected for the Gregorian century rule
(quotient (* 3 century) 4))
(quotient;; ...corrected for Metonic cycle inaccuracy.
(+ 5 (* 8 century)) 25)
(* 30 century));; Keeps value positive.
30))
(adjusted-epact ;; Adjust for 29.5 day month.
(if (or (= shifted-epact 0)
(and (= shifted-epact 1) (< 10 (mod year 19))))
;; Then
(1+ shifted-epact)
;; Else
shifted-epact))
(paschal-moon;; Day after full moon on or after March 21.
(- (absolute-from-gregorian (list 4 19 year))
adjusted-epact)))
;; Return the Sunday following the Paschal moon.
(Kday-on-or-before (+ paschal-moon 7) 0)))
(defun pentecost (year)
;; Absolute date of Pentecost in Gregorian $year$.
(+ 49 (easter year)))
(defun islamic-date (month day year)
;; List of the absolute dates of Islamic $month$, $day$
;; that occur in Gregorian $year$.
(let* ((jan1 (absolute-from-gregorian (list 1 1 year)))
(dec31 (absolute-from-gregorian (list 12 31 year)))
(y (extract-year (islamic-from-absolute jan1)))
;; The possible occurrences in one year are
(date1 (absolute-from-islamic (list month day y)))
(date2 (absolute-from-islamic (list month day (1+ y))))
(date3 (absolute-from-islamic (list month day (+ 2 y)))))
;; Combine in one list those that occur in current year
(append
(if (<= jan1 date1 dec31)
(list date1) nil)
(if (<= jan1 date2 dec31)
(list date2) nil)
(if (<= jan1 date3 dec31)
(list date3) nil))))
(defun mulad-al-nabi (year)
;; List of absolute dates of Mulad-al-Nabi occurring in
;; Gregorian $year$.
(islamic-date 3 12 year))
(defun yom-kippur (year)
;; Absolute date of Yom Kippur occurring in Gregorian $year$.
(absolute-from-hebrew (list 7 10 (+ year 3761))))
(defun passover (year)
;; Absolute date of Passover occurring in Gregorian $year$.
(absolute-from-hebrew (list 1 15 (+ year 3760))))
(defun purim (year)
;; Absolute date of Purim occurring in Gregorian $year$.
(absolute-from-hebrew
(list
(last-month-of-hebrew-year (+ year 3760));; Adar or Adar II
14
(+ year 3760))))
(defun ta-anit-esther (year)
;; Absolute date of Ta'anit Esther occurring in Gregorian $year$.
(let* ((purim-date (purim year)))
(if ;; Purim is on Sunday
(= (mod purim-date 7) 0)
;; Then return prior Thursday
(- purim-date 3)
;; Else return previous day
(1- purim-date))))
(defun tisha-b-av (year)
;; Absolute date of Tisha B'Av occurring in Gregorian $year$.
(let* ((ninth-of-av
(absolute-from-hebrew (list 5 9 (+ year 3760)))))
(if ;; Ninth of Av is Saturday
(= (mod ninth-of-av 7) 6)
;; Then return the next day
(1+ ninth-of-av)
;; Else return
ninth-of-av)))
(defun hebrew-birthday (birthdate year)
;; Absolute date of the anniversary of Hebrew $birthdate$
;; occurring in Hebrew $year$.
(let* ((birth-day (extract-day birthdate))
(birth-month (extract-month birthdate))
(birth-year (extract-year birthdate)))
(if ;; It's Adar in a normal year or Adar II in a leap year,
(= birth-month (last-month-of-hebrew-year birth-year))
;; Then use the same day in last month of $year$.
(absolute-from-hebrew
(list (last-month-of-hebrew-year year) birth-day year))
;; Else use the normal anniversary of the birth date,
;; or the corresponding day in years without that date
(absolute-from-hebrew (list birth-month birth-day year)))))
(defun yahrzeit (death-date year)
;; Absolute date of the anniversary of Hebrew $death$-$date$
;; occurring in Hebrew $year$.
(let* ((death-day (extract-day death-date))
(death-month (extract-month death-date))
(death-year (extract-year death-date)))
(cond
;; If it's Heshvan 30 it depends on the first anniversary; if
;; that was not Heshvan 30, use the day before Kislev 1.
((and (= death-month 8)
(= death-day 30)
(not (long-heshvan (1+ death-year))))
(1- (absolute-from-hebrew (list 9 1 year))))
;; If it's Kislev 30 it depends on the first anniversary; if
;; that was not Kislev 30, use the day before Teveth 1.
((and (= death-month 9)
(= death-day 30)
(short-kislev (1+ death-year)))
(1- (absolute-from-hebrew (list 10 1 year))))
;; If it's Adar II, use the same day in last month of
;; year (Adar or Adar II).
((= death-month 13)
(absolute-from-hebrew
(list (last-month-of-hebrew-year year) death-day year)))
;; If it's the 30th in Adar I and $year$ is not a leap year
;; (so Adar has only 29 days), use the last day in Shevat.
((and (= death-day 30)
(= death-month 12)
(not (hebrew-leap-year year)));; Corrected 5/19/93 by EMR
(absolute-from-hebrew (list 11 30 year)))
;; In all other cases, use the normal anniversary of the
;; date of death.
(t (absolute-from-hebrew
(list death-month death-day year))))))
(defconstant mayan-days-before-absolute-zero
;; Number of days of the Mayan calendar epoch before absolute day 0,
;; according to the Goodman-Martinez-Thompson correlation.
1137140)
(defun absolute-from-mayan-long-count (count)
;; Absolute date corresponding to the Mayan long count $count$,
;; which is a list ($baktun$ $katun$ $tun$ $uinal$ $kin$).
(+ (* (first count) 144000);; Baktun.
(* (second count) 7200) ;; Katun.
(* (third count) 360) ;; Tun.
(* (fourth count) 20) ;; Uinal.
(fifth count) ;; Kin (days).
(- ;; Days before absolute date 0.
mayan-days-before-absolute-zero)))
(defun mayan-long-count-from-absolute (date)
;; Mayan long count date of absolute date $date$.
(let* ((long-count (+ date mayan-days-before-absolute-zero))
(baktun (quotient long-count 144000))
(day-of-baktun (mod long-count 144000))
(katun (quotient day-of-baktun 7200))
(day-of-katun (mod day-of-baktun 7200))
(tun (quotient day-of-katun 360))
(day-of-tun (mod day-of-katun 360))
(uinal (quotient day-of-tun 20))
(kin (mod day-of-tun 20)))
(list baktun katun tun uinal kin)))
(defun quotient (m n)
(floor m n))
(defconstant mayan-haab-at-epoch '(8 18))
(defun mayan-haab-from-absolute (date)
;; Mayan haab date of absolute date $date$.
(let* ((long-count (+ date mayan-days-before-absolute-zero))
(day-of-haab
(mod (+ long-count
(first mayan-haab-at-epoch)
(* 20 (1- (second mayan-haab-at-epoch))))
365))
(day (mod day-of-haab 20))
(month (1+ (quotient day-of-haab 20))))
(list day month)))
(defun mayan-haab-difference (date1 date2)
;; Number of days from Mayan haab date $date1$ to the next
;; occurrence of Mayan haab date $date2$.
(mod (+ (* 20 (- (second date2) (second date1)))
(- (first date2) (first date1)))
365))
(defun mayan-haab-on-or-before (haab date)
;; Absolute date of latest date on or before absolute date $date$
;; that is Mayan haab date $haab$.
(- date
(mod (- date
(mayan-haab-difference
(mayan-haab-from-absolute 0) haab))
365)))
(defun adjusted-mod (m n)
;; Positive remainder of $m/n$ with $n$ instead of 0.
(1+ (mod (1- m) n)))
(defconstant mayan-tzolkin-at-epoch '(4 20))
(defun mayan-tzolkin-from-absolute (date)
;; Mayan tzolkin date of absolute date $date$.
(let* ((long-count (+ date mayan-days-before-absolute-zero))
(number
(adjusted-mod (+ long-count
(first mayan-tzolkin-at-epoch))
13))
(name
(adjusted-mod (+ long-count
(second mayan-tzolkin-at-epoch))
20)))
(list number name)))
(defun mayan-tzolkin-difference (date1 date2)
;; Number of days from Mayan tzolkin date $date1$ to the next
;; occurrence of Mayan tzolkin date $date2$.
(let* ((number-difference (- (first date2) (first date1)))
(name-difference (- (second date2) (second date1))))
(mod (+ number-difference
(* 13 (mod (* 3 (- number-difference name-difference))
20)))
260)))
(defun mayan-tzolkin-on-or-before (tzolkin date)
;; Absolute date of latest date on or before absolute date $date$
;; that is Mayan tzolkin date $tzolkin$.
(- date
(mod (- date (mayan-tzolkin-difference
(mayan-tzolkin-from-absolute 0)
tzolkin))
260)))
(defun mayan-haab-tzolkin-on-or-before (haab tzolkin date)
;; Absolute date of latest date on or before $date$ that is Mayan
;; haab date $haab$ and tzolkin date $tzolkin$; returns nil if such
;; a haab-tzolkin combination is impossible.
(let* ((haab-difference
(mayan-haab-difference (mayan-haab-from-absolute 0)
haab))
(tzolkin-difference
(mayan-tzolkin-difference (mayan-tzolkin-from-absolute 0)
tzolkin))
(difference (- tzolkin-difference haab-difference)))
(if (= (mod difference 5) 0)
(- date
(mod (- date
(+ haab-difference (* 365 difference)))
18980))
nil)));; haab-tzolkin combination is impossible.
(defun french-last-day-of-month (month year)
;; Last day of {\em month, year} on the French Revolutionary calendar.
(if (< month 13)
30
(if (french-leap-year year)
6
5)))
(defun french-leap-year (year)
;; True if {\em year} is a leap year on the French Revolutionary calendar.
(or (member year '(3 7 11));; Actual.
(member year '(15 20)) ;; Anticipated.
(and (> year 20) ;; Proposed.
(= 0 (mod year 4))
(not (member (mod year 400) '(100 200 300)))
(not (= 0 (mod year 4000))))))
(defun absolute-from-french (date)
;; Absolute date of French Revolutionary {\em date}.
(let* ((month (first date))
(day (second date))
(year (third date)))
(+ 654414;; Days before start of calendar.
(* 365 (1- year));; Days in prior years.
;; Leap days in prior years.
(if (< year 20)
(quotient year 4);; Actual and anticipated practice,
;; that is, years 3, 7, 11, and 15.
;; Proposed rule--there were 4 leap years before year 20.
(+ (quotient (1- year) 4)
(- (quotient (1- year) 100))
(quotient (1- year) 400)
(- (quotient (1- year) 4000))))
(* 30 (1- month));; Days in prior months this year.
day)));; Days so far this month.
(defun french-from-absolute (date)
;; French Revolutionary date (month day year) of absolute {\em date};
;; returns nil if $date$ is before the French Revolution.
(if (< date 654415)
nil;; pre-French Revolutionary date.
(let* ((approx ;; Approximate year from below.
(quotient (- date 654414) 366))
(year ;; Search forward from the approximation.
(+ approx
(sum 1 y approx
(>= date
(absolute-from-french (list 1 1 (1+ y)))))))
(month ;; Search forward from Vendemiaire.
(1+ (sum 1 m 1
(> date
(absolute-from-french
(list m
(french-last-day-of-month m year)
year))))))
(day ;; Calculate the day by subtraction.
(- date
(1- (absolute-from-french (list month 1 year))))))
(list month day year))))
(defconstant solar-sidereal-year (+ 365 279457/1080000))
(defconstant solar-month (/ solar-sidereal-year 12))
(defconstant lunar-sidereal-month (+ 27 4644439/14438334))
(defconstant lunar-synodic-month (+ 29 7087771/13358334))
(defun solar-longitude (days)
;; Mean sidereal longitude of the sun, in degrees,
;; at date and fraction of day $days$.
(* (mod (/ days solar-sidereal-year) 1) 360))
(defun zodiac (days)
;; Zodiacal sign of the sun, as integer in range 1..12,
;; for date and fraction of day $days$.
(1+ (quotient (solar-longitude days) 30)))
(defun old-hindu-solar-from-absolute (date)
;; Hindu solar month, day, and year of absolute date $date$.
(let* ((hdate (+ date 1132959 1/4));; Sunrise on Hindu date.
(year (quotient hdate solar-sidereal-year))
(month (zodiac hdate))
(day (1+ (floor (mod hdate solar-month)))))
(list month day year)))
(defun absolute-from-old-hindu-solar (date)
;; Absolute date corresponding to Hindu solar date $date$.
(let* ((month (first date))
(day (second date))
(year (third date)))
(floor (+ (* year solar-sidereal-year);; Days in elapsed years.
(* (1- month) solar-month) ;; In months.
day -1/4 ;; Whole days until midnight.
-1132959)))) ;; Days before absolute day 0.
(defun lunar-longitude (days)
;; Mean sidereal longitude of the moon, in degrees,
;; at date and fraction of day $days$.
(* (mod (/ days lunar-sidereal-month) 1) 360))
(defun lunar-phase (days)
;; Longitudinal distance between the sun and the moon, as an integer
;; in the range 1..30, at date and fraction of day $days$.
(1+ (quotient
(mod (- (lunar-longitude days) (solar-longitude days))
360)
12)))
(defun new-moon (days)
;; Time of the most recent mean conjunction at or before
;; date and fraction of day $days$.
(- days (mod days lunar-synodic-month)))
(defun old-hindu-lunar-from-absolute (date)
;; Hindu lunar month, day, and year of absolute date $date$.
(let* ((hdate (+ date 1132959)) ;; Hindu date.
(sunrise (+ hdate 1/4)) ;; Sunrise on that day.
(last-new-moon ;; Last new moon.
(new-moon sunrise))
(next-new-moon ;; Next new moon.
(+ last-new-moon lunar-synodic-month))
(day (lunar-phase sunrise)) ;; Day of month.
(month ;; Month of lunar year.
(adjusted-mod (1+ (zodiac last-new-moon)) 12))
(leapmonth ;; If next month the same.
(= (zodiac last-new-moon)
(zodiac next-new-moon)))
(next-month ;; Beginning of next month.
(+ next-new-moon
(if leapmonth lunar-synodic-month 0)))
(year ;; Solar year of next month.
(quotient next-month solar-sidereal-year)))
(list month leapmonth day year)))
(defun old-hindu-lunar-precedes (date1 date2)
;; True if Hindu lunar $date1$ precedes $date2$.
(let* ((month1 (first date1))
(month2 (first date2))
(leap1 (second date1))
(leap2 (second date2))
(day1 (third date1))
(day2 (third date2))
(year1 (fourth date1))
(year2 (fourth date2)))
(or (< year1 year2)
(and (= year1 year2)
(or (< month1 month2)
(and (= month1 month2)
(or (and leap1 (not leap2))
(and (equal leap1 leap2)
(< day1 day2)))))))))
(defun absolute-from-old-hindu-lunar (date)
;; Absolute date corresponding to Hindu lunar date $date$;
;; returns nil if no such date exists.
(let* ((years (fourth date)) ;; Elapsed years.
(months (- (first date) 2));; Elapsed whole months,
;; minus a month's possible difference between the
;; solar and lunar year.
(approx;; Approximate date from below by adding days...
(+ (floor (* years solar-sidereal-year)) ;; in years,
(floor (* months lunar-synodic-month));; in months,
-1132959)) ;; and before absolute date 0.
(try
(+ approx ;; Search forward to correct date,
(sum 1 i approx ;; or just past it.
(old-hindu-lunar-precedes
(old-hindu-lunar-from-absolute i)
date)))))
(if (equal (old-hindu-lunar-from-absolute try) date)
try
nil)));; $date$ non-existent on Hindu lunar calendar.
| 33,811 | Common Lisp | .l | 798 | 33.932331 | 76 | 0.56094 | staudtlex/calcal-lisp | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | c890e3365040a8157b8c5a0d1cf927c0cc05db8a0e454878c83438d4d187dd7c | 41,921 | [
357087
] |
41,936 | pong.lisp | lambdanil_lispy-sdl2-stuff/pong.lisp | (let ((quicklisp-init (merge-pathnames "quicklisp/setup.lisp"
(user-homedir-pathname))))
(when (probe-file quicklisp-init)
(load quicklisp-init)))
(ql:quickload :sdl2)
(ql:quickload :sdl2-image)
(ql:quickload :sdl2-ttf)
(ql:quickload :bt-semaphore)
(defparameter *font* nil)
(defconstant *screen-width* 1280)
(defconstant *screen-height* 960)
(defmacro with-window-renderer ((window renderer) &body body)
`(sdl2:with-init (:video)
(sdl2:with-window (,window
:title "Common Lisp SDL2 Pong"
:w *screen-width*
:h *screen-height*
:flags '(:shown))
(let ((,renderer (sdl2:create-renderer window -1 '(:ACCELERATED :PRESENTVSYNC))))
,@body))))
(defun draw-circle (renderer cx cy radius)
(let* ((diameter (* radius 2))
(x (- radius 1))
(y 0)
(tx 1)
(ty 1)
(err (- tx diameter))
(status 0))
(loop while (>= x y) do
(dolist (octant (list (cons (+ cx x) (- cy y))
(cons (+ cx x) (+ cy y))
(cons (- cx x) (- cy y))
(cons (- cx x) (+ cy y))
(cons (+ cx y) (- cy x))
(cons (+ cx y) (+ cy x))
(cons (- cx y) (- cy x))
(cons (- cx y) (+ cy x))))
(eval `(sdl2:render-draw-point ,renderer ,(car octant) ,(cdr octant))))
(when (< status 0) (setq status -1))
(when (<= err 0)
(incf y)
(incf err ty)
(incf ty 2))
(when (> err 0)
(decf x)
(incf tx 2)
(incf err (- tx diameter))))
status))
(defun fill-circle (renderer x y radius)
(let ((ox 0)
(oy radius)
(d (- radius 1))
(status 0))
(loop while (>= oy ox) do
(dolist (q (list `(,(- x oy) ,(+ y ox) ,(+ x oy) ,(+ y ox))
`(,(- x ox) ,(+ y oy) ,(+ x ox) ,(+ y oy))
`(,(- x ox) ,(- y oy) ,(+ x ox) ,(- y oy))
`(,(- x oy) ,(- y ox) ,(+ x oy) ,(- y ox))))
(eval
`(sdl2:render-draw-line ,renderer
,(nth 0 q)
,(nth 1 q)
,(nth 2 q)
,(nth 3 q))))
(when (< status 0)
(setq status -1)
(return status))
(cond
((>= d (* 2 ox)) (decf d (+ (* 2 ox) 1)) (incf ox))
((< d (* 2 (- radius oy))) (incf d (* 2 (- oy 1))) (decf oy))
(t (incf d (* 2 (- oy ox 1))) (decf oy) (incf ox))))
status))
(defmacro draw-rect (renderer x y width height)
`(sdl2:with-rects ((fill-rect ,x ,y ,width ,height))
(sdl2:render-draw-rect ,renderer fill-rect)))
(defmacro fill-rect (renderer x y width height)
`(sdl2:with-rects ((fill-rect ,x ,y ,width ,height))
(sdl2:render-fill-rect ,renderer fill-rect)))
(defclass tex ()
((renderer
:initarg :renderer
:initform (error "Must supply a renderer"))
(width
:accessor tex-width
:initform 0 )
(height
:accessor tex-height
:initform 0)
(texture
:accessor tex-texture
:initform nil)))
(defun free-tex (tex)
(with-slots (texture) tex
(sdl2:destroy-texture texture)))
(defun set-color (tex r g b)
(sdl2:set-texture-color-mod (tex-texture tex) r g b))
(defun load-texture-from-file (renderer filename)
(let ((tex (make-instance 'tex :renderer renderer)))
(with-slots (renderer texture width height) tex
(let ((surface (sdl2-image:load-image filename)))
(setf width (sdl2:surface-width surface))
(setf height (sdl2:surface-height surface))
(sdl2:set-color-key surface :true (sdl2:map-rgb (sdl2:surface-format surface)
0 #xFF #xFF))
(setf texture (sdl2:create-texture-from-surface renderer surface))))
tex))
(defun load-texture-from-text (renderer text)
(let ((tex (make-instance 'tex :renderer renderer)))
(with-slots (renderer texture width height) tex
(let ((surface (sdl2-ttf:render-text-solid *font* text 255 255 255 0)))
(setf width (sdl2:surface-width surface))
(setf height (sdl2:surface-height surface))
(setf texture (sdl2:create-texture-from-surface renderer surface))))
tex))
(defun render (tex x y &key clip angle center (flip :none))
(with-slots (renderer texture width height) tex
(sdl2:render-copy-ex renderer
texture
:source-rect clip
:dest-rect (sdl2:make-rect x
y
(if clip (sdl2:rect-width clip) width)
(if clip (sdl2:rect-height clip) height))
:angle angle
:center center
:flip (list flip))))
(defstruct rect x y width height)
(defstruct circle x y radius filled)
(defparameter *dx* 3)
(defparameter *dy* 3)
(defconstant *pad-width* 25)
(defconstant *pad-height* 148)
(defparameter *left-pad*
(let ((width *pad-width*) (height *pad-height*))
(make-rect :x 0
:y (- (/ *screen-height* 2) (/ height 2))
:width width
:height height)))
(defparameter *right-pad*
(let ((width *pad-width*) (height *pad-height*))
(make-rect :x (- *screen-width* width)
:y (- (/ *screen-height* 2) (/ height 2))
:width width
:height height)))
(defparameter *ball*
(make-rect :x (- (/ *screen-width* 2) (/ 16 2))
:y (- (/ *screen-height* 2) (/ 16 2))
:width 16
:height 16))
(defun detect-collision (ball pad)
(let* ((pxf (rect-x pad))
(pxs (+ pxf (rect-width pad)))
(pyf (rect-y pad))
(pys (+ pyf (rect-height pad)))
(byf (rect-y ball))
(bys (+ byf (rect-height ball)))
(bxf (rect-x ball))
(bxs (+ bxf (rect-width ball))))
(when (and (>= bys pyf) (<= byf pys))
(if (> *dx* 0)
(when (and (<= (- pxf bxs) 0) (>= (- pxf bxs) -4))
(setf *dx* (* *dx* -1)))
(when (and (<= (- bxf pxs) 0) (>= (- bxf pxs) -4))
(setf *dx* (* *dx* -1)))))))
(defparameter *keyr* nil)
(defparameter *keyl* nil)
(defparameter *l-score* 0)
(defparameter *r-score* 0)
(defun eval-key (key)
(cond ((string= key "u") (when (> (rect-y *right-pad*) 0)
(decf (rect-y *right-pad*) 4)))
((string= key "d") (when (< (rect-y *right-pad*) (- *screen-height*
(rect-height *right-pad*)))
(incf (rect-y *right-pad*) 4)))
((string= key "lu") (when (> (rect-y *left-pad*) 0)
(decf (rect-y *left-pad*) 4)))
((string= key "ld") (when (< (rect-y *left-pad*) (- *screen-height*
(rect-height *left-pad*)))
(incf (rect-y *left-pad*) 4)))))
(bt:make-thread (lambda ()
(loop with r = nil while t do
(incf (rect-x *ball*) *dx*)
(incf (rect-y *ball*) *dy*)
(when
(or (>= (rect-y *ball*)
(- *screen-height* (/ (rect-height *ball*) 2)))
(<= (rect-y *ball*)
(+ (/ (rect-height *ball*) 2))))
(setf *dy* (* *dy* -1)))
(when (>= (rect-x *ball*)
(- *screen-width* (/ (rect-width *ball*) 2)))
(incf *l-score*)
(setf r t))
(when (<= (rect-x *ball*)
(+ (/ (rect-width *ball*) 2)))
(incf *r-score*)
(setf r t))
(when r
(setf (rect-x *ball*)
(- (/ *screen-width* 2) (/ (rect-width *ball*) 2)))
(setf (rect-y *ball*)
(- (/ *screen-height* 2) (/ (rect-height *ball*) 2)))
(setf (rect-y *left-pad*)
(- (/ *screen-height* 2) (/ (rect-height *left-pad*) 2)))
(setf (rect-y *right-pad*)
(- (/ *screen-height* 2) (/ (rect-height *right-pad*) 2)))
(sleep 1)
(setf r nil))
(detect-collision *ball* *left-pad*)
(detect-collision *ball* *right-pad*)
(eval-key *keyr*)
(eval-key *keyl*)
(sleep 0.006)))
:name "move-thread")
(with-window-renderer
(window renderer)
(sdl2-image:init '(:png))
(sdl2-ttf:init)
(setf *font* (sdl2-ttf:open-font "./square.ttf" 28))
(let ((l-tex (load-texture-from-text renderer "0"))
(r-tex (load-texture-from-text renderer "0"))
(last-l *l-score*)
(last-r *r-score*))
(sdl2:with-event-loop
(:method :poll)
(:quit () t)
(:keydown (:keysym keysym)
(case (sdl2:scancode keysym)
(:scancode-up (setf *keyr* "u"))
(:scancode-down (setf *keyr* "d"))
(:scancode-w (setf *keyl* "lu"))
(:scancode-s (setf *keyl* "ld"))
(t (progn (setf *keyr* nil) (setf *keyl* nil)))))
(:idle ()
(when (not
(or (sdl2:keyboard-state-p :scancode-up)
(sdl2:keyboard-state-p :scancode-down)))
(setf *keyr* nil))
(when (not
(or (sdl2:keyboard-state-p :scancode-w)
(sdl2:keyboard-state-p :scancode-s)))
(setf *keyl* nil))
(when (not (and (= last-l *l-score*)
(= last-r *r-score*)))
(setf l-tex (load-texture-from-text renderer (write-to-string *l-score*)))
(setf r-tex (load-texture-from-text renderer (write-to-string *r-score*))))
(setf last-l *l-score*)
(setf last-r *r-score*)
(sdl2:set-render-draw-color renderer 0 0 0 255)
(sdl2:render-clear renderer)
(sdl2:set-render-draw-color renderer 255 255 255 0)
(sdl2:render-draw-line renderer
(/ *screen-width* 2)
0
(/ *screen-width* 2)
*screen-height*)
(fill-circle renderer
(+ (rect-x *ball*) (/ (rect-width *ball*) 2))
(+ (rect-y *ball*) (/ (rect-height *ball*) 2))
(/ (rect-width *ball*) 2))
(fill-rect renderer
(rect-x *left-pad*)
(rect-y *left-pad*)
(rect-width *left-pad*)
(rect-height *left-pad*))
(fill-rect renderer
(rect-x *right-pad*)
(rect-y *right-pad*)
(rect-width *right-pad*)
(rect-height *right-pad*))
(set-color l-tex 255 255 255)
(set-color r-tex 255 255 255)
(render r-tex (+ (/ *screen-width* 2) 40) 40)
(render l-tex (- (/ *screen-width* 2) 40 28) 40)
(sdl2:render-present renderer)))
(free-tex l-tex)
(free-tex r-tex))
(sdl2-ttf:quit)
(sdl2-image:quit))
| 9,900 | Common Lisp | .lisp | 279 | 28.62724 | 93 | 0.544355 | lambdanil/lispy-sdl2-stuff | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | d0e0569a20ee9ff51b793d4b53e234dcffbbb1b4196dfc2410e63ddce176eed2 | 41,936 | [
-1
] |
41,937 | tetris.lisp | lambdanil_lispy-sdl2-stuff/tetris.lisp | (let ((quicklisp-init (merge-pathnames "quicklisp/setup.lisp"
(user-homedir-pathname))))
(when (probe-file quicklisp-init)
(load quicklisp-init)))
(ql:quickload :sdl2)
(ql:quickload :sdl2-image)
(ql:quickload :bt-semaphore)
(ql:quickload :sdl2-ttf)
(setf *random-state* (make-random-state t))
(defconstant *outline-mode* t)
(defconstant sq-size 46) ;; The on-screen size of a single square
(defconstant *grid-height* 20)
(defconstant *grid-width* 10)
(defconstant piece-dim 5)
(defconstant *w* *grid-width*)
(defconstant *h* (+ *grid-height* (1- piece-dim)))
(defconstant *screen-width* (+ (* (+ *w* piece-dim 1) sq-size) 3))
(defconstant *screen-height* (+ (* *grid-height* sq-size) 2))
(defvar sleep-time 0.4)
(defvar *score* 0)
(defparameter *font* nil)
(defclass tex ()
((renderer
:initarg :renderer
:initform (error "Must supply a renderer"))
(width
:accessor tex-width
:initform 0 )
(height
:accessor tex-height
:initform 0)
(texture
:accessor tex-texture
:initform nil)))
(defun free-tex (tex)
(with-slots (texture) tex
(sdl2:destroy-texture texture)))
(defun load-texture-from-text (renderer text)
(let ((tex (make-instance 'tex :renderer renderer)))
(with-slots (renderer texture width height) tex
(let ((surface (sdl2-ttf:render-text-solid *font* text 255 255 255 0)))
(setf width (sdl2:surface-width surface))
(setf height (sdl2:surface-height surface))
(setf texture (sdl2:create-texture-from-surface renderer surface))))
tex))
(defmacro with-window-renderer ((window renderer) &body body)
`(sdl2:with-init (:video)
(sdl2:with-window (,window
:title "Common Lisp SDL2 Tetris"
:w *screen-width*
:h *screen-height*
:flags '(:shown))
(let ((,renderer (sdl2:create-renderer window -1 '(:ACCELERATED :PRESENTVSYNC))))
,@body))))
(defmacro draw-rect (renderer x y width height)
"Draw a rectangle with SDL2."
`(sdl2:with-rects ((fill-rect ,x ,y ,width ,height))
(sdl2:render-draw-rect ,renderer fill-rect)))
(defun fill-rect (renderer x y width height &key (no-outline nil))
"Draw a filled rectangle with SDL2."
(sdl2:with-rects ((fill-rect x y width height))
(sdl2:render-fill-rect renderer fill-rect)
(when (not no-outline)
(sdl2:set-render-draw-color renderer 200 200 200 255)
(sdl2:render-draw-rect renderer fill-rect)))) ;; outline
(defvar grid (make-array (list *h* *w*) :initial-element ".")) ;; 2D game grid
(defvar piece) ;; 2D grid of the current piece
(defvar piece-pos) ;; position of current piece
(defvar not-shuffled) ;; set to nil when order of pieces has been shuffled for loop
(defvar piece-index) ;; index of piece in current list of pieces
(defvar exit-game nil) ;; game will exit on next loop when set to t
(defvar next-piece nil) ;; 2D grid of the next piece
(defstruct pos x y)
;; Colors for each piece type, same as in original tetris
(defconstant colors '(
("i" . (0 255 255))
("l" . (255 95 31))
("j" . (0 0 255))
("t" . (191 64 191))
("o" . (255 255 0))
("z" . (255 0 0))
("s" . (0 255 0))
))
;; Letters are bound to colors
;; This list is shuffled around to get random piece order
(defvar *pieces*
'(
#2A(("." "." "i" "." ".")
("." "." "i" "." ".")
("." "." "i" "." ".")
("." "." "i" "." ".")
("." "." "." "." "."))
#2A(("." "." "." "." ".")
("." "." "l" "." ".")
("." "." "l" "." ".")
("." "." "l" "l" ".")
("." "." "." "." "."))
#2A(("." "." "." "." ".")
("." "." "j" "." ".")
("." "." "j" "." ".")
("." "j" "j" "." ".")
("." "." "." "." "."))
#2A(("." "." "." "." ".")
("." "." "t" "." ".")
("." "t" "t" "t" ".")
("." "." "." "." ".")
("." "." "." "." "."))
#2A(("." "." "." ".")
("." "o" "o" ".")
("." "o" "o" ".")
("." "." "." "."))
#2A(("." "." "." ".")
("." "." "z" ".")
("." "z" "z" ".")
("." "z" "." ".")
("." "." "." "."))
#2A(("." "." "." ".")
("." "s" "." ".")
("." "s" "s" ".")
("." "." "s" ".")
("." "." "." "."))
))
(defun random-from-list (list)
"Get random element from list."
(let ((len (length list)))
(nth (random len) list)))
(defun print-grid (grid)
"Print a 2D array."
(let ((width (array-dimension grid 1))
(height (array-dimension grid 0)))
(loop for i from 0 below height do
(progn
(loop for j from 0 below width do
(format t "~a" (aref grid i j)))
(format t "~%")))))
(defun render-piece (grid)
"Render current piece into supplied grid."
(let ((pw (array-dimension piece 1))
(ph (array-dimension piece 0)))
(loop for i from 0 below ph do
(loop for j from 0 below pw do
(when (string/= (aref piece i j) ".")
(setf (aref grid
(+ i (pos-y piece-pos))
(+ j (pos-x piece-pos)))
(aref piece i j)))))))
(defun store-piece ()
"Permanently store current piece in the grid."
(render-piece grid))
(defun copy-array (array &key
(element-type (array-element-type array))
(fill-pointer (and (array-has-fill-pointer-p array)
(fill-pointer array)))
(adjustable (adjustable-array-p array)))
"Copy an array, multi-dimensional arrays supported too."
(let ((dims (array-dimensions array)))
(adjust-array
(make-array dims
:element-type element-type :fill-pointer fill-pointer
:adjustable adjustable :displaced-to array)
dims)))
(defun print-game ()
"Print the game into command line."
(let ((render (copy-array grid)))
(render-piece render)
(print-grid render)))
(defun shuffle-list (list)
"Shuffle a list randomly."
(loop for i from (length list) downto 2
do (rotatef (nth (random i) list)
(nth (1- i) list)))
list)
(defun predict-piece ()
"Return next piece."
(if (> piece-index 5)
(if not-shuffled
(progn
(shuffle-list *pieces*)
(setf not-shuffled nil)
(car *pieces*))
(car *pieces*))
(nth (+ piece-index 1) *pieces*)))
(defun random-piece ()
"Get the next piece."
(incf piece-index)
(when (> piece-index 6)
(setq piece-index 0))
(setf not-shuffled t)
(setf piece (nth piece-index *pieces*)))
(defun reset-pos ()
"Reset piece position."
(setf (pos-x piece-pos) (- (/ *w* 2) (floor piece-dim 2)))
(setf (pos-y piece-pos) 0))
(defun init-game ()
"Initialize game."
(setf piece (random-from-list *pieces*))
(setf piece-pos (make-pos :x (- (/ *w* 2) (floor piece-dim 2)) :y 0))
(shuffle-list *pieces*)
(setf not-shuffled t)
(setq piece-index 0)
(setf piece (car *pieces*)))
(defun move-piece-skip ()
"Move piece all the way to the bottom."
(loop while (not (has-landed)) do
(incf (pos-y piece-pos)))
(store-piece)
(random-piece)
(reset-pos))
(defun move-piece ()
"Move piece once."
(if (not (has-landed))
(incf (pos-y piece-pos))
(progn
(store-piece)
(random-piece)
(reset-pos))))
(defun transpose-matrix (matrix)
"Return transposed matrix."
(let* ((new-matrix (make-array (list (array-dimension matrix 1)
(array-dimension matrix 0))))
(height (array-dimension matrix 0))
(width (array-dimension matrix 1))
(new-height width)
(new-width height))
(loop for i from 0 below new-height do
(loop for j from 0 below new-width do
(setf (aref new-matrix i j)
(aref matrix j i))))
new-matrix))
(defun reverse-rows (matrix)
"In-place reverse rows in matrix."
(let* ((height (array-dimension matrix 0))
(width (array-dimension matrix 1))
(tmp nil)
(start 0)
(end (- width 1)))
(loop for i from 0 below height do
(progn
(setq start 0)
(setq end (- width 1))
(loop while (< start end) do
(setf tmp (aref matrix i start))
(setf (aref matrix i start) (aref matrix i end))
(setf (aref matrix i end) tmp)
(incf start)
(decf end))))))
(defun is-valid-pos (y x)
"Check if position is not occupied or outside of game grid."
(when (and
(>= x 0)
(< x *w*)
(>= y 0)
(< y *h*))
(string= (aref grid y x)
".")))
(defun has-landed ()
"Check if piece has landed."
(check-collision :down t))
(defun clear-row (n)
"Clear supplied row."
(decf sleep-time 0.003)
(loop for i from n above 0 do
(loop for j from 0 below (array-dimension grid 1) do
(setf (aref grid i j) (aref grid (- i 1) j)))))
(defun check-rows ()
"Find filled rows and clear them."
(let ((do-clear nil) (cleared 0))
(loop for i from 0 below (array-dimension grid 0) do
(loop for j from 0 below (array-dimension grid 1) do
(progn
(when (string= (aref grid i j) ".")
(return))
(when (= j (- (array-dimension grid 1) 1))
(clear-row i)
(incf cleared)))))
(case cleared
(1 (incf *score* 40))
(2 (incf *score* 100))
(3 (incf *score* 300))
(4 (incf *score* 1200)))))
(defun check-loss ()
"Check if the player has lost."
(loop for i from 0 below (array-dimension grid 1) do
(when (string/= (aref grid 2 i) ".")
(setf exit-game t))))
(defun check-collision (&key up right left down (piece piece))
"Check collision in specified direction for specified piece."
(let ((mx (cond (right 1)
(left -1)
(t 0)))
(my (cond (down 1)
(up -1)
(t 0)))
(landed nil))
(loop for i from 0 below (array-dimension piece 0) do
(loop for j from 0 below (array-dimension piece 1) do
(when (and
(string/= (aref piece i j) ".")
(not (is-valid-pos (+ i (pos-y piece-pos) my)
(+ j (pos-x piece-pos) mx))))
(setq landed t))))
landed))
(defun rotate-piece ()
"Rotate the current piece."
(let ((new-piece nil))
(setf new-piece (transpose-matrix piece))
(reverse-rows new-piece)
(when (not (check-collision :piece new-piece)) ;; check rotation collision
(setf piece new-piece))))
(defun move (&key up down left right)
"Move the current piece in specified direction."
(when (not (check-collision :up up :down down :left left :right right))
(cond (right (incf (pos-x piece-pos)))
(left (decf (pos-x piece-pos))))
(cond (down (incf (pos-y piece-pos)))
(up (decf (pos-y piece-pos))))))
(defun set-color-by-piece (renderer letter)
"Set renderer color according to specific grid entry."
(let* ((color (assoc letter colors :test #'string=))
(r (nth 1 color))
(g (nth 2 color))
(b (nth 3 color)))
(sdl2:set-render-draw-color renderer r g b 255)))
(defun render-game-grid (renderer)
"Render the game grid into the SDL2 window."
(let* ((rows '())
(render-grid (copy-array grid))
(h (+ (* sq-size (- (array-dimension grid 0) (- piece-dim 1))) 1))
(w (+ (* sq-size (array-dimension grid 1)) 1))
(bxf (+ w sq-size -1))
(bxl (+ w (* sq-size (+ piece-dim 1)) 1))
(byl (+ (* sq-size piece-dim) 1)))
(sdl2:render-draw-line renderer 0 0 w 0) ;; Render game grid outline
(sdl2:render-draw-line renderer 0 0 0 h)
(sdl2:render-draw-line renderer 0 h w h)
(sdl2:render-draw-line renderer w 0 w h)
(sdl2:render-draw-line renderer bxf 0 bxl 0 ) ;; Next piece box outline
(sdl2:render-draw-line renderer bxf 0 bxf byl)
(sdl2:render-draw-line renderer bxf byl bxl byl)
(sdl2:render-draw-line renderer bxl 0 bxl byl)
(render-piece render-grid)
(loop for i from 0 below (array-dimension next-piece 0) do
(loop for j from 0 below (array-dimension next-piece 1) do
(when (string/= (aref next-piece i j) ".")
(set-color-by-piece renderer (aref next-piece i j))
(fill-rect renderer
(+ bxf 1 (* j sq-size))
(+ (* i sq-size) 1)
sq-size sq-size))))
(loop for i from (- piece-dim 1) below (array-dimension grid 0) do
(loop for j from 0 below (array-dimension grid 1) do
(when (string/= (aref render-grid i j) ".")
(when (and *outline-mode* (not (member j rows)) (string= (aref grid i j) "."))
(setf rows (cons j rows))
(sdl2:set-render-draw-color renderer 22 22 22 255)
(loop for k from i below *h* do
(fill-rect renderer
(+ (* j sq-size 1) 1)
(+ (* (- k (- piece-dim 1)) sq-size) 1)
sq-size
sq-size
:no-outline t)))
(set-color-by-piece renderer (aref render-grid i j))
(fill-rect renderer
(+ (* j sq-size) 1)
(+ (* (- i (- piece-dim 1)) sq-size) 1)
sq-size sq-size))))))
(defun set-color (tex r g b)
(sdl2:set-texture-color-mod (tex-texture tex) r g b))
(defun render (tex x y &key clip angle center (flip :none))
(with-slots (renderer texture width height) tex
(sdl2:render-copy-ex renderer
texture
:source-rect clip
:dest-rect (sdl2:make-rect x
y
(if clip (sdl2:rect-width clip) width)
(if clip (sdl2:rect-height clip) height))
:angle angle
:center center
:flip (list flip))))
(defun run ()
(setf *random-state* (make-random-state t))
(init-game)
(bt:make-thread (lambda ()
(let ((start nil)
(end nil))
(loop while t do
(setq start (get-internal-real-time))
(move-piece)
(check-rows)
(check-loss)
(setq end (get-internal-real-time))
(setf next-piece (predict-piece))
(sleep (- sleep-time (/ (- end start) 1000000)))))) ;; Substract time spent on calculations
:name "thread")
(with-window-renderer
(window renderer)
(sdl2-image:init '(:png))
(sdl2-ttf:init)
(setf *font* (sdl2-ttf:open-font "./square.ttf" 28))
(let ((l-tex (load-texture-from-text renderer "0")))
(sdl2:with-event-loop
(:method :poll)
(:quit () t)
(:keydown (:keysym keysym)
(case (sdl2:scancode keysym)
(:scancode-right (move :right t))
(:scancode-left (move :left t))
(:scancode-up (rotate-piece))
(:scancode-down (move-piece))
(:scancode-space (move-piece-skip))))
(:idle ()
(when exit-game
(format t "~%Game over!~%")
(sdl2:push-quit-event))
(sdl2:set-render-draw-color renderer 0 0 0 255)
(sdl2:render-clear renderer)
(setf l-tex (load-texture-from-text renderer (write-to-string *score*)))
(set-color l-tex 255 255 255)
(render l-tex (* sq-size (+ *grid-width* (ceiling piece-dim 2))) (* (1+ piece-dim) sq-size))
(sdl2:set-render-draw-color renderer 255 255 255 0)
(render-game-grid renderer)
(sdl2:render-present renderer)))
(free-tex l-tex))
(sdl2-ttf:quit)
(sdl2-image:quit)))
;;(sb-ext:save-lisp-and-die #p"tetris" :toplevel #'run :executable t :compression 9)
(run)
| 14,856 | Common Lisp | .lisp | 429 | 29.433566 | 100 | 0.589972 | lambdanil/lispy-sdl2-stuff | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | fbb5120c285661af96370d1ce28bf227039e5cfc0bb3242167665fccfb9fae0c | 41,937 | [
-1
] |
41,938 | sdl-fn.lisp | lambdanil_lispy-sdl2-stuff/sdl-fn.lisp | (defmacro with-window-renderer ((window renderer) &body body)
`(sdl2:with-init (:video)
(sdl2:with-window (,window
:title "Common Lisp SDL2 Tetris"
:w *screen-width*
:h *screen-height*
:flags '(:shown))
(let ((,renderer (sdl2:create-renderer window -1 '(:ACCELERATED :PRESENTVSYNC))))
,@body))))
(defun draw-circle (renderer cx cy radius)
(let* ((diameter (* radius 2))
(x (- radius 1))
(y 0)
(tx 1)
(ty 1)
(err (- tx diameter))
(status 0))
(loop while (>= x y) do
(dolist (octant (list (cons (+ cx x) (- cy y))
(cons (+ cx x) (+ cy y))
(cons (- cx x) (- cy y))
(cons (- cx x) (+ cy y))
(cons (+ cx y) (- cy x))
(cons (+ cx y) (+ cy x))
(cons (- cx y) (- cy x))
(cons (- cx y) (+ cy x))))
(eval `(sdl2:render-draw-point ,renderer ,(car octant) ,(cdr octant))))
(when (< status 0) (setq status -1))
(when (<= err 0)
(incf y)
(incf err ty)
(incf ty 2))
(when (> err 0)
(decf x)
(incf tx 2)
(incf err (- tx diameter))))
status))
(defun fill-circle (renderer x y radius)
(let ((ox 0)
(oy radius)
(d (- radius 1))
(status 0))
(loop while (>= oy ox) do
(dolist (q (list `(,(- x oy) ,(+ y ox) ,(+ x oy) ,(+ y ox))
`(,(- x ox) ,(+ y oy) ,(+ x ox) ,(+ y oy))
`(,(- x ox) ,(- y oy) ,(+ x ox) ,(- y oy))
`(,(- x oy) ,(- y ox) ,(+ x oy) ,(- y ox))))
(eval
`(sdl2:render-draw-line ,renderer
,(nth 0 q)
,(nth 1 q)
,(nth 2 q)
,(nth 3 q))))
(when (< status 0)
(setq status -1)
(return status))
(cond
((>= d (* 2 ox)) (decf d (+ (* 2 ox) 1)) (incf ox))
((< d (* 2 (- radius oy))) (incf d (* 2 (- oy 1))) (decf oy))
(t (incf d (* 2 (- oy ox 1))) (decf oy) (incf ox))))
status))
(defmacro draw-rect (renderer x y width height)
`(sdl2:with-rects ((fill-rect ,x ,y ,width ,height))
(sdl2:render-draw-rect ,renderer fill-rect)))
(defmacro fill-rect (renderer x y width height)
`(sdl2:with-rects ((fill-rect ,x ,y ,width ,height))
(sdl2:render-fill-rect ,renderer fill-rect)))
(defclass tex ()
((renderer
:initarg :renderer
:initform (error "Must supply a renderer"))
(width
:accessor tex-width
:initform 0 )
(height
:accessor tex-height
:initform 0)
(texture
:accessor tex-texture
:initform nil)))
(defun free-tex (tex)
(with-slots (texture) tex
(sdl2:destroy-texture texture)))
(defun set-color (tex r g b)
(sdl2:set-texture-color-mod (tex-texture tex) r g b))
(defun load-texture-from-file (renderer filename)
(let ((tex (make-instance 'tex :renderer renderer)))
(with-slots (renderer texture width height) tex
(let ((surface (sdl2-image:load-image filename)))
(setf width (sdl2:surface-width surface))
(setf height (sdl2:surface-height surface))
(sdl2:set-color-key surface :true (sdl2:map-rgb (sdl2:surface-format surface)
0 #xFF #xFF))
(setf texture (sdl2:create-texture-from-surface renderer surface))))
tex))
(defun render (tex x y &key clip angle center (flip :none))
(with-slots (renderer texture width height) tex
(sdl2:render-copy-ex renderer
texture
:source-rect clip
:dest-rect (sdl2:make-rect x
y
(if clip (sdl2:rect-width clip) width)
(if clip (sdl2:rect-height clip) height))
:angle angle
:center center
:flip (list flip))))
| 3,820 | Common Lisp | .lisp | 107 | 27.224299 | 93 | 0.516608 | lambdanil/lispy-sdl2-stuff | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | f1f43062979ef14572ef380175d55a736dc5fe28dd37fc21d59a7a8774198de8 | 41,938 | [
-1
] |
41,957 | example.lisp | agentlans_clinalg/example.lisp | (ql:quickload 'clinalg)
(defpackage :foo (:use :cl :clinalg))
(in-package :foo)
(make-vector 3)
(let ((m (make-matrix 3 15)))
(fill-with (lambda (i j) (+ i j)) m))
(let ((m (make-matrix 5 4 :complex-double)))
(fill-with (lambda (i j) (complex i j))
m))
;; Slice part of vector from index 4 (inclusive)
;; to 1 (non-inclusive)
(let ((v (make-vector 5 :complex-double)))
(dotimes (i 5)
(setf (vref v i) i))
(vector-slice v 4 1))
;; Example of slicing a row from a matrix
(defparameter *foo* (make-matrix 3 15 :complex-double))
(fill-with (lambda (i j) (+ i j)) *foo*)
(matrix-row *foo* 2)
;; Slice submatrix
(matrix-slice *foo* 0 2 4 7)
| 658 | Common Lisp | .lisp | 21 | 29.238095 | 55 | 0.64557 | agentlans/clinalg | 0 | 0 | 0 | LGPL-2.1 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | f4cf614414d2185e579391bf4323752ceec6539bd6fa6979b43b4d1e743f4175 | 41,957 | [
-1
] |
41,958 | array.lisp | agentlans_clinalg/array.lisp | ;; Copyright 2022 Alan Tseng
;;
;; This file is part of clinalg.
;;
;; clinalg is free software: you can redistribute it and/or modify it under the
;; terms of the GNU Lesser General Public License as published by the Free
;; Software Foundation, either version 3 of the License, or (at your option)
;; any later version.
;;
;; clinalg is distributed in the hope that it will be useful, but WITHOUT ANY
;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
;; details.
;;
;; You should have received a copy of the GNU Lesser General Public License
;; along with clinalg. If not, see <https://www.gnu.org/licenses/>.
(defpackage :clinalg
(:use :cl
:cffi :trivial-garbage)
(:export
;; Complex types
:complex-float
:complex-double
;; Classes
:num-vector
:full-matrix
;; Constructors
:make-vector
:make-matrix
;; Accessors
:vref
:mref
:fill-with
;; Slices
:copy
:vector-slice
:matrix-row
:matrix-col
:matrix-slice
;; Properties
:size
:stride
:num-type
:rows
:cols
:ld
:pointer))
(in-package :clinalg)
(defparameter *cffi-lisp-types*
'((:float . single-float)
(:double . double-float)
(:char . character)
(:short . integer)
(:int . integer)
(:long . integer)
(:long-long . integer)))
(defun complex-type? (type)
(member type '(:complex-float :complex-double)))
(defun real-type (type)
(case type
(:complex-float :float)
(:complex-double :double)
(t type)))
(defun data-n (type n)
"Number of elements needed to allocate n numbers of the given type."
(if (complex-type? type)
(* 2 n)
n))
(defun cast-to-cffi-type (x cffi-type)
"Cast x into a Lisp type that can work with CFFI's type system."
(let ((type (cdr (assoc cffi-type *cffi-lisp-types*))))
(cond (type (coerce x type))
((complex-type? cffi-type) (error "Complex type"))
(t x))))
(defun foreign-calloc (type n)
"Returns a garbage-collectable pointer of n elements
of given type initialized to 0."
(let ((p (foreign-alloc (real-type type)
:count (if (complex-type? type)
(* 2 n)
n)
:initial-element
(cast-to-cffi-type 0 (real-type type)))))
(finalize p (lambda () (foreign-free p)))))
(defclass num-vector ()
((ptr :initarg :ptr)
(offset :initarg :offset)
(type :initarg :type
:reader num-type)
(stride :initarg :stride
:reader stride)
(n :initarg :n
:reader size))
(:documentation
"Numeric vector of specified type in C."))
(defclass full-matrix ()
((ptr :initarg :ptr)
(offset :initarg :offset)
(type :initarg :type
:reader num-type)
(rows :initarg :rows
:reader rows)
(cols :initarg :cols
:reader cols)
(ld :initarg :ld
:reader ld))
(:documentation
"A full storage matrix of specified type in C."))
(defmethod vector-index ((v num-vector) i)
"Returns the index of v[i]."
(with-slots (offset stride) v
(+ offset (* i stride))))
(defmethod matrix-index ((m full-matrix) i j)
"Returns the index of m[i,j] in row-major order."
(with-slots (offset ld) m
(+ offset (* i ld) j)))
(defun make-vector (n &optional (type :double))
"Returns a vector of length n with the specified type.
Elements are uninitialized."
(let ((p (foreign-calloc type n)))
(make-instance
'num-vector
:ptr p
:offset 0
:type type
:stride 1
:n n)))
(defun make-matrix (rows cols &optional (type :double))
"Returns a matrix with the specified type and dimensions.
Elements are uninitialized."
(let ((p (foreign-calloc type (* rows cols))))
(make-instance
'full-matrix
:ptr p
:offset 0
:type type
:rows rows
:cols cols
:ld cols)))
(defun pointer (obj)
"Returns pointer to the first element of the array."
(with-slots (ptr offset type) obj
(inc-pointer ptr (* offset
(if (complex-type? type) 2 1)))))
(defun between? (x bound)
(and (<= 0 x) (< x bound)))
(defun vector-in-bounds? (v i)
"Whether i is a valid index for vector v."
(with-slots (n) v
(between? i n)))
(defun check-vector-in-bounds (v i)
(unless (vector-in-bounds? v i)
(error "Subscript out of bounds.")))
(defun matrix-in-bounds? (m i j)
"Whether [i,j] is valid index for matrix m."
(with-slots (rows cols) m
(and (between? i rows)
(between? j cols))))
(defun check-matrix-in-bounds (m i j)
(unless (matrix-in-bounds? m i j)
(error "Subscripts out of bounds.")))
(defun memref (ptr type i)
"Returns p[i] where p's elements have the specified type."
(macrolet ((ref (i) `(mem-aref ptr (real-type type) ,i)))
(if (complex-type? type)
;; Get the real and imaginary parts
(let ((re (ref (* 2 i)))
(im (ref (+ (* 2 i) 1))))
(complex re im))
;; Just a regular real type
(ref i))))
;; (cast-to-cffi-type 0.0 (real-type :complex-double))
(defun (setf memref) (z ptr type i)
"Sets p[i] = z."
(macrolet ((ref (i) `(mem-aref ptr (real-type type) ,i)))
(if (complex-type? type)
;; Set complex number
(let ((typ (real-type type)))
(setf (ref (* 2 i))
(cast-to-cffi-type (realpart z) typ))
(setf (ref (+ (* 2 i) 1))
(cast-to-cffi-type (imagpart z) typ))
z) ; return the complex number
;; Real number
(setf (ref i) (cast-to-cffi-type z type)))))
(defmethod vref ((v num-vector) i)
"Returns v[i]."
(check-vector-in-bounds v i)
(with-slots (ptr type) v
(memref ptr type (vector-index v i))))
(defmethod (setf vref) (new-val (v num-vector) i)
"Sets v[i] = new_val."
(check-vector-in-bounds v i)
(with-slots (ptr type) v
(setf (memref ptr type (vector-index v i))
new-val)))
(defmethod mref ((m full-matrix) i j)
"Returns the element at m[i,j]."
(check-matrix-in-bounds m i j)
(with-slots (ptr type) m
(memref ptr type (matrix-index m i j))))
(defmethod (setf mref) (new-val (m full-matrix) i j)
"Sets m[i,j] = new_val."
(check-matrix-in-bounds m i j)
(with-slots (ptr type) m
(setf (memref ptr type (matrix-index m i j))
new-val)))
;; Sets obj[i] = f(i) for i = 0..size(obj)
(defmethod fill-with (f (obj num-vector))
(dotimes (i (size obj))
(setf (vref obj i) (funcall f i)))
obj)
;; Sets obj[i,j] = f(i,j) for i in [0,rows) and j in [0,cols)
(defmethod fill-with (f (obj full-matrix))
(dotimes (i (rows obj))
(dotimes (j (cols obj))
(setf (mref obj i j) (funcall f i j))))
obj)
| 6,504 | Common Lisp | .lisp | 218 | 26.252294 | 80 | 0.649249 | agentlans/clinalg | 0 | 0 | 0 | LGPL-2.1 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | a17917841235cd9af1592f623897b42e31b4fec9edbd173f4c8fc1c26812f64c | 41,958 | [
-1
] |
41,959 | print.lisp | agentlans_clinalg/print.lisp | (in-package :clinalg)
(defun number->string (x)
"String indicating the base 10 number
to 4 decimal places in normal or scientific notation."
(cond ((integerp x) (format nil "~A" x))
((complexp x)
;; Complex number
(let ((re (realpart x))
(im (imagpart x)))
(format nil "~A~A~Ai"
(number->string re)
(if (>= im 0) "+" "-")
(number->string (abs im)))))
((numberp x)
;; Real number
(let ((ax (abs x)))
(if (and (not (= ax 0))
(or (< ax 1e-4) (> ax 1e4)))
(format nil "~,4E" x)
(format nil "~,4F" x))))
;; All other data types
(t (format nil "~A" x))))
(defun spaces (n)
"Returns a string composed of n spaces."
(let ((fmt (format nil "~~~A@A" n)))
(format nil fmt "")))
(defun pad-left (str width)
"Adds spaces to the left of str to match the
specified width."
(concatenate 'string
(spaces (- width (length str)))
str))
(defun max-length (lst)
"Returns the maximum length of strings in lst."
(apply #'max (mapcar #'length lst)))
(defun display-indices (n)
"Lists the indices of rows or columns for pretty-printing."
(if (> n 10)
(list 0 1 2 nil (- n 3) (- n 2) (- n 1))
(loop for i from 0 below n collect i)))
(defun get-printed-matrix-entry (m i j)
"m[i,j] as a string, or ... if i or j is NIL."
(if (or (not i) (not j))
"..."
(number->string (mref m i j))))
(defun get-column (m col row-indices)
"m[row_indices,col]"
(loop for i in row-indices
collect (get-printed-matrix-entry m i col)))
(defun pretty-print-vector (stream v)
"Formats the vector v and prints it to stream."
(let* ((n (size v))
(d (display-indices n))
(data (loop for i in d
collect
(if i
(number->string (vref v i))
"...")))
(width (max-length data)))
(loop for x in data do
(format stream "~A~%"
(pad-left x width)))))
;; (pretty-print-vector t (make-vector 15))
(defun pretty-print-matrix (stream m)
"Formats the matrix m and prints it to stream."
(let ((rs (display-indices (rows m)))
(cs (display-indices (cols m)))
(last-col (- (cols m) 1)))
;; Figure out how wide each column should be
(let ((col-widths
(mapcar (lambda (c)
(cons c (max-length (get-column m c rs))))
cs)))
(loop for i in rs do
(loop for (j . width) in col-widths do
;; Print the column entry
(format stream "~A" (pad-left
(get-printed-matrix-entry m i j)
width))
;; Print column separator or line end
(if (and j (= j last-col))
(terpri stream)
(format stream " ")))))))
;; Example:
;; (pretty-print-matrix t (make-matrix 13 50))
;; Overload the default object printing in Lisp
(defmethod print-object ((obj num-vector) stream)
(print-unreadable-object (obj stream :type nil :identity nil)
(format stream "Vector of type ~A (size ~A)~%" (num-type obj) (size obj))
(pretty-print-vector stream obj)))
(defmethod print-object ((obj full-matrix) stream)
(print-unreadable-object (obj stream :type nil :identity nil)
(format stream "Full storage matrix of type ~A (size ~A x ~A)~%"
(num-type obj) (rows obj) (cols obj))
(pretty-print-matrix stream obj)))
| 3,193 | Common Lisp | .lisp | 95 | 29.410526 | 77 | 0.618924 | agentlans/clinalg | 0 | 0 | 0 | LGPL-2.1 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | a79b7528f40228e2881161cd8fb626f93e3718f0a42473447945fb75cee47925 | 41,959 | [
-1
] |
41,960 | slice.lisp | agentlans_clinalg/slice.lisp | (in-package :clinalg)
(defmethod copy ((v num-vector))
"Creates an independent copy of vector v."
(with-slots (n type) v
(let ((cp (make-vector n type)))
(dotimes (i n)
(setf (vref cp i) (vref v i)))
cp)))
(defmethod copy ((m full-matrix))
"Creates an independent copy of matrix m."
(with-slots (type rows cols) m
(let ((cp (make-matrix rows cols type)))
(dotimes (i rows)
(dotimes (j cols)
(setf (mref cp i j) (mref m i j))))
cp)))
(defun vector-slice (v start end)
"Returns v[start:end)"
(check-vector-in-bounds v start)
(check-vector-in-bounds v (- end 1))
(let ((start-ind (vector-index v start)))
(make-instance 'num-vector
:n (abs (- end start))
:stride (* (stride v)
(if (>= end start)
1 -1))
:type (num-type v)
:offset start-ind
:ptr (slot-value v 'ptr))))
(defmethod matrix-row ((m full-matrix) i)
"Returns m[i,:] as a NUM-VECTOR."
(check-matrix-in-bounds m i 0)
(with-slots (ptr type cols) m
(make-instance 'num-vector
:n cols
:stride 1
:type type
:offset (matrix-index m i 0)
:ptr ptr)))
(defmethod matrix-col ((m full-matrix) j)
"Returns m[:,j] as a NUM-VECTOR."
(check-matrix-in-bounds m 0 j)
(with-slots (ptr type rows ld) m
(make-instance 'num-vector
:n rows
:stride ld
:type type
:offset (matrix-index m 0 j)
:ptr ptr)))
(defmethod matrix-slice ((m full-matrix) start-row end-row start-col end-col)
"Returns m[[start_row:end_row), [start_col:end_col)]."
(check-matrix-in-bounds m start-row start-col)
(check-matrix-in-bounds m end-row end-col)
(assert (>= end-row start-row))
(assert (>= end-col start-col))
(with-slots (ptr type ld) m
(make-instance 'full-matrix
:rows (- end-row start-row)
:cols (- end-col start-col)
:ld ld
:type type
:offset (matrix-index m start-row start-col)
:ptr ptr)))
| 1,930 | Common Lisp | .lisp | 63 | 26.15873 | 77 | 0.623858 | agentlans/clinalg | 0 | 0 | 0 | LGPL-2.1 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | b1fd68147b7ad56c13d434698eb2b1915624d8199da370f6706b11c6c4444d7e | 41,960 | [
-1
] |
41,961 | clinalg.asd | agentlans_clinalg/clinalg.asd | (defsystem "clinalg"
:description "Matrices and vector data types to interface with C."
:version "0.0.1"
:author "Alan Tseng"
:licence "LGPL3"
:depends-on ("cffi" "trivial-garbage")
:serial t
:components ((:file "array")
(:file "print")
(:file "slice")))
| 317 | Common Lisp | .asd | 10 | 24.5 | 70 | 0.576547 | agentlans/clinalg | 0 | 0 | 0 | LGPL-2.1 | 9/19/2024, 11:50:58 AM (Europe/Amsterdam) | c33aa08618fb3042a636f45cd07c9fabe628196dbe441604b66192e1a5980925 | 41,961 | [
-1
] |
41,983 | @CREATE_MTXT.lsp | nobodybusiness_lisp-backup/!lsp/@CREATE_MTXT.lsp | ;szybkie tworzenie mtxt
(defun c:TXTTOMTX ()
(setq ss (ssget))
(setq i 0)
(setq multitext "")
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '1 (entget $ent))))
;(print $name)
(setq multitext (strcat multitext $name "\n"))
(setq i (+ i 1))
)
(print multitext)
)
| 376 | Common Lisp | .l | 16 | 19.375 | 49 | 0.569444 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 99cec073755a130ada4ddf8a34663f20a6b0754d051799949e80df0277c17fc7 | 41,983 | [
-1
] |
41,984 | DelFil.lsp | nobodybusiness_lisp-backup/!lsp/DelFil.lsp | ;; Modified by Chip Harper 09/24/03
;;
;; By R. ROBERT BELL
;;
;; Lisp s³u¿y do usuwania filtrów warstw.
(defun C:DelFil ()
(vl-Load-Com)
(vl-Catch-All-Apply '(lambda ()
(vla-Remove
(vla-GetExtensionDictionary
(vla-Get-Layers
(vla-Get-ActiveDocument
(vlax-Get-Acad-Object)))) "ACAD_LAYERFILTERS")))
(prompt "\n...")
(prompt "\n***All layer filters have been deleted!***")
(princ)
)
; (C:LayerFiltersDelete)
; (princ)
| 568 | Common Lisp | .l | 19 | 20.947368 | 76 | 0.519196 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 4ab2eeb212846d8b2fce7173528b3d08661a0c9b5cb60295df7edeaeb05af1a6 | 41,984 | [
-1
] |
41,985 | @PIGTAIL_ZMIANA_RODZAJU.lsp | nobodybusiness_lisp-backup/!lsp/@PIGTAIL_ZMIANA_RODZAJU.lsp | (defun c:@pigtail_zmiana_rodzaju ()
(setq ss (ssget))
(setq i 0)
(setq rodzaj (getint "<1>Niewyspawane<2>Wyspawane: "))
(setq HHorLU (getint "<1>HH<2>LU: "))
(setq obecnyNumer 0)
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq wspl (cdr (assoc '10 $LST)))
(setq $attag (strcase $attag))
(if (OR (= $attag "PRZEL_MIESZKANIA") (= $attag "PRZEL_USLUGI"))
(progn
(setq $VAL (atoi (cdr (assoc 1 $LST))))
(setq obecnyNumer $VAL)
)
)
(setq entToDel $ent)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
(entdel entToDel)
)
(insertPrzelBlok rodzaj HHorLU wspl obecnyNumer)
(setq i (+ i 1))
)
)
(defun insertPrzelBlok (rodzaj HHorLU wspl obecnyNumer)
(setq wspl (mapcar '+ wspl '(-6.68 -8.7 0.0)))
(if (= rodzaj 2)
;wyspawane
(if (= HHorLU 1)
;mieszkania
(progn
(setq wspl (mapcar '+ wspl '(-2.91 0.0 0.0)))
(setq coDoWstawienia "przel_mieszk_blok_wyspawane")
)
;uslugi
(progn
(setq wspl (mapcar '+ wspl '(-2.91 0.0 0.0)))
(setq coDoWstawienia "przel_uslugi_blok_wyspawane")
)
)
;niewyspawane
(if (= HHorLU 1)
;mieszkania
(progn
(setq wspl (mapcar '+ wspl '(-2.91 0.0 0.0)))
(setq coDoWstawienia "przel_mieszk_blok_wpiete")
)
;uslugi
(progn
;(setq wspl (mapcar '+ wspl '(-2.91 0.0 0.0)))
(setq coDoWstawienia "przel_uslugi_blok_wpiete")
)
)
)
(command "-insert" coDoWstawienia wspl "" "" "" obecnyNumer)
)
| 1,899 | Common Lisp | .l | 63 | 23.190476 | 71 | 0.543075 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | ad3bd2831bd3adaaf35050c58a595e85e86730c37ad9d8aa61517a57c8817629 | 41,985 | [
-1
] |
41,986 | @TXT2MTXT.lsp | nobodybusiness_lisp-backup/!lsp/@TXT2MTXT.lsp |
;; Text1MtextJust.lsp [command name: T1MJ]
;; TXT2MTXT command does not preserve all aspects of justification. For
;; one selected Text entity, retains horizontal component [except Aligned/
;; Fit have Center imposed], but imposes Top for vertical component to
;; all, regardless of Text entity's original justification.
;; T1MJ converts each selected Text entity separately to Mtext with same or
;; equivalent justification as original Text, including vertical component.
;; "Equivalent" for Text-entity justifications not used with Mtext:
;; Left/Center/Right become Bottom-Left/Bottom-Center/Bottom-Right;
;; Middle becomes Middle-Center;
;; Aligned/Fit become Bottom-Center with new insertion point half-way
;; between original Text entity's baseline alignment/fit points, so that
;; any positional change is minimized.
;; Will sometimes result in slight positional change, depending on specific
;; justification involved, text font, and/or whether text content includes
;; characters extending above or below height of capital letters [e.g. lower-
;; case letters with descenders, parentheses/brackets/braces, slashes, etc.].
;; Fit-justified Text will retain original height, but lose width adjustment.
;; Kent Cooper, 18 February 2014
(defun C:T1MJ ; = Text to 1-line Mtext, retaining Justification
(/ *error* cmde tss inc tent tobj tins tjust)
(defun *error* (errmsg)
(if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
(princ (strcat "\nError: " errmsg))
) ; if
(command "_.undo" "_end")
(setvar 'cmdecho cmde)
(princ)
) ; defun - *error*
(setq cmde (getvar 'cmdecho))
(setvar 'cmdecho 0)
(command "_.undo" "_begin")
(prompt "\nTo change Text to 1-line Mtext, preserving Justification,")
(if (setq tss (ssget "_:L" '((0 . "TEXT"))))
(repeat (setq inc (sslength tss))
(setq
tent (ssname tss (setq inc (1- inc)))
tobj (vlax-ename->vla-object tent)
tins (vlax-get tobj 'TextAlignmentPoint)
tjust (vla-get-Alignment tobj)
) ; setq
(cond
((= tjust 0) (setq tjust 7 tins (vlax-get tobj 'InsertionPoint))) ; Left
((< tjust 3) (setq tjust (+ tjust 7))) ; 1/2 [Center/Right] to 7/8/9
((= tjust 4) (setq tjust 5)) ; Middle to Middle-Center
((member tjust '(3 5)) ; Aligned/Fit
(setq
tjust 8 ; to Bottom-Center
tins (mapcar '/ (mapcar '+ (vlax-get tobj 'InsertionPoint) tins) '(2 2 2))
; with new insertion point
) ; setq
) ; Aligned/Fit
((setq tjust (- tjust 5))) ; all vertical-horizontal pair justifications
) ; cond
(command "_.txt2mtxt" tent "") ; convert, then
(setq tobj (vlax-ename->vla-object (entlast))) ; replace Text with new Mtext
(vla-put-AttachmentPoint tobj tjust) ; original Text's justification [or equiv.]
(vlax-put tobj 'InsertionPoint tins) ; original Text's insertion
) ; repeat
) ; if
(command "_.undo" "_end")
(setvar 'cmdecho cmde)
(princ)
) ; defun -- T1MJ
(vl-load-com)
(prompt "\nType T1MJ to change Text to 1-line Mtext, preserving Justification.")
Text2Mtext no move.lsp
Wyœwietlam Text2Mtext no move.lsp.
| 3,458 | Common Lisp | .l | 68 | 44.632353 | 98 | 0.638757 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a3f732fe9957d6d736d1cf5353e921391062cd54768025b2a991214742218c0e | 41,986 | [
-1
] |
41,987 | @remeber_prop_mm.lsp | nobodybusiness_lisp-backup/!lsp/@remeber_prop_mm.lsp |
(defun c:@mm ()
(command "matchprop" rememberEntity)
)
(defun c:@rem_ent ()
(setq rememberEntity (car (entsel)))
) | 129 | Common Lisp | .l | 6 | 18.166667 | 38 | 0.663793 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | cb569840a72f4764365b859213a32a0b1d0c7084242341f4bda56fa175645554 | 41,987 | [
-1
] |
41,988 | scalanie.lsp | nobodybusiness_lisp-backup/!lsp/scalanie.lsp | (defun c:scalanie()
(#scalanie)
)
(defun #scalanie()
(vl-load-com)
;pokaz warstwy do scalenia
(command "-layer" "off" "*" "")
(command "-layer" "on" "tkd_do_scalenia" "")
(command "-layer" "on" "tkd_zbr" "")
(command "-layer" "on" "tkd_opisy_zbr" "")
;select circles
(setq $zaznaczenie (ssget))
;pokaz warstwy
(command "-layer" "on" "*" "")
) | 392 | Common Lisp | .l | 15 | 21.866667 | 48 | 0.571429 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 2d29853052bdcf3031cee95cc960292b0785eaf48930f52753b45c461ebbbacd | 41,988 | [
-1
] |
41,989 | Compare dwg - CDWG.lsp | nobodybusiness_lisp-backup/!lsp/Compare dwg - CDWG.lsp |
;porownywanie plikow dwg -> zmiany, usunięcia
(vl-load-com)
(defun c:cdwg (/ app cdoc fichier dbx name sp1
sp2 e obj lstmodif lstajout
lstsuppr lst obj sup h
)
(setq app (vlax-get-acad-object)
cdoc (vla-get-activedocument app)
name (vla-get-fullname cdoc)
s (getvar "date")
t1 (* 86400.0 (- s (fix s)))
)
(while (not fichier)
(setq fichier
(getfiled
"Select OLD Plan"
(strcat (vl-filename-directory (vla-get-fullname cdoc))
"/"
)
"dwg"
4
)
)
)
(vla-open (if (< (atoi (substr (getvar "ACADVER") 1 2)) 16)
(setq dbx (vlax-create-object "ObjectDBX.AxDbDocument"))
(setq dbx (vlax-create-object
(strcat "ObjectDBX.AxDbDocument."
(substr (getvar "ACADVER") 1 2)
)
)
)
)
fichier
)
(vla-saveas
cdoc
(strcat (substr name 1 (- (strlen name) 4))
" Changes"
)
)
(setq sp1 (vla-get-modelspace cdoc)
sp2 (vla-get-modelspace dbx)
)
(vlax-for lay (setq layers (vla-get-layers cdoc))
(vlax-put lay 'color 253)
(vla-put-lock lay :vlax-false)
)
(vlax-for lay (vla-get-layers dbx)
(vla-put-lock lay :vlax-false)
)
(vlax-for ent sp1
(setq lst1 (cons ent lst1))
)
(defun equal-objects (obj1 obj2)
(vl-every
(function
(lambda (p)
(and
(or
(not (vlax-property-available-p obj1 p))
(equal (vlax-get obj1 p)
(vlax-get obj2 p)
1e-9
)
)
(or
(not
(and (= (vla-get-ObjectName obj1) "AcDbBlockReference")
(= (vla-get-HasAttributes obj1))
)
)
(and
(vl-every 'equal-objects
(vlax-invoke obj1 'getAttributes)
(vlax-invoke obj2 'getAttributes)
)
(= (vla-get-count
(vla-item (vla-get-blocks cdoc) (vla-get-name obj2))
)
(vla-get-count
(vla-item (vla-get-blocks dbx) (vla-get-name obj1))
)
)
)
)
)
)
)
'(ObjectName Center Radius Coordinates
StartPoint EndPoint StartAngle EndAngle
MajorAxis RadiusRatio TextString InsertionPoint
Width Height Rotation XScaleFactor
YScaleFacor ZScaleFactor Layer
)
)
)
(vlax-put (vla-add layers "0.Ajouts") 'color 172)
(vlax-put (vla-add layers "0.Modications") 'color 10)
(vlax-put (vla-add layers "0.Suppressions") 'color 92)
(vlax-for ent sp2
(if (handent (setq h (vla-get-handle ent)))
(if (entget (handent h))
(progn
(setq e (vla-handletoobject cdoc h))
(cond ((and (not (equal-objects ent e))
(vlax-property-available-p e 'layer)
)
(vla-put-layer e "0.Modications")
)
)
(setq lst1 (vl-remove e lst1))
)
(vla-put-layer
(car (vlax-invoke dbx 'CopyObjects (list ent) sp1))
"0.Suppressions"
)
)
(vla-put-layer
(car (vlax-invoke dbx 'CopyObjects (list ent) sp1))
"0.Suppressions"
)
)
)
(if lst1
(foreach n lst1
(cond ((vlax-property-available-p e 'layer)
(vla-put-layer n "0.Ajouts")
)
)
)
)
(vlax-for b (vla-get-blocks cdoc)
(vlax-for i b
(vla-put-color i 256)
)
)
(vlax-release-object dbx)
(vla-purgeall cdoc)
(vla-save cdoc)
(setq s (getvar "date")
t2 (* 86400.0 (- s (fix s)))
tt (- t2 t1)
)
(if (> tt 60)
(progn
(setq sec (rem tt 60)
mn (strcat (rtos (/ (- tt sec) 60) 2 0) " mn ")
sec (strcat (rtos sec 2 0) " s")
)
)
(progn
(setq sec (strcat (rtos tt 2 0) " s")
mn "0 mn "
)
)
)
(alert
(strcat
"Igal Averbuh Compare Legend\n\nAdditions are GREEN \nModifications in RED\nDeletions in BLUE \n\nTime of comparing : "
mn
sec
)
)
)
(c:cdwg) | 5,159 | Common Lisp | .l | 166 | 18.192771 | 126 | 0.44946 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 08fe89526e7ce3b5159c14e25e891d525dc1f7fd785b68ef24866074d8024f66 | 41,989 | [
-1
] |
41,990 | @NUMERACJA_STUDNI.lsp | nobodybusiness_lisp-backup/!lsp/@NUMERACJA_STUDNI.lsp | ;numeracja studni wyrzuconych z qgis
(defun c:@numeracja_studni (/ $filepath $fil $fl $data $el $x $y $HH $LU $UL $NR $liczba $wsp)
(vl-load-com)
(setq $lsp_adress_num "C:/Users/Desktop/Piotr P/test/automatyczna numeracja dzialek")
(setq $filepath $lsp_adress_num) ; ustawia sciezke do folderu z dwg
(setq $fl (open (strcat $lsp_adress_num "/pts.txt") "r")) ; otwarcie do odczytania pliku z danymi
(while (setq $fil (read-line $fl)) ;petla do odczytu linni
(if (not (null $fil))
(setq $data (append $data (list $fil))) ; przypisanie danych do listy
)
)
(close $fl) ; zamkniecie pliku txt
(while $data ; petla przypisania danych z listy do zmiennych
;=======WSP X=============
(setq $el (car $data)) ; odczyt wspl x
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $x (atof $el))
(setq $data (cdr $data)) ; skrocenie listy o x
;=======WSP Y=============
(setq $el (car $data)) ; odczyt wspl y
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $y (atof $el))
(setq $data (cdr $data)) ; skrocenie listy o y
;=======ULICA=============
(setq $el (car $data)) ; odczyt wspl UL
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $UL $el)
(setq $data (cdr $data)) ; skrocenie listy o UL
;=======NUMER=============
(setq $el (car $data)) ; odczyt wspl NR
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $NR $el)
(setq $data (cdr $data)) ; skrocenie listy o NR
;=======Wstawienie bloku============
(setq $x (rtos $x))
(setq $y (rtos $y))
(setq $wsp (strcat $x "," $y))
(command "-insert" "numeracja_blok" $wsp "0.5" "0.5" "" "" $NR $UL)
;(command "_explode" "_l") ; explode tabelki
)
)
| 1,955 | Common Lisp | .l | 45 | 38.466667 | 101 | 0.550839 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 23d3bc84976243680a4b067504cba719fe4fa7b3b3aa7e38408206dccb955238 | 41,990 | [
-1
] |
41,991 | @studnie_nazwa.lsp | nobodybusiness_lisp-backup/!lsp/@studnie_nazwa.lsp | (defun c:@studnie_nazwa ()
(vl-load-com)
(setq $blk nil)
(setq $blk (ssget '((0 . "INSERT") (2 . "numeracja_blok"))))
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; asystent
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "ULICA")
(setq $VAL_ZM (CDR (assoc 1 $LST)))
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $pt (getpoint "Pkt wstawienia:"))
(command "insert" "nazwa_studni" $pt "1" "1" "" $VAL_ZM)
;przesuniecie studni po wstawieniu
(setq $studnia (entlast))
(command "_move" $studnia "" $pt pause)
(princ)
)
| 786 | Common Lisp | .l | 24 | 27.916667 | 63 | 0.566138 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | f1378a6b4ec16d208f44194eb988fa7655dff71e65198e287083d229323f9044 | 41,991 | [
-1
] |
41,992 | @layer_select.lsp | nobodybusiness_lisp-backup/!lsp/@layer_select.lsp | ;wybor warstwy
(defun c:@layer_select()
(setq TargEnt (car (entsel "\nSelect object on layer to select: ")))
(setq TargLayer (assoc 8 (entget TargEnt)))
(sssetfirst nil (ssget "_X" (list TargLayer)))
(princ)
) | 219 | Common Lisp | .l | 7 | 29 | 70 | 0.685446 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 57e40868dbe0d2841bc083a5e0d316379ecab8f942da0596b98f77bd1783ad19 | 41,992 | [
-1
] |
41,993 | @przyłącza_wszystkie.lsp | nobodybusiness_lisp-backup/!lsp/@przyłącza_wszystkie.lsp | (defun c:@przylacza_wszytskie ()
(setq $SS (ssget '((0 . "LWPOLYLINE")))) ; przyjecie z zaznaczenie tylko plinii
(setq $zm_linni 0)
(while
(setq $line (ssname $ss $zm_linni))
;znajduje wszytskie pkt plinni i przypisuje je do listy
(setq $plist (mapcar 'cdr
(vl-remove-if-not
'(lambda (x) (= (car x) 10))
(entget $line)
)
)
)
(setq $i 1)
(setq $p_list_length (length $plist))
(while $plist ; petla przypisania wszystkich pkt
(setq $i_string (rtos $i 2 0))
(setq $name (read (strcat "$p" $i_string)))
(set $name (car $plist))
(setq $plist (cdr $plist))
(setq $i (+ 1 $i))
)
(setq $n 1)
(setq $dline_zm 0)
(setq $dline1 0)
(while (< $n $p_list_length)
(setq $n_string (rtos $n 2 0))
(setq $pA (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $n (+ 1 $n))
(setq $n_string (rtos $n 2 0))
(setq $pB (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $dline_zm (+ (fix (distance $pA $pB)) 1))
(setq $dline1 (+ $dline1 $dline_zm))
)
(setq $angle (@RtD (angle $pB $pA))) ; ustawienie obrotu tekstu zgodnie ze standardem
(if (or
(and (> $angle 90) (< $angle 180))
(and (> $angle 180) (< $angle 270))
)
(setq $angle (- $angle 180))
)
(setq $dline (+ 3 $dline1)) ;domyslne dodanie 3
;Sugeruj d³ugoœc z cada?
(setq $wsp (mapcar '(lambda (x y) (+ (* 0.5 x) (* 0.5 y))) ;dodaje polowy z listy punktow
$pA
$pB
)
)
(command "-insert" "dl_linni" $wsp "" "" $angle "" "" "" "" "" "" "" "" $dline) ;wstawia blok dlugosci linni
(setq $zm_linni (+ $zm_linni 1))
)
)
;=======SET NUMBER OF OTWR========
(defun c:@otw ()
(setq ss (ssget '((0 . "INSERT") (2 . "dl_linni")))) ; only specific block
(setq i 0)
(setq ile_otw (getint "Ile otworów?\n"))
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND"))
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "ILOTW")
(progn
(setq $VAL (atoi (cdr (assoc 1 $LST))))
(setq $VAL_new ile_otw)
(setq $VAL (subst (cons 1 (rtos $VAL_new 2 0)) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
(@otworwka ss)
)
)
;=======Radians to Degrees======
(defun @RtD (r) (* 180.0 (/ r pi)))
| 2,910 | Common Lisp | .l | 84 | 26.190476 | 114 | 0.480483 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | fda56acec63325469cc7d2ea84e1078ddf154e66f5623a548384447bf1751d4d | 41,993 | [
-1
] |
41,994 | @ON,OFF.lsp | nobodybusiness_lisp-backup/!lsp/@ON,OFF.lsp | ;widoczność warstw
(defun @OFF ()
(setq $selected_layers (ssget))
(setq $i 0)
(setq $zaznaczenie 0)
(command "-layer" "_of" "" "_y" "")
(while $zaznaczenie
(setq $zaznaczenie (ssname $selected_layers $i))
(setq $warstwa (cdr (assoc 8 (entget $zaznaczenie))))
(command "-layer" "_on" $warstwa "")
(princ)
(setq $i (1+ $i))
)
(princ)
)
(defun @OFF_BASE_FUNCTION ($selected_layers)
(setq $i 0)
(setq $zaznaczenie 0)
(command "-layer" "_of" "" "_y" "")
(while $zaznaczenie
(setq $zaznaczenie (ssname $selected_layers $i))
(setq $warstwa (cdr (assoc 8 (entget $zaznaczenie))))
(command "-layer" "_on" $warstwa "")
(princ)
(setq $i (1+ $i))
)
(princ)
)
(defun @OFF_BASE()
(@OFF_BASE_FUNCTION $selected_layers_for_function)
)
(defun @base ()
(setq $selected_layers_for_function (ssget))
)
(defun @ON ()
(command "-layer" "_on" "" "_y" "")
)
(defun @OFF_one ()
(setq $selected_layers (ssget))
(setq $i 0)
(setq $zaznaczenie 0)
(while $zaznaczenie
(setq $zaznaczenie (ssname $selected_layers $i))
(setq $warstwa (cdr (assoc 8 (entget $zaznaczenie))))
(command "-layer" "_of" $warstwa "_y" "")
(setq $i (1+ $i))
)
(princ)
)
(defun @lock ()
(setq $selected_layers (ssget))
(setq $i 0)
(setq $zaznaczenie 0)
(while $zaznaczenie
(setq $zaznaczenie (ssname $selected_layers $i))
(setq $warstwa (cdr (assoc 8 (entget $zaznaczenie))))
(command "-layer" "_lo" $warstwa "_y" "")
(setq $i (1+ $i))
)
(princ)
)
(defun @lock_all ()
(command "-layer" "_lo" "" "_y" "")
)
(defun @unlock_all ()
(command "-layer" "_un" "" "_y" "")
)
(defun @unlock ()
(setq $selected_layers (ssget))
(setq $i 0)
(setq $zaznaczenie 0)
(while $zaznaczenie
(setq $zaznaczenie (ssname $selected_layers $i))
(setq $warstwa (cdr (assoc 8 (entget $zaznaczenie))))
(command "-layer" "_un" $warstwa "_y" "")
(setq $i (1+ $i))
)
(princ)
)
(defun @freeze ()
(setq $selected_layers (ssget))
(setq $i 0)
(setq $zaznaczenie (ssname $selected_layers $i))
(while $zaznaczenie
(setq $zaznaczenie (ssname $selected_layers $i))
(setq $warstwa (cdr (assoc 8 (entget $zaznaczenie))))
(command "-layer" "_f" $warstwa "_y" "")
(setq $i (1+ $i))
)
(princ)
)
(defun @unfreeze ()
(command "-layer" "_t" "" "_y" "")
)
(defun c:@layer_select()
(setq TargEnt (car (entsel "\nSelect object on layer to select: ")))
(setq TargLayer (assoc 8 (entget TargEnt)))
(sssetfirst nil (ssget "_X" (list TargLayer)))
(princ)
)
(defun c:@freeze_map()
(command "-layer" "_f" "mapa" "_y" "")
)
(defun c:@unfreeze_map()
(command "-layer" "_t" "mapa" "_y" "")
)
(defun c:@nr_studni_magistrala_on()
(command "-layer" "_f" "pkt_adresy_hh" "")
)
(defun c:@nr_studni_magistrala_off()
(command "-layer" "_t" "pkt_adresy_hh" "")
)
(defun c:@viewports_on()
(command "-layer" "_f" "VIEWPORTS" "")
)
(defun c:@viewports_off()
(command "-layer" "_t" "VIEWPORTS" "")
)
(DEFUN C:XLO ( / lay) (PRINC "\nXref layer off")
(WHILE (princ (strcat "\nTurned off " (setq LAY (cdr (assoc 8 (entget (car (NENTSEL "\nSelect item: "))))))))(command "-layer" "of" LAY ""))(princ)) | 3,313 | Common Lisp | .l | 120 | 23.741667 | 148 | 0.580919 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 25a16b1c060977131471da309c656805f8fb48aa0d72f160e90a5a4b7294a9c5 | 41,994 | [
-1
] |
41,995 | @KABEL3x4.lsp | nobodybusiness_lisp-backup/!lsp/@KABEL3x4.lsp | ;wsatwienie schematu kabla 3x4
(defun C:@KABEL3x4 ()
(vl-load-com)
;1.1 SPR BIEZACA WARSTWE
(setq curent_layer (getvar "CLAYER"))
;1.2 USTAW WARSTWE KABLI (pierwszy czerwony)
(setq new_layer "�wiat bud.")
(command "-layer" "s" new_layer "")
(command "cecolor" 1)
;2.1 PL
(command "polyline" pause pause pause pause "")
;2.2 ODSUNI�CIE i zmiana koloru
;kolory 12J
(setq i 0)
(setq color_list '(
5
7
84
1
5
7
84
1
5
7
84
)
)
(while (< i 11)
(progn
(setq color (nth i color_list))
(setq last_line (entlast))
(command "offset" 15.6 last_line "99999999999.999,-9999999999999999.0,0.0" "")
(setq last_line (entget (entlast)))
(setq last_line (subst (cons 62 color) (assoc 62 last_line) last_line)) ; podmiana definicji promienia
(entmod last_line) ; zastosowanie zmiany
(setq i (+ i 1))
)
)
;3. wstawianie opisu
(setq $wsp (getpoint))
(command "-insert" "kabel3x4" $wsp "" "" "")
(command "explode" "l" "" )
;4. USTAW WARSTE NA POPRZEDNIA
(command "-layer" "s" curent_layer "")
(command "cecolor" 256)
(print)
)
| 1,416 | Common Lisp | .l | 48 | 20 | 110 | 0.502209 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 89bfdc158a590ef83e0a97e2a6130e44869b7f0b2c70eb3d01d1f1afd8146033 | 41,995 | [
-1
] |
41,996 | liczenie_potrzebnego_zbrojenia.lsp | nobodybusiness_lisp-backup/!lsp/liczenie_potrzebnego_zbrojenia.lsp | ;funkcja do liczenia wystarczanjacego zbrojenia do plyty
(defun C:az ()
(progn
;ustalenie srednic zbrojenia
(setq srednice (list 8 10 12 16 20 25))
(setq liczba_srednic (length srednice))
;podanie wartosci z abc przez uzytkownika
(setq
wartosc_abc (getreal "Wprowadz wartosc z ABC: ")
)
(setq indeks_srednic 0)
;petla srednic
(repeat liczba_srednic
(progn
(setq srednica (nth indeks_srednic srednice))
(setq rozstaw 30)
(setq wartosc_rozstawu 0)
;petla rozstawu
(while (and (< wartosc_rozstawu wartosc_abc) (> rozstaw 5))
(progn
(setq wartosc_rozstawu_co_1
(* (* 314.1592654 (/ srednica 2)) (/ srednica 2))
);setq
(setq wartosc_rozstawu (/ wartosc_rozstawu_co_1 rozstaw))
(setq rozstaw (1- rozstaw))
);progn
);while - petlarozstawu
(princ srednica)
(princ "\\")
(if (= rozstaw 5)
(princ "brak")
(princ (1+ rozstaw));else
);if
(princ " ")
(setq indeks_srednic (1+ indeks_srednic))
);progn
);repeat -petla srednic
(princ)
);progn
);defun | 1,107 | Common Lisp | .l | 40 | 22.5 | 65 | 0.629213 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 1eac236f4db20a79ec55114d487f51c27f2353227fba550dd517ae399a420573 | 41,996 | [
-1
] |
41,997 | @tabelka.lsp | nobodybusiness_lisp-backup/!lsp/@tabelka.lsp | (defun c:@tabelka ()
(vl-load-com)
(setq $changeOnlyProjAndDate (getstring "Zmiana tylko projektabta i daty <y>"))
(if (= $changeOnlyProjAndDate "")
(progn
(setq $NAZWA_PROJEKTU "s")
(setq $SKALA "s")
(setq $NR-RYS "s")
(setq $inne_oznacz "")
)
(progn
(setq $NAZWA_PROJEKTU (getstring "Nazwa projektu:"))
(setq $SKALA (getstring "Skala:"))
(setq $NR-RYS (getstring "nr rys:"))
(setq $inne_oznacz (strcase (getstring "Inne oznaczenia:<s/null>")))
)
)
(setq $projektant_inicj (strcase (getstring "Projektant:<PK MG MN PP>")))
(setq $PROJEKTANT nil)
(setq $PROJEKTANT
(cond
((= $projektant_inicj "PK") "")
((= $projektant_inicj "MG") "")
((= $projektant_inicj "MN") "")
(t " ")
)
)
(setq $asystent "")
(setq d (rtos (getvar "CDATE") 2 6)
;get the date and time and convert to text
yr (strcat "20" (substr d 3 2))
;extract the year
mo (substr d 5 2)
;extract the month
day (substr d 7 2)
;extract the day
) ;setq
;===========================================
(setq $blk nil)
(setq $blk (ssget '((0 . "INSERT") (2 . "Tabelka5"))))
(setq $VAL 0)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "PROJEKT1")
(if (/= $NAZWA_PROJEKTU "s")
(progn
(setq $VAL_new $NAZWA_PROJEKTU)
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; STALE
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "PROJEKT2")
(progn
(if (= $inne_oznacz "s")
(progn
(setq $VAL_new "Budowa sieci FTTH - Orange Polska S.A.")
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; SKALA
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "SKALA")
(if (/= $SKALA "s")
(progn
(setq $VAL_new $SKALA)
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; NR RYS
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "NR-RYS")
(if (/= $NR-RYS "s")
(progn
(setq $VAL_new $NR-RYS)
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; PROJEKTANT
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "PROJEKTOWAL")
(progn
(setq $VAL_new $PROJEKTANT)
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; DATA
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "DATA-PR")
(progn
(setq $VAL_new (strcat day "-" mo "-" yr))
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; SPRAWDZIL
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "SPRAWDZIL")
(progn
(setq $VAL_new "")
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; DATA
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "DATA-SPR")
(progn
(setq $VAL_new (strcat day "-" mo "-" yr))
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; asystent
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "RYSOWAL")
(progn
(setq $VAL_new $asystent)
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; DATA
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "DATA-RYS")
(progn
(setq $VAL_new "")
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
)
(defun c:@nr_rys ()
(setq $blk nil)
(setq $blk (ssget '((0 . "INSERT") (2 . "Tabelka5"))))
(setq $VAL 0)
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(setq $nr_rys (getint "Nr_rys:"))
(if (= $nr_rys 0)
(setq $prefix (getstring "Prefix:"))
(progn
(while (not (equal $name "SEQEND"))
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "NR-RYS")
(progn
(setq $VAL_new (strcat $prefix (rtos $nr_rys 2 0)))
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
)
)
)
| 7,990 | Common Lisp | .l | 261 | 23.586207 | 82 | 0.502012 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 1fa913be72d8d1b0e18608bb91ed6b7d97aef25375ee6a1de3c12f8bfe3181a4 | 41,997 | [
-1
] |
41,998 | @SUM_D_LINE.lsp | nobodybusiness_lisp-backup/!lsp/@SUM_D_LINE.lsp | (defun @SUM_D_LINE ()
(setq $dline_zm 0)
(setq $dline1 0)
(if (setq $SS (ssget '((0 . "LWPOLYLINE")))) ; przyjecie z zaznaczenie tylko plinii
(progn
(setq $ss_length (sslength $ss))
(setq $ss_i 0)
(while (< $ss_i $ss_length)
(setq $line (ssname $ss $ss_i))
;znajduje wszytskie pkt plinni i przypisuje je do listy
(setq $plist (mapcar 'cdr
(vl-remove-if-not
'(lambda (x) (= (car x) 10))
(entget $line)
)
)
)
(setq $i 1)
(setq $p_list_length (length $plist))
(while $plist ; petla przypisania wszystkich pkt
(setq $i_string (rtos $i 2 0))
(setq $name (read (strcat "$p" $i_string)))
(set $name (car $plist))
(setq $plist (cdr $plist))
(setq $i (+ 1 $i))
)
(setq $n 1)
(while (< $n $p_list_length)
(setq $n_string (rtos $n 2 0))
(setq $pA (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $n (+ 1 $n))
(setq $n_string (rtos $n 2 0))
(setq $pB (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $dline_zm (+ (fix (distance $pA $pB)) 1))
(setq $dline1 (+ $dline1 $dline_zm))
)
(setq $ss_i (+ 1 $ss_i))
)
)
)
(print (+(/ $dline1 100) 1) )
(princ)
)
| 1,500 | Common Lisp | .l | 43 | 23.55814 | 87 | 0.4372 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 7b3448636b94bda029bb86afc572e5afc4617114b06e199f80bdc8aa5ab61eca | 41,998 | [
-1
] |
41,999 | @angle.lsp | nobodybusiness_lisp-backup/!lsp/@angle.lsp | ;zwraca pelen kat
(defun @angle ()
(setq $p1 (getpoint "Wskaz 1 pkt"))
(setq $p2 (getpoint "Wskaz 2 pkt"))
(setq $angle (@RtD (angle $p2 $p1)))
(@FIX $angle)
)
(defun @fix ($unfixed)()
(setq $fixed (fix $unfixed))
(setq $delta ( - $unfixed $fixed))
(if (> $delta 0.5)
(setq $fixed (+ $fixed 1))
)
(print $fixed)
)
| 340 | Common Lisp | .l | 15 | 20 | 40 | 0.578462 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | c47519814fe1336ba675564f9051f7a9eace11d083d8945852c250f9d92325b6 | 41,999 | [
-1
] |
42,000 | @newDlPoBudynku.lsp | nobodybusiness_lisp-backup/!lsp/@newDlPoBudynku.lsp | ;dlugosci po buynku
(defun c:@dll ()
(vl-load-com)
(setq $okw nil)
(setq $okw (ssget '((0 . "LWPOLYLINE"))))
(setq $okw_name (ssname $okw 0))
(setq $i 1)
(setq $okw_length_sum 0)
(setq $plist (mapcar 'cdr
(vl-remove-if-not
'(lambda (x) (= (car x) 10))
(entget $okw_name)
)
)
)
(setq $p_list_length (length $plist))
(while $plist ; petla przypisania wszystkich pkt
(setq $i_string (rtos $i 2 0))
(setq $name (read (strcat "$p" $i_string)))
(set $name (car $plist))
(setq $plist (cdr $plist))
(setq $i (+ 1 $i))
)
(print $p1)
(setq $n 1)
(while (< $n $p_list_length)
(setq $n_string (rtos $n 2 0))
(setq $pA (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $n (+ 1 $n))
(setq $n_string (rtos $n 2 0))
(setq $pB (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $okw_length (distance $pA $pB))
(setq $okw_length_sum (+ $okw_length_sum $okw_length))
)
(setq $okw_length_sum (/ (fix $okw_length_sum) 100))
(setq $zapas_tras(+ 1 (fix (* 0.1 $okw_length_sum))))
(setq $okw_tras (+ $okw_length_sum $zapas_tras))
(setq $zapas 10)
(setq $liczba_poz (getint "Liczba pi�ter:"))
(setq $zapas_pion (* $liczba_poz 4))
(setq $okw_inst (+ $okw_tras $zapas $zapas_pion))
(print $okw_tras)
(print $okw_inst)
(princ)
(@zmianaWStrzalce $okw_tras $okw_inst)
)
(defun @zmianaWStrzalce(okw_tras okw_inst)
(vl-load-com)
(setq ss (ssget))
(setq $ent_ename (ssname ss 0))
(setq $ent_data (entget $ent_ename))
(setq $text (cdr (assoc '304 $ent_data)))
(setq x1 (strcat "d�. tras. " (rtos okw_tras 2 0) "m"))
(setq x2 (strcat "d�. inst. " (rtos okw_inst 2 0) "m"))
(setq $newText (vl-string-subst x1 "d�. tras. _" $text))
(setq $newText2 (vl-string-subst x2 "d�. inst. _" $newText))
(setq $VAL (subst (cons 304 $newText2) (assoc 304 $ent_data) $ent_data))
(entmod $VAL)
(entupd $ent_ename)
)
| 2,084 | Common Lisp | .l | 60 | 28.616667 | 74 | 0.554169 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 36b8d0d30ba80f3a4976ceda97e9dbab69a31c524164e4d6b9d6d17a5a15ce65 | 42,000 | [
-1
] |
42,001 | @viewport_zoom.lsp | nobodybusiness_lisp-backup/!lsp/@viewport_zoom.lsp | (defun @viewport_zoom ()
(command "mspace"
"zoom" "_e"
"zoom" "_ob" pause ""
)
(princ)
(setq get (ssget "x" '((0 . "viewport") (-4 . ">") (69 . 1))))
(setq $ent_ename (ssname get 0))
(setq $ent_data (entget $ent_ename))
(setq heightInPaper (cdr (assoc 41 $ent_data)))
(setq viewHeight (cdr (assoc 45 $ent_data)))
(setq scale (/ (* 0.2 viewHeight) heightInPaper))
(command "zoom" "_s" scale)
(command "._pspace")
)
| 475 | Common Lisp | .l | 15 | 26.6 | 65 | 0.552402 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 178c0c7784d8ed68b3864b69f266250f53b48bffb0a8ed82f444bfb6929e4df5 | 42,001 | [
-1
] |
42,002 | @numeracja_dzialek.lsp | nobodybusiness_lisp-backup/!lsp/@numeracja_dzialek.lsp |
;numeracja pkt adresowych wyrzuconych z qgis
(defun @numeracja_dzialek (/ $filepath $fil $fl $data $el $x $y $HH $LU $UL $NR $liczba $wsp)
(vl-load-com)
(setq $lsp_adress_num "C:/Users/Desktop/test/Signaline/automatyczna numeracja dzialek")
(setq $filepath $lsp_adress_num) ; ustawia sciezke do folderu z dwg
(setq $fl (open (strcat $lsp_adress_num "/pts.txt") "r")) ; otwarcie do odczytania pliku z danymi
(while (setq $fil (read-line $fl)) ;petla do odczytu linni
(if (not (null $fil))
(setq $data (append $data (list $fil))) ; przypisanie danych do listy
)
)
(close $fl) ; zamkniecie pliku txt
(while $data ; petla przypisania danych z listy do zmiennych
;=======WSP X=============
(setq $el (car $data)) ; odczyt wspl x
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $x (atof $el))
(setq $data (cdr $data)) ; skrocenie listy o x
;=======WSP Y=============
(setq $el (car $data)) ; odczyt wspl y
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $y (atof $el))
(setq $data (cdr $data)) ; skrocenie listy o y
;=======HOUSE HOLDY=============
(setq $el (car $data)) ; odczyt wspl HH
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $HH (atof $el))
(setq $data (cdr $data)) ; skrocenie listy o HH
;=======LOKALE USLUGOWE=============
(setq $el (car $data)) ; odczyt wspl LU
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $LU (atof $el))
(setq $data (cdr $data)) ; skrocenie listy o LU
;=======ULICA=============
(setq $el (car $data)) ; odczyt wspl UL
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $UL $el)
(setq $data (cdr $data)) ; skrocenie listy o UL
;=======NUMER=============
(setq $el (car $data)) ; odczyt wspl NR
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $NR $el)
(setq $data (cdr $data)) ; skrocenie listy o NR
;=======Wstawienie bloku============
(setq $x (rtos $x))
(setq $y (rtos $y))
(setq $wsp (strcat $x "," $y))
(setq $UL (strcat "ul. " $UL " " $NR))
(setq $NR "")
(if (= $HH 0) ; jezeli 0 HH
(progn
(setq $liczba (rtos $LU))
(setq $liczba (strcat "HH:" $liczba "LU"))
)
(progn
(if (= $LU 0) ;jezeli 0 LU
(progn
(setq $liczba (rtos $HH))
(setq $liczba (strcat "HH:" $liczba))
)
(progn ; jezeli HH i Lu <>0
(setq $liczba (rtos $HH))
(setq $liczba (strcat "HH:" $liczba " LU:" (RTOS $LU)))
)
)
)
)
(command "-insert" "numeracja_blok_2" $wsp "" "" "" $liczba $UL)
;(command "_explode" "_l") ; explode tabelki
)
)
| 3,077 | Common Lisp | .l | 77 | 33.636364 | 101 | 0.512342 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 84d23c36e5eef8b18f4c38c2932e1e51d043041136c019d913f77710327fbd21 | 42,002 | [
-1
] |
42,003 | @MIESZKANIA_USŁUGI_ZMIANA_NUMERACJI.lsp | nobodybusiness_lisp-backup/!lsp/@MIESZKANIA_USŁUGI_ZMIANA_NUMERACJI.lsp | ;zmiana numeracji w blokach
(defun c:@mieszkania_u�ugi_zmiana_numeracji ()
(setq ss (ssget))
(setq i 0)
(setq plus_minus (getint "+/-:"))
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "USLUGI_NUMER")
(progn
(setq $VAL (atoi (substr (cdr (assoc 1 $LST)) 2)))
(setq $VAL_new (+ $VAL plus_minus))
(setq $VAL (subst (cons 1 (strcat "U" (rtos $VAL_new 2 0))) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(if (= $attag "NUMER_MIESZKANIA")
(progn
(setq $VAL (atoi (cdr (assoc 1 $LST))))
(setq $VAL_new (+ $VAL plus_minus))
(setq $VAL (subst (cons 1 (rtos $VAL_new 2 0)) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
)
| 1,229 | Common Lisp | .l | 38 | 24.447368 | 92 | 0.495382 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 583b3d4e4e3442a797d23ed58cbc094162591793fb9801c80503a535c405bc8e | 42,003 | [
-1
] |
42,004 | @HH_SCHEMAT.lsp | nobodybusiness_lisp-backup/!lsp/@HH_SCHEMAT.lsp | ;tworzenie calej jednej mufy z adresow zaznaczonych
(defun c:@HH_SCHEMAT ()
(vl-load-com)
(setq ss (ssget))
(setq i 0)
(setq $VAL "")
(setq fromTop 0) ;nr HH od g�ry
(setq pta (getpoint "PKT wstawienia:"))
(while (< i (sslength ss))
;#ulice
(setq $VAL "")
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND"))
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "ULICA")
(setq $VAL (substr (cdr (assoc 1 $LST)) (+ 3 (vl-string-search "." (cdr (assoc 1 $LST))))))
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
;#ulice
;#HH
(setq HHandLUvalue 1)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND"))
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "HH")
(if (not (equal "HH:1" (cdr (assoc 1 $LST))))
(progn
(setq HHandLU (cdr (assoc 1 $LST)))
(setq $val (strcat $VAL " (" HHandLU ")"))
(setq HHandLUvalue 0)
(setq asciiHHandLu (vl-string->list HHandLU))
(foreach n asciiHHandLu
(if (and (>= n 48) (<= n 57))
(setq HHandLUvalue (+ HHandLUvalue (- n 48)))
)
)
)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
;#HH
(setq ptx (rtos (car pta) 2 6))
(setq pty (rtos (- (cadr pta) (* fromTop 15.6)) 2 6))
(setq ptz (rtos (caddr pta) 2 6))
(setq ptb (strcat ptx "," pty "," ptz))
(command "-insert" (strcat "ULICA_NR_HH_" (itoa HHandLUvalue)) ptb "" "" "" $VAL)
(command "@burst" "_L" "")
(setq i (+ i 1))
(setq fromTop (+ fromTop HHandLUvalue))
)
(command "explode" pause)
)
(defun c:@usuwanie_attdef ()
(setq ss (ssget "all" '((0 . "ATTDEF"))))
(command "_erase" ss "")
)
(defun c:@test ()
(setq i 1)
(setq st "test_")
(setq sti (strcat st (itoa i)))
)
| 2,304 | Common Lisp | .l | 73 | 24.547945 | 100 | 0.505603 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | f6c04ce83b2e3b1fdadaa7ca86209054e98d1fcb1be84655fcce577674122e02 | 42,004 | [
-1
] |
42,005 | @uklad_mieszkan_kable_osd.lsp | nobodybusiness_lisp-backup/!lsp/@uklad_mieszkan_kable_osd.lsp | (defun c:@uklad_mieszkan ()
(vl-load-com)
;pkt wstepny
(setq repeatTubes 1)
(setq 1spawNaStart (getstring "Czy jeden spaw na start? : <y>"))
;typ kabla np 2x6
(setq numberOfCabelTubes (getint "Ile tub :"))
(setq numberOfTubesWkl (getint "Ile wlokien w tubie :"))
;liczba wykorzystanych tub
(setq numberOfUsedTubesStart (getint "Pierwsza uzyta tuba :"))
(while (/= repeatTubes 0)
(@tubes)
(setq point (getpoint "Pkt wstawienia:"))
(@Mtext)
(setq numberOfUsedTubesStart (+ numberOfUsedTubesStart numerOfUsedTubes))
(setq ifReapeat (getstring "Czy nastepne tuby? : <y>"))
(if (= ifReapeat "n")
(setq repeatTubes 0)
)
)
)
(defun @tubes ()
(setq numerOfUsedTubes (getint "Ile uzytych tub"))
;przedzial wlokien / lub do konca
(setq fromUsedTubes (getstring "Przedzaia³ w³ókien - k - do koñca <formTubes>"))
;piêtro
(setq level (getstring "Ktory poziom :"))
(if (= level "0")
(setq level "parter")
(setq level (strcat "poziom " level))
)
;HH i LU
;(command "@ile_HH_LU" pause pause pause pause pause pause)
(setq HH_ile (getstring "HH ile: "))
(setq LU_ile (getstring "LU ile: "))
(setq HH (getstring "HH od-do:"))
(setq LU (getstring "LU :"))
)
(defun @Mtext ()
;1st line
(setq krotnoscKabla (rtos (* numberOfCabelTubes numberOfTubesWkl) 2 0))
(setq pierwszeWloknoTuby (rtos (+ (* (- numberOfUsedTubesStart 1) numberOfTubesWkl) 1) 2 0))
(setq ostatnieWloknoTuby (rtos (* (- (+ numberOfUsedTubesStart numerOfUsedTubes) 1) numberOfTubesWkl) 2 0))
(if (= fromUsedTubes "k")
(setq przedzial (strcat pierwszeWloknoTuby "-" krotnoscKabla))
(setq przedzial (strcat pierwszeWloknoTuby "-" ostatnieWloknoTuby))
)
(setq 1line (strcat "OSD_/OKW_/" krotnoscKabla "J_(" przedzial ")"))
;2nd line
(if (> (atoi LU_ile) 0)
(setq 2line (strcat level " (lok. " HH "," LU ") " "(" HH_ile "HH + " LU_ile "LU)"))
(setq 2line (strcat level " (lok. " HH ") " "(" HH_ile "HH)"))
)
;3th line
(setq 3line (strcat "HPC_1626_CT_" (rtos numberOfCabelTubes 2 0) "X" (rtos numberOfTubesWkl 2 0) "G657"))
;4th line
(@colorTube)
;sets 4t line
(@printMtext)
)
(defun @printMtext ()
(command "MTEXT" point point 1line 2line 3line 4line "")
)
(defun @colorTube ()
(setq valueOfUsedTubes numerOfUsedTubes)
(setq valueOfCurrentTube numberOfUsedTubesStart)
(setq 4line "")
(while (> valueOfUsedTubes 0)
(setq colorTuby
(cond
((= valueOfCurrentTube 1) " Cz. ")
((= valueOfCurrentTube 2) " Ni. ")
((= valueOfCurrentTube 3) " Bi. ")
((= valueOfCurrentTube 4) " Zi. ")
((= valueOfCurrentTube 5) " Fi. ")
((= valueOfCurrentTube 6) " Po. ")
((= valueOfCurrentTube 7) " Sz. ")
((= valueOfCurrentTube 8) " Zo. ")
((= valueOfCurrentTube 9) " Br. ")
((= valueOfCurrentTube 10) " Ro. ")
((= valueOfCurrentTube 11) " Ca. ")
((= valueOfCurrentTube 12) " Tu. ")
)
)
(setq pierwszeWloknoTubyColor (rtos (+ (* (- valueOfCurrentTube 1) numberOfTubesWkl) 1) 2 0))
(setq ostatnieWloknoTubyColor (rtos (* valueOfCurrentTube numberOfTubesWkl) 2 0))
(if (/= 1spawNaStart "n")
(setq wlWTubie pierwszeWloknoTubyColor)
(setq wlWTubie (strcat pierwszeWloknoTubyColor "-" ostatnieWloknoTubyColor))
)
(setq temp4line (strcat "Tuba " (rtos valueOfCurrentTube 2 0) ", " colorTuby " w³. " wlWTubie ", "))
(setq 4line (strcat 4line temp4line))
(setq valueOfCurrentTube (+ valueOfCurrentTube 1))
(setq valueOfUsedTubes (- valueOfUsedTubes 1))
)
)
| 3,674 | Common Lisp | .l | 96 | 33.052083 | 110 | 0.636643 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | fccb15bfe8c6e08694753fc0706d80afd6d684136525914b523eb54bb2863d24 | 42,005 | [
-1
] |
42,006 | @textChangePlusOne.lsp | nobodybusiness_lisp-backup/!lsp/@textChangePlusOne.lsp | (defun c:@Y ()
(setq $text (entget (ssname (ssget '((0 . "TEXT"))) 0)))
(setq $dist (getdist "Odleg³osc miedzy elementami:"))
(setq $end (getint "Do jakiego numeru:"))
(setq $i (atoi (cdr (assoc 1 $text))))
(command "_copy" (cdr (assoc -1 $text)) "" "d" (strcat "0,-" (rtos $dist 0 2) ",0"))
(setq $i (+ $i 1))
(while (<= $i $end)
(setq $textToChange (entget (entlast)))
(print $textToChange)
(setq x (rtos $i 0 2))
(entmod (subst (cons 1 x) (assoc 1 $textToChange) $textToChange))
(command "_copy" (cdr (assoc -1 $textToChange)) "" "d" (strcat "0,-" (rtos $dist 0 2) ",0"))
(setq $i (+ $i 1))
)
(command "_erase" (cdr (assoc -1 (entget (entlast)))) "")
(princ)
)
(defun c:@textChangePlusOneX ()
(setq $text (entget (ssname (ssget '((0 . "TEXT"))) 0)))
(setq $dist (getdist "Odleg³osc miedzy elementami:"))
(setq $end (getint "Do jakiego numeru:"))
(setq $i (atoi (cdr (assoc 1 $text))))
(command "_copy" (cdr (assoc -1 $text)) "" "d" (strcat (rtos $dist 0 2) ",0" ",0"))
(setq $i (+ $i 1))
(while (<= $i $end)
(setq $textToChange (entget (entlast)))
(print $textToChange)
(setq x (rtos $i 0 2))
(entmod (subst (cons 1 x) (assoc 1 $textToChange) $textToChange))
(command "_copy" (cdr (assoc -1 $textToChange)) "" "d" (strcat (rtos $dist 0 2) ",0," "0"))
(setq $i (+ $i 1))
)
(command "_erase" (cdr (assoc -1 (entget (entlast)))) "")
(princ)
) | 1,467 | Common Lisp | .l | 36 | 36.361111 | 97 | 0.555168 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 7fb8bf49550d91e5b7e7fa8dc74a824a4d626177756873a8bf81d187b6fe8453 | 42,006 | [
-1
] |
42,007 | @OPP_OSD_OKH_OKW.lsp | nobodybusiness_lisp-backup/!lsp/@OPP_OSD_OKH_OKW.lsp | ;zamiana numeracji na wskazana
(defun c:@OPP_OSD_OKH_OKW ()
(vl-load-com)
(setq $OPP_OSD_OKH_OKW "C:/Users/Desktop/Piotr P/test/OPP_OSD_OKH_OKW/")
(setq $data nil)
(setq $filepath $OPP_OSD_OKH_OKW) ; ustawia sciezke do folderu mieszkan
(setq $fl (open (strcat $filepath "OPP_OSD_OKH_OKW.txt") "r")) ; otwarcie do odczytania pliku z danymi
(while (setq $fil (read-line $fl)) ;petla do odczytu linni
(if (not (null $fil))
(setq $data (append $data (list $fil))) ; przypisanie danych do listy
)
)
(close $fl) ; zamkniecie pliku txt
;MTEXT
(setq ss (ssget "x" '((0 . "mtext"))))
(setq i 0)
(while (< i (sslength ss))
(setq $tempData $data)
;**********************
(while $tempData
(setq alternativeAssoc 0) ;na wypadek wiekszej ilosci znakow
(setq $ent_ename (ssname ss i))
(setq $ent_data (entget $ent_ename))
(setq $text (cdr (assoc '1 $ent_data))) ;szczytanie tekstu
(setq $textAlternative (cdr (assoc '3 $ent_data)))
(setq textToChange (car $tempData))
(setq textToChange (vl-string-left-trim "\"" textToChange))
(setq textToChange (vl-string-right-trim "\"" textToChange))
(setq $tempData (cdr $tempData))
(setq textToInsert (car $tempData))
(setq textToInsert (vl-string-left-trim "\"" textToInsert))
(setq textToInsert (vl-string-right-trim "\"" textToInsert))
(setq $tempData (cdr $tempData))
(if (= $text nil)
() ;do nothing
(progn
(setq $newText (vl-string-subst textToInsert textToChange $text))
(setq $VAL (subst (cons 1 $newText) (assoc 1 $ent_data) $ent_data))
(entmod $VAL)
(entupd $ent_ename)
)
)
(if (= $textAlternative nil)
() ;do nothing
(progn
(setq $newTextAlternative (vl-string-subst textToInsert textToChange $textAlternative))
(setq $VAL (subst (cons 1 $newTextAlternative) (assoc 3 $ent_data) $ent_data))
(entmod $VAL)
(entupd $ent_ename)
)
)
)
;**********************
(setq i (+ i 1))
)
;ARROW
(setq ss (ssget "X" '((0 . "MULTILEADER"))))
(setq i 0)
(while (< i (sslength ss))
(setq $tempData $data)
;**********************
(while $tempData
(setq alternativeAssoc 0) ;na wypadek wiekszej ilosci znakow
(setq $ent_ename (ssname ss i))
(setq $ent_data (entget $ent_ename))
(setq $text (cdr (assoc 304 $ent_data))) ;szczytanie tekstu
(setq textToChange (car $tempData))
(setq textToChange (vl-string-left-trim "\"" textToChange))
(setq textToChange (vl-string-right-trim "\"" textToChange))
(setq $tempData (cdr $tempData))
(setq textToInsert (car $tempData))
(setq textToInsert (vl-string-left-trim "\"" textToInsert))
(setq textToInsert (vl-string-right-trim "\"" textToInsert))
(setq $tempData (cdr $tempData))
(setq $newText (vl-string-subst textToInsert textToChange $text))
(setq $VAL (subst (cons 304 $newText) (assoc 304 $ent_data) $ent_data))
(entmod $VAL)
(entupd $ent_ename)
)
;**********************
(setq i (+ i 1))
)
)
| 3,293 | Common Lisp | .l | 83 | 32.421687 | 106 | 0.579787 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | dd7f1df603e5652000f587eae62d2372245b0b246f2a45d7676d3d94483f9cba | 42,007 | [
-1
] |
42,008 | @oznacznie_dl_l_2.lsp | nobodybusiness_lisp-backup/!lsp/@oznacznie_dl_l_2.lsp | ql(defun c:@oznaczenie_dl_l_2 ()
(vl-load-com)
(if (setq $SS (ssget '((0 . "LWPOLYLINE")))) ; przyjecie z zaznaczenie tylko plinii
(progn
(setq $i 0)
(setq $line 0)
(while $line
(setq $line (ssname $ss $i))
;znajduje wszytskie pkt plinni i przypisuje je do listy
(setq $plist (mapcar 'cdr
(vl-remove-if-not
'(lambda (x) (= (car x) 10))
(entget $line)
)
)
)
(setq $p1 (car $plist)) ;poczatek linni
(setq $p2 (cadr $plist)) ;koniec linni
(setq $wsp (mapcar '(lambda (x y) (+ (* 0.5 x) (* 0.5 y))) ;dodaje polowy z listy punktow
$p1
$p2
)
)
(setq $angle (@RtD (angle $p2 $p1))) ; ustawienie obrotu tekstu zgodnie ze standardem
(if (or
(and (>= $angle 90) (<= $angle 180))
(and (>= $angle 180) (<= $angle 270))
)
(setq $angle (- $angle 180))
)
(setq $dline1 (+ (fix (* (distance $p1 $p2) 1000)) 1)) lo ;odczytana z autocada
(setq $dline (+ 3 $dline1))
(command "-insert" "dl_linni_2" $wsp "1" "" $angle "" "" "" "" "" "" "" "" $dline) ;wstawia blok dlugosci linni
(setq $i (+ 1 $i))
)
)
)
)
;=======Radians to Degrees======
(defun @RtD (r) (* 180.0 (/ r pi)))
| 1,556 | Common Lisp | .l | 40 | 25.8 | 127 | 0.412929 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | ea3e1d1c449e2ac01bb4862d5c692307d77c29f73e24bd84c7bf517f9bc9188a | 42,008 | [
-1
] |
42,009 | @studnie_nazwa_v2.lsp | nobodybusiness_lisp-backup/!lsp/@studnie_nazwa_v2.lsp | (defun c:st ()
(vl-load-com)
(setq $blk nil)
(setq $blk (ssget '((0 . "INSERT") (2 . "numeracja_blok"))))
(setq $ent (ssname $blk 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; asystent
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "ULICA")
(setq $VAL_ZM (CDR (assoc 1 $LST)))
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $pt (getpoint "Pkt wstawienia:"))
;test isSlup?
(if (= isSlup 1)
(setq variable "nazwa_slupa_v1")
(setq variable "nazwa_studni_v2")
)
(command "insert" variable $pt "1" "1" "")
(setq $studnia (entlast))
(command "_explode" $studnia)
(setq $studnia (entlast))
;*******************
(setq $ent_data (entget $studnia))
(setq $text (cdr (assoc 1 $ent_data)))
(setq textToChange "#nazwaStudni")
(setq textToInsert $VAL_ZM)
(setq $newText (vl-string-subst textToInsert textToChange $text))
(setq $VAL (subst (cons 1 $newText) (assoc 1 $ent_data) $ent_data))
(entmod $VAL)
(entupd $studnia)
;*******************
;przesuniecie studni po wstawieniu
;(setq $studnia (entlast))
(command "_move" $studnia "" $pt pause)
(princ)
)
(defun c:sl()
(setq isSlup 1)
(c:st)
(setq isSlup 0)
)
| 1,421 | Common Lisp | .l | 47 | 25.595745 | 70 | 0.577548 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | c87908e88e77130c96c8bf8bb0cc7a43e13f54703d1bb84c1bd24cfdf7ffd2b7 | 42,009 | [
-1
] |
42,010 | @print.lsp | nobodybusiness_lisp-backup/!lsp/@print.lsp | ;wydruk automatyczny wszystkich layoputow
(defun c:@print ()
(vl-load-com)
(foreach lay (layoutlist) (setvar "CTAB" lay) (command "_PLOT" "N" "" "" "" "" "Y" ""))
(command "_qsave")
)
| 196 | Common Lisp | .l | 6 | 29.666667 | 90 | 0.611702 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | c3b61d295b8b7786c23e5bcaf27a17a35947fe60a90dca103c68aff2ce1a2329 | 42,010 | [
-1
] |
42,011 | @dl_insta.lsp | nobodybusiness_lisp-backup/!lsp/@dl_insta.lsp | ;zczytanie dlugosci z blokow + dodanie budynkowych
(defun @dl_insta ()
(vl-load-com)
(setq $blk nil)
(setq $blk (ssget '((0 . "INSERT") (2 . "dl_linni"))))
(setq $i 0)
(setq $blk_length (sslength $blk))
(setq $VAL 0)
(while (< $i $blk_length)
(setq $ent (ssname $blk $i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND"))
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "DL")
(setq $VAL_ZM (CDR (assoc 1 $LST)))
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $VAL_ZM (atoi $VAL_ZM))
(setq $VAL (+ $VAL $VAL_ZM))
(setq $i (+ 1 $i))
)
(print $VAL)
(setq $zapas (getint "Ile po budynku:"))
(setq $val (+ $val $zapas))
(print $VAL)
(princ)
)
(defun c:sui ()
(vl-load-com)
(setq $blk nil)
(setq $blk (ssget '((0 . "INSERT") (2 . "dl_linni"))))
(setq $i 0)
(setq $blk_length (sslength $blk))
(setq $VAL 0)
(while (< $i $blk_length)
(setq $ent (ssname $blk $i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND"))
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "DL")
(setq $VAL_ZM (CDR (assoc 1 $LST)))
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $VAL_ZM (atoi $VAL_ZM))
(setq $VAL (+ $VAL $VAL_ZM))
(setq $i (+ 1 $i))
)
(setq $VAL (+(fix(* $VAL 1.1)) 1))
(print $VAL)
(setq $zapas (getint "Ile zapasu:"))
(setq $val (+ $val $zapas))
(print $VAL)
(setq $bud_inst (getint "Budynkowa inst."))
(setq $val (+ $val $bud_inst))
(print $VAL)
(princ)
)
| 1,914 | Common Lisp | .l | 67 | 23.268657 | 57 | 0.515972 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 9094c18b5c319167e02ec0920a6082f48fb347f148413ff3dc1b2f1f6b722ca5 | 42,011 | [
-1
] |
42,012 | @textAlign.lsp | nobodybusiness_lisp-backup/!lsp/@textAlign.lsp | (defun @textalign ()
(setq $line (entget (ssname (ssget '((0 . "LWPOLYLINE"))) 0)))
(setq $text (ssname (ssget '((0 . "Mtext"))) 0))
(setq $pA (cdr (assoc 10 $line)))
(setq $pB (cdr (assoc 10 (reverse $line))))
(setq $angle (- (@RtD (angle $pB $pA)) 90)) ; -90
(LM:setattributevalue($text "rl@totation" rtos($angle 2 3)))
)
(defun changeAtributte (blk tag val / $ent $name)
(setq $ent blk)
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ; SKALA
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "SKALA")
(if (/= $SKALA "s")
(progn
(setq $VAL_new $SKALA)
(setq $VAL (subst (cons 1 $VAL_new) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
)
)
)
(defun LM:setattributevalue ( blk tag val / end enx )
(while
(and
(null end)
(setq blk (entnext blk))
(= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk)))))
)
(if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
(if (entmod (subst (cons 1 val) (assoc 1 (reverse enx)) enx))
(progn
(entupd blk)
(setq end val)
)
)
)
)
)
| 1,453 | Common Lisp | .l | 45 | 22.955556 | 74 | 0.467765 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | f85a0ca2a0009f589c4788665a0ea147436d9fd9cc938af80f22884f694a2f82 | 42,012 | [
-1
] |
42,013 | @osd_renumeracja.lsp | nobodybusiness_lisp-backup/!lsp/@osd_renumeracja.lsp | (defun c:@osd_renumeracja ()
(setq ss (ssget))
(setq i 0)
(setq plus_minus (- (getint "od ilu numeracja") 1))
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "NRZL")
(progn
(setq $VAL (atoi (substr (cdr (assoc 1 $LST)) (vl-string-search "/" (cdr (assoc 1 $LST))))))
(setq $VAL_new (+ $VAL plus_minus i))
(setq $val_new_string (strcat "PE1/" (rtos $VAL_new 2 0)))
(setq $VAL (subst (cons 1 $val_new_string) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
)
(defun c:@osd_zero_odl ()
(setq ss (ssget))
(setq i 0)
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (OR (= $attag "DLTRAS") (= $attag "DLINSTAL") (= $attag "DLOPT"))
(progn
(setq $val_new_string "?-?")
(setq $VAL (subst (cons 1 $val_new_string) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
)
(defun c:@osd_okr_odl ()
;ADD MODE TYPES -> FROM EXISTING OSD, ODL OD PKT, TXT
(setq ss (ssget))
(setq i 0)
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(setq $odlA (getint "odl tras: "))
(setq $odlB (getint "odl inst: "))
(setq $odlC (getint "odl opt: "))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (OR (= $attag "DLTRAS") (= $attag "DLINSTAL") (= $attag "DLOPT"))
(progn
;(setq $val_A (getint "odl:"))
(setq $val_A
(cond
((= $attag "DLTRAS") $odlA)
((= $attag "DLINSTAL") $odlB)
((= $attag "DLOPT") $odlC)
)
)
(setq $val_B (getint "pktA:"))
(setq $val_new (+ $val_A $val_B))
(setq $val_new_string (strcat (rtos $val_A 2 0) "-" (rtos $VAL_new 2 0)))
(setq $VAL (subst (cons 1 $val_new_string) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
)
(defun c:@osd_okr_odl_calk ()
(setq ss (ssget))
(setq i 0)
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (OR (= $attag "DLTRAS") (= $attag "DLINSTAL") (= $attag "DLOPT"))
(progn
(setq $val_A (getint "pktA:"))
(setq $val_B (getint "pktB:"))
(setq $val_new (- $val_B $val_A))
(setq $val_new_string (strcat (rtos $val_new 2 0) "-" (rtos $VAL_B 2 0)))
(setq $VAL (subst (cons 1 $val_new_string) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
)
(defun c:@osd_odl_plik ()
(setq $odl_adress "C:/Users/signaline.pl/Desktop/Piotr P/test/Signaline/ODLEGLOSCI_PKT_OSD/")
(setq $data nil)
(setq $filepath $odl_adress) ; ustawia sciezke do folderu mieszkan
(setq $fl (open (strcat $filepath "odl.txt") "r")) ; otwarcie do odczytania pliku z danymi
(while (setq $fil (read-line $fl)) ;petla do odczytu linni
(if (not (null $fil))
(setq $data (append $data (list $fil))) ; przypisanie danych do listy
)
)
(close $fl) ; zamkniecie pliku txt
(setq ss (ssget))
(setq i 0)
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (OR (= $attag "DLTRAS") (= $attag "DLINSTAL") (= $attag "DLOPT"))
(progn
(setq $data_str (car $data))
(setq $val_new_string (vl-string-subst "" "\"" (vl-string-subst "" "\"" (vl-string-subst "-" ";" $data_str))))
(setq $VAL (subst (cons 1 $val_new_string) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
(setq $data (cdr $data))
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
)
(defun c:@osd_kopiuj_wartosci ()
(setq ss (ssget))
(setq i 0)
(setq dataList (list))
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (OR (= $attag "DLTRAS") (= $attag "DLINSTAL") (= $attag "DLOPT"))
(progn
(setq $VAL (cdr (assoc 1 $LST)))
(setq dataList (append dataList (list $VAL)))
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
(setq ss (ssget))
(setq i 0)
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (OR (= $attag "DLTRAS") (= $attag "DLINSTAL") (= $attag "DLOPT"))
(progn
(setq $val_new_string (car dataList))
(setq $VAL (subst (cons 1 $val_new_string) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
(setq dataList (cdr dataList))
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
)
| 7,127 | Common Lisp | .l | 212 | 26.075472 | 121 | 0.502246 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 4e59004cecacf228e44f49e69ed5c5bb7ea2f5c9cf4fbde5c5425f73f2091832 | 42,013 | [
-1
] |
42,014 | @DELFRZ.lsp | nobodybusiness_lisp-backup/!lsp/@DELFRZ.lsp | ;usuwanie zamrozonych warst
(DEFUN @DELFRZ ()
(PROMPT "\n*ERASE ALL FROZEN ENTITIES BY LAYER(with purge)* ")
(SETQ LTSRL 1)
(SETQ LP 1)
(WHILE LP
(SETQ LTS (TBLNEXT "LAYER" LTSRL))
(SETQ LTSRL NIL)
(SETQ LTSF (CDR (ASSOC 70 LTS)))
(SETQ LTST (CDR (ASSOC 2 LTS)))
(SETQ LTSTF (LIST (CONS 8 LTST)))
(IF (= LTSF 1)
(PROGN
(SETQ EBS (SSGET "X" LTSTF))
(COMMAND "ERASE" EBS "")
(COMMAND "LAYER" "T" "0" "S" "0" "")
(COMMAND "PURGE" "LA" LTST "N")
));END PROGN/IF LTSF
(IF (= LTS NIL) (SETQ LP NIL))
);END LP
(PRINC)
);END EFEL | 583 | Common Lisp | .l | 22 | 22.727273 | 64 | 0.588968 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | c92677cb1d2738cee8e3c5e732bb304af370d5ced6ee16f6fd70aff4049f511a | 42,014 | [
-1
] |
42,015 | @dl_okw.lsp | nobodybusiness_lisp-backup/!lsp/@dl_okw.lsp | ;dlogosc po budynku
(defun @dl_okw ()
(vl-load-com)
(setq $okw nil)
(setq $okw (ssget '((0 . "LWPOLYLINE"))))
(setq $okw_name (ssname $okw 0))
(setq $i 1)
(setq $okw_length_sum 0)
(setq $plist (mapcar 'cdr
(vl-remove-if-not
'(lambda (x) (= (car x) 10))
(entget $okw_name)
)
)
)
(setq $p_list_length (length $plist))
(while $plist ; petla przypisania wszystkich pkt
(setq $i_string (rtos $i 2 0))
(setq $name (read (strcat "$p" $i_string)))
(set $name (car $plist))
(setq $plist (cdr $plist))
(setq $i (+ 1 $i))
)
(print $p1)
(setq $n 1)
(while (< $n $p_list_length)
(setq $n_string (rtos $n 2 0))
(setq $pA (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $n (+ 1 $n))
(setq $n_string (rtos $n 2 0))
(setq $pB (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $okw_length (distance $pA $pB))
(setq $okw_length_sum (+ $okw_length_sum $okw_length))
)
(setq $okw_length_sum (/ (fix $okw_length_sum) 100))
(setq $zapas (* 10 (fix (+ (* 0.01 $okw_length_sum) 1))))
(if (and(>= $okw_length_sum 15)(<= $okw_length_sum 25))
(setq $zapas (+ $zapas 5))
)
(if (> $okw_length_sum 25)
(setq $zapas (+ $zapas 10))
)
(setq $liczba_piet (getint "Liczba pi�ter:"))
(setq $zapas_pion (* $liczba_piet 4))
(setq $okw_obl (+ $okw_length_sum $zapas $zapas_pion))
(print $okw_obl)
(princ)
)
(defun c:dll ()
(@SUM_D_LINE)
(@DL_OKW)
(print (strcat (rtos (+(/ $dline1 100) 1) 2 0) "/" (rtos $okw_obl 2 0)))
(princ)
)
| 1,688 | Common Lisp | .l | 54 | 25.055556 | 74 | 0.518405 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | c0eeb3bd808eb8b41ec8b0be70b9fb6427200c893af18a5ff4a191e6c068f7dc | 42,015 | [
-1
] |
42,016 | @oznaczenie_sl.lsp | nobodybusiness_lisp-backup/!lsp/@oznaczenie_sl.lsp | ;oznaczenie slupowe
(defun @oznacz_kier_sl ()
(vl-load-com)
(setq $p1 (getpoint "Pocz�tek;")) ;poczatek linni
(setq $p2 (getpoint "Koniec:")) ;koniec linni
(setq $angle (- (@RtD (angle $p1 $p2)) 90)) ; ustawienie obrotu tekstu zgodnie ze standardem
(command "-insert" "kierunek_blok" $p1 "" "" $angle)
(setq $angle (+ $angle 180))
(command "-insert" "kierunek_blok" $p2 "" "" $angle)
)
| 423 | Common Lisp | .l | 10 | 38.9 | 96 | 0.617433 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a11688f2152ff3fa2793fb192a85d1c40ae63d638455eb2e0cdb64b31ecbf475 | 42,016 | [
-1
] |
42,017 | @flatten.lsp | nobodybusiness_lisp-backup/!lsp/@flatten.lsp | ; FLATTEN.LSP version 2.2, 25 June 1997
;
; FLATTEN sets the Z-coordinates of LINEs, POLYLINEs, LWPOLYLINEs, CIRCLEs,
; ARCs, TEXT, DIMENSIONs, Block INSERTs, POINTs, HATCHes, and SOLIDs to 0.
;------------------------------------------------------------------------
; copyright 1990, 1993, 1994, 1997 by Mark Middlebrook
; Daedalus Consulting
; 435 Clifton Street
; Oakland, CA 94618
; e-mail: [email protected]
;
; You are free to distribute FLATTEN.LSP to others so long as you do not
; charge for it.
;------------------------------------------------------------------------
;*Why Use FLATTEN?:
;
; FLATTENing is useful in at least two situations:
; 1) You receive a DXF file created by another CAD package and discover
; that all the Z coordinates contain small round-off errors. These
; round-off errors can prevent you from object snapping to intersections
; and make your life difficult in other ways as well.
; 2) In a supposedly 2D drawing, you accidentally create one entity with a
; Z elevation and end up with a drawing containing entities partly in and
; partly outside the Z=0 X-Y plane. As with the round-off problem, this
; situation can make object snaps and other procedures difficult.
;------------------------------------------------------------------------
;*How to Use FLATTEN:
;
; FLATTEN v.2.2 works with AutoCAD R12 through R14.
;
; To run FLATTEN, load it using AutoCAD's APPLOAD command, or type:
; (load "FLATTEN")
; at the AutoCAD Command prompt. Once you've loaded FLATTEN.LSP, type:
; FLATTEN
; to run it. FLATTEN will tell you what it's about to do and ask you
; to confirm that you really want to flatten entities in the current
; drawing. If you choose to proceed, FLATTEN prompts you to select entities
; to be flattened (press ENTER to flatten all entities in the drawing).
; After you've selected entities and pressed ENTER, FLATTEN goes to work.
; It reports the number of entities it flattens and the number left
; unflattenened (because they were entities not recognized by FLATTEN; see
; the list of supported entities above).
;
; If you don't like the results, just type U to undo FLATTEN's work.
;
; Note that FLATTEN flattens entities onto the Z=0 X-Y plane in AutoCAD's
; World Coordinate System (WCS).
;------------------------------------------------------------------------
(defun c:@FLATTEN (/ olderr oldcmd ss1 ss1len i numchg numnot numno0 ssno0
ename elist etype yorn)
;*error handler
(setq olderr *error*)
(defun *error* (msg)
(if (= msg "quit / exit abort")
(princ)
(princ (strcat "error: " msg))
)
(setq *error* olderr)
(command "._UCS" "_Restore" "$FLATTEN-TEMP$"
"._UCS" "_Delete" "$FLATTEN-TEMP$")
(command "._UNDO" "_End")
(setvar "CMDECHO" oldcmd)
(princ)
)
;*setup
(setq oldcmd (getvar "CMDECHO"))
(setvar "CMDECHO" 0)
(command "._UNDO" "_Group")
(command "._UCS" "_Save" "$FLATTEN-TEMP$" "._UCS" "World") ;set World UCS
;*get input
(prompt (strcat
"\nFLATTEN sets the Z coordinates of lines, polylines, circles, arcs,"
"\ntext, dimensions, block inserts, points, hatches, and solids to zero."
))
(initget "Yes No")
(setq yorn (getkword "\nDo you want to continue <Y>: "))
(cond ((/= yorn "No")
(graphscr)
(prompt "\nChoose entities to FLATTEN ")
(prompt "[press return to select all entities in the drawing]")
(setq ss1 (ssget))
(if (null ss1) ;if enter...
(setq ss1 (ssget "X")) ;select all entities in database
)
;*initialize variables
(setq ss1len (sslength ss1) ;length of selection set
i 0 ;loop counter
numchg 0 ;number changed counter
numnot 0 ;number not changed counter
numno0 0 ;number not changed and Z /= 0 counter
ssno0 (ssadd) ;selection set of unchanged entities
);setq
;*do the work
(prompt "\nWorking.")
(while (< i ss1len) ;while more members in the SS
(if (= 0 (rem i 10)) (prompt "."))
(setq ename (ssname ss1 i) ;entity name
elist (entget ename) ;entity data list
etype (cdr (assoc 0 elist)) ;entity type
)
;*Keep track of entities not flattened
(if (not (member etype
'("LINE" "POLYLINE" "TEXT" "INSERT"
"CIRCLE" "ARC" "POINT" "SOLID"
"DIMENSION" "LWPOLYLINE" "HATCH" "MTEXT")))
(progn ;leave others alone
(setq numnot (1+ numnot))
(if (/= 0.0 (car (reverse (assoc 10 elist))))
(progn ;add it to special list if Z /= 0
(setq numno0 (1+ numno0))
(ssadd ename ssno0)
)
)
)
)
;*change group 10 Z coordinate to 0 for listed entity types
(if (member etype '("LINE" "POLYLINE" "TEXT" "INSERT" "CIRCLE"
"ARC" "POINT" "SOLID" "DIMENSION" "HATCH"
"MTEXT"))
(setq elist (zeroz 10 elist) ;change entities in list above
numchg (1+ numchg)
)
)
;*change group 11 Z coordinate to 0 for LINEs, TEXT, and SOLIDs
(if (member etype '("LINE" "TEXT" "SOLID" "DIMENSION"))
(setq elist (zeroz 11 elist))
)
;*change groups 12 and 13 Z coordinate to 0 for SOLIDs
(if (member etype '("SOLID"))
(progn
(setq elist (zeroz 12 elist))
(setq elist (zeroz 13 elist))
)
)
;*change groups 13, 14, 15, and 16 Z coordinate to 0 for DIMENSIONs
(if (member etype '("DIMENSION"))
(progn
(setq elist (zeroz 13 elist))
(setq elist (zeroz 14 elist))
(setq elist (zeroz 15 elist))
(setq elist (zeroz 16 elist))
)
)
;*special handling for R14 LWPOLYLINEs
(if (member etype '("LWPOLYLINE"))
(progn
(setq elist (subst (cons 38 0.0) (assoc 38 elist) elist)
numchg (1+ numchg)
)
(entmod elist)
)
)
(setq i (1+ i)) ;next entity
);while
(prompt " Done.")
;*print results
(prompt (strcat "\n" (itoa numchg) " entity(s) flattened"))
(prompt (strcat "\n" (itoa numnot) " entity(s) not flattened"))
(if (/= 0 numno0) ;if there any entities in ssno0, show them
(progn
(prompt (strcat " [" (itoa numno0)
" with non-zero base points]"))
(getstring "\nPress enter to see non-zero unchanged entities... ")
(command "._SELECT" ssno0)
(getstring "\nPress enter to unhighlight them... ")
(command "")
)
)
))
(command "._UCS" "_Restore" "$FLATTEN-TEMP$"
"._UCS" "_Delete" "$FLATTEN-TEMP$")
(command "._UNDO" "_End")
(setvar "CMDECHO" oldcmd)
(setq *error* olderr)
(princ)
)
;*function to change Z coordinate to 0
(defun zeroz (key zelist / oplist nplist)
(setq oplist (assoc key zelist)
nplist (reverse (append '(0.0) (cdr (reverse oplist))))
zelist (subst nplist oplist zelist)
)
(entmod zelist)
)
(prompt "\nFLATTEN v.2.2 loaded. Type FLATTEN to run it.")
(princ)
;;;eof | 8,150 | Common Lisp | .l | 187 | 33.508021 | 82 | 0.527146 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 6ff8e0c649ae098e35a16be5df2cbea2b51c99d792efbf29b7c153794eded130 | 42,017 | [
-1
] |
42,018 | @przejscie_kabla_po_dzialkach.lsp | nobodybusiness_lisp-backup/!lsp/@przejscie_kabla_po_dzialkach.lsp | (defun C:@przejscie_kabla_po_dzialkach ()
(setvar "CmdEcho" 0)
(@area_for_pts) ;odnalezienie pts dla zaznaczenia
(command "_zoom" "e") ; zoom na calosc, aby lapalo zaznaczenie wartosci z calego rysunku a nie tylko z "widoku"
(@select_texts) ;wyrzucenie danych z tekstow
(setvar "CmdEcho" 1)
(princ) ;uwaga
)
(defun @area_for_pts ()
(vl-load-com)
(setq $area (entget (car (entsel)))) ;pobiera od uzytkownika zaznaczenie
(setq $pts nil)
(while
(setq $zm_pt (assoc 10 $area)) ; odczytujemy pierwszy wiercho³ek
(setq $pts (append $pts (list (cdr $zm_pt)))) ; dodajemy go do listy wynikowej
(setq $area (cdr (member $zm_pt $area))) ; "obcinamy" pocz¹tek listy za wyszukany element, by kolejny by³ wyszukany jako pierwszy.
)
(setq $tryb "jedn")
(if (= (getstring "Tryb: all/jedn <jedn>") "all")
(setq $tryb "all")
)
)
(defun @select_texts ()
(setq $seltexts (ssget "WP" $pts '((0 . "TEXT")))) ; ustalenie obwiedni odczytania wynikow
(setq $length_seltext (sslength $seltexts)) ;ilosc elementow zaznaczenia
(setq $n 0) ; wartosc poczatkowa n elementu
(setq $datalist "") ; usuniecie wartosci z datalist
(while (< $n $length_seltext)
(setq $el (ssname $seltexts $n)) ; odczyt nazwy n elementu z obiedni
(command "_zoom" "_object" $el "") ; ;zoom na nty elem
(command "_zoom" "_s" "0.02") ;oddalenie widoku od elementu
(command "_chprop" $el "" "_c" "7" "") ;obecnie edytowany element na bialo
(if (= $tryb "all")
(progn
(setq $data (cdr (assoc 1 (entget $el)))) ; odczyt tekstu n elementu
(setq $n (+ $n 1))
(setq $datalist (strcat $data "\n" $datalist)) ;dodanie do listy wynikow
(command "_chprop" $el "" "_c" "5" "") ;obecnie edytowany element na niebiesko
)
(progn
(initget "Y N Z _YES NO UNDO")
(setq $option (getkword "\nCzy dodac dzialke do wykazu?(Y/N/Z) <NO>")) ;potwierdzenie czy przechodzi, jak nie to powrot
(if (= $option "YES") ;jezeli dodac do obiektow
(
progn
(setq $data (cdr (assoc 1 (entget $el)))) ; odczyt tekstu n elementu
(setq $n (+ $n 1))
(setq $datalist (strcat $data "\n" $datalist)) ;dodanie do listy wynikow
(command "_chprop" $el "" "_c" "5" "") ;obecnie edytowany element na niebiesko
)
(if (= $option "UNDO") ; jezeli cofnac o krok
(progn
(setq $n (- $n 1))
(command "_chprop" $el "" "_c" "bylayer" "") ;przywraca kolor by layer
)
(progn ; else - domyslnie nie dodawac do obiektow
(setq $n (+ $n 1))
(command "_chprop" $el "" "_c" "1" "") ;obecnie edytowany element na czerwono
)
)
)
)
)
)
(princ "\nDzialki w obwiedni:\n")
(princ $datalist) ; wyrzucenie wynikow
;
)
(defun c:@pokaz_liste_dzialek ()
(alert $datalist)
)
| 3,160 | Common Lisp | .l | 71 | 36.816901 | 144 | 0.553102 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 7268d4a2f015d027214d762fceba9f83689360e1bf9d7781c3160b3b0fcec5ff | 42,018 | [
-1
] |
42,019 | @MTEXT_BETTER.lsp | nobodybusiness_lisp-backup/!lsp/@MTEXT_BETTER.lsp | ;tworzenie mtxt o okreslonej wielkosci
(defun C:@T2 ()
(vl-load-com)
(command "insert" "TEXT_2" pause "" "" "")
(command "explode" "l" )
(command "_ddedit" "l" )
)
(defun C:@T5 ()
(vl-load-com)
(command "insert" "TEXT_5" pause "" "" "")
(command "explode" "l" )
(command "_ddedit" "l" )
)
(defun C:@T10 ()
(vl-load-com)
(command "insert" "TEXT_10" pause "" "" "")
(command "explode" "l" )
(command "_ddedit" "l" )
) | 448 | Common Lisp | .l | 19 | 20.894737 | 45 | 0.562791 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 4232f774d6944562349745627b342cc435ec957b4c5b6fcba8b0bd3021227b7e | 42,019 | [
-1
] |
42,020 | @getCoordinates.lsp | nobodybusiness_lisp-backup/!lsp/@getCoordinates.lsp | ;nie uzywac wip
(defun C:@getCoordinates ()
(vl-load-com)
;.........
(setq coordninates ( getpoint "Wska� pkt"))
;.............
(setq Dx (car coordninates))
(setq Dy (cadr coordninates))
;X
(setq fixDx (fix Dx))
(setq Mx (* 60 (- Dx fixDx)))
(setq fixMx ( fix Mx))
(setq Sx (* 60 (- Mx fixMx)))
(setq fixSx (rtos Sx 2 1))
(setq corX ( strcat (rtos fixDx 2 0) "�" (rtos fixMx 2 0) "'" fixSx "''N" ))
;Y
(setq fixDy (fix Dy))
(setq My (* 60 (- Dy fixDy)))
(setq fixMy ( fix My))
(setq Sy (* 60 (- My fixMy)))
(setq fixSy ( rtos Sy 2 1))
(setq corY ( strcat (rtos fixDy 2 0) "�" (rtos fixMy 2 0) "'" fixSy "''E" ))
(setq corXY ( strcat corX "," corY))
(print)
(print corXY)
(princ)
(command "DIMLEADER" coordninates pause "" corXY "")
(princ)
) | 829 | Common Lisp | .l | 29 | 24.586207 | 78 | 0.56901 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a159eba6c86d544fde099ba21ab6609621f5a82fa37526f7bf6b6fe7743ddef5 | 42,020 | [
-1
] |
42,021 | @uwagi.lsp | nobodybusiness_lisp-backup/!lsp/@uwagi.lsp | ;dodawanie warstwy dzisiaj
(defun @uwagi ()
(vl-load-com)
(@today)
(setq $nazwa (strcat $dzis "_uwagi"))
(command "-layer" "_m" $nazwa "_c" "red" "" "_p" "_n" "" "")
)
(defun @TODAY (/ d yr mo day)
;define the function and declare all variabled local
(setq d (rtos (getvar "CDATE") 2 6)
;get the date and time and convert to text
yr (substr d 3 2)
;extract the year
mo (substr d 5 2)
;extract the month
day (substr d 7 2)
;extract the day
) ;setq
(setq $dzis (strcat day mo yr))
;string 'em together
) ;defu
| 618 | Common Lisp | .l | 21 | 23.47619 | 63 | 0.569231 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 7962dd2bb96131488bce6ee607b790bb068558c6d05351f284d27a31b1c16f15 | 42,021 | [
-1
] |
42,022 | @nr_wkl.lsp | nobodybusiness_lisp-backup/!lsp/@nr_wkl.lsp | ;numeracja kolenych wlokien
(defun c:@wkl ()
(setq $nr_pocz (getint "Nr poczt.:"))
(setq $pt (getpoint "PKT wstawienia"))
(setq $n_string "")
(setq $n 0)
(while (< $n 12)
(setq $n_string (rtos (+ $n 1) 2 0))
(setq $pA (read (strcat "$p" $n_string)))
(setq $nr_wkl (rtos (+ $n $nr_pocz) 2 0))
(set $pA $nr_wkl)
(setq $n (+ 1 $n))
)
(command "insert" "nr_wlokien" $pt "" "" "" $p1 $p2 $p3 $p4 $p5 $p6 $p7 $p8 $p9 $p10 $p11 $p12)
)
(defun c:@wl()
(setq #wl (getint "Nr wlk:"))
(setq #fullTubes (fix (/ (- #wl 1) 12)))
(setq #tube (+ 1 #fullTubes))
(setq #firstInTube (+ (* #fullTubes 12) 1))
(setq #lastInTube (+ #firstInTube 11))
(print (strcat "W tubie " (rtos #tube 2 0) " , pierwsze wl. " (rtos #firstInTube 2 0) " , ostatnie wl. " (rtos #lastInTube 2 0)))
(princ)
) | 831 | Common Lisp | .l | 24 | 31 | 131 | 0.549505 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | f15830ec5443c7a7cd68eef9d5586f69b595a9160c07263a8bbafe4eabc2f8cf | 42,022 | [
-1
] |
42,023 | @viewport_trasa.lsp | nobodybusiness_lisp-backup/!lsp/@viewport_trasa.lsp | ;tworzenie layoutow
(defun c:@viewport_trasa ()
(setq scaleType nil)
(setq testScale nil)
(while (/= testScale T)
(setq scaleType (getstring "<S>chemat,<T>rasowy,<R>zut: "))
(if (or (= scaleType "s") (= scaleType "t") (= scaleType "r"))
(setq testScale T)
)
)
(@ALL_VIEWPORTS)
(setq $layoutstart_value (getint "numer utworzonego layoutu startowego"))
(setq $layoutstart (rtos $layoutstart_value 2 0))
(@GOTO_LAYOUT $layoutstart)
(while $names
(setq $name (car $names))
(@VIEWPORT_ZOOM_TO_ONE_BY_HANDLE $name)
(@COPYLAYOUT)
(@GOTO_LAYOUT $layout_name_new)
(setq $names (cdr $names))
)
(command "layout" "_d" "")
(@CHANGESCALEOFALLVIEWPORTS)
)
(defun @all_viewports ()
(vl-load-com)
(setq $viewports_selection (ssget '((0 . "LWPOLYLINE") (8 . "viewports"))))
(setq $i 0)
(setq $names (list))
(setq $number_of_elements_in_selection (sslength $viewports_selection))
(while (< $i $number_of_elements_in_selection)
(setq $ent (ssname $viewports_selection $i))
(setq $names (append $names (list $ent)))
(setq $i (+ 1 $i))
)
(print $names)
)
(defun @copylayout ()
(setq $layout_name (getvar 'ctab))
(setq $layout_name_value (atoi $layout_name))
(setq $layout_name_new_value (+ 1 $layout_name_value))
(setq $layout_name_new (rtos $layout_name_new_value 2 0))
(command "._layout" "_c" $layout_name $layout_name_new)
)
(defun @goto_layout ($layout_to_go)
(setvar 'CTab $layout_to_go)
)
(defun @viewport_zoom_to_one_by_handle (x)
(command "mspace"
"zoom" "_e"
"zoom" "_ob" x
)
(princ)
(command "zoom" "_s" "1.019999999999999999")
(command "._pspace")
)
(defun @ChangeScaleOfAllViewports ()
(setq get (ssget "x" '((0 . "viewport") (-4 . ">") (69 . 1))))
(setq $ent_ename (ssname get 0))
(setq $ent_data (entget $ent_ename))
(setq heightInPaper (cdr (assoc 41 $ent_data)))
(setq viewHeight (cdr (assoc 45 $ent_data)))
(cond
((= scaleType "s") (setq scaleTypeValue 0.2))
((= scaleType "t") (setq scaleTypeValue 2))
((= scaleType "r") (setq scaleTypeValue 0.1))
)
(setq scale (/ (* scaleTypeValue viewHeight) heightInPaper))
(foreach lay (layoutlist) (setvar "CTAB" lay) (command "mspace" "zoom" "_s" scale "._pspace"))
)
| 2,350 | Common Lisp | .l | 70 | 29.085714 | 97 | 0.621145 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 3896911149e7e4d79d7571560f3dd9f72561604f6557aaf9cca52a1cbb48e7f6 | 42,023 | [
-1
] |
42,024 | @getangle.lsp | nobodybusiness_lisp-backup/!lsp/@getangle.lsp | ;nie uzywac
(defun @getangle ()
;!zbedne
(setq $p1 (getpoint "Wskaz pkt w kierunku strzalki"))
(setq $p2 (getpoint "Wskaz pkt w kierunku konca"))
(setq $angle (- (@RtD (angle $p2 $p1)) 90))
(command "_ro" pause pause pause pause $angle )
(princ)
(command "_.move" "_p" "" )
) | 298 | Common Lisp | .l | 10 | 26.6 | 56 | 0.612457 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 204f9efb170ad482ced632d593ee275aa1835bbaa874eff8286b2f8f8d99d5ca | 42,024 | [
-1
] |
42,025 | @MieszkaniaSzybkie.lsp | nobodybusiness_lisp-backup/!lsp/@MieszkaniaSzybkie.lsp | ;mieszkania schemat przy nie kolejnym numerowaniu
(defun c:@mieszkania ()
(setq $fromNumber 0)
(setq $numberOf 0)
(setq $ifUslugi (getint "<1 - LU><0 - HH>"))
(setq $fromNumber (getint "from number:"))
(setq $numberOf (getint "number of:"))
(setq $i 0)
(setq $j 0)
(setq $licz_wierszy 4)
(setq $wsp (getpoint "wska� pkt wstawienia"))
(setq $wsp_x (car $wsp))
(setq $wsp_y (cadr $wsp))
(if (= 1 $ifUslugi)
(while (< $i $numberOf)
(progn
(setq nameU (strcat "U" (rtos $fromNumber 2 0)))
(command "-insert" "uslugi_blok" $wsp "" "" "" nameU)
(print)
(setq $wsp_x (+ $wsp_x 50))
(setq $wsp (strcat (rtos $wsp_x 2) "," (rtos $wsp_y 2)))
(setq $i (+ 1 $i))
(setq $fromNumber (+ $fromNumber 1))
)
)
(while (< $i $numberOf)
(progn
(command "-insert" "mieszkania_blok" $wsp "" "" "" $fromNumber)
(print)
(setq $wsp_x (+ $wsp_x 50))
(setq $wsp (strcat (rtos $wsp_x 2) "," (rtos $wsp_y 2)))
(setq $i (+ 1 $i))
(setq $fromNumber (+ $fromNumber 1))
)
)
)
)
| 1,153 | Common Lisp | .l | 37 | 24.567568 | 72 | 0.514337 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 939e7621dd0264d6f1b4e46cf8b075b3171cd5dd666cabf7e2f2d6641645b4a6 | 42,025 | [
-1
] |
42,026 | @PIGTAIL ZMIANA NUMERACJI.lsp | nobodybusiness_lisp-backup/!lsp/@PIGTAIL ZMIANA NUMERACJI.lsp | (defun c:@pigtail_zmiana_numeracji ()
(setq ss (ssget))
(setq i 0)
(setq plus_minus (getint "+/-:"))
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (OR (= $attag "PRZEL_MIESZKANIA") (= $attag "PRZEL_USLUGI"))
(progn
(setq $VAL (atoi (cdr (assoc 1 $LST))))
(setq $VAL_new (+ $VAL plus_minus))
(setq $VAL (subst (cons 1 (rtos $VAL_new 2 0)) (assoc 1 $LST) $LST))
(entmod $VAL)
(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
)
| 893 | Common Lisp | .l | 28 | 24.75 | 79 | 0.50289 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 7bd87af60fcfaa785460b1a6a1d606ec207eadddc8413afb93066f5aee3594b0 | 42,026 | [
-1
] |
42,027 | @deletelayouts.LSP | nobodybusiness_lisp-backup/!lsp/@deletelayouts.LSP | ;usuwanie wszystkich layoutow
(defun C:laydel ()
(vl-load-com)
(vlax-for layout (vla-get-layouts
(vla-get-ActiveDocument (vlax-get-acad-object))
)
(if (/= (vla-get-name layout) "Model")
(vla-delete layout)
)
)
) | 252 | Common Lisp | .l | 11 | 18.272727 | 54 | 0.616667 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 6a2b4f8dfc301a25e0e1b4b635d50efc9646bc737e8821d478fd770a2365c583 | 42,027 | [
-1
] |
42,028 | @OSD-OKW-NUMERACJA.lsp | nobodybusiness_lisp-backup/!lsp/@OSD-OKW-NUMERACJA.lsp | (defun c:@OSD_OKW_NUMERACJA_ARROW ()
(vl-load-com)
(setq ss (ssget))
(setq i 0)
(setq osdOrOkw (getint "OSD <1> OKW <2>"))
(setq startingOSDprefix (getstring "OSD_OKW_Prefix: "))
(setq startingOSDnumber (getreal "OSD_OKW: "))
(if (= osdOrOkw 1)
(setq osdOrOkwText "OSD")
)
(if (= osdOrOkw 2)
(setq osdOrOkwText "OKW")
)
(while (< i (sslength ss))
(setq currentOSDnumber (+ startingOSDnumber i))
(setq currentOSD (strcat startingOSDprefix (rtos currentOSDnumber 2 0)))
(setq $ent_ename (ssname ss i))
(setq $ent_data (entget $ent_ename))
(setq $text (cdr (assoc 304 $ent_data)))
(setq x1 (strcat osdOrOkwText currentOSD))
(setq $newText (vl-string-subst x1 (strcat osdOrOkwText "_") $text))
(setq $VAL (subst (cons 304 $newText) (assoc 304 $ent_data) $ent_data))
(entmod $VAL)
(entupd $ent_ename)
(setq i (+ i 1))
)
)
(defun c:@OSD_OKW_NUMERACJA_MTEXT ()
;DODAC WERSJE DLA BUDYNKOW, A NIE WEERSJI S
(vl-load-com)
(setq ss (ssget))
(setq i 0)
(setq startingOSDprefix (getstring "OSD_Prefix: "))
(setq startingOSDnumber (getreal "OSD: "))
(setq startingOKWprefix (getstring "OKW_Prefix: "))
(setq startingOKWnumber (getreal "OKW: "))
(while (< i (sslength ss))
(setq alternativeAssoc 0)
(setq currentOSDnumber (+ startingOSDnumber i))
(if (/= startingOKWnumber nil)
(setq currentOKWnumber (+ startingOKWnumber i))
(setq currentOKWnumber "")
)
(setq currentOSD (strcat startingOSDprefix (rtos currentOSDnumber 2 0)))
(if (/= startingOKWnumber nil)
(setq currentOKW (strcat startingOKWprefix (rtos currentOKWnumber 2 0)))
(setq currentOKW startingOKWprefix)
)
(setq $ent_ename (ssname ss i))
(setq $ent_data (entget $ent_ename))
(setq $text (cdr (assoc '1 $ent_data)))
(if (/= (vl-string-search "OSD_" $text) 0)
(progn
(setq $text (cdr (assoc '3 $ent_data)))
(setq alternativeAssoc 1)
)
)
(setq x1 (strcat "OSD" currentOSD))
(setq x2 (strcat "OKW" currentOKW))
(setq $newText (vl-string-subst x1 "OSD_" $text))
(setq $newText2 (vl-string-subst x2 "OKW_" $newText))
(if (/= alternativeAssoc 1)
(setq $VAL (subst (cons 1 $newText2) (assoc 1 $ent_data) $ent_data))
(setq $VAL (subst (cons 1 $newText2) (assoc 3 $ent_data) $ent_data))
)
(entmod $VAL)
(entupd $ent_ename)
(setq i (+ i 1))
)
)
| 2,500 | Common Lisp | .l | 70 | 30.185714 | 79 | 0.624073 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a9835362994087b6bc1853ba8c7dea6586498d81b1478235841f0d2cf8537a49 | 42,028 | [
-1
] |
42,029 | @offsetMultiple.lsp | nobodybusiness_lisp-backup/!lsp/@offsetMultiple.lsp | ;wielokrotne osuniecie
(defun c:@offsetMultiple ()
(command "_offset" pause pause "m")
)
| 97 | Common Lisp | .l | 4 | 21.25 | 38 | 0.688172 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | f14ea65847635f48bfcd5450d88d763b304b4b56c6a673e8b9065d213372bfb3 | 42,029 | [
-1
] |
42,030 | @NR_OSD_OKW.lsp | nobodybusiness_lisp-backup/!lsp/@NR_OSD_OKW.lsp | ;renumeracja wszystkich oznaczen OKW_ na kolejne od zadanego
(defun c:@NR_OSD_OKW ()
(vl-load-com)
(setq ss (ssget))
(setq i 0)
(while (< i (sslength ss))
(setq $VAL 0)
(setq $ent (ssname ss i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND")) ;NAZWA PROJEKTU
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "OSD")
(progn
(setq insideString (nth 2 (assoc '1 $LST)))
(setq test (strcat insideString ""))
;(setq newStringInside (vl-string-subst "xxxxxx" "OKW_" newStringInside))
;(setq $VAL (subst (cons 1 newStringInside) (assoc '1 $LST) $LST))
;(entmod $VAL)
;(entupd $ent)
)
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq i (+ i 1))
)
)
| 963 | Common Lisp | .l | 30 | 25.1 | 84 | 0.536977 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | b0cd19d2f079f4890205815509c9f49a074d292592b923ee40f704d592f9a411 | 42,030 | [
-1
] |
42,031 | @STUDNIA_OZNACZENIE.lsp | nobodybusiness_lisp-backup/!lsp/@STUDNIA_OZNACZENIE.lsp | ;wstawienia studni
(defun c:@STUDNIA_OZNACZENIE()
(vl-load-com)
(command "-insert" "studnia_blok" pause "0.5" "0.5" pause)
(princ)
)
| 142 | Common Lisp | .l | 6 | 21.166667 | 60 | 0.669118 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 2b542dab751bd1a273b7c3a18f9094ba5fd8fb22eabfaefd3689e7be72eb269f | 42,031 | [
-1
] |
42,032 | @Laycolor.lsp | nobodybusiness_lisp-backup/!lsp/@Laycolor.lsp | ;zmiana koloru warstwy
(defun c:@LayColor (/ ss clr col i lst lay)
(vl-Load-Com) ; dodaj if bo od 1-255
(if (setq ss (ssget "_:L"))
(progn
(setq lst ","
ccl (getvar 'CECOLOR)
)
(repeat (setq i (sslength ss))
(setq
lay (strcat
(cdr (assoc 8 (entget (ssname ss (setq i (1- i))))))
","
)
)
(if (not (vl-string-search (strcat "," lay) lst))
(setq lst (strcat lst lay))
)
)
(setq lst (vl-string-trim "," lst))
(initdia)
(command "_.COLOR")
(setq col (getvar 'CECOLOR))
(setvar 'CECOLOR ccl)
(command "_.LAYER" "_Color")
(if (wcmatch col "RGB:*")
(command "_T" (substr col 5))
(command col)
)
(command lst "")
)
)
) | 859 | Common Lisp | .l | 33 | 17.484848 | 69 | 0.453446 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | e3b8b2de153baf6656afd5b403b83a1927685a4a072843a8d852006e2ec6d5b1 | 42,032 | [
-1
] |
42,033 | @kabel_opis_new.lsp | nobodybusiness_lisp-backup/!lsp/@kabel_opis_new.lsp | ;opis dla kabla
(defun c:@op ()
(vl-load-com)
(setq ss (ssget))
(setq $ent_ename (ssname ss 0))
(setq $ent_data (entget $ent_ename))
(setq $text (cdr (assoc '304 $ent_data)))
(setq x1 (strcat "d�. tras. " (getstring "d�. tras:") "m"))
(setq x2 (strcat "d�. inst. " (getstring "d�. inst:") "m"))
(setq x3 (strcat "d�. opt. " (getstring "d�. opt:") "m"))
(setq $newText (vl-string-subst x1 "d�. tras. _" $text))
(setq $newText2 (vl-string-subst x2 "d�. inst. _" $newText))
(setq $newText3 (vl-string-subst x3 "d�. opt. _" $newText2))
(setq $VAL (subst (cons 304 $newText3) (assoc 304 $ent_data) $ent_data))
(entmod $VAL)
(entupd $ent_ename)
)
(defun c:@op2 ()
(vl-load-com)
(setq ss (ssget))
(setq $ent_ename (ssname ss 0))
(setq $ent_data (entget $ent_ename))
(setq $text (cdr (assoc '1 $ent_data)))
(setq x1 (strcat "d�. tras. " (getstring "d�. tras:") "m"))
(setq x2 (strcat "d�. inst. " (getstring "d�. inst:") "m"))
(setq x3 (strcat "d�. opt. " (getstring "d�. opt:") "m"))
(setq $newText (vl-string-subst x1 "d�. tras. _" $text))
(setq $newText2 (vl-string-subst x2 "d�. inst. _" $newText))
(setq $newText3 (vl-string-subst x3 "d�. opt. _" $newText2))
(setq $VAL (subst (cons 1 $newText3) (assoc 1 $ent_data) $ent_data))
(entmod $VAL)
(entupd $ent_ename)
)
| 1,384 | Common Lisp | .l | 33 | 37.212121 | 74 | 0.582635 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 5d835a7f33cdd66b8159cef69c2cda26d8678d4bd715e1605fcf03b25a04974e | 42,033 | [
-1
] |
42,034 | chowanie_wyników_abc.lsp | nobodybusiness_lisp-backup/!lsp/chowanie_wyników_abc.lsp | ;chowanie wynikow
(defun c:abcc ()
(command "_.layer" "_s" "0" "")
(command "_.layer" "_f" "tkd_abc_*" "")
)
(defun c:abco ()
(command "_.layer" "_s" "0" "")
(command "_.layer" "_t" "tkd_abc_*" "")
(command "_.layer" "_f" "tkd_abc_elements" "")
) | 262 | Common Lisp | .l | 10 | 23.7 | 48 | 0.50996 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 4cd59a9e3023c0f95d21e528efd572478a8d17f807f88a6bbb059d68df3bf03b | 42,034 | [
-1
] |
42,035 | poprzedni_wybor.lsp | nobodybusiness_lisp-backup/!lsp/poprzedni_wybor.lsp | ;funkcja poprzedniego zaznaczenia
(defun C:ost ()
(command
"_pselect"
"_p"
""
)
(princ)
) ;defun
;--------------------------------
;funkcja stworzenia zapisu
(defun c:zap ()
(setq zapis (ssget))
)
;--------------------------------
;funkcja pzywrocenia zaznaczenia
(defun c:chk ()
(command
"_select"
zapis
""
)
)
;--------------------------------------------
;funkcja przesuwanie zbrojenia x-x
;(defun c:1 ()
; (command
; "_move" "g" "x-x" "" "d" "0,5000,0"
; )
;)
;funkcja przesuwanie zbrojenia x-x
;(defun c:2 ()
; (command
; "_move" "g" "x-x" "" "d" "0,-5000,0"
; )
;)
;funkcja przesuwanie zbrojenia x-x
;(defun c:3 ()
; (command
; "_move" "g" "y-y" "" "d" "0,5000,0"
; )
;)
;funkcja przesuwanie zbrojenia x-x
;(defun c:4 ()
; (command
; "_move" "g" "y-y" "" "d" "0,-5000,0"
; )
;)
;-------------------------------------------
;poka¿ siatke i wartoœci abc
(defun c:` ()
(command "-layer" "thaw" "tkd_abc_elements"
"on" "tkd_abc_elements"
""
)
(command "-layer" "on" "tkd_abc_elements" "")
(command "-layer" "thaw" "tkd_abc_pole_dx"
"on" "tkd_abc_pole_dx"
""
)
(command "-layer" "on" "tkd_abc_pole_dx" "")
(command "-layer" "thaw" "tkd_abc_pole_dy"
"on" "tkd_abc_pole_dy"
""
)
(command "-layer" "on" "tkd_abc_pole_dy" "")
(command "-layer" "thaw" "tkd_abc_pole_gx"
"on" "tkd_abc_pole_gx"
""
)
(command "-layer" "on" "tkd_abc_pole_gx" "")
(command "-layer" "thaw" "tkd_abc_pole_gy"
"on" "tkd_abc_pole_gy"
""
)
(command "-layer" "on" "*" "")
(command "-layer" "unlock" "*" "")
(command "-layer" "lock" "xref" "")
(princ)
)
------------------------------------------
; Audyt - grzesiek
;(defun c:`` ()
;(command "-purge" "a" "n" "")
;(command "-purge" "r" "*" "n" "")
;(command "audit" "y")
;(command "-purge" "a" "n" "")
;(command "-purge" "r" "*" "n" "")
;(command "_zoom" "e")
;(command "_qsave" "" "" "y")
;(princ "ok")
;)
(defun c:`` ()
(command "-purge" "a" "n" "")
(command "-purge" "r" "*" "n")
(command "_zoom" "e")
(command "_qsave")
(princ "ok")
;(command "-layer" "color" "cyan" "tkd_pomoce" "")
)
(defun #test10 ()
(command "-purge" "a" "n" "")
(command "-purge" "r" "*" "n")
(command "_zoom" "e")
(command "_qsave")
(princ "ok")
)
(defun c:wy ()
(command "_.Tilemode" 1)
(#test10)
(command "_.layer" "off" "*" "" "")
(command "_.layer" "on" "tkd_wykaz_zbrojenia" "" "")
(setq $tmp_wykaz (ssget))
(command "erase" $tmp_wykaz "")
(command "_.layer" "on" "*" "")
(#fwykaz_new)
)
(defun c:9 ()
(command "_.Tilemode" 0)
)
;xref lock/unlock/freeze/thaw
(defun c:xxl ()
(command "_.layer" "_s" "0" "")
(command "_.layer" "_lo" "xref" "")
)
(defun c:xxu ()
(command "_.layer" "_s" "0" "")
(command "_.layer" "_u" "xref" "")
)
(defun c:xz ()
(command "_.layer" "_s" "0" "")
(command "_.layer" "_f" "xref" "")
)
(defun c:xa ()
(command "_.layer" "_s" "0" "")
(command "_.layer" "_t" "xref" "")
)
;abc pokaz ukryj
(defun c:abcc ()
(command "_.layer" "_s" "0" "")
(command "_.layer" "_f" "tkd_abc_*" "")
)
(defun c:abco ()
(command "_.layer" "_s" "0" "")
(command "_.layer" "_t" "tkd_abc_*" "")
(command "_.layer" "_f" "tkd_abc_elements" "")
)
;wymroz xref
;odczyt danych txt
(defun c:zmiana_xrefa ()
;(command "-xref" "p" "K-00 Szalunki - Budynek B" "363-PW-K-00-R00" "")
;(command "-rename" "b" "K-00 Szalunki - Budynek B" "363-PW-K-00-R00" "")
(command
"-xref" "t"
"363-PP-K-19do22 Plany szalunkowe - budynek nr 4"
"n" ""
)
(command "-xref" "t" "tabelka" "n" "")
(command "-xref" "u" "363w_R_bud.4" "")
)
(defun c:LayColor (/ ss clr col i lst lay)
(vl-Load-Com)
(if (setq ss (ssget "_:L"))
(progn
(setq lst ","
ccl (getvar 'CECOLOR)
)
(repeat (setq i (sslength ss))
(setq
lay (strcat
(cdr (assoc 8 (entget (ssname ss (setq i (1- i))))))
","
)
)
(if (not (vl-string-search (strcat "," lay) lst))
(setq lst (strcat lst lay))
)
)
(setq lst (vl-string-trim "," lst))
(initdia)
(command "_.COLOR")
(setq col (getvar 'CECOLOR))
(setvar 'CECOLOR ccl)
(command "_.LAYER" "_Color")
(if (wcmatch col "RGB:*")
(command "_T" (substr col 5))
(command col)
)
(command lst "")
)
)
(princ)
)
;skrót pod hideobjects
(defun c:0 () (command "hideobjects" pause ""))
(defun c:7 () (command "UNISOLATEOBJECTS" ""))
(defun c:1 ()
(command "_.layer" "_f" "tkd_pomoce" ""))
(defun c:2 () (command "_.layer" "_t" "tkd_pomoce" ""))
| 5,102 | Common Lisp | .l | 199 | 20.683417 | 79 | 0.460282 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a64222a9a58459e8c6ab04200b4825bdc635fb00b98600f7b0fa43af425bf076 | 42,035 | [
-1
] |
42,036 | @oznacznie_dl_l.lsp | nobodybusiness_lisp-backup/!lsp/@oznacznie_dl_l.lsp | (defun @oznaczenie_dl_przyl ()
(if (setq $SS (ssget '((0 . "LWPOLYLINE")))) ; przyjecie z zaznaczenie tylko plinii
(progn
(setq $line (ssname $ss 0))
;znajduje wszytskie pkt plinni i przypisuje je do listy
(setq $plist (mapcar 'cdr
(vl-remove-if-not
'(lambda (x) (= (car x) 10))
(entget $line)
)
)
)
(setq $i 1)
(setq $p_list_length (length $plist))
(while $plist ; petla przypisania wszystkich pkt
(setq $i_string (rtos $i 2 0))
(setq $name (read (strcat "$p" $i_string)))
(set $name (car $plist))
(setq $plist (cdr $plist))
(setq $i (+ 1 $i))
)
(setq $n 1)
(setq $dline_zm 0)
(setq $dline1 0)
(while (< $n $p_list_length)
(setq $n_string (rtos $n 2 0))
(setq $pA (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $n (+ 1 $n))
(setq $n_string (rtos $n 2 0))
(setq $pB (vl-symbol-value (read (strcat "$p" $n_string))))
(setq $dline_zm (+ (fix (distance $pA $pB)) 1))
(setq $dline1 (+ $dline1 $dline_zm))
)
)
)
(setq $angle (@RtD (angle $pB $pA))) ; ustawienie obrotu tekstu zgodnie ze standardem
(if (or
(and (> $angle 90) (< $angle 180))
(and (> $angle 180) (< $angle 270))
)
(setq $angle (- $angle 180))
)
(setq $dline2 (getint "D³ugoœc przy³¹cza (autom. +3):")) ; podana z qgis
(if (or (>= (- $dline2 $dline1) 25) (>= (- $dline1 $dline2) 25))
(alert (strcat "Ró¿nica d³ugoœci ponad 25m\nQgis: " (rtos $dline2 2 0) "\nMapa: " (rtos $dline1 2 0)))
)
(if (> (- $dline2 $dline1) 10)
(setq $dline (+ (fix (/ (+ $dline1 $dline2) 2)) 3))
(progn
(if (> $dline1 $dline2) ; warunek sparwdzajacy wieksza dlugosc
(setq $dline (+ 3 $dline1)) ;domyslne dodanie 3
(setq $dline (+ 3 $dline2))
)
)
)
;Sugeruj d³ugoœc z cada?
(setq $otwr (getint "Liczba otworów:"))
(if (= $otwr 0)
(setq $otwr "")
)
(setq $wsp (mapcar '(lambda (x y) (+ (* 0.5 x) (* 0.5 y))) ;dodaje polowy z listy punktow
$pA
$pB
)
)
(command "-insert" "dl_linni" $wsp "" "" $angle "" "" "" "" "" "" "" $otwr $dline) ;wstawia blok dlugosci linni
)
;=======Radians to Degrees======
(defun @RtD (r) (* 180.0 (/ r pi)))
| 2,543 | Common Lisp | .l | 69 | 27.84058 | 115 | 0.480956 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 918fe765fbba90bee4ef1e05c991daa9e75d899948b93f7a07f1cf40124459f6 | 42,036 | [
-1
] |
42,037 | @PIGTAIL.lsp | nobodybusiness_lisp-backup/!lsp/@PIGTAIL.lsp | (defun c:@pigtail ()
(setq $fromNumber 0)
(setq $toNumber 0)
(setq $ifUslugi (getint "<2 - AU><1 - LU><0 - HH>"))
(setq $fromNumber (getint "from number:"))
(if (/= $ifUslugi 2)
(progn
(setq $toNumber (getint "to number:"))
)
)
(if (= $ifUslugi 2)
(progn
;==============================
(setq $naIleProcent (getint "<200><70><100>"))
(if $naIleProcent
() ;nth
(setq $naIleProcent 100)
)
(vl-load-com)
(setq $ile_mieszk nil)
(setq $ile_mieszk (ssget '((0 . "INSERT") (2 . "mieszkania_blok"))))
(if $ile_mieszk
(setq $ile_mieszk_length (sslength $ile_mieszk))
(setq $ile_mieszk_length 0)
)
;=====================================
(setq $ile_lu nil)
(setq $ile_lu (ssget '((0 . "INSERT") (2 . "uslugi_blok"))))
(if $ile_lu
(setq $ile_lu_length (sslength $ile_lu))
(setq $ile_lu_length 0)
)
;=====================================
(if (= $ile_LU_length 0)
(progn
(setq $toNumber (-(+ $fromNumber (* $ile_mieszk_length (/ $naIleProcent 100))) 1))
(setq $ifUslugi 0)
)
)
(if (= $ile_mieszk_length 0)
(progn
(setq $toNumber (- (+ $fromNumber(* $ile_LU_length (/ $naIleProcent 100))) 1))
(setq $ifUslugi 1)
)
)
;==============================
)
)
(setq $i 0)
(setq $j 0)
(setq $licz_wierszy 4)
(setq $wsp (getpoint "wska¿ pkt wstawienia"))
(setq $wsp_x (car $wsp))
(setq $wsp_y (cadr $wsp))
(if (= 1 $ifUslugi)
(while (<= $fromNumber $toNumber)
(progn
(command "-insert" "przel_uslugi_blok" $wsp "" "" "" $fromNumber)
(print)
(if (= $j 0)
(progn
(setq $wsp_y (+ $wsp_y 25))
(setq $wsp (strcat (rtos $wsp_x 2) "," (rtos $wsp_y 2)))
(setq $j 1)
)
(progn
(setq $wsp_y (- $wsp_y 25))
(setq $wsp_x (+ $wsp_x 25))
(setq $wsp (strcat (rtos $wsp_x 2) "," (rtos $wsp_y 2)))
(setq $j 0)
)
)
(setq $i (+ 1 $i))
(setq $fromNumber (+ $fromNumber 1))
)
)
(while (<= $fromNumber $toNumber)
(progn
(command "-insert" "przel_mieszk_blok" $wsp "" "" "" $fromNumber)
(print)
(if (= $j 0)
(progn
(setq $wsp_y (+ $wsp_y 25))
(setq $wsp (strcat (rtos $wsp_x 2) "," (rtos $wsp_y 2)))
(setq $j 1)
)
(progn
(setq $wsp_y (- $wsp_y 25))
(setq $wsp_x (+ $wsp_x 25))
(setq $wsp (strcat (rtos $wsp_x 2) "," (rtos $wsp_y 2)))
(setq $j 0)
)
)
(setq $i (+ 1 $i))
(setq $fromNumber (+ $fromNumber 1))
)
)
)
)
| 2,929 | Common Lisp | .l | 99 | 20.878788 | 93 | 0.420848 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | f6ca328c4d1da3481295c17bd3fb1478438dff2e41a786431a9bf156311d216f | 42,037 | [
-1
] |
42,038 | @mieszkania.lsp | nobodybusiness_lisp-backup/!lsp/@mieszkania.lsp | ;generowanie schematu mieszkn z txt
(setq $lsp_adress_mieszkania "C:/Users/Desktop/Piotr P/test/uklad mieszkan/")
(defun @blok_tabelka (x)
(vl-load-com)
(command "-insert" "tabelka_blok" x "" "" "") ;wstawia blok tabelki
(command "_explode" "_l") ; explode tabelki
)
(defun c:@wstawienie_blokow ()
(vl-load-com)
(setq $pigtail_z_innych 0)
(setq a "0,0")
(setq b "900,0,0") ; PRZESUNIECIE
(@blok_tabelka a)
(setq $filepath $lsp_adress_mieszkania) ; ustawia sciezke do folderu mieszkan
(setq $fl (open (strcat $filepath "mieszkania.txt") "r")) ; otwarcie do odczytania pliku z danymi
(while (setq $fil (read-line $fl)) ;petla do odczytu linni
(if (not (null $fil))
(setq $data (append $data (list $fil))) ; przypisanie danych do listy
)
)
(close $fl) ; zamkniecie pliku txt
(setq $zm_przel (getint "\nNumer pierwszej prze��cznicy:")) ; numer pierwszej przelaczicy
;===Ustalenie na ile w��kien jest projektowane========
(setq $zm_ilosci_wlokien (getint "\nProjektowane na 200%/100%/120%/70% <100%>:")) ; +odczyt z excela!
(if (= $zm_ilosci_wlokien 200)
(setq $zm_ilosci_wlokien 1.999)
(if (= $zm_ilosci_wlokien 70)
(setq $zm_ilosci_wlokien 0.699)
(setq $zm_ilosci_wlokien 0.999)
)
)
;==============================
(setq $start_ilo_lu 0)
(while $data ; petla przypisania danych z list do zmiennych
(@blok_tabelka b)
(if (= (car $data) "\"+\"") ; warunek: przeskok do nastepnej klatki
(progn
(setq $data (cdr $data))
(setq $data (cdr $data))
(setq $data (cdr $data))
(setq $data (cdr $data))
(setq $data (cdr $data)) ;!dodatkowo na mufe
(setq $data (cdr $data)) ; usunjiecie znaku + i calej linni z excela
(command "_.move" "_all" "" "_d" "-9blade00,0,0" "")
(setq $wsp_x (- $wsp_x 50))
)
)
;=======HH=============
(setq $el (car $data)) ; odczyt wspl HH
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $HH (atof $el)) ; string to real
(setq $data (cdr $data)) ; skrocenie listy o HH
;=======LU=============
(setq $el (car $data)) ; odczyt wspl LU
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $LU (atof $el)) ; string to real
(setq $data (cdr $data)) ; skrocenie listy o LU
;=======1ST NUMBER=============
(setq $el (car $data)) ; odczyt 1st NR
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $NR (atof $el)) ; string to real
(setq $data (cdr $data)) ; skrocenie listy o NR
;=======last NUMBER=============
(setq $el (car $data)) ; odczyt LNR
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $LNR (atof $el)) ; string to real
(setq $data (cdr $data)) ; skrocenie listy o LNR
;=======LVL=============
(setq $el (car $data)) ; odczyt wspl LVL
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $LVL (atof $el)) ; string to real
(setq $data (cdr $data)) ; skrocenie listy o LVL
;=======MUFA=============
(setq $el (car $data)) ; odczyt wspl MUFA
(setq $el (vl-string-left-trim "\"" $el))
(setq $el (vl-string-right-trim "\"" $el))
(setq $MUFA (atof $el)) ; string to real
(setq $data (cdr $data)) ; skrocenie listy o MUFA
;=======KONIEC PRZYPISANIA DANYCH JEDNEGO POZIOMU=============
;=======WSP STARTOWE====================
(setq $wsp_star_x 50)
(setq $wsp_star_y (+ 250 (* $LVL 50))) ; w zaleznosci od pierwszego pietra z danymi
(setq $wsp (strcat (rtos $wsp_star_x) "," (rtos $wsp_star_y))) ; zmiana na wspl zg z zapisem cad
;=======BLOK MIESZKAN===================
(setq i 1) ; zmienna przesuniecia x
(while (<= $NR $LNR) ;petla sprawdzajaca numer mieszkania od 1st do ostatniego
(command "-insert" "mieszkania_blok" $wsp "" "" "" $NR) ;wstawia blok mieszkan
(setq $wsp_x (+ $wsp_star_x (* i 50))) ; przesuniecie po 50 co mieszkanie
(setq $wsp (strcat (rtos $wsp_x) "," (rtos $wsp_star_y)))
(setq i (+ i 1))
(setq $NR (+ $NR 1))
)
;=======LOKALE USLUGOWE=================
(if (= $LU 0) ; warunek obecnosci LU
(princ) ; do_nothing
(progn
(setq $zm_lokale 1) ; startowa watrosc zmiennej lokali
(while (<= $zm_lokale $LU) ;petla lokali
(setq $zm_nazwa_uslugi (strcat "U" (rtos (+ $zm_lokale $start_ilo_lu)))) ; przypisanie zmiennej nazwy dla uslug
(command "-insert" "uslugi_blok" $wsp "" "" "" $zm_nazwa_uslugi) ;wstawia blok lu
(setq $wsp_x (+ $wsp_star_x (* i 50))) ; przesuniecie po 50 co blok
(setq $wsp (strcat (rtos $wsp_x) "," (rtos $wsp_star_y)))
(setq i (+ i 1))
(setq $zm_lokale (+ $zm_lokale 1))
)
(setq $start_ilo_lu (+ $start_ilo_lu $LU)) ; zmiana poczatku numeracji LU dla nastepnych poziomow
)
)
;=========BLOK PRZELOCZNIC MIESZKANIOWYCH======
(if (= $HH 0) ; sprawdzenie czy istnieja HH na poziomie
(princ)
(progn
(if (= $zm_ilosci_wlokien 1.199) ; zmiana dla 70/120 %
(setq $zm_ilosci_wlokien 0.699)
)
(if (= $zm_ilosci_wlokien 1.999) ; zmiana dla 70/120 %
(setq $zm_ilosci_wlokien 1.999)
)
(setq j 1) ; zmienna przesuniecia w kierunku y
(if (= $MUFA 0) ; warunek istnienia mufy na poziomie
(progn
(setq $pigtail_z_innych (+ $pigtail_z_innych $HH)) ; zachowanie do pozniejszego wykorzystania
(setq $l_przel_msk 0)
)
(progn
(setq $HH (+ $HH $pigtail_z_innych)) ; wykorzystanie z innych poziom�w
(setq $pigtail_z_innych 0)
(setq $l_przel_msk (+ 1 (fix (* $zm_ilosci_wlokien $HH)))) ; liczba przel mieszkaniowych na poziomach
)
)
(setq $max_przel_msk (- (+ $zm_przel $l_przel_msk) 1))
(while (<= $zm_przel $max_przel_msk) ;petla
(command "-insert" "przel_mieszk_blok" $wsp "" "" "" $zm_przel) ;wstawia blok przelacznic
(if (= j 1) ; zmienne po y
(progn
(setq $wsp_y (+ $wsp_star_y (* j 25)))
(setq $wsp (strcat (rtos $wsp_x) "," (rtos $wsp_y)))
(setq $zm_przel (+ $zm_przel 1))
(setq j 0)
)
(progn
(setq $wsp_x (+ $wsp_x 25))
(setq $wsp_y (+ $wsp_star_y (* j 25)))
(setq $wsp (strcat (rtos $wsp_x) "," (rtos $wsp_y)))
(setq $zm_przel (+ $zm_przel 1))
(setq j 1)
)
)
)
)
)
;======BLOK PRZELACZNIC USLUGOWYCH==========
;! JESZCZE JAKIS BLAD - PIERWSZA PRZELACZNICA ZLE SIE WSTAWIA
(if (= $LU 0) ; warunek obecnosci LU
(princ) ; do_nothing
(progn
(if (= $zm_ilosci_wlokien 0.699) ; zmiana 70/120%
(setq $zm_ilosci_wlokien 1.199)
)
(if (= $zm_ilosci_wlokien 0.999) ; zmiana dla 70/120 %
(setq $zm_ilosci_wlokien 0.999)
)
;(if (= $MUFA 0) ; warunek istnienia mufy na poziomie
; (progn
; (setq $pigtail_z_innych_LU (+ $pigtail_z_innych_LU $LU)) ; zachowanie do pozniejszego wykorzystania
; (setq $l_przel_usl 0)
;)
;(progn
; (setq $LU (+ $LU $pigtail_z_innych_LU)) ; wykorzystanie z innych poziom�w
;(setq $pigtail_z_innych_LU 0)
(setq $l_przel_usl (+ 1 (fix (* $zm_ilosci_wlokien $LU)))) ; liczba przel USL na poziomach
;)
;)
(setq $max_przel_usl (- (+ $zm_przel $l_przel_usl) 1))
(setq j 1) ; pierwsze przesuniecie w y
(while (<= $zm_przel $max_przel_usl) ;petla
(command "-insert" "przel_uslugi_blok" $wsp "" "" "" $zm_przel) ;wstawia blok przelacznic
(if (= j 1) ; zmienne po y
(progn
(setq $wsp_y (+ $wsp_star_y (* j 25)))
(setq $wsp (strcat (rtos $wsp_x) "," (rtos $wsp_y)))
(setq $zm_przel (+ $zm_przel 1))
(setq j 0)
)
(progn
(setq $wsp_x (+ $wsp_x 25))
(setq $wsp_y (+ $wsp_star_y (* j 25)))
(setq $wsp (strcat (rtos $wsp_x) "," (rtos $wsp_y)))
(setq $zm_przel (+ $zm_przel 1))
(setq j 1)
)
)
)
)
)
)
;===========overkill============
(command "-overkill" "_all" "" "_T" "_N" "_E" "_N" "_P" "_N" "")
)
(defun C:@opisy_1 ()
(setq $pkt_wst (getpoint "PKT wstawienia:"))
;(setq $nr_osd (getstring "Numer OSD:<_>"))
;(if (= $nr_osd "")
;(setq $nr_osd "_")
; )
;(setq $nr_OKW (getstring "Numer OKW:<_>"))
;(if (= $NR_OKW "")
; (setq $NR_OKW "_")
; )
(setq $nr_osd "_")
(setq $nr_OKW "_")
(setq $wlokna_okw (getstring "W�okna OKW: <2X6>"))
(if (= $wlokna_okw "")
(setq $wlokna_okw "2X6")
)
(setq $WLOKNA_W_KABLU (getstring "W�okna w kablu: <_-_>"))
(if (= $WLOKNA_W_KABLU "")
(setq $WLOKNA_W_KABLU "_-_")
(setq $WLOKNA_W_KABLU (strcat "1-" $WLOKNA_W_KABLU))
)
(setq $IL_wlokna_okw
(cond
((= $wlokna_okw "2X6") "12")
((= $wlokna_okw "4X4") "16")
((= $wlokna_okw "2X12") "24")
((= $wlokna_okw "4X6") "24")
((= $wlokna_okw "4X8") "32")
((= $wlokna_okw "3X12") "36")
((= $wlokna_okw "4X12") "48")
((= $wlokna_okw "8X6") "48")
((= $wlokna_okw "8X8") "64")
((= $wlokna_okw "6X12") "72")
((= $wlokna_okw "12X6") "72")
((= $wlokna_okw "10X8") "80")
((= $wlokna_okw "8X12") "96")
((= $wlokna_okw "12X12") "144")
((/= $wlokna_okw NIL) "?")
)
)
(setq $tekst1 (strcat "OSD" $nr_osd "/OKW" $NR_OKW "/" $il_wlokna_okw "J_(" $WLOKNA_W_KABLU ")"))
(setq $tekst2 (strcat "HPC_1626_CT_" $wlokna_okw "G657"))
(command "-insert" "osd" $pkt_wst "" "" "" $tekst1 $tekst2 "") ;wstawia blok osd - multiline
)
(defun C:@opisy_2 ()
;dodaj petle od danego wlokna ile wlokien -> automat numeru tuby!
;(setq $pkt_wst (getpoint "PKT wstawienia:"))
(setq $poziom (getstring "Poziom:"))
(if (= $poziom "0")
(setq $poziom "parter")
(setq $poziom (strcat "pi�tro " $poziom))
)
(setq $liczba_tub (getint "Liczba tub<1>"))
(if (= $liczba_tub nil)
(setq $liczba_tub 1)
)
(setq $od_tub (getint "od tuby<1>"))
(if (= $od_tub nil)
(setq $od_tub 1)
)
(setq $color_tuba "")
(setq krotnosc_tuby (getint "krotnosc tuby"))
(repeat $liczba_tub
(setq $tuba $od_tub)
(setq $color_tuba_zm
(cond
((= $tuba 1) ", Tuba 1, Cz, w�. ")
((= $tuba 2) ", Tuba 2, Ni, w�. ")
((= $tuba 3) ", Tuba 3, Bi, w�. ")
((= $tuba 4) ", Tuba 4, Zi, w�. ")
((= $tuba 5) ", Tuba 5, Fi, w�. ")
((= $tuba 6) ", Tuba 6, Po, w�. ")
((= $tuba 7) ", Tuba 7, Sz, w�. ")
((= $tuba 8) ", Tuba 8, Zo, w�. ")
((= $tuba 9) ", Tuba 9, Br, w�. ")
((= $tuba 10) ", Tuba 10, Ro, w�. ")
((= $tuba 11) ", Tuba 11, Ca, w�. ")
((= $tuba 12) ", Tuba 12, Tu, w�. ")
)
)
(setq $wlk_w_tubie_wl (strcat (rtos (+ (* (- $od_tub 1) krotnosc_tuby) 1) 2 0) "-" (rtos (* $od_tub krotnosc_tuby) 2 0))) ;;(getstring (strcat "wl w�okna w tubie " (rtos $od_tub 2 0) "<od-do>")))
(if (= $liczba_tub 1)
(setq $color_tuba (strcat $color_tuba $color_tuba_zm $wlk_w_tubie_wl))
(setq $color_tuba (strcat $color_tuba $color_tuba_zm $wlk_w_tubie_wl))
)
(setq $od_tub (+ 1 $od_tub))
)
(vl-load-com)
(setq $il_mieszk nil)
(setq $ile_mieszk (ssget '((0 . "INSERT") (2 . "mieszkania_blok"))))
(setq $i 0)
(setq $ile_mieszk_length (sslength $ile_mieszk))
(setq $VAL 0)
(setq $nr_miesz_min 999)
(setq $nr_miesz_max 0)
(setq $nr_dodat "")
(while (< $i $ile_mieszk_length)
(setq $ent (ssname $ile_mieszk $i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND"))
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "NUMER_MIESZKANIA")
(setq $VAL (CDR (assoc 1 $LST)))
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $VAL_ZM (atoi $VAL))
(if (/= (rtos $val_zm 2 0) $VAL)
(setq $nr_dodat (strcat $nr_dodat ", " $VAL))
)
(setq $nr_miesz_min
(if (< $VAL_ZM $nr_miesz_min)
(progn
$VAL_ZM
)
(progn
$nr_miesz_min
)
)
)
(setq $nr_miesz_max
(if (> $VAL_ZM $nr_miesz_max)
(progn
$VAL_ZM
)
(progn
$nr_miesz_max
)
)
)
(setq $i (+ 1 $i))
(setq $HH_z_i $i)
)
(setq $czy_LU (getint "czy LU?<1-tak,0-nie>"))
(if (= $czy_LU 1)
(@LU_do_opis)
(progn
;(setq $tekst1 (strcat $poziom ", " "(lok. " (rtos $nr_miesz_min 2 0) "-" (rtos $nr_miesz_max 2 0) ") (liczba HH: " (rtos $i 2 0) ")"))
(command "-insert" "osd_2" $pkt_wst "" "" "" $tekst1 $tekst2 $color_tuba "")
)
)
)
(defun c:@ile_HH_LU ()
(vl-load-com)
(setq $ile_mieszk nil)
(setq $ile_mieszk (ssget '((0 . "INSERT") (2 . "mieszkania_blok"))))
(if $ile_mieszk
(setq $ile_mieszk_length (sslength $ile_mieszk))
(setq $ile_mieszk_length 0)
)
;=====================================
(setq $ile_lu nil)
(setq $ile_lu (ssget '((0 . "INSERT") (2 . "uslugi_blok"))))
(if $ile_lu
(setq $ile_lu_length (sslength $ile_lu))
(setq $ile_lu_length 0)
)
;=====================================
(print (strcat (rtos $ile_mieszk_length 2 0) "HH+" (rtos $ile_lu_length) "LU"))
(princ)
)
(defun c:@opis ()
(C:@opisy_1)
(c:@opisy_2)
)
(defun c:@licz_wkl ()
(setq i 1)
(setq $pocz_pigtail 1)
(setq $ost_pigtail (getint "ost. pigtail:"))
(setq $liczba_pigtaili (- (+ 1 $ost_pigtail) $pocz_pigtail))
(setq $typ_kabla 12)
(while (< (* i $typ_kabla) $liczba_pigtaili)
(setq i (+ 1 i))
)
(setq i (- i 1))
(setq $wlk_na_ost_tubie (- $liczba_pigtaili (* i $typ_kabla)))
(setq $colorWkl
(cond
((= $wlk_na_ost_tubie 1) " Cz.")
((= $wlk_na_ost_tubie 2) " Ni.")
((= $wlk_na_ost_tubie 3) " Bi.")
((= $wlk_na_ost_tubie 4) " Zi.")
((= $wlk_na_ost_tubie 5) " Fi.")
((= $wlk_na_ost_tubie 6) " Po.")
((= $wlk_na_ost_tubie 7) " Sz.")
((= $wlk_na_ost_tubie 8) " Zo.")
((= $wlk_na_ost_tubie 9) " Br.")
((= $wlk_na_ost_tubie 10) " Ro.")
((= $wlk_na_ost_tubie 11) " Ca.")
((= $wlk_na_ost_tubie 12) " Tu.")
)
)
(setq $tubaNr (+ 1 i))
(setq $colorTuba
(cond
((= $tubaNr 1) " Cz. ")
((= $tubaNr 2) " Ni. ")
((= $tubaNr 3) " Bi. ")
((= $tubaNr 4) " Zi. ")
((= $tubaNr 5) " Fi. ")
((= $tubaNr 6) " Po. ")
((= $tubaNr 7) " Sz. ")
((= $tubaNr 8) " Zo. ")
((= $tubaNr 9) " Br. ")
((= $tubaNr 10) " Ro. ")
((= $tubaNr 11) " Ca. ")
((= $tubaNr 12) " Tu. ")
)
)
(setq $specyfikacja_kabla (strcat
(rtos i 2 0)
" tub + "
(rtos $wlk_na_ost_tubie 2 0)
" wlk. na tubie "
(rtos (+ 1 i) 2 0)
$colortuba
"("
(rtos (- (* (+ 1 i) 12) 11) 2 0)
"-" (rtos (* (+ 1 i) 12) 2 0)
")"
$colorWkl
)
)
(print $specyfikacja_kabla)
(princ)
)
(defun @LU_do_opis ()
(vl-load-com)
(setq $il_LU nil)
(setq $ile_LU (ssget '((0 . "INSERT") (2 . "uslugi_blok"))))
(setq $i 0)
(setq $ile_LU_length (sslength $ile_LU))
(setq $string "")
(while (< $i $ile_LU_length)
(setq $ent (ssname $ile_LU $i))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $ent))))
(while (not (equal $name "SEQEND"))
(setq $LST (entget $ent))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "USLUGI_NUMER")
(setq $string (strcat $string "," (CDR (assoc 1 $LST))))
)
(setq $ent (entnext $ent))
(setq $name (cdr (assoc '0 (entget $ent))))
)
(setq $i (+ 1 $i))
)
(setq $tekst1 (strcat $poziom ", " "(lok. " (rtos $nr_miesz_min 2 0) "-" (rtos $nr_miesz_max 2 0) $string ") (liczba HH: " (rtos $HH_z_i 2 0) "+" (rtos $ile_LU_length 2 0) "LU" ")"))
(setq $tekst2 (strcat $tekst2 $color_tuba))
(command "-insert" "osd_2" $pkt_wst "" "" "" $tekst1 $tekst2 "")
)
| 17,462 | Common Lisp | .l | 461 | 30.004338 | 201 | 0.484666 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 4eb4369b7ef8872ebbb8ac634f14cce48da5afed17171308175a08d1905d59b7 | 42,038 | [
-1
] |
42,039 | @Audyt_i_Purge.lsp | nobodybusiness_lisp-backup/!lsp/@Audyt_i_Purge.lsp | ;glebokie czysczenie rysunku
; Audyt pliku
(defun @Audyt_i_Purge ()
;purgowanie wszystkich elementow
(command "-purge" "a" "*" "n")
(command "-purge" "r" "*" "n")
;naprawa i sprawdzenie bledow na rys
(command "audit" "y")
;purgowanie wszystkich elementow
(command "-purge" "a" "*" "n")
(command "-purge" "r" "*" "n")
;oddalenie dla sprawdzenie "resztek" rysunkowcyh
(command "_zoom" "e")
;(command "_qsave" "" "" "y")
(princ "\nelementy zostaly usuniete")
(princ)
)
| 507 | Common Lisp | .l | 17 | 26.411765 | 50 | 0.628571 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 8bb9817956e5aa375a4b27e72e818d1759386b69affa732c9a5fe7a2d5988b9f | 42,039 | [
-1
] |
42,040 | @invertSelection.lsp | nobodybusiness_lisp-backup/!lsp/@invertSelection.lsp | ;odrocenie zaznaczenie (usun wszystko poza zaznaczonym obszarem)
(defun c:@invertSelection ()
(vl-load-com)
(setq $area (entget (car (entsel)))) ;pobiera od uzytkownika zaznaczenie
(setq $pts nil)
(while
(setq $zm_pt (assoc 10 $area)) ; odczytujemy pierwszy wiercho�ek
(setq $pts (append $pts (list (cdr $zm_pt)))) ; dodajemy go do listy wynikowej
(setq $area (cdr (member $zm_pt $area))) ; "obcinamy" pocz�tek listy za wyszukany element, by kolejny by� wyszukany jako pierwszy.
)
(setq ssBox (ssget "CP" $pts)) ; ustalenie obwiedni odczytania wynikow
(setq ssAll (ssget "_A" ))
(command "_copybase" "0,0,0" ssBox "")
(command "_erase" ssAll "")
(command "_pasteblock" "0,0,0" )
(command "_explode" (entlast))
)
| 787 | Common Lisp | .l | 17 | 41.705882 | 141 | 0.655263 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | d842959db69c30035e190073bb832bacd3bf1671d5414dd9705e9dcb0d9c6501 | 42,040 | [
-1
] |
42,041 | @BURST.lsp | nobodybusiness_lisp-backup/!lsp/@BURST.lsp | ;rozbicie z przypisaniem wartosci
(defun c:@BURST ( / ssbl cnt en eL )
; select all Blocks and set a loop counter
(setq ssbl (ssget (list (cons 0 "INSERT")(cons 66 1)))
cnt 0
)
;loop through all selected blocks...
(repeat (sslength ssbl)
; get Block entity list
(setq en (ssname ssbl cnt)
en (entnext en) ; get first attribute
el (entget en)
)
; loop through all attributes of this Block Reference...
(while (not (equal (cdr (assoc 0 el)) "SEQEND"))
(entmake (list '(0 . "TEXT")
'(100 . "AcDbEntity")
'(100 . "AcDbText")
(cons 1 (cdr (assoc 1 el)) ) ; txt-string
(cons 7 (cdr (assoc 7 el)) ) ; style
(cons 10 (cdr (assoc 10 el)))
;(cons 11 pt2) optional meaningful ONLY if 72 or 73 NON ZERO !
(cons 40 (cdr (assoc 40 el)) ) ; txt-height
(cons 72 (cdr (assoc 73 el))) ; h-just
(cons 73 (cdr (assoc 74 el))) ; v-just
)
); entmake
; find next attribute entity list for this Block Reference...
(setq en (entnext en)
el (entget en)
)
); loop attributes...
; go to next block reference...
(setq cnt (1+ cnt))
);repeat blocks...
); defun
| 1,336 | Common Lisp | .l | 36 | 27.944444 | 82 | 0.536413 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | aaa81713a916dd41334df9f40aef6a01046db3425ae4f7c5b5ea9f925e17aee5 | 42,041 | [
-1
] |
42,042 | @otworowka.lsp | nobodybusiness_lisp-backup/!lsp/@otworowka.lsp | (defun @otworwka (ssgetFromOutside)
(vl-load-com)
(if ssgetFromOutside
(setq $zazn ssgetFromOutside)
(setq $zazn (ssget '((0 . "INSERT") (2 . "dl_linni")))) ;dl_linni
)
(setq $zazn_name (ssname $zazn 0))
(setq $name "none")
(setq $name (cdr (assoc '0 (entget $zazn_name))))
(while (not (equal $name "SEQEND")) ; petla przypisania wartosci z bloku
(setq $LST (entget $zazn_name))
(setq $attag (cdr (assoc '2 $LST)))
(setq $attag (strcase $attag))
(if (= $attag "ILOTW")
(setq $VAL (CDR (assoc 1 $LST)))
)
(setq $zazn_name (entnext $zazn_name))
(setq $name (cdr (assoc '0 (entget $zazn_name))))
)
;(command "-layer" "_s" "otworowka" "")
(setq $otw_name (strcat "o" $VAL))
(setq $p1 (getpoint "Wskaz pkt w kierunku strzalki"))
(setq $p2 (getpoint "Wskaz pkt w kierunku konca"))
(setq $angle (- (@RtD (angle $p2 $p1)) 90)) ; -90
(command "-insert" $otw_name pause "" "" $angle)
)
| 983 | Common Lisp | .l | 26 | 33.115385 | 84 | 0.584117 | nobodybusiness/lisp-backup | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 00627d5f1ededf152b51f2be63ecb5fdd1b7592cd5999c189524efca20e022b2 | 42,042 | [
-1
] |
42,116 | nineteen.lisp | drvdputt_aoc-2022/src/nineteen.lisp | (declaim (optimize (speed 3) (debug 0) (safety 0)))
;; try to write a generic state search thing
(load "dfss.lisp")
(defun dfss/recurse (cur-state
end-p
state-score-f
next-states-f)
"Depth first state search.
STATE can be anything. This state (and evolutions of this state) will be passed to the
functions below. The details depend on these functions.
END-P should take a STATE as an argument, and return t when the state is considered final. It
is recommended that the STATE tracks the depth in some way, and cuts off the calculation at a
certain depth.
STATE-SCORE-F should return the score of a STATE. Will be called once END-PREDICATE-F returns
t, and we start going back up in the recursion. Branches will be chosen to maximize the
score. Best state is passed all the way upward.
NEXT-STATES-F should return a list of STATE objects, each one typically has a different type
of evolution step applied to them. Try to make this function as restrictive as possible, so
that the number of possible paths does not explode. If no options are returned, the branch
also ends and the score is returned."
(if (funcall end-p cur-state)
;; If the current state is past the end, just return the score. This breaks off the
;; recursion.
;; (funcall state-score-f cur-state)
cur-state
;; Else, get the possible next states, and recursively continue the state evolution. Once
;; the end of the branch returns the score, it will be passed up and returned here. The
;; maximum score of all t he sub-branches is passed up.
(let ((next-states (funcall next-states-f cur-state)))
(if next-states
(loop for next-state in next-states
with best-state = nil
with best-score = nil
do (let* ((return-state (dfss/recurse next-state
end-p
state-score-f
next-states-f))
(return-score (funcall state-score-f return-state)))
(when (or (not best-score) (> return-score best-score))
(setq best-score return-score
best-state return-state)))
finally (return best-state))
;; if no states are returned, end the calculation
(funcall state-score-f cur-state)))))
;; CONSTANTS FOR PUZZLE
(defparameter maxtime 24)
;; example cost blueprint
(defparameter bp1-e (list :orebot-ore 4
:claybot-ore 2
:obsbot-ore 3
:obsbot-clay 14
:geobot-ore 2
:geobot-obs 7
;; utility, so we don't have to take max later
:max-ore 4))
(defparameter bp2-e (list :orebot-ore 2
:claybot-ore 3
:obsbot-ore 3
:obsbot-clay 8
:geobot-ore 3
:geobot-obs 12
;; utility, so we don't have to take max later
:max-ore 3))
;; state. Let's learn something new and use defstruct. This is a macro that automatically
;; defines a bunch of functions, to make something that almost looks object-oriented.
(defstruct state
(time 0 :type integer)
;; resources
(ore 0 :type integer)
(clay 0 :type integer)
(obsidian 0 :type integer)
(geode 0 :type integer)
;; bots
(orebots 1 :type integer)
(claybots 0 :type integer)
(obsbots 0 :type integer)
(geobots 0 :type integer))
(defun state/end-p (s)
(>= (state-time s) maxtime))
(defun state/score-f (s)
(state-geode s))
(defun state/advance (s bp &key
(buy-orebot nil)
(buy-claybot nil)
(buy-obsbot nil)
(buy-geobot nil))
"Important detail. Check cost first, then collect ore, then update number of bots. If we
collect first, then the cost check is wrong. If we collect last, then the income is too high;
the new bot should not yet be included in the income multiplier."
(let ((ns (copy-state s)))
;; advance time
(incf (state-time ns))
;; increment resources (1 per turn per bot)
(incf (state-ore ns) (state-orebots ns))
(incf (state-clay ns) (state-claybots ns))
(incf (state-obsidian ns) (state-obsbots ns))
(incf (state-geode ns) (state-geobots ns))
;; spending and update number of bots
(when buy-orebot
(decf (state-ore ns) (getf bp :orebot-ore))
(incf (state-orebots ns)))
(when buy-claybot
(decf (state-ore ns) (getf bp :claybot-ore))
(incf (state-claybots ns)))
(when buy-obsbot
(decf (state-ore ns) (getf bp :obsbot-ore))
(decf (state-clay ns) (getf bp :obsbot-clay))
(incf (state-obsbots ns)))
(when buy-geobot
(decf (state-ore ns) (getf bp :geobot-ore))
(decf (state-obsidian ns) (getf bp :geobot-obs))
(incf (state-geobots ns)))
ns))
;; we can make this dumb or smart. Will drastically affect runtime.
(defun state/next-f (s bp)
(let ((next-list nil)
;; the basic checks of what's possible + optimization to not buy too many
(can-buy-orebot (and (< (state-orebots s) (getf bp :max-ore))
(>= (state-ore s) (getf bp :orebot-ore))))
(can-buy-claybot (and (< (state-claybots s) (getf bp :obsbot-clay))
(>= (state-ore s) (getf bp :claybot-ore))))
(can-buy-obsbot (and (< (state-obsbots s) (getf bp :geobot-obs))
(>= (state-ore s) (getf bp :obsbot-ore))
(>= (state-clay s) (getf bp :obsbot-clay))))
;; can buy as many as we want here
(can-buy-geobot (and (>= (state-ore s) (getf bp :geobot-ore))
(>= (state-obsidian s) (getf bp :geobot-obs)))))
;; Now, we can invent all kinds of logic.
(cond
;; First optimization: if we have enough income to buy a geobot at every turn, always buy
;; a geobot, and never wait. Possible simplification of this: always buy geobot if
;; available. Reduced runtime from 100 to 38 seconds!
((and can-buy-geobot)
;; (>= (state-orebots s) (getf bp :geobot-ore))
;; (>= (state-obsbots s) (getf bp :geobot-obs)))
(push (state/advance s bp :buy-geobot t) next-list))
;; Another optimization idea:
;; - if (orebots >= max ore cost), stop making orebots
;; - if (claybots >= max clay cost), stop making claybots
;; - if (obsbots >= max obsidian cost), stop making obsbots
;; This info is included in the can-buy-bot booleans above
;; If we can't buy anything, waiting is the only option
((not (or can-buy-orebot can-buy-claybot can-buy-obsbot can-buy-geobot))
(push (state/advance s bp) next-list))
;; if none of the above shortcuts were triggered, try different options.
(t
(let ((waiting-useless
(or
;; no clay income -> always buy if have enough ore for both
(and (= 0 (state-claybots s)) can-buy-orebot can-buy-claybot)
;; no obsidian income -> always buy if have enough resources for all three
(and (= 0 (state-obsbots s)) can-buy-orebot can-buy-claybot can-buy-obsbot))))
;; Add an option to WAIT, unless we flagged waiting as useless
(unless waiting-useless
nil)
;; (push (state/advance s bp) next-list))
;; ^^ let's see what happens if we never wait
;; >> result: MUCH faster, but doesn't always work (sometimes, all money is spent on
;; >> clay bots, instead of waiting until we have enough ore for an obsidian bot)
;; mainly a problem when clay bots are cheaper than obs bots, and obs bots require a
;; lot of clay
;; effortlessly solves the 32 case for example blueprint 2 though!
;; Add available purchase options (except when not possible or not needed, as
;; calculated in the let block above
(when can-buy-orebot
(push (state/advance s bp :buy-orebot t) next-list))
(when can-buy-claybot
(push (state/advance s bp :buy-claybot t) next-list))
(when can-buy-obsbot
(push (state/advance s bp :buy-obsbot t) next-list))
(when can-buy-geobot
(push (state/advance s bp :buy-geobot t) next-list)))))
;; (print (list can-buy-orebot can-buy-claybot can-buy-obsbot can-buy-geobot))
;; (print next-list)
next-list))
(defun do-puzzle (blueprint time)
(setq maxtime time)
(let ((s (make-state)))
(dfss/recurse s
#'state/end-p
#'state/score-f
;; blueprint is chosen here by pickling next-f
(lambda (s) (state/next-f s blueprint)))))
(load "string.lisp")
(load "file.lisp")
(defparameter lines (read-default-input "nineteen"))
(defun bp/init (line)
(let ((numbers (find-all-int line)))
(list :orebot-ore (nth 1 numbers)
:claybot-ore (nth 2 numbers)
:obsbot-ore (nth 3 numbers)
:obsbot-clay (nth 4 numbers)
:geobot-ore (nth 5 numbers)
:geobot-obs (nth 6 numbers)
;; utility, so we don't have to take max later
:max-ore (max (nth 1 numbers) (nth 2 numbers) (nth 3 numbers) (nth 5 numbers)))))
(defparameter bps (mapcar #'bp/init lines))
(defun do-all-bps (depth)
(loop for bp in bps
for id from 1
for final-state = (do-puzzle bp depth)
for quality = (* id (state-geode final-state))
do (print id)
do (print final-state)
do (print (list id "quality" quality))
sum quality))
;; this requires a hack to never wait. Works, because for the first three, the obsbot and
;; claybot are equally expensive
(defun do-part2 ()
(let ((first-three-geode (loop for bp in (subseq bps 0 3)
for final-state = (do-puzzle bp 32)
do (print final-state)
collect (state-geode final-state))))
(apply #'* first-three-geode)))
| 10,509 | Common Lisp | .lisp | 214 | 38.21028 | 96 | 0.588952 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 07a713b9af67d96fc3a5fededaa1809a8720d3d14fa80d498df0148603e9f458 | 42,116 | [
-1
] |
42,117 | four.lisp | drvdputt_aoc-2022/src/four.lisp | (load "file.lisp")
(defvar lines (read-lines #p"four-input.txt"))
;; (defvar lines (read-lines #p"four-example-input.txt"))
(defun is-contained (min1 max1 min2 max2)
(or
;; 2 contained in 1
(and (<= min1 min2) (<= max2 max1))
;; 1 contained in 2
(and (<= min2 min1) (<= max1 max2))))
(defun is-partial-overlap (min1 max1 min2 max2)
(not (or
;; all of 1 smaller than 2
(< max1 min2)
;; all of 1 bigger than 2
(< max2 min1))))
(load "num.lisp")
(load "string.lisp")
(defun elves-have-complete-overlap (line)
(let ((result (apply (function is-contained) (find-all-int line))))
result))
(defun elves-have-partial-overlap (line)
(apply (function is-partial-overlap) (find-all-int line)))
(print (count T (mapcar
(function elves-have-complete-overlap) lines)))
(print (count-predicate lines (function elves-have-partial-overlap)))
| 872 | Common Lisp | .lisp | 25 | 32 | 69 | 0.680572 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 84710ac445efc83094b07d5011410dd87eb474359ecf7a36e4cf6ba661ba9089 | 42,117 | [
-1
] |
42,118 | num.lisp | drvdputt_aoc-2022/src/num.lisp | (defun int-histogram (numbers)
(loop for n in numbers
with hist = (make-hash-table :test 'eq)
if (gethash n hist)
do (incf (gethash n hist))
else
do (setf (gethash n hist) 1)
end
finally (return hist)))
(defun at-max (valuelist otherlist)
(let ((maxval 0)
(maxother 0))
(loop for v in valuelist
for x in otherlist
do (if (> v maxval)
(setq maxval v maxother x)
nil))
(list maxval maxother)))
(defun count-predicate (value_list predicate_function)
(count T (mapcar predicate_function value_list)))
(defun range (imin imax &key (exclude-start nil) (exclude-end nil))
(let ((ascending (< imin imax))
(start imin)
(end imax))
(if exclude-start
(if ascending (incf start) (decf start)))
(if exclude-end
(if ascending (decf end) (incf end)))
(if ascending
(loop for i from start to end collect i)
(loop for i from start downto end collect i))))
(defun pprint-char-array (a)
(terpri)
(loop for i from 0 below (array-dimension a 0)
do (loop for j from 0 below (array-dimension a 1)
do (write-char (aref a i j))
finally (terpri))))
(defun pprint-int-array (a)
(terpri)
(loop for i from 0 below (array-dimension a 0)
do (loop for j from 0 below (array-dimension a 1)
do (write-string (write-to-string (aref a i j)))
finally (terpri))))
| 1,415 | Common Lisp | .lisp | 43 | 27.511628 | 67 | 0.635699 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a4015cd1b6c3ff05779cdba2f13871a5644e49837717d62dd9b63885b36b1665 | 42,118 | [
-1
] |
42,119 | eighteen.lisp | drvdputt_aoc-2022/src/eighteen.lisp | (load "file.lisp")
(load "string.lisp")
(ql:quickload :array-operations)
(defparameter lines (read-default-input "eighteen"))
(defparameter lines-e (read-default-input "eighteen-example"))
(defun integer-array (n)
(make-array n :element-type 'integer :initial-element 0))
;; let's go the easy route. Just make a 3d array, since the maximum input is only 19 anyway.
(defun cube/init (lines)
(let* ((n (length lines))
(xs (integer-array n))
(ys (integer-array n))
(zs (integer-array n)))
(loop for l in lines
for i from 0
for xyz = (find-all-int l)
for x = (first xyz)
for y = (second xyz)
for z = (third xyz)
do (setf (elt xs i) x
(elt ys i) y
(elt zs i) z))
(let* ((dimensions (mapcar (lambda (i) (1+ (reduce #'max i)))
(list xs ys zs)))
(a (integer-array dimensions)))
(print dimensions)
(loop for x across xs
for y across ys
for z across zs
do (setf (aref a x y z) 1))
a)))
(defun cube/surface-area (cube)
;; go over all cells, and if a cell is 1, check the neighboring cell. Input is small enough
;; for this to be fast.
(let ((num-sides 0))
(array-operations:nested-loop (i j k) (array-dimensions cube)
(when (> (aref cube i j k) 0)
(incf num-sides (- 6 (cube/num-neighbors cube i j k)))))
num-sides))
(defun cube/num-neighbors (cube i j k)
(let ((num-neighbors
(count t
(list
(and (> i 0) (> (aref cube (1- i) j k) 0))
(and (< i (1- (array-dimension cube 0))) (> (aref cube (1+ i) j k) 0))
(and (> j 0) (> (aref cube i (1- j) k) 0))
(and (< j (1- (array-dimension cube 1))) (> (aref cube i (1+ j) k) 0))
(and (> k 0) (> (aref cube i j (1- k)) 0))
(and (< k (1- (array-dimension cube 2))) (> (aref cube i j (1+ k)) 0))))))
;; (print num-neighbors)
;; (print (length (cube/empty-neighbors cube i j k)))
;; (print (cube/empty-neighbors cube i j k))
num-neighbors))
;; (defun cube/
;; part 2
;; idea: set up a dijkstra type walker to find all outside points. Then sum the number of
;; neighbors of the outside points. Do not need distances here can be simpler than dijkstra.
;; Just need the walker, and it can stop exploring if the neighboring nodes have already been
;; visited.
;; first set up indexing system. Map i j k to single index
(defun flat-index (i j k dimensions)
;; i + j * ni + k * ni * nj
(+ i
(* j (elt dimensions 0))
(* k (elt dimensions 0) (elt dimensions 1))))
(defun ijk-index (flat-index dimensions)
(let* ((ni (elt dimensions 0))
(nj (elt dimensions 1))
(i (mod flat-index ni))
(j (floor (mod flat-index (* ni nj)) ni))
(k (floor flat-index (* ni nj))))
(list i j k)))
;; returns all i j k for empty neighbors
(defun cube/empty-neighbors (cube i j k)
(let ((neighbors nil))
(when (and (> i 0) (= (aref cube (1- i) j k) 0))
(push (list (1- i) j k) neighbors))
(when (and (< i (1- (array-dimension cube 0))) (= (aref cube (1+ i) j k) 0))
(push (list (1+ i) j k) neighbors))
(when (and (> j 0) (= (aref cube i (1- j) k) 0))
(push (list i (1- j) k) neighbors))
(when (and (< j (1- (array-dimension cube 1))) (= (aref cube i (1+ j) k) 0))
(push (list i (1+ j) k) neighbors))
(when (and (> k 0) (= (aref cube i j (1- k)) 0))
(push (list i j (1- k)) neighbors))
(when (and (< k (1- (array-dimension cube 2))) (= (aref cube i j (1+ k)) 0))
(push (list i j (1+ k)) neighbors))
neighbors))
(load "dijkstra.lisp")
(defun do-puzzle (lines)
(cube/surface-area (cube/init lines)))
(defun do-part2 (lines)
(let* ((cube (cube/init lines))
(ds (array-dimensions cube))
(num-nodes (apply (function *) ds))
(visited
(dijkstra/visitor
num-nodes
(flat-index 0 0 0 ds)
;; flat index wrapper around neighbor function
(lambda (flat-index)
(let* ((ijk (ijk-index flat-index ds))
(neighbors (cube/empty-neighbors cube (first ijk) (second ijk) (third ijk))))
;; (print "exploring")
;; (print neighbors)
(mapcar (lambda (ijk) (flat-index (first ijk) (second ijk) (third ijk) ds))
neighbors))))))
;; then, for every visited cell, sum the number of neighbors (not working? why? is this not
;; equivalent?)
(loop for v across visited
for flat-index from 0
for ijk = (ijk-index flat-index ds)
;; if not visited
if (= v -1)
count t into novisit
and do (print (list (first ijk) (second ijk) (third ijk)
(aref cube (first ijk) (second ijk) (third ijk))))
;; if visited
if (= v 1)
sum (cube/num-neighbors cube (first ijk) (second ijk) (third ijk))
into surface
finally (print (list :novisit novisit :surface surface)))
;; alternate method: fill the holes (set cube value to one for every unvisited node), and
;; then run the old calculation
(loop for v across visited
for flat-index from 0
for ijk = (ijk-index flat-index ds)
;; if not visited, fill the hole
if (= v -1)
do (setf (aref cube (first ijk) (second ijk) (third ijk)) 1))
(cube/surface-area cube)))
;; we get novisit = 14, which is correct for the example. Only the surface area calculation
;; still has something wrong.
| 5,790 | Common Lisp | .lisp | 133 | 34.864662 | 98 | 0.557456 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 16a564f58306d8fd7c8f4b6a591ae949f6e67e3497a3a85d95002e5462dd0c4f | 42,119 | [
-1
] |
42,120 | eight.lisp | drvdputt_aoc-2022/src/eight.lisp | (load "file.lisp")
(defvar lines nil)
(defvar nrows nil)
(defvar ncols nil)
(setq lines (read-default-input "eight"))
(setq nrows (length lines))
(setq ncols (length (first lines)))
(defun make-tree-array-zeros ()
(make-array (list nrows ncols) :element-type 'integer))
(defun make-array-same-shape (array)
(make-array (array-dimensions array) :element-type 'integer))
(defvar tree_array (make-tree-array-zeros))
(defvar visible_tree_array (make-tree-array-zeros))
;; fill the tree map as an array of ints
(loop for irow below nrows
for l in lines
do (loop for icol below ncols
for c across l
do (setf (aref tree_array irow icol) (digit-char-p c))))
;; now determine for each position, if it is visible
;; we do four actions:
;; row wise
;; row wise reverse
;; col wise
;; col wise reverse
(defun array/row (array irow)
(make-array (array-dimension array 1)
:displaced-to array
:displaced-index-offset (* irow (array-dimension array 1))))
(defun array/col (array icol)
(let ((col-array (make-array (array-dimension array 0))))
(loop for irow from 0 below (array-dimension array 0)
do (setf (aref col-array irow) (aref array irow icol)))
col-array))
;; return indices of visible trees given a certain ordening of trees
(defun forward-visible-indices (trees)
(let ((tmax -1))
(loop for tree across trees
for i = 0 then (+ 1 i)
if (> tree tmax)
collect i
and do (setq tmax (max tree tmax)))))
;; same but looking from the other side
(defun backward-visible-indices (trees)
(let ((backward-result (forward-visible-indices (reverse trees))))
(mapcar (lambda (i)
(- (- (length trees) 1) i))
backward-result)))
;; check visibility for all rows
(loop for irow below nrows
do (let ((rowtrees (array/row tree_array irow)))
(loop for icol in (concatenate
'list
(forward-visible-indices rowtrees)
(backward-visible-indices rowtrees))
do (incf (aref visible_tree_array irow icol)))))
;; check visibility for all cols
(loop for icol below ncols
do (let ((coltrees (array/col tree_array icol)))
(loop for irow in (concatenate
'list
(forward-visible-indices coltrees)
(backward-visible-indices coltrees))
do (incf (aref visible_tree_array irow icol)))))
;; count total number of trees with visibility > 0
(loop for irow below nrows
sum (loop for icol below ncols
count (> (aref visible_tree_array irow icol) 0)))
;; calculate scenic scores for a vector of trees (in one direction)
(defun forward-scenic-scores (trees)
(concatenate
'list
;; first two are always zero and one
'(0 1)
;; the rest depend on the trees to the left
(loop for i from 2 below (length trees)
for current-tree = (elt trees i)
;; find distance of previous tree bigger than this one
for back-dist = (loop for i-back from 1 to i
;; when current tree <= previous tree
for previous-tree = (elt trees (- i i-back))
if (<= current-tree previous-tree)
return i-back
;; at finally, we're one too far
;; (yes, e.g. from 1 to 5 actually goes to 6 at "finally")
finally (return (- i-back 1)))
collect (if (> back-dist 0) back-dist 1))))
(defun backward-scenic-scores (trees)
(reverse (forward-scenic-scores (reverse trees))))
;; now apply this in 4 directions
(defun tree-array/scenic-scores (tree-array)
(let ((l_scores (make-array-same-shape tree-array))
(r_scores (make-array-same-shape tree-array))
(d_scores (make-array-same-shape tree-array))
(u_scores (make-array-same-shape tree-array))
(nrows (array-dimension tree-array 0))
(ncols (array-dimension tree-array 1)))
(loop for irow below nrows
do (let ((trees (array/row tree-array irow)))
(loop for icol below ncols
for l_score in (forward-scenic-scores trees)
for r_score in (backward-scenic-scores trees)
do (setf (aref l_scores irow icol) l_score)
do (setf (aref r_scores irow icol) r_score))))
(loop for icol below ncols
do (let ((trees (array/col tree-array icol)))
(loop for irow below nrows
for u_score in (forward-scenic-scores trees)
for d_score in (backward-scenic-scores trees)
do (setf (aref d_scores irow icol) d_score)
do (setf (aref u_scores irow icol) u_score))))
;; set the edges to zero
(loop for irow below nrows
do (setf (aref l_scores irow (- ncols 1)) 0)
do (setf (aref r_scores irow 0) 0))
(loop for icol below ncols
do (setf (aref u_scores (- nrows 1) icol) 0)
do (setf (aref d_scores 0 icol) 0))
(list :left l_scores :right r_scores :down d_scores :up u_scores)))
(ql:quickload :array-operations)
(defun tree-array/scenic-multiply (tree-array)
(let ((scores (tree-array/scenic-scores tree-array)))
(reduce (lambda (a b)
(aops:each (function *) a b))
(list (getf scores :up)
(getf scores :down)
(getf scores :left)
(getf scores :right)))))
(defvar small_test (make-array '(5 5) :element-type 'integer
:initial-contents '((3 0 3 7 3)
(2 5 5 1 2)
(6 5 3 3 2)
(3 3 5 4 9)
(3 5 3 9 0))))
(print (tree-array/scenic-scores small_test))
(print (tree-array/scenic-multiply small_test))
(print (tree-array/scenic-multiply tree_array))
(print (loop for i across (aops:flatten (tree-array/scenic-multiply tree_array))
maximize i))
| 5,458 | Common Lisp | .lisp | 139 | 34.582734 | 80 | 0.671453 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 0a298e6dfeb579d562e9d7cc9b3922eb40f6393c8ed30d21281a56294e955445 | 42,120 | [
-1
] |
42,121 | eleven.lisp | drvdputt_aoc-2022/src/eleven.lisp | (defvar all-divisors-used nil)
(load "file.lisp")
(defvar lines nil)
(setq lines (read-default-input "eleven"))
(defun monkey/init (items ;; list of int
operation ;; function(old) -> new
divisor ;; int for divisible test
next-true ;; index of next monkey if True
next-false) ;; index of next monkey if False
(list :items items
:operation operation
:divisor divisor
:next-true next-true
:next-false next-false))
;; tip: I can test this using (funcall (monkey/parse-operation "
;; Operation: new = old + 2") 10) etc.
(defun monkey/parse-operation
(operation-line)
;; second line has three forms
;; Operation: new = old + 8
;; Operation: new = old * 3
;; Operation: new = old * old
;; character 23 is either + or *
;; then there's a number or "old"
(let ((operator (if (char= (char operation-line 23) #\+)
(function +)
(function *)))
(second-operand (subseq operation-line 25)))
(if (string= second-operand "old")
;; if this is "old" return square function
(lambda (old) (* old old))
;; else we need to parse the number
(let ((number (parse-integer second-operand)))
(lambda (old) (funcall operator old number))))))
(load "string.lisp")
(defun monkey/parse (lines) ;; 5 lines from the init file
(monkey/init
;; first line contains items
(find-all-int (first lines))
;; second line is one of these functions
(monkey/parse-operation (nth 1 lines))
;; third line contains the divisor at character 21
(parse-integer (subseq (nth 2 lines) 21))
;; fourth line contains next monkey at character 29
(parse-integer (subseq (nth 3 lines) 29))
;; fifth lines contains next monkey at character 30
(parse-integer (subseq (nth 4 lines) 30))))
;; n is number of monkeys to parse from lines
;; 7 lines per monkey
;; data is at lines 1 - 5
(defun monkeys/parse-all (lines n)
(loop for i from 0 to n
for offset = (* 7 i)
collect (monkey/parse (subseq lines (+ offset 1) (+ offset 6)))))
;; process all items held by monkey. Deletes items from monkey, and
;; returns thrown items as conses (value target). hopefully edits in
;; place? If not figure out a return mechanism.
;; returns resulting (cons value target)
(defun monkey/process-item (monkey item)
(let* ((item-inspected ;; apply function
(funcall (getf monkey :operation) item))
(item-final ;; divide by 3 (floor to get integer, not ratio)
(floor (/ item-inspected 3)))
(target ;; divisibility test
(if (= (mod item-final (getf monkey :divisor)) 0)
(getf monkey :next-true)
(getf monkey :next-false))))
(cons item-final target)))
;; same but doesn't divide by 3 this causes the number to inflate way
;; to big BUT all the divisors in the puzzle are prime numbers! What
;; we need, is a way to reduce the size of the number, while keeping
;; its divisibility properties. I'm thinking either doing some kind of
;; modulus operation, or something with prime factors. (just leave out
;; all prime factors that are not in the list of divisors. Maybe they
;; don't matter)
;; in fact the multipliers and divisers in the problem are (almost?)
;; consecutive primes
(defun monkey/process-item-alt (monkey item)
(let* ((item-final ;; apply function
(mod (funcall (getf monkey :operation) item)
all-divisors-used))
;; don't divide by 3 anymore
(target ;; divisibility test
(if (= (mod item-final (getf monkey :divisor)) 0)
(getf monkey :next-true)
(getf monkey :next-false))))
(cons item-final target)))
;; this variable chooses which processing function we are using (one
;; for part 1 the other for part 2)
;; (defvar monkey-process (function monkey/process-item))
(defvar monkey-process (function monkey/process-item-alt))
(defun monkey/do-turn (monkey)
(let ((thrown-items
(loop for item in (getf monkey :items)
;; do (print (list "processing" item))
collect (funcall monkey-process monkey item))))
(setf (getf monkey :items) (list))
thrown-items))
;; takes list of monkey structs and index of monkey doing the turn.
;; Edits everything in-place
;; returns list of thrown items too, for later reference
(defun monkeys/do-turn (monkeys i)
(let ((thrown-items (monkey/do-turn (nth i monkeys))))
;; distribute the thrown items over the other monkeys
(loop for thrown-item in thrown-items
for value = (car thrown-item)
for target = (cdr thrown-item)
;; do (print (list "monkey" i "trows at" target))
do (setf (getf (nth target monkeys) :items)
(append (getf (nth target monkeys) :items)
(list value))))
thrown-items))
;; do round and count how many items were thrown by each monkey
(defun monkeys/do-round (monkeys)
(loop for i from 0 below (length monkeys)
collect (length (monkeys/do-turn monkeys i))))
(defun monkeys/count-activity-for-rounds (monkeys num-rounds)
(let* ((monkey-counters (make-list (length monkeys) :initial-element 0))
(print-frequency (/ num-rounds 40)))
(loop for n from 0 below num-rounds
if (= (mod n print-frequency) 0)
do (print (list "round" n))
;; do round and sum up number of items for each monkey
do (let ((round-counts (monkeys/do-round monkeys)))
;; (print round-counts)
(map-into monkey-counters
(function +)
monkey-counters
round-counts)))
monkey-counters))
;; need multiple test monkeys to test this
;; (defvar test-monkey (monkey/parse
;; (list
;; " Starting items: 79, 98"
;; " Operation: new = old * 19"
;; " Test: divisible by 23"
;; " If true: throw to monkey 2"
;; " If false: throw to monkey 3")))
;; (print (list "before turn" test-monkey))
;; (print (monkey/do-turn test-monkey))
;; (print (list "after turn" test-monkey))
;; EXAMPLE TEST HERE
(defvar example-lines (read-lines "eleven-example-input.txt"))
(defvar example-monkeys nil)
(setq example-monkeys (monkeys/parse-all example-lines 3))
;; round 1 test
;; (monkeys/do-round example-monkeys)
;; (print (monkeys/count-activity-for-rounds example-monkeys 20))
;; works now
;; 10000 test for example monkeys
(setq all-divisors-used (* 23 19 13 17))
(let* ((ms (monkeys/parse-all example-lines 3))
(result (sort (monkeys/count-activity-for-rounds ms 10000)
(function >))))
(print (list result (* (first result) (second result)))))
;; TAKING THE MODULUS ACTUALLY WORKED! modulus using product of all divisors
;; FINAL CODE HERE
;; (let* ((test-monkeys (monkeys/parse-all lines 7))
;; (result (monkeys/count-activity-for-rounds test-monkeys 20))
;; (srtresult (sort (copy-list result) (function >))))
;; (print (list result srtresult (* (first srtresult) (second srtresult)))))
;; 10000 run
(setq all-divisors-used (* 13 3 7 2 19 5 11 17))
(let* ((test-monkeys (monkeys/parse-all lines 7))
(result (sort (monkeys/count-activity-for-rounds test-monkeys 10000)
(function >))))
(print (list result (* (first result) (second result)))))
| 6,968 | Common Lisp | .lisp | 167 | 38.479042 | 78 | 0.691672 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 65876587ed4eb0c433b3c35344acdcabae26a0593e11b91b5371f29efad03762 | 42,121 | [
-1
] |
42,122 | nine.lisp | drvdputt_aoc-2022/src/nine.lisp | (load "file.lisp")
(defvar lines)
(setq lines (read-default-input "nine"))
;; (setq lines (read-lines "nine-example-input.txt"))
(defun rope/default ()
(list :xh 0 :yh 0 :xt 0 :yt 0))
;; rope: see definition above, direction: character D, U, R, or L
(defun rope/move-once (rope direction)
(let ((newrope (copy-list rope)))
(cond ((char= direction #\D)
(decf (getf newrope :yh)))
((char= direction #\U)
(incf (getf newrope :yh)))
((char= direction #\R)
(incf (getf newrope :xh)))
((char= direction #\L)
(decf (getf newrope :xh))))
(rope/update-tail newrope)))
(load "string.lisp")
;; returns all new configurations the rope went through during this move
(defun rope/apply-move-instruction (rope line)
(let ((newrope (copy-list rope))
(direction (char line 0))
(n (first (find-all-int line))))
(loop repeat n
do (setq newrope (rope/move-once newrope direction))
collect newrope)))
(defun multi-rope/apply-move-instruction (multi-rope line)
(let ((direction (char line 0))
(n (first (find-all-int line))))
(loop repeat n
with new-multi-rope = multi-rope
do (setq new-multi-rope (multi-rope/move-once new-multi-rope direction))
collect new-multi-rope)))
(defun rope/update-tail (rope)
(let* ((newrope rope)
(dx (- (getf rope :xh) (getf rope :xt)))
(dy (- (getf rope :yh) (getf rope :yt)))
(bigdx (>= (abs dx) 2))
(bigdy (>= (abs dy) 2))
(bigdiag (or (and bigdx (>= (abs dy) 1))
(and bigdy (>= (abs dx) 1)))))
;; all conditions where x needs to move
(if (or bigdx bigdiag)
(incf (getf newrope :xt) (signum dx)))
(if (or bigdy bigdiag)
(incf (getf newrope :yt) (signum dy)))
newrope))
;; now we need to collect all unique positions of the tail
(defun rope/tail (rope)
(cons (getf rope :xt) (getf rope :yt)))
(let* ((r (rope/default))
(unique-tails (list (rope/tail r))))
(loop for l in lines
do (let* ((ropes (rope/apply-move-instruction r l)))
;; update rope configuration to the last of the series we
;; just computed
(setq r (car (last ropes)))
;; go over the new tail positions
(loop for newrope in ropes
for tail = (rope/tail newrope)
if (not (member tail unique-tails :test (function equal)))
do (push tail unique-tails))))
(print unique-tails)
(print (length unique-tails)))
;; for the second part, we compose the long rope of multiple segments
;; like the above. The head of the (n+1)th segment is always equal to
;; the tail of the nth one.
(defun multi-rope/default (length)
(loop repeat length
collect (rope/default)))
(defun rope/match-preceding-tail (rope preceding-rope)
(let ((newrope (copy-list rope)))
(setf (getf newrope :xh) (getf preceding-rope :xt))
(setf (getf newrope :yh) (getf preceding-rope :yt))
(rope/update-tail newrope)))
(defun multi-rope/move-once (multi-rope direction)
(loop for segment in multi-rope
with preceding-rope = nil
with updated-rope = nil
;; for the head, do a regular move
if (not preceding-rope)
do (setq updated-rope
(rope/move-once segment direction))
;; for the rest, set the head equal to the tail of the
;; preceding segment, then update the tail
else
do (setq updated-rope
(rope/match-preceding-tail segment preceding-rope))
end
do (setq preceding-rope updated-rope)
collect updated-rope))
(defun multi-rope/tail (multi-rope)
(rope/tail (car (last multi-rope))))
;; with the new tail definition in place, we can use the same code to
;; collect the unique tails
(let* ((multi-rope (multi-rope/default 9))
(unique-tails (list (multi-rope/tail multi-rope))))
(loop for l in lines
do (let* ((multi-rope-steps (multi-rope/apply-move-instruction multi-rope l)))
;; go over the new tail positions
(loop for new-multi-rope in multi-rope-steps
for tail = (multi-rope/tail new-multi-rope)
if (not (member tail unique-tails :test (function equal)))
do (push tail unique-tails))
;; update rope configuration to the last of the series we
;; just computed
(setq multi-rope (car (last multi-rope-steps)))))
(print unique-tails)
(print (length unique-tails)))
| 4,192 | Common Lisp | .lisp | 110 | 34.381818 | 79 | 0.676847 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 3ef65fe7a3247b49fa7bfdae9bee5192404d09c4cd6e82ae9e6f4773e35b48e1 | 42,122 | [
-1
] |
42,123 | twentyone.lisp | drvdputt_aoc-2022/src/twentyone.lisp | (load "file.lisp")
(defparameter lines-e (read-default-input "twentyone-example"))
(defparameter lines (read-default-input "twentyone"))
(defstruct monkey
name
value ;; nil by default. Will be set once the monkey is able to execute its operation.
operation ;; operation and dependens will remain nil for constant monkeys
dep1
dep2)
(defun parse-monkey (line)
;; name is the first 4 characters
(let* ((name (subseq line 0 4))
(is-operation (= (length line) 17))
(m (make-monkey :name name)))
(if is-operation
;; operation monkey: initial value is nil
(setf (monkey-dep1 m) (subseq line 6 10)
(monkey-dep2 m) (subseq line 13)
(monkey-operation m) (let ((c (char line 11)))
(cond ((char= c #\*) #'*)
((char= c #\-) #'-)
((char= c #\+) #'+)
((char= c #\/) #'floor))))
;; constant monkey: just set the number
(setf (monkey-value m) (parse-integer (subseq line 6))))
m))
(defun parse-monkey-table (lines)
(let ((table (make-hash-table :test 'equal)))
(loop for l in lines
for m = (parse-monkey l)
do (setf (gethash (monkey-name m) table) m))
table))
(defun process-monkeys (monkey-table start)
(let* ((m (gethash start monkey-table))
(value (monkey-value m)))
(if value
;; if this is not nil, return it
value
;; Else, recurse to get dependant values, and apply the operation to them.
(funcall (monkey-operation m)
(process-monkeys monkey-table (monkey-dep1 m))
(process-monkeys monkey-table (monkey-dep2 m))))))
(defun do-puzzle (lines start)
(let ((table (parse-monkey-table lines)))
(process-monkeys table start)))
(defun reset-monkey-values (monkey-table)
(loop for m being each hash-value of monkey-table
;; if monkey has an operation, reset its value to nil
if (monkey-operation m)
do (setf (monkey-value m) nil)))
(defun do-part2 (lines guess-min guess-max &key (method 'scan) (stijgend nil))
(let* (;; we found two numbers. Guess min has the value too big,
;; and guess max has it to small. So do bisection between
;; those.
(table (parse-monkey-table lines))
(root-dep1 (monkey-dep1 (gethash "root" table)))
(root-dep2 (monkey-dep2 (gethash "root" table)))
(vmin guess-min)
(vmax guess-max)
(humn-value (cond ((eq method 'scan) vmin)
((eq method 'bisect) (floor (+ guess-min guess-max) 2))))
(v1 nil)
(v2 nil))
(flet ((refresh-v1-v2 ()
;; reset everything with new humn value
(reset-monkey-values table)
(setf (monkey-value (gethash "humn" table)) humn-value)
(setq v1 (process-monkeys table root-dep1)
v2 (process-monkeys table root-dep2))
(print (list "humn v1 v2" humn-value v1 v2))))
;; (print (loop for k being each hash-key of table collect k))
;; (print root-dep1)
;; (print root-dep2)
(cond ((eq method 'bisect)
(loop
;; move humn to middle of the bounds
do (setq humn-value (floor (+ vmin vmax) 2))
;; calc v1 and v2
do (refresh-v1-v2)
do (print (list "vmin humn vmax" vmin humn-value vmax))
;; check return condition
if (or (= v1 v2) (< (abs (- vmin vmax)) 2))
return humn-value
;; adjust search bounds
if (or (and stijgend (< v1 v2))
(and (not stijgend) (> v1 v2)))
;; if stijgend and too small, or dalend and too big, need bigger number
do (setq vmin humn-value)
else
;; else, need smaller number
do (setq vmax humn-value)))
((eq method 'scan)
(loop
;; calc v1 and v2
do (refresh-v1-v2)
;; check return condition (actually, we want to scan everything
;; if (or (= v1 v2) (< (abs (- vmin vmax)) 2))
;; return humn-value
if (= v1 v2)
do (print "^^^^^^^")
if (> humn-value vmax)
return nil
;; increment humn by 1
do (incf humn-value)))))))
;; part 2 needs two parts
;; first: do bisection search to find the right range
;; (do-part2 lines 0 1000000000000000 :method 'bisect :stijgend nil)
;; then, look at the output, and do a scan to find the first humn value for which we are
;; successful. (there are 6)
;; (do-part2 lines 3059361893915 3059361893929 :method 'scan :stijgend nil)
| 4,925 | Common Lisp | .lisp | 110 | 33.509091 | 88 | 0.555391 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a2cff4d744fc424952be612b2cbf7d720754bf6773c07fa913279f60734cbcd3 | 42,123 | [
-1
] |
42,124 | ten.lisp | drvdputt_aoc-2022/src/ten.lisp | (load "file.lisp")
(defvar lines (read-default-input "ten"))
(defun cpu-state/default ()
(list :cycle 1 :x 1))
(defun cpu-state/noop (cpu-state)
(let ((new-state (copy-list cpu-state)))
(incf (getf new-state :cycle))
new-state))
(defun cpu-state/addx (cpu-state i)
(let ((new-state (copy-list cpu-state)))
(incf (getf new-state :cycle) 2)
(incf (getf new-state :x) i)
new-state))
(load "string.lisp")
(defun cpu-state/instruction (cpu-state line)
(if (starts-with line "addx")
(cpu-state/addx cpu-state (parse-integer (subseq line 5)))
(cpu-state/noop cpu-state)))
;; will only work for when distance between the elements of at-cycles is large enough...
(defun find-signal-strengths (at-cycles lines)
(let ((cpu-state (cpu-state/default))
(previous-state (cpu-state/default))
(probe-cycles at-cycles))
(loop for l in lines
do (print l)
do (setq cpu-state (cpu-state/instruction cpu-state l))
;; get the signal strength of the previous cycles, once we
;; exceed one of the requested ones caution: the thing
;; below works because the steps of 20 and 40 are big
;; enough. if the probe cycles are closely spaced, it
;; won't work anymore, because the cycles can jump by 2
;; and the probe index only by 1
if (> (getf cpu-state :cycle) (car probe-cycles))
do (print (list (getf previous-state :x) (car probe-cycles)))
and collect (* (getf previous-state :x) (car probe-cycles)) into signal-strengths
and do (setq probe-cycles (cdr probe-cycles))
;; this means we don't need to go any further
when (not probe-cycles)
return signal-strengths
do (setq previous-state cpu-state)
finally (return signal-strengths))))
(defun all-cpu-states (lines)
(cons (cpu-state/default)
(loop for l in lines
with cpu-state = (cpu-state/default)
do (setq cpu-state (cpu-state/instruction cpu-state l))
collect cpu-state)))
(defun pixel-value (pixel cpu-state)
(if (<= (abs (- (mod pixel 40) (getf cpu-state :x))) 1)
#\#
#\.))
(defun compute-pixels (states)
;; pixels are numbered from 0 to 239, cycles from 1 to 240
(loop for pixel from 0 to 239
for cycle = (+ pixel 1)
with rest-of-states = states
;; if our cycle goes past that of the next state, move to that
;; state. Asserts that we have enough states to do this (will
;; crash otherwise because cdr returns nil)
if (>= cycle (getf (second rest-of-states) :cycle))
do (setq rest-of-states (cdr rest-of-states))
;; collect the pixel values. If pixel i is within 1 distance
;; of sprite x, the pixel turns on
do (print (list pixel
(car rest-of-states)
(pixel-value pixel (car rest-of-states))))
collect (pixel-value pixel (car rest-of-states))))
(defun draw-image (lines)
(let* ((pixels (compute-pixels (all-cpu-states lines))))
(print pixels)
(terpri)
(loop for p in pixels
for i from 0
do (write-char p)
if (= (mod i 40) 39)
do (terpri))))
(defvar example-lines (read-lines "ten-example-input.txt"))
(print (apply (function +) (find-signal-strengths at-cycles lines)))
| 3,145 | Common Lisp | .lisp | 78 | 36.192308 | 88 | 0.679029 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a86be6e56c8b840d94b68ec9b0aa1456885194a06970d30cbbb8ace680fa8d17 | 42,124 | [
-1
] |
42,125 | twentythree.lisp | drvdputt_aoc-2022/src/twentythree.lisp | (load "file.lisp")
(load "num.lisp")
(ql:quickload :array-operations)
(ql:quickload :alexandria)
;; reuse old function
(defparameter a-small (read-array "twentythree-small-input.txt" 'char))
;; putting everything on a grid would results in O(N) runtime for neighbor checking (go over
;; entire array, and for every position, check 9 squares if elf).
;; working without a grid would result in O(N**2) scaling (need to go over all points)
;; So having an array, and just eating the overhead of expanding it every now and then, would
;; definitely be preferable.
(defstruct elf-array
a
yoffset
xoffset
)
(defun elf-array/shrinkwrap (xys)
"Make an elf-array instance that just fits around the list of elves
Will be filled with 0 and 1. Higher integers when xys contains duplicates."
(let* ((xymax ;; get maximum of each column
(aops:each-index j
(aops:reduce-index #'max i
(aref xys i j))))
(xymin ;; get minimum of each column
(aops:each-index j
(aops:reduce-index #'min i
(aref xys i j))))
;; first y then x for array dimensions
(dimensions (list (1+ (- (aref xymax 1) (aref xymin 1)))
(1+ (- (aref xymax 0) (aref xymin 0)))))
(ea (make-elf-array :a (make-array dimensions :element-type 'integer :initial-element 0)
:xoffset (elt xymin 0)
:yoffset (elt xymin 1))))
(loop for n from 0 below (array-dimension xys 0)
do (incf (elf-array/get ea (aref xys n 0) (aref xys n 1))))
;; (print xymax)
;; (print xymin)
;; (print dimensions)
;; (print ea)
ea))
(defun elf-array/get (ea x y)
(let ((x0 (- x (elf-array-xoffset ea)))
(y0 (- y (elf-array-yoffset ea))))
(aref (elf-array-a ea) y0 x0)))
(defun elf-array/occupied (ea x y)
"Returns nil if free or out of bounds"
(let ((a (elf-array-a ea))
(x0 (elf-array-xoffset ea))
(y0 (elf-array-yoffset ea)))
(if (or (< x x0) (>= x (+ x0 (array-dimension a 1)))
(< y y0) (>= y (+ y0 (array-dimension a 0))))
;; out of bounds
nil
;; check occupancy
(> (elf-array/get ea x y) 0))))
(defun (setf elf-array/get) (new-value ea x y)
"here, I learn how to use a setf expander"
(let ((x0 (- x (elf-array-xoffset ea)))
(y0 (- y (elf-array-yoffset ea))))
(setf (aref (elf-array-a ea) y0 x0) new-value)))
(defparameter directions
'('north 'south 'west 'east))
(defstruct elf-state
xys ;; current position of each elf
;; rotates between each step (north south west east)
(nswe 0))
(defun elf-state/init (file)
(let ((char-a (read-array file 'char))
(es (make-elf-state))
(N 0))
;; first, count the number of elves
(setq N (loop for c across (aops:flatten char-a)
count (char= c #\#)))
;; then, set up arrays for these positions, and fill them in
(let ((xys (make-array (list N 2) :element-type 'integer))
(n 0))
(aops:each-index (i j)
(when (char= (aref char-a i j) #\#)
;; x = j, y = i
(setf (aref xys n 0) j
(aref xys n 1) i)
(incf n)))
(setf (elf-state-xys es) xys))
es))
(defun elf-array/nswe-free (ea x y nswe)
"Return t if direction is free, based on the check described in the instructions"
(cond
;; north: check N, NE, NW
((= nswe 0)
(loop for xi from (1- x) to (1+ x)
if (elf-array/occupied ea xi (1- y))
return nil
finally (return t)))
;; south
((= nswe 1)
(loop for xi from (1- x) to (1+ x)
if (elf-array/occupied ea xi (1+ y))
return nil
finally (return t)))
;; west
((= nswe 2)
(loop for yi from (1- y) to (1+ y)
if (elf-array/occupied ea (1- x) yi)
return nil
finally (return t)))
;; east
((= nswe 3)
(loop for yi from (1- y) to (1+ y)
if (elf-array/occupied ea (1+ x) yi)
return nil
finally (return t)))))
(defun move-x (nswe x)
(cond
;; west
((= nswe 2) (1- x))
;; east
((= nswe 3) (1+ x))
;; north and south
(t x)))
(defun move-y (nswe y)
(cond
;; north
((= nswe 0) (1- y))
;; south
((= nswe 1) (1+ y))
(t y)))
(defun elf-state/step (es)
"part 1: fill in the proposed move array
a. Set up position array
b. Go over all elves, and check the position array for neighbors. This determines their move.
part 2: check if proposed moves are possible
a. count number of moves to each tile (store in array)
b. go over all elves, and check count on their destination. Adjust xy[n] if elf n is free to move.
part 3: advance pointers"
(let* ((N (array-dimension (elf-state-xys es) 0))
;; set up position array (for neighbor checking)
(xys (elf-state-xys es))
(ea (elf-array/shrinkwrap xys))
;; set up move count array (for move conflict checking)
(move-xys (alexandria:copy-array xys))
(ea-move nil))
;; set move direction for all points
(loop for n from 0 below N
for x = (aref xys n 0)
for y = (aref xys n 1)
;; try the 4 directions (with mod rollover)
for free-directions = (loop for i from 0 below 4
for nswe = (mod (+ (elf-state-nswe es) i) 4)
if (elf-array/nswe-free ea x y nswe)
collect nswe)
;; If at least one is free, move into the first available direction. But if all 4 are
;; free, do nothing.
if (and free-directions
(not (= (length free-directions) 4)))
;; do (print (list x y "can move in direction" nswe))
do (setf (aref move-xys n 0) (move-x (first free-directions) x)
(aref move-xys n 1) (move-y (first free-directions) y)))
;; (print "move goals")
;; (print move-xys)
;; count moves to each tile
(setq ea-move (elf-array/shrinkwrap move-xys))
;; (print "positions")
;; (pprint-int-array (elf-array-a ea))
;; (print "move")
;; (pprint-int-array (elf-array-a ea-move))
;; check for conflicts and execute moves
(loop for n from 0 below N
for x-move = (aref move-xys n 0)
for y-move = (aref move-xys n 1)
;; if only one elf is moving to this tile, we can do the move, otherwise do nothing
if (= (elf-array/get ea-move x-move y-move) 1)
do (setf (aref (elf-state-xys es) n 0) x-move
(aref (elf-state-xys es) n 1) y-move))
;; advance pointers
(setf (elf-state-nswe es)
(mod (1+ (elf-state-nswe es)) 4))
es))
(defun do-puzzle (fn num-steps)
(loop repeat num-steps
with es = (elf-state/init fn)
do (pprint-int-array (elf-array-a (elf-array/shrinkwrap (elf-state-xys es))))
do (print "-------")
do (elf-state/step es)
finally (let ((a (elf-array-a (elf-array/shrinkwrap (elf-state-xys es)))))
(pprint-int-array a)
(print (reduce #'+ (aops:flatten a)))
(print (- (reduce #'* (array-dimensions a)) (reduce #'+ (aops:flatten a)))))))
(defun do-part2 (fn)
(loop for counter from 1
with es = (elf-state/init fn)
with previous = (alexandria:copy-array (elf-state-xys es))
for new = (alexandria:copy-array (elf-state-xys (elf-state/step es)))
;; do (print (list new previous))
if (equalp new previous)
return counter
else
do (setq previous new)))
| 7,792 | Common Lisp | .lisp | 197 | 31.370558 | 103 | 0.567664 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a3353ee67ecc41576694cf819188ecc62fb4ef5109267c62e5ed1af5412a74a3 | 42,125 | [
-1
] |
42,126 | fifteen.lisp | drvdputt_aoc-2022/src/fifteen.lisp | (load "string.lisp")
;; manhattan distance = delta x + delta y
(defun distance (x1 y1 x2 y2)
(+ (abs (- x1 x2)) (abs (- y1 y2))))
(defun sensor/parse (line)
(let* ((numbers (find-all-int line))
(sx (first numbers))
(sy (second numbers))
(bx (nth 2 numbers))
(by (nth 3 numbers)))
(list :sx sx :sy sy :bx bx :by by :r (distance sx sy bx by))))
(defun sensor/in-exclusion-zone (sensor x y)
(<= (distance x y (getf sensor :sx) (getf sensor :sy))
(getf sensor :r)))
;; excluded x-range for sensor, given a y coordinate. Nil if no points. return value is (cons
;; xmin xmax)
(defun sensor/exclusion-x (sensor y)
(let ((ydist (abs (- y (getf sensor :sy))))
(r (getf sensor :r)))
(if (> ydist r)
nil
(let ((x (getf sensor :sx))
(dx (- r ydist)))
(cons (- x dx) (+ x dx))))))
;; try innefficient, simplest approach first
(defun scan-row (sensors y)
(let ((start 0)
(stop 0))
;; first, need to determine scan range
(loop for s in sensors
for xmin-xmax = (sensor/exclusion-x s y)
;; do (print (list "ran exclusion" xmin-xmax))
if xmin-xmax
minimize (car xmin-xmax) into xmin
and maximize (cdr xmin-xmax) into xmax
finally (setq start xmin stop xmax))
;; then do scan
(print (list "running scan from" start "to" stop))
(loop for x from start to stop
if (sensors/in-any-exclusion-zone sensors x y)
collect x)))
(load "file.lisp")
(defvar lines-e (read-lines "fifteen-example-input.txt"))
(defvar sensors-e (loop for l in lines-e collect (sensor/parse l)))
(defvar lines (read-default-input "fifteen"))
(load "list-tools.lisp")
(defun do-puzzle (lines y)
(let* ((sensors (loop for l in lines collect (sensor/parse l)))
(excluded-points (scan-row sensors y))
(beacons-in-line (list-tools/unique
(loop for s in sensors
if (= (getf s :by) y)
collect (cons (getf s :bx) (getf s :by)))
(function equal))))
;; we still need to count the number of beacons on the row itself. they don't count for the
;; answer. Some sensors share a beacon! Collect only unique ones.
;; (print excluded-points)
(print beacons-in-line)
(- (length excluded-points) (length beacons-in-line))))
(defun sensors/in-any-exclusion-zone (sensors x y)
(loop for s in sensors
if (sensor/in-exclusion-zone s x y)
return t
finally
(return nil)))
;; brute force is too slow...
(defun do-part2 (lines xmax ymax)
(let* ((sensors (loop for l in lines collect (sensor/parse l)))
(x-found 0)
(y-found 0))
(loop for y from 0 to ymax
if (or (< ymax 101)
(= 0 (mod y (floor ymax 100))))
do (print (list "progress" y))
if (loop with x = 0
;; no need to do x+1! We go forward according to the sensor ranges. And if
;; we're out of range, then we've found our point!
if (loop for s in sensors
for zone = (sensor/exclusion-x s y)
;; if in exclusion zone
if (and zone (>= x (car zone)) (<= x (cdr zone)))
;; skip forward and stop checking sensors
;; do (print (list "in exclusion zone" zone "skipping forward to" (+ 1 (cdr zone))))
do (setq x (+ 1 (cdr zone)))
and return nil ;; this will restart the loop with the updated x
;; if all sensors were passed, then we have our result
finally (return t))
;; if result was found, save it and return
do (setq x-found x y-found y)
and return t
;; if result was not found (we're past the maximum), move to the next row
if (> x xmax)
return nil)
return t)
(print (cons x-found y-found))
;; "tuning frequency"
(+ (* x-found xmax) y-found)))
(print (do-part2 lines 4000000 4000000))
| 4,322 | Common Lisp | .lisp | 98 | 33.244898 | 114 | 0.542606 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 0a232b75dc566058648a7d632d1a2ef9f4c37758ce97431c1ea299d0b6899d6e | 42,126 | [
-1
] |
42,127 | seventeen.lisp | drvdputt_aoc-2022/src/seventeen.lisp | (load "file.lisp")
(defparameter gas (car (read-default-input "seventeen")))
(defparameter gas-e ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>")
(defparameter the-gas gas)
;; (setq the-gas gas-e)
;; width of the chamber
(defparameter width 7)
;; lowest and highest possible x
(defparameter x-lo 0)
(defparameter x-hi (1- width))
(defun xy/xs (points)
(mapcar (function car) points))
(defun xy/ys (points)
(mapcar (function cdr) points))
(defun xy/width (points)
(let ((xs (xy/xs points)))
(+ 1 (- (apply (function max) xs) (apply (function min) xs)))))
(defun xy/height (points)
(let ((ys (xy/ys points)))
(+ 1 (- (apply (function max) ys) (apply (function min) ys)))))
(defun xy/shift (points dx dy)
(mapcar (lambda (p)
(cons (+ dx (car p)) (+ dy (cdr p))))
points))
(load "num.lisp")
(defun xy/pprint (points)
(let* ((x0 (apply (function min) (xy/xs points)))
(y0 (apply (function min) (xy/ys points)))
(w (xy/width points))
(h (xy/height points))
(a (make-array (list h w) :element-type 'character :initial-element #\.)))
(loop for x in (xy/xs points)
for y in (xy/ys points)
do (setf (aref a
;; flip the y direction
(- (- h 1) (- y y0))
;; keep the x direction
(- x x0)) #\#))
(pprint-char-array a)))
;; each rock is a list of coordinates, relative to its bottom left point (since the left and
;; bottom edge are most important for the spawning)
;; points is list of x . y conses
;; precalculates some stuff too
(defun init-rock (points)
(list :xy points
:w (xy/width points)
:h (xy/height points)))
(defparameter rock-types
(make-array
5
:element-type 'list
:initial-contents
(mapcar (function init-rock)
(list
;;----
'((0 . 0) (1 . 0) (2 . 0) (3 . 0))
;; |
;;-+-
;; |
'((1 . 2)
(0 . 1) (1 . 1) (2 . 1)
(1 . 0))
;; |
;; |
;;__|
'((2 . 2)
(2 . 1)
(0 . 0) (1 . 0) (2 . 0))
;;|
;;|
;;|
;;|
'((0 . 3)
(0 . 2)
(0 . 1)
(0 . 0))
;;++
;;++
'((0 . 1) (1 . 1)
(0 . 0) (1 . 0))))))
(defparameter state-ini
(list
:rock-count 0
;; refers to an entry of the rock type array
:rock-index 0
;; rocks spawn at x = 2 and y = highest-occupied + 4 (with y = 0 being the lowest most
;; position a rock can take, and y = -1 the position of the floor)
:rock-x 2
:rock-y 3
;; list of all coordinates occupied by rock pieces. Starts with just the floor at -1.
:occupied-xy (loop for x from 0 to x-hi collect (cons x -1))
;; highest rock, to determine spawn point. Updated every time a rock stops.
:occupied-ymax -1
;; refers to an entry in the gas pattern
:gas-index 0))
(defun state/rock-type (state)
(elt rock-types (getf state :rock-index)))
(defun state/rock-points (state)
(let ((rock-layout (getf (state/rock-type state) :xy))
(dx (getf state :rock-x))
(dy (getf state :rock-y)))
(xy/shift rock-layout dx dy)))
;; evolve the state by one step = one wind move + one rock fall. Returns t if a rock has
;; stopped, nil if it is still falling.
(defun state/step (state &key (print-after nil) (discard-threshold nil))
(let ((rock-stopped nil))
;; apply wind move and advance wind pointer by one
(let ((wind-char (elt the-gas (getf state :gas-index))))
;; (print wind-char)
(cond
;; if wind goes left and rock can move left (TODO check for obstruction by other rocks)
((and (char= wind-char #\<)
;; check left wall
(> (getf state :rock-x) x-lo)
;; check left collision
(not (state/move-will-cause-collision-p state -1 0)))
(decf (getf state :rock-x)))
;; if wind goes right and rock can move right
((and (char= wind-char #\>)
;; check right wall. One past rightmost block = x + w
;; ####
;; 01234
(<= (+ (getf state :rock-x) (getf (state/rock-type state) :w)) x-hi)
;; check right collision
(not (state/move-will-cause-collision-p state 1 0)))
(incf (getf state :rock-x)))))
;; advance gas index (with modulo rollover)
(setf (getf state :gas-index)
(mod (1+ (getf state :gas-index)) (length the-gas)))
(if (and
;; check floor
(> (getf state :rock-y) 0)
;; check down collision
(not (state/move-will-cause-collision-p state 0 -1)))
;; drop the rock by 1
(decf (getf state :rock-y))
;; else, add the rock's points to the occupied points, advance the rock pointer, and
;; reset the rock x y trackers.
(progn (setq rock-stopped t)
(state/finalize-rock state)))
;; optional optimization
(when (and discard-threshold
(> (xy/height (getf state :occupied-xy)) discard-threshold))
(state/discard-points state))
;; optional print
(when print-after (state/pprint state))
rock-stopped))
(defun state/finalize-rock (state)
(let ((rock-points (state/rock-points state))
(rock-type (state/rock-type state)))
;; store the rock points
(setf (getf state :occupied-xy)
(append rock-points (getf state :occupied-xy)))
;; update the highest point if one of the new rocks is higher
(setf (getf state :occupied-ymax)
(max (getf state :occupied-ymax) (1- (+ (getf state :rock-y)
(getf rock-type :h)))))
;; advance the rock index and set the spawn coordinates of the new rock (a gap of 3 needs to
;; exist)
(setf (getf state :rock-index) (mod (1+ (getf state :rock-index)) (length rock-types))
(getf state :rock-y) (+ 4 (getf state :occupied-ymax))
(getf state :rock-x) 2)
(incf (getf state :rock-count))))
(defun state/pprint (state)
(terpri)
(xy/pprint (getf state :occupied-xy))
(terpri))
(defun state/move-will-cause-collision-p (state dx dy)
;; no need to check if we're still above the other blocks
(if (> (getf state :rock-y) (1+ (getf state :occupied-ymax)))
nil
;; "some of the shifted rock points are member of the occupied list"
(some (lambda (p)
(member p (getf state :occupied-xy) :test #'equal))
(xy/shift (state/rock-points state) dx dy))))
;; optimization that only keeps points above a corridor "jam". Discard everything below.
(defun state/discard-points (state)
;; sort by y value. Bigger y should come first.
(sort (getf state :occupied-xy) (lambda (p1 p2) (> (cdr p1) (cdr p2))))
;; (print "sorted")
;; (print (getf state :occupied-xy))
(setf (getf state :occupied-xy)
(subseq (getf state :occupied-xy)
0
(floor (length (getf state :occupied-xy)) 2))))
;; (print "after cut")
;; (print (getf state :occupied-xy)))
(defun do-puzzle ()
(let ((nustate (copy-list state-ini)))
(loop with counter = 0
;; if a rock has stopped, increase the counter
if (state/step nustate :discard-threshold 400)
do (incf counter)
when (= counter 2022)
return (getf nustate :occupied-ymax))))
(defun do-part2 (num-rocks discard-threshold)
(let ((nustate (copy-list state-ini)))
(loop with counter = 0
;; optimization needed: switch from alist to vector to find periodicity, store states
;; per gas cycle, while keeping rock cycle fixed. Indexed on gas-index.
with rock0-gasi = (make-array (length the-gas) :element-type 'list :initial-element nil)
;; also store the second occurrence, to check for consistency. If delta h between
;; current and second, and second and first are the same, then print out what we
;; found.
with second-rock0-gasi = (make-array (length the-gas) :element-type 'list :initial-element nil)
;; if a rock has stopped, check a bunch of things
if (state/step nustate :discard-threshold discard-threshold :print-after nil)
do (incf counter)
and do (when (= (getf nustate :rock-index) 0)
;; IDEA: look for periodicity. Find if there's any number of rocks after which the original
;; configuration is recovered, specifically:
;; - flat floor at y
;; - rock type index at 0
;; - rock position at (2 . y + 4) (always true when a new rock spawns)
;; - wind index at 0
;; after having a rock cycle, store the state at a certain gas index. Once
;; we re-encounter this gas index,
;; (print rock0-gasi-alist)
(let* ((gas-i (getf nustate :gas-index))
(entry (elt rock0-gasi gas-i))
(second-entry (elt second-rock0-gasi gas-i)))
;; add to list if not there
(when (not entry)
(setf (elt rock0-gasi gas-i) (copy-list nustate)))
;; add to second list if already in first list but not second
(when (and (not second-entry) entry)
(setf (elt second-rock0-gasi gas-i) (copy-list nustate)))
;; when we have two recorded entries, check consistency
(when (and entry second-entry)
(let* ((first-state entry)
(second-state second-entry)
(delta-r1 (- (getf nustate :rock-count) (getf second-state :rock-count)))
(delta-y1 (- (getf nustate :occupied-ymax) (getf second-state :occupied-ymax)))
(delta-r2 (- (getf second-state :rock-count) (getf first-state :rock-count)))
(delta-y2 (- (getf second-state :occupied-ymax) (getf first-state :occupied-ymax))))
;; check delta r and delta y consistency
(when (and (= delta-r1 delta-r2) (= delta-y1 delta-y2))
(print (concatenate 'string
"periodicity found! after rock nr"
(write-to-string counter)))
(print "state at start of period")
(print first-state)
(state/pprint first-state)
(print "second state")
(print second-state)
(state/pprint second-state)
(print "current state")
(print nustate)
(state/pprint nustate)
(print (concatenate
'string
"rock start . delta rock . delta H"
(write-to-string
(list
(getf first-state :rock-count)
delta-r1
delta-y1)))))))))
and when (= counter num-rocks)
return (getf nustate :occupied-ymax))))
(defparameter n 1000000000000)
;; instructions: run (do-part2), and note down the offset, delta and deltaH from the output.
;; for example, this works
(defparameter offset 15)
(defparameter delta 35)
(defparameter delta-h 53)
;; for big input, try this 105 1715 2616
(setq offset 105
delta 1715
delta-h 2616)
(let*
;; the number of rocks to simulate once the period has started
((num-periods (floor (- n offset) delta))
(remainder (mod (- n offset) delta))
(numrocks-non-periodic (+ offset remainder))
(height-periodic (* num-periods delta-h))
(height-non-periodic (do-part2 numrocks-non-periodic (* delta-h 4))))
(print (+ 1 height-periodic height-non-periodic)))
| 12,530 | Common Lisp | .lisp | 277 | 33.620939 | 116 | 0.538764 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 04cb3f24bc2623a74188347af5cb3bfefda6cf972ad1af715c343649c00c4bac | 42,127 | [
-1
] |
42,128 | fourteen.lisp | drvdputt_aoc-2022/src/fourteen.lisp | (load "file.lisp")
(load "string.lisp")
(load "num.lisp")
;; return list of x and y coordinates of all the wall segments, from a
;; wall specification (list of the x and y of the corners)
(defun make-wall-path (xs ys)
(let* ((prev-x (car xs))
(prev-y (car ys))
(path (list (cons prev-x prev-y))))
(loop for x in (cdr xs)
for y in (cdr ys)
;; do (print (list x y prev-x prev-y))
;; do (print (list "delta x" (- x prev-x)))
if (not (= 0 (- x prev-x))) ;; either xdiff or ydiff, diagonal not possible
do (loop for xi in (range prev-x x :exclude-start t)
;; do (print (list "traversing x" xi))
do (push (cons xi y) path))
else
do (loop for yi in (range prev-y y :exclude-start t)
;; do (print (list "traversing y" yi))
do (push (cons x yi) path))
end
do (setq prev-x x prev-y y))
path))
(defun path/parse (line)
;; loop in pairs. See https://lispcookbook.github.io/cl-cookbook/iteration.html
(loop for rest on (find-all-int line) by #'cddr
collect (car rest) into x
collect (car (cdr rest)) into y
finally (return (make-wall-path x y))))
(defun paths/parse (lines)
(loop for l in lines
collect (path/parse l)))
;; path does not need to be contigous, just collection of conses, each one has integers (x . y)
(defun path/extent (path)
(loop for coord in path
for x = (car coord)
for y = (cdr coord)
maximize x into xmax
minimize x into xmin
maximize y into ymax
minimize y into ymin
finally (return (list
:xoffset xmin
:yoffset ymin
:dimensions (list (+ 1 (- ymax ymin)) (+ 1 (- xmax xmin)))))))
;; turn collection of paths and source into collection of coords
(defun paths/flatten (paths source)
(let* ((pathcoords (apply (function append) paths))
(allcoords (cons source pathcoords)))
allcoords))
;; paths = list of (x . y) conses
;; source = one cons, indicating where the sand source is
(defun paths/map (paths source)
;; make one big path and calculate the extent of it
(let* ((allcoords (paths/flatten paths source))
;; also need source position in extent calculation
(extent (path/extent allcoords))
;; use the extent to initialize this array of size (ny nx)
(map-array (make-array (getf extent :dimensions)
:element-type 'character
:initial-element #\.)))
;; fill in all the coordinates with #
(loop for c in allcoords
;; i = y - yoffset
for i = (- (cdr c) (getf extent :yoffset))
;; j = x - xoffset
for j = (- (car c) (getf extent :xoffset))
do (setf (aref map-array i j) #\#))
;; mark the source
(setf (aref map-array
(- (cdr source) (getf extent :yoffset))
(- (car source) (getf extent :xoffset)))
#\+)
(list :map map-array
:extent extent)))
(defun pprint-char-array (a)
(loop for i from 0 below (array-dimension a 0)
do (loop for j from 0 below (array-dimension a 1)
do (write-char (aref a i j))
finally (terpri))))
;; (pprint-char-array (paths/map (paths/parse lines-e) (cons 500 0)))
;; (pprint-char-array (paths-map (paths/parse lines)))
;; function that returns nil if out of bounds
;; 0 if free
;; 1 if obstructed
(defun sandmap/try-move (sandmap i j)
(let ((nj (array-dimension sandmap 1))
(ni (array-dimension sandmap 0)))
;; (print (list "trying move to" i j ni nj))
(cond ((or (< i 0)
(>= i ni)
(< j 0)
(>= j nj))
nil)
((char= (aref sandmap i j) #\.)
0)
(t
1))))
;; return nil if off screen, return current if settled
;; position is (x . y) (after applying offset)
(defun sandmap/next-position (sandmap position)
(let* ((x (car position))
(y (cdr position))
;; first handle y one down
(ydown (+ 1 y)))
;; way to loop in pairs
(loop for (i j) on (list ydown x
ydown (- x 1)
ydown (+ x 1))
by #'cddr while j
for try-result = (sandmap/try-move sandmap i j)
;; do (print try-result)
;; out of bounds
if (not try-result)
return nil
;; free -> return new position
else if (= 0 try-result)
return (cons j i)
end
;; everything blocked -> return current position.
finally (return position))))
;; returns final position of sand
(defun sandmap/drop (sandmap position)
(let ((cur-pos position))
(loop for i from 0 below (array-dimension sandmap 0) ;; just a practical limit to the number of drops
for new-pos = (sandmap/next-position sandmap cur-pos)
;; out of bounds
if (not new-pos)
return nil
;; settled
if (equal new-pos cur-pos)
return new-pos
;; moving
else
do (setq cur-pos new-pos))))
;; update map with new sand. Returns new sand position, which is nil if sand flows off map
;; (meaning that more steps won't do anything new)
(defun sandmap/step (sandmap source)
(let ((final-sand-pos (sandmap/drop sandmap source)))
(if final-sand-pos
(setf (aref sandmap (cdr final-sand-pos) (car final-sand-pos)) #\o))
;; (pprint-char-array sandmap)
final-sand-pos))
(defun do-puzzle (lines source)
(let* ((paths (paths/parse lines))
(the-map (paths/map paths source))
(sandmap (getf the-map :map))
(extent (getf the-map :extent))
(source-offset (cons (- (car source) (getf extent :xoffset))
(- (cdr source) (getf extent :yoffset)))))
(loop for counter from 0
;; step and check if nil
if (not (sandmap/step sandmap source-offset))
return counter)))
(defvar source (cons 500 0))
(defvar lines-e (list
"498,4 -> 498,6 -> 496,6"
"503,4 -> 502,4 -> 502,9 -> 494,9"))
(defvar lines (read-default-input "fourteen"))
;; (do-puzzle lines-e source)
;; (do-puzzle lines source)
;; for part 2, build in a custom path representing the floor
(defun paths/add-floor (paths source)
(let* ((allcoords (paths/flatten paths source))
(ymax (loop for c in allcoords maximize (cdr c)))
(yfloor (+ ymax 2))
(xmin (loop for c in allcoords minimize (car c)))
(xmax (loop for c in allcoords maximize (car c)))
;; set x range big enough to accomodate both triangle with width 2 * height, and the
;; original x range
(xminfloor (min xmin (- (car source) yfloor)))
(xmaxfloor (max xmax (+ (car source) yfloor)))
;; now, generate a big list of points representing the floor
(floorpoints (loop for x from xminfloor to xmaxfloor
collect (cons x yfloor))))
;; add these points as an extra path
(cons floorpoints paths)))
(defun do-part2 (lines source)
(let* ((paths (paths/parse lines))
(paths-with-floor (paths/add-floor paths source))
(the-map (paths/map paths-with-floor source))
(sandmap (getf the-map :map))
(extent (getf the-map :extent))
(source-offset (cons (- (car source) (getf extent :xoffset))
(- (cdr source) (getf extent :yoffset)))))
;; (pprint-char-array sandmap)))
(loop for counter from 1
for sand-pos = (sandmap/step sandmap source-offset)
if (or (not sand-pos)
(equal sand-pos source-offset))
do (pprint-char-array sandmap)
and do (print sand-pos)
and return counter)))
| 8,053 | Common Lisp | .lisp | 194 | 32.278351 | 105 | 0.569641 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a9ef07501c56fafe89f20bbcdd7ad676cb64966775a9feed73a0a003ffd530b0 | 42,128 | [
-1
] |
42,129 | list-tools.lisp | drvdputt_aoc-2022/src/list-tools.lisp | ;; collect unique values
(defun list-tools/unique (input-list test)
(let ((unique-entries nil))
(loop for item in input-list
if (not (member item unique-entries :test test))
do (push item unique-entries))
unique-entries))
(defun pprint-hashmap (h)
(loop for k being each hash-key of h
do (print
(concatenate
'string
k
" : "
(write-to-string (gethash k h))))))
| 500 | Common Lisp | .lisp | 15 | 23 | 58 | 0.572062 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 0eef86edfb966a82823d076223c825fa921bdc5378481864276a67b55eab60e0 | 42,129 | [
-1
] |
42,131 | twentyfour.lisp | drvdputt_aoc-2022/src/twentyfour.lisp | (load "file.lisp")
(load "dfss.lisp")
(ql:quickload :array-operations)
(ql:quickload :alexandria)
(defparameter +time-limit+ 20)
(defparameter chars-e (read-array "twentyfour-example-input.txt" 'char))
(defparameter chars-c (read-array "twentyfour-complex-input.txt" 'char))
(defparameter chars (read-array "twentyfour-input.txt" 'char))
(defstruct state
;; list of blizzards, will evolve
blz
;; occupancy, derived from walls + blizzard position
a)
(defstruct blizzard
(direction #\> :type character)
(x 0 :type integer)
(y 0 :type integer))
(defun state/parse (char-array)
(let ((a nil)
(blz nil))
;; go over all chars, parsing the blizzards, and returning the occupancy array
(setq a (aops:each-index (i j)
(let ((c (aref char-array i j)))
;; side effect: add the blizzard to the list
(when (or (char= c #\>)
(char= c #\v)
(char= c #\<)
(char= c #\^))
(push (make-blizzard
:direction c
:x j
:y i)
blz))
;; return occupancy value
(if (char= c #\.) 0 1))))
(make-state :blz blz :a a)))
(defun state/copy (s)
"Need deep copy for state machinery"
(make-state :blz (loop for b in (state-blz s)
collect (copy-blizzard b))
:a (alexandria:copy-array (state-a s))))
(defun set-occupancy-from-blizzard-list (a blz)
"set central (non-wall) values to zero, then go over blizzards and fill everything in"
(aops:each-index! a (i j)
(if (and (> i 0)
(< i (1- (array-dimension a 0)))
(> j 0)
(< j (1- (array-dimension a 1))))
;; clear central values
0
;; keep edge values
(aref a i j)))
(loop for b in blz
for j = (blizzard-x b)
for i = (blizzard-y b)
do (incf (aref a i j))))
(defun state/step (s)
"No arguments, as the evolution is fixed
1. update blizzards
2. update occupancy array"
(let ((ny-2 (- (array-dimension (state-a s) 0) 2))
(nx-2 (- (array-dimension (state-a s) 1) 2)))
(loop for b in (state-blz s)
for c = (blizzard-direction b)
for y = (blizzard-y b)
for x = (blizzard-x b)
;; update its position
do (cond ((char= c #\>)
;; x++, if x == n-2, go back to 0 + 1
(setf (blizzard-x b)
(if (= x nx-2)
1
(1+ x))))
((char= c #\v)
;; y++
(setf (blizzard-y b)
(if (= y ny-2)
1
(1+ y))))
((char= c #\<)
;; x--
(setf (blizzard-x b)
(if (= x 1)
nx-2
(1- x))))
((char= c #\^)
;; y--
(setf (blizzard-y b)
(if (= y 1)
ny-2
(1- y)))))))
(set-occupancy-from-blizzard-list (state-a s) (state-blz s)))
(defun dfss-end-p (s)
(let ((x (getf s :x))
(y (getf s :y))
(occupancy (state-a (getf s :field))))
"End criterion is a. killed by blizzard b. reached the goal"
(or
;; final position
(and (= x (- (array-dimension occupancy 1) 2))
(= y (- (array-dimension occupancy 0) 1)))
;; killed (occupancy is not zero)
(not (= (aref occupancy y x) 0))
;; timeout
(> (getf s :time) +time-limit+))))
(defun dfss-score-f (s)
(let ((x (getf s :x))
(y (getf s :y))
(occupancy (state-a (getf s :field))))
;; print every time the score gets evaluated. Good way to track end of branches.
(print (getf s :time))
(cond
;; if at the final square, score needs to be higher for shorter time
((and (= x (- (array-dimension occupancy 1) 2))
(= y (- (array-dimension occupancy 0) 1)))
(/ 1 (getf s :time)))
;; if anywhere else, score zero because we're either killed, stuck, or back at the start
(t 0))))
(defun dfss-next-states-f (s)
"Return states corresponding to steps that will not kill us (assuming simultaneous blizzard and
player movement)"
(let* ((x (getf s :x))
(y (getf s :y))
(nx (array-dimension (state-a (getf s :field)) 1))
(ny (array-dimension (state-a (getf s :field)) 0))
;; make sure to copy, so that we can rewind a dead branch
(new-field (state/copy (getf s :field)))
;; possible new xs (right down left up wait)
(new-xs (list (1+ x) x (1- x) x x))
(new-ys (list y (1+ y) y (1- y) y)))
;; first, evolve the blizzards, so we know where we can go
(state/step new-field)
;; if we're next to the goal, always return only that move
(if (and (= x (- nx 2))
(= y (- ny 2)))
(list
(list :time (1+ (getf s :time))
:x (- nx 2)
:y (- ny 1)
:field new-field))
;; else, try options
(loop for new-x in new-xs
for new-y in new-ys
;; do not include in moveset if...
unless (or
;; ... out of bounds
(< new-x 0)
(>= new-x nx)
(< new-y 0)
(>= new-y ny)
;; ... we go back to the starting position (waiting still allowed). In other
;; words, (1 1) to (0 1) is not allowed.
(and (= y 1) (= x 1) (= new-y 0) (= new-x 1))
;; ... we would collide
(> (aref (state-a new-field) new-y new-x) 0))
collect (list :time (1+ (getf s :time))
:x new-x
:y new-y
:field new-field)))))
(defun do-puzzle (char-array)
(let ((dfss-cur-state (list
:time 0
:x 1
:y 0
:field (state/parse char-array))))
(print "initial state")
(print dfss-cur-state)
(dfss-score-f
(dfss/recurse dfss-cur-state
#'dfss-end-p
#'dfss-score-f
#'dfss-next-states-f))))
| 6,639 | Common Lisp | .lisp | 172 | 26.197674 | 98 | 0.463782 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 37487ab62e7dabbb950143e2e57a83b46ff5fd697ae0e991f8aeb64bcd39a699 | 42,131 | [
-1
] |
42,132 | one.lisp | drvdputt_aoc-2022/src/one.lisp | #!/usr/bin/sbcl --script
(load "file.lisp")
(defvar input (read-lines #p"one-input.txt"))
;; (print input)
;;
;; (print (cl-ppcre:split "0" input))
(defvar sums (list))
(defvar indices (list))
(let ((currentsum 0)
(elf 1))
(loop for x in input
when (string= x "")
collect currentsum into maxes
and collect elf into elves
and do (setq currentsum 0 elf (+ 1 elf))
else
do (setq currentsum (+ currentsum (parse-integer x)))
finally (setq sums maxes indices elves)))
;; sort in reverse order
(defvar sortedmaxes (sort sums #'>))
;; print first three
(print (subseq sortedmaxes 0 3))
(print (apply (function +) (subseq sortedmaxes 0 3)))
| 685 | Common Lisp | .lisp | 23 | 26.565217 | 58 | 0.663126 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 8136ba8802c972eb5a0bc325e1367291f4bf91e5d9854b6f8c2b92efed0446eb | 42,132 | [
-1
] |
42,133 | twelve.lisp | drvdputt_aoc-2022/src/twelve.lisp | (defun char-math (integer-operation c1 c2)
(funcall integer-operation
(char-int c1) (char-int c2)))
(defun char- (c1 c2)
(char-math (function -) c1 c2))
(defun array/find-value (a char)
(loop for i from 0 below (array-dimension a 0)
for jfound = (loop for j from 0 below (array-dimension a 1)
for c = (aref a i j)
if (char= c char)
return j
finally (return nil))
if jfound
return (list i jfound)
finally (return nil)))
;; first is i (col), second is j (row)
(defun find-start (hmap)
(array/find-value hmap #\S))
(defun find-end (hmap)
(array/find-value hmap #\E))
(defun opposite-step (dir)
(cond ((char= dir #\u) #\d)
((char= dir #\d) #\u)
((char= dir #\l) #\r)
((char= dir #\r) #\l)))
(defun ij-step (i j dir)
(cond ((char= #\u dir) (cons (- i 1) j))
((char= #\d dir) (cons (+ i 1) j))
((char= #\l dir) (cons i (- j 1)))
((char= #\r dir) (cons i (+ j 1)))))
(defun hmap/peek (hmap i j dir)
(let* ((newij (ij-step i j dir))
(newi (car newij))
(newj (cdr newij)))
(if (or (< newi 0) (>= newi (array-dimension hmap 0))
(< newj 0) (>= newj (array-dimension hmap 1)))
nil
(aref hmap newi newj))))
(defun hmap/step-possible (hmap i j step)
(let ((peek (hmap/peek hmap i j step))
(c (aref hmap i j)))
(and
;; destination can not be nil
peek
;; height increase at most 1
(<= (char- peek c) 1))))
(defun hmap/step-possible-reverse (hmap i j step)
(let ((peek (hmap/peek hmap i j step))
(c (aref hmap i j)))
(and
;; destination can not be nil
peek
;; height decrease at most 1
(>= (char- peek c) -1))))
;; find all possible steps (not backwards)
;; returns list with up to three of UDLR
;; prev-step can also be nil
(defun hmap/step-options (hmap i j prev-step step-possible-f)
(let ((bad-dir (if prev-step
(opposite-step prev-step)
nil)))
(loop for dir in '(#\d #\r #\u #\l)
;; we cannot do anything with the direction we came from
if (and
;; direction needs to be allowed
(or (not bad-dir) (not (char= dir bad-dir)))
;; and possible in terms of height
(funcall step-possible-f hmap i j dir))
collect dir)))
;; recursive explore until end is found
;; i j is starting position
(defun dmap/set-neighbour-distance (dmap i j ni nj)
(let* ((olddist (aref dmap ni nj))
(newdist (+ (aref dmap i j) 1)))
(if (= olddist -1)
(setf (aref dmap ni nj) newdist)
;; if old distance was already set, check if we need to shrink it
(setf (aref dmap ni nj) (min newdist olddist)))))
;; if the d(neighbor) is unset (-1), or bigger than d(ij) + 1, we need to go
;; down that path
(defun dmap/needs-explore-p (dmap i j ni nj)
(let* ((nd (aref dmap ni nj)))
(or (= -1 nd)
(> nd (+ 1 (aref dmap i j))))))
;; to be used recursively. Initiation of recursion uses #\r as initial
;; step (since entry point is always on the left edge)
(defun hmap/fill-dmap-entrypoint (hmap dmap i j
&key (step-possible-f (function hmap/step-possible)))
(setf (aref dmap i j) 0)
(hmap/fill-dmap-neighbors hmap dmap i j
:prev-dir nil
:step-possible-f step-possible-f))
(defun hmap/fill-dmap-neighbors (hmap dmap i j
&key
(prev-dir nil)
(step-possible-f (function hmap/step-possible)))
(let ((dirs (hmap/step-options hmap i j prev-dir step-possible-f)))
;; first fill the map values, and remember which ones need to be explored
(let ((dirs-to-explore
(loop for dir in dirs
for ij-neighbor = (ij-step i j dir)
for ni = (car ij-neighbor)
for nj = (cdr ij-neighbor)
if (dmap/needs-explore-p dmap i j ni nj)
collect dir
;; needs to happen after the above check
do (dmap/set-neighbour-distance dmap i j ni nj))))
;; then recursively explore the directions that need exploring
(loop for dir in dirs-to-explore
for ij-neighbor = (ij-step i j dir)
for ni = (car ij-neighbor)
for nj = (cdr ij-neighbor)
do (hmap/fill-dmap-neighbors hmap dmap ni nj
:prev-dir dir
:step-possible-f step-possible-f)))
dmap))
(load "file.lisp")
(defvar hmap-big (read-array "twelve-input.txt" "chars"))
(defvar start-big (find-start hmap-big))
(defvar end-big (find-end hmap-big))
(defvar hmap-e (read-array "twelve-input-example.txt" "chars"))
(defvar start-e (find-start hmap-e))
(defvar end-e (find-end hmap-e))
(defun make-array-same-dim (a)
(make-array (array-dimensions a) :initial-element -1 :element-type 'integer))
(defun do-puzzle (hmap istart jstart iend jend)
(let ((dmap (make-array-same-dim hmap)))
(setf (aref hmap istart jstart) #\a)
(setf (aref hmap iend jend) #\z)
(hmap/fill-dmap-entrypoint hmap dmap istart jstart)
(print dmap)
(print (aref dmap iend jend))
dmap))
;; (defun print-progress (hmap dmap)
;; 0)
;; (defvar dmap-big (do-puzzle hmap-big
;; (first start-big) (second start-big)
;; (first end-big) (second end-big)))
;; part two
;; d(new, end) = d(start, end) - d(start, new)
;; Does not work if tile is not ON the shortest path!
;; (defun dmap/distance-to-end (dmap new-start end)
;; (-
;; ;; distance from start to end
;; (aref dmap (first end) (second end))
;; ;; distance from start to new point
;; (aref dmap (first new-start) (second new-start))))
;; alternative way, set up a distance map with a different starting point
;; (defvar dmap-from-end (do-puzzle hmap-big
;; (first end-big) (second end-big)
;; (first start-big) (second start-big)))
;; go over all characters in the map. If character is at height a, check distance and remember the minimum.
(defun hmap/find-closest-a (hmap dmap)
(loop for i from 0 below (array-dimension hmap 0)
minimize (loop for j from 0 below (array-dimension hmap 1)
for d = (aref dmap i j)
if (and (char= (aref hmap i j) #\a) (not (= -1 d)))
minimize d)))
(defun do-part2 (hmap istart jstart iend jend)
(let ((dmap (make-array-same-dim hmap)))
(setf (aref hmap istart jstart) #\a)
(setf (aref hmap iend jend) #\z)
(hmap/fill-dmap-entrypoint hmap dmap iend jend
:step-possible-f (function hmap/step-possible-reverse))
(print dmap)
(print (hmap/find-closest-a hmap dmap))))
;; (do-part2 hmap-e (first start-e) (second start-e) (first end-e) (second end-e))
(do-part2 hmap-big (first start-big) (second start-big) (first end-big) (second end-big))
| 6,414 | Common Lisp | .lisp | 168 | 34.494048 | 107 | 0.648392 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | eb13aae6cf9fa97db88c7a7799dec6399071a611aa31617e2ccf0e7fe5be0938 | 42,133 | [
-1
] |
42,134 | twenty.lisp | drvdputt_aoc-2022/src/twenty.lisp | (load "file.lisp")
(load "string.lisp")
(load "num.lisp")
(load "list-tools.lisp")
(defparameter numbers (mapcar #'parse-integer (read-default-input "twenty")))
(defparameter numbers-e '(1 2 -3 3 -2 0 4))
(defun shift-element-position (position-table i delta)
"Shift element by skipping over <delta> elements (with rollover).
set p_i to p_i + 1 + delta (skips over <delta> elements, so when delta = 1, skip over 1 ->
position goes from 0 to 2. delta = 2
Forward case
------------
0 1 2 3 4 5
delta 1
x 1 0 2 3 4 5
0 now at position 2
be shifted left)
0 1 2 3 4 5
delta 5
x 1 2 3 4 5 (0 skips over 5 back to position 0)
so delta + 1 = 6 should result in p-new = 0
backwards case (skip over the one at the other end!)
--------------
0 1 2 3 4 5
delta -1 from 1 (skip over zero and end up in final position)
1
0 x 2 3 4 5
0 1 2 3 4 5
delta -1
x 1 2 3 4 0 5
0 now at position (n - 1)
0 1 2 3 4 5
delta -2
x 1 2 3 0 4 5
0 now at position (n - 2)
Finalizing
----------
Shifts then need to happen to remove the position marked as x.
Detail: for a list of length 7, six skips brings us back to the starting point! It's modulo (n - 1)!
"
(let* ((n (length position-table))
(p-old (aref position-table i))
;; modulo n - 1!
(p-new (mod (+ p-old delta) (1- n))))
;; Special case: if p-new mod n-1 == 0, move to pos n-1 (equivalent because cyclical, but we
;; do it to have the same output as the example)
(when (= 0 p-new) (setq p-new (1- n)))
;; (print (list "p-old" p-old "p-new" p-new))
;; Do the shifts, except when delta == 0 or pnew == pold
(unless (or (= delta 0) (= p-new p-old))
;; first, make space by doing the shifts
(if (> p-new p-old)
;; forward (p-new > p-old)
;; 0 1 2 3 4 5
;; 2
;; 0 1 x 3 4 5
;; 5 stays in place, but 3 and 4 need to make space for p-new
;; so > p-old and <= p-new
;; and don't care about i, we overwrite it anyway
(loop for j from 0 below n
for pj across position-table
;; shift left if between p-old and p-new
if (and (> pj p-old)
(<= pj p-new))
do (decf (aref position-table j)))
;; backward (p-new < p-old)
;; 0 1 2 3 4 5
;; 4
;; 0 1 2 3 x 5
;; 1 stays in place, 2 and 3 and 4 (p-new) shift right
;; so < p-old and >= p-new
(loop for j from 0 below n
for pj across position-table
;; shift right if between p-new and p-old
if (and (>= pj p-new)
(< pj p-old))
do (incf (aref position-table j))))
;; then at the end, set p-new
(setf (aref position-table i) p-new))))
(defun mix (numbers &key (times 1))
;; The numbers are not unique!
(let ((h (int-histogram numbers)))
(loop for k being each hash-key of h
if (> (gethash k h) 1)
do (print (list k (gethash k h)))))
;; instead of working with a list which we constantly modify, and searching the number in it,
;; make a table to keep track of the position of each number. This table is a vector, where
;; the position in the vector (index) refers to the original order, and the value refers to
;; the new order. At the start, value = index
(let* ((nn (length numbers))
(position-table
(make-array
nn
:element-type 'integer
:initial-contents (loop for i from 0 below nn collect i))))
(loop repeat times
do (loop for v in numbers
for i from 0
do (shift-element-position position-table i v)))
position-table))
(defun pprint-position-table (numbers position-table)
(print (parse-position-table numbers position-table)))
(defun parse-position-table (numbers position-table)
(loop for v in numbers
for p across position-table
with a = (make-array (length numbers) :element-type 'integer)
do (setf (aref a p) v)
finally (return a)))
(defun grove-coordinates (mixed-numbers)
(let* ((index0 (position 0 mixed-numbers))
(k1-k2-k3 (mapcar (lambda (offset)
(elt mixed-numbers (mod (+ index0 offset) (length mixed-numbers))))
'(1000 2000 3000))))
(print "zero is at")
(print index0)
(print "1000 2000 and 3000 are")
(print k1-k2-k3)
(print "sum is")
(print (apply #'+ k1-k2-k3))))
(defun do-puzzle (numbers)
(let* ((final-position-table (mix numbers))
(mixed-numbers (parse-position-table numbers final-position-table)))
(grove-coordinates mixed-numbers)))
(defun do-part2 (numbers)
(let* ((decryption-key 811589153)
(mix-times 10)
(alt-numbers (mapcar (lambda (n) (* decryption-key n)) numbers))
(final-position-table (mix alt-numbers :times 10))
(mixed-numbers (parse-position-table alt-numbers final-position-table)))
(grove-coordinates mixed-numbers)))
| 5,174 | Common Lisp | .lisp | 130 | 32.3 | 102 | 0.583898 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 70b10b7084a98a6dd5e7490dcaf0ec81d054cd8f79cc4b1b1017800bd8fadaa8 | 42,134 | [
-1
] |
42,135 | five.lisp | drvdputt_aoc-2022/src/five.lisp | (load "file.lisp")
(defvar lines nil)
(setq lines (read-default-input "five"))
;; find line that starts with " 1" to figure out number of stacks
(load "string.lisp")
(defvar stack_definition_line nil)
(defvar stack_definition_index nil)
(loop for l in lines
for i = 0 then (+ 1 i)
when (starts-with l " 1")
do (setq stack_definition_line l stack_definition_index i))
(defvar num_stacks (first (last (find-all-int stack_definition_line))))
(defvar initialization_lines (reverse (subseq lines 0 stack_definition_index)))
(defvar move_lines (subseq lines (+ 2 stack_definition_index)))
;; make list of empty lists
(defun stacks/initialize (num-stacks)
(loop repeat num-stacks
collect (list)))
;; the positions of the characters in the line
(defun input-position (stacknr)
(+ 1 (* 4 stacknr)))
;; we'll need to call this from bottom to top
;; line looks like this
;; " [R] [D] [L] [C] [N] [Q] [R]"
(defun stacks/add-input-line (stacks line)
(loop for stack in stacks
for n from 0 to (length stacks)
for c = (char line (input-position n))
;; do (print (list n c))
when (alpha-char-p c)
do (push c stack)
collect stack))
(defvar startstacks (stacks/initialize num_stacks))
(print (list "initializing using" initialization_lines))
(loop for line in initialization_lines
;; do (print startstacks)
do (setq startstacks (stacks/add-input-line startstacks line)))
(defun stacks/pretty-print (stacks)
(loop for stack in stacks
do (print (reverse stack))))
(stacks/pretty-print startstacks)
;; move crate from src to dst (zero based index)
(defun stacks/apply-move (stacks src dst)
(let ((crate (first (nth src stacks))))
(loop for s in stacks
for i = 0 then (+ 1 i)
collect (cond
;; remove first from source
((= i src) (cdr s))
;; crate on stack
((= i dst) (cons crate s))
;; else return unchanged
(t s)))))
(defun stacks/apply-move-alt (stacks num src dst)
(let
;; crates to be moved = first N of src
((crates (subseq (nth src stacks) 0 num)))
(loop for s in stacks
for i = 0 then (+ 1 i)
collect (cond
;; remove first N from source
((= i src) (subseq s num))
;; put crates on stack in order
((= i dst) (concatenate 'list crates s))
;; else return unchanged
(t s)))))
(defun stacks/apply-move-instruction-line (stacks line)
(let ((numbers (find-all-int line))
(newstacks stacks))
;; first number indicates repetitions
(dotimes (i (first numbers))
;; second is src, third is dst
(setq newstacks (stacks/apply-move
newstacks
(- (nth 1 numbers) 1)
(- (nth 2 numbers) 1))))
newstacks))
(defun stacks/apply-move-instruction-line-alt (stacks line)
(let ((numbers (find-all-int line))
(newstacks stacks))
;; second is src, third is dst
(setq newstacks (stacks/apply-move-alt
newstacks
(first numbers)
(- (nth 1 numbers) 1)
(- (nth 2 numbers) 1)))
newstacks))
(print "final configuration")
(let ((stacks startstacks))
(loop for l in move_lines
do (setq stacks (stacks/apply-move-instruction-line stacks l)))
(stacks/pretty-print stacks))
(print "alt configuration")
(let ((stacks startstacks))
(loop for l in move_lines
do (setq stacks (stacks/apply-move-instruction-line-alt stacks l)))
(stacks/pretty-print stacks))
| 3,382 | Common Lisp | .lisp | 97 | 31.020619 | 79 | 0.673494 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 46e7d494200ad05c9f4b252a04ae5aa7b5c528e87f419d6a73c16f7d927574b7 | 42,135 | [
-1
] |
42,136 | sixteen.lisp | drvdputt_aoc-2022/src/sixteen.lisp | (load "file.lisp")
(load "string.lisp")
(load "list-tools.lisp")
(defparameter lines-e (read-default-input "sixteen-example"))
(defparameter lines (read-default-input "sixteen"))
(defun valve/parse (line)
(list
;; fixed position
:name (subseq line 6 8)
;; the only number in the string
:rate (first (find-all-int line))
;; value to the left of every comma, plus the last two characters
:tunnels (loop for i in (index-of-all-matches line ",")
collect (subseq line (- i 2) i) into tunnels
finally
(return (append tunnels
(list (subseq line (- (length line) 2))))))
))
;; opt for hash table instead of association list, because I want to be able to loop over the
;; (unique) keys. Includes all CONSTANTS for the valves. The actual state is in a separate
;; vector.
(defun valvedef/initialize (lines)
;; (loop for l in lines
;; for v = (valve/parse l)
;; collect (cons (getf v :name) v)))
(let ((state (make-hash-table :test #'equal)))
(loop for l in lines
for v = (valve/parse l)
for i from 0
;; add an index to each valve. We will use this index to set up a vector tracking
;; which valves are open
do (setf (getf v :index) i)
do (setf (gethash (getf v :name) state) v))
state))
(defun valvedef/print (valvedef)
(pprint-hashmap valvedef))
(defun valvestate/open (valvedef state name)
(let ((i (getf (gethash name valvedef) :index))
(new-state (copy-seq state)))
(setf (elt new-state i) t)
new-state))
;; true if all non-zero valves are open
(defun valvestate/allopen (valvedef state)
(loop for v being each hash-value of valvedef
if (and
;; if a non-zero valve...
(> (getf v :rate) 0)
;; ...is closed
(not (elt state (getf v :index))))
return nil
finally
(return t)))
(defun valvestate/closed (valvedef state name)
(not (elt state (getf (gethash name valvedef) :index))))
;; score that a valve will deliver, given that you start opening it at the current time. Opening
;; at minute 1 -> flows from minutes 2 - 30 --> total volume = 29 * rate.
(defun valve/score (valve time-remaining)
(* (getf valve :rate) time-remaining))
;; trivial optimization: if we are at a certain node, at minute 29, the optimal move is to just
;; open the valve. Or wait if all non-zero valves are already open.
;; returns alist of possible moves and the resulting score
;; pos is a valve name referring to the current position
(defun max-score-move (valvedef state pos time-remaining &key (prev-pos nil))
(cond ((= time-remaining 0)
(list :best (cons 'wait 0) :history (list (cons 'wait 0))))
((= time-remaining 1)
;; if still closed, open it. Else wait.
(let ((best (if (valvestate/closed valvedef state pos)
(let ((score (valve/score (gethash pos valvedef) 1)))
;; (print (list time-remaining "opening last valve for" score))
(if (> score 0)
(cons 'open score)
(cons 'wait 0)))
(cons 'wait 0))))
(list :best best :history (list best))))
(t
(try-score-move-recursions
valvedef
state
pos
time-remaining
:prev-pos prev-pos))))
;; returns (:best best :history (list moves))
(defun try-score-move-recursions (valvedef state pos time-remaining &key (prev-pos nil))
;; (print (list time-remaining pos (action-suggest valvedef state pos :prev-pos prev-pos)))
(loop for action in (action-suggest valvedef state pos :prev-pos prev-pos)
with best-result = nil
with this-result = nil
with best-action = nil
;; if the action is wait, then we quit
if (equal 'wait (car action))
return (list :best (cons 'wait 0) :history (list (cons 'wait 0)))
;; else, calculate the score for this action. The score is the points we gain by
;; doing the action (= 0 for any moves), plus whatever is scored recursively.
if (equal 'move (car action))
;; Move --> recurse from a different position. Pass current position as prev-pos, to
;; make sure we don't immediately move back
do (setq this-result (max-score-move
valvedef
state
(cdr action)
(- time-remaining 1)
:prev-pos pos))
if (equal 'open-valve (car action))
;; Open valve --> recurse from same position with the state changed
do (progn
(setq this-result (max-score-move
valvedef
(valvestate/open valvedef state pos)
pos
(- time-remaining 1)))
;; do not forget to add the score of the valve we just opened!
(incf (cdr (getf this-result :best))
(valve/score
(gethash pos valvedef)
time-remaining)))
;; do (print (list this-result best-result))
if (or
(not best-result)
(> (cdr (getf this-result :best))
(cdr (getf best-result :best))))
;; If first move or higher score remember it.
do (setq best-result this-result
best-action action)
finally
;; for the best result, we return the move-score and add the new step to history
(return (list :best (getf best-result :best)
:history (append (getf best-result :history)
(list best-action))))))
;; Smart way to reduce the number of possible moves. Score calculation does not happen yet here,
;; just a list of things to try
;; returns list of actions ('move tunnelname) ('wait nil) or ('open-valve nil)
(defun action-suggest (valvedef state pos &key (prev-pos nil))
(let ((action-list nil))
(if (valvestate/allopen valvedef state)
;; if all relevant valves are open, wait
(push (cons 'wait pos) action-list)
;; else, look at the valve and...
(let ((v (gethash pos valvedef)))
;; add all the moves
(loop for tunnel in (getf v :tunnels)
if (or (not prev-pos) (not (string= tunnel prev-pos)))
do (push (cons 'move tunnel) action-list))
;; add open-valve if flow rate nonzero and valve closed
(when (and (> (getf v :rate) 0)
(valvestate/closed valvedef state pos))
(push (cons 'open-valve pos) action-list))))
(if action-list
action-list
;; if after all this, still no actions are available, there's no use in further pursuing
;; this path, since it's not the most efficient (e.g. we end up in a dead end for the
;; second time, after already opening the valve). signal wait to end the search
(list (cons 'wait pos)))))
;; -------- alternate method --------
;; (load "~/quicklisp/setup.lisp")
(ql:quickload :alexandria)
(load "dijkstra.lisp")
(defun best-score-all-permutations (valvedef matrix valves-to-permute time
&key (size nil) (valves-fixed nil))
(let* ((max-score nil)
(best-permutation nil)
(counter 0)
(the-size (if size size (length valves-to-permute)))
(num-permutations
(apply (function *)
(loop for i from 1 to the-size collect i))))
;; (print (list num-permutations "permutations"))
(alexandria:map-permutations
(lambda (permutation)
(let ((score (open-valves-in-order valvedef
matrix
;; prefix the valve order with the fixed valves if so
;; requested
(if valves-fixed
(append valves-fixed permutation)
permutation)
time)))
(when (or (not max-score) (> score max-score))
(setq max-score score
best-permutation permutation))))
;; (incf counter)
;; (when (= 0 (mod counter 1000000))
;; (print (list counter score max-score)))))
valves-to-permute
;; optionally, only try out all subset permutations
:length the-size)
;; (print (open-valves-heuristic valvedef matrix non-zero-names 30))
(list :score max-score :order best-permutation)))
;; go over all valves in valve-order, given the distance matrix between those valves. For each
;; valve, move to it, then open it. The travel time and opening time are subtracted from time
;; remaining, and the resulting valve score is added to the total.
;; we will frequently have repeated orders (e.g. a b c . with . running over all others). The
;; keys are (cons time valves).
(defparameter open-valves-in-order-memo (make-hash-table :test #'equal))
(defun open-valves-in-order-memo-set (time valve-order time-remaining score)
(let ((memo-key (cons time valve-order)))
;; (print (list "saving memo" memo-key :time-remaining time-remaining :score score))
(unless (gethash memo-key open-valves-in-order-memo)
(setf (gethash memo-key open-valves-in-order-memo)
(list :time-remaining time-remaining :score score)))))
(defun open-valves-in-order (valvedef matrix valve-order time &key (use-memo nil))
(let ((valves-todo valve-order)
(time-remaining time)
(start-score 0)
(v (gethash "AA" valvedef)))
;; find the best memo
(when use-memo
(loop for end from (- (length valve-order) 1) downto 1
;; if we have a memo for the first n
for skipped-valves = (subseq valve-order 0 end)
for memo-key = (cons time skipped-valves)
for memo-value = (gethash memo-key open-valves-in-order-memo)
if memo-value
do (setq valves-todo (subseq valve-order end)
;; score is remembered
start-score (getf memo-value :score)
;; time skips forward
time-remaining (getf memo-value :time-remaining))
;; starting valve is no longer "AA"
and do (setq v
(gethash (car (last skipped-valves)) valvedef))
;; and do (print (list valve-order "memo'd" skipped-valves "continuing with" valves-todo))
and return nil))
(loop for next-name in valves-todo
for next-v = (gethash next-name valvedef)
with score = start-score
;; travel to the valve and open it (by adding score). Time decreases with travel time
;; + 1
do (setq time-remaining (- time-remaining
(+ (aref matrix (getf v :index) (getf next-v :index))
1))
v next-v)
if (<= time-remaining 0)
;; make memo
do (when use-memo (open-valves-in-order-memo-set time valve-order time-remaining score))
;; if time is up, do not count the score! We do not want negative numbers
;; muddying the waters.
and return score
else
;; only increase score if positive time
do (incf score (valve/score v time-remaining))
finally (progn
;; also make memo here
(when use-memo (open-valves-in-order-memo-set time valve-order time-remaining score))
;; (print score)
(return score)))))
(defun open-valves-heuristic (valvedef matrix nonzero-valves time)
;; (print valve-order)
(loop for i from 0
with time-remaining = time
with v = (gethash "AA" valvedef)
;; find best scoring move
for best-name = (loop for name in nonzero-valves
for next-v = (gethash name valvedef)
for d = (aref matrix (getf v :index) (getf next-v :index))
for score = (valve/score next-v (- time-remaining (+ 1 d)))
with best-score = 0
with best-name = nil
;; do (print (list name next-v d score))
if (> score best-score)
do (setq best-score score
best-name name)
finally (return best-name))
for next-v = (gethash best-name valvedef)
do (print (list best-name time-remaining nonzero-valves score))
;; if all scores are negative, the loop above will return nil
if (not best-name)
return score
;; travel to the valve and open it. Time decreases with travel time + 1
do (setq time-remaining (- time-remaining (+ (aref matrix (getf v :index) (getf next-v :index)) 1))
v next-v)
summing (valve/score v time-remaining) into score
;; remove from the todo list
do (setq nonzero-valves (remove best-name nonzero-valves :test #'equal))
;; time is up or valves are all open
if (or (not nonzero-valves)
(<= time-remaining 0))
return score
finally (progn
;; (print score)
(return score))))
(defun make-distance-matrix (valvedef)
(let* ((index-names-alist (loop for v being each hash-value of valvedef
collect (cons (getf v :index) (getf v :name))))
(matrix (dijkstra/distance-matrix-init (length index-names-alist))))
;; first, fill in the matrix
;; (print index-names-alist)
;; we go over all array rows.
(loop for i from 0 below (array-dimension matrix 0)
;; If we find a -1, start a dijkstra explorer from there.
if (loop for j from 0 below (array-dimension matrix 1)
when (= -1 (aref matrix i j)) return t)
do (dijkstra/recurse
matrix
i
;; function that gets the index of the neighbors
(lambda (i)
;; find name based on index
(let* ((name (cdr (assoc i index-names-alist)))
;; find valve based on name
(v (gethash name valvedef))
;; get tunnels from valve
(tunnels (getf v :tunnels)))
;; convert every tunnel in the list to index
;; (print tunnels)
(mapcar (lambda (name)
(car (rassoc name index-names-alist :test 'equal)))
tunnels)))))
;; and do (print matrix))
matrix))
;; get (list :score and :order)
(defun smart-find-best-permutation (valvedef matrix valves time permutation-depth)
;; try combinations (e.g. 7 out of all) and remember with which valve the best result was.
;; Then continue with "best start" + combinations (7 out of remainig vales).
(let ((valves-fixed nil)
(valves-to-permute (copy-list valves)))
(loop for num-fixed from 0 below (length valves)
for result = (best-score-all-permutations
valvedef matrix valves-to-permute time
:size (min permutation-depth (length valves-to-permute))
:valves-fixed valves-fixed)
;; the partial permutations tell us that this might be the best order to continue
for best-next = (car (getf result :order))
;; do (print result)
do (setq
;; use the first valve of this order as the next "fixed opening" step
valves-fixed (append valves-fixed (list best-next))
;; and stop including it in the permutation search
valves-to-permute (remove best-next valves-to-permute))
;; do (print (list :fixed valves-fixed :permute valves-to-permute))
finally
(progn
;; (print (list "best order" valves-fixed))
(return (list :score (getf result :score)
:order valves-fixed))))))
;; go over all ways to divide the valves between the two actors (give all combinations to left,
;; and the rest to right). Calculate the best permutation and score individually.
(defun do-part2 (valvedef matrix valves time permutation-depth)
(flet ((score-f (actor1-combination)
;; valves for actor2 are all valves except those for actor1
(let* ((actor2-combination (remove-if
(lambda (v) (member v actor1-combination))
valves))
(result1 (smart-find-best-permutation valvedef
matrix
actor1-combination
time
permutation-depth))
(result2 (smart-find-best-permutation valvedef
matrix
actor2-combination
time
permutation-depth)))
;; (print (list "divide" actor1-combination actor2-combination))
(+ (getf result1 :score) (getf result2 :score)))))
;; go over all sizes of actor1 (up to half size), and keep track of the best combo while
;; trying all combinations of that size.
(loop for actor1-size from (floor (length valves) 3) to (floor (length valves) 2)
maximize (let ((best-combo nil)
(best-score 0))
(alexandria:map-combinations
(lambda (combo)
(let ((combo-score (funcall #'score-f combo)))
(when (> combo-score best-score)
(setq best-score combo-score
best-combo combo))))
valves
:length actor1-size)
(print (list "best combo of size" actor1-size
"is" best-combo
"with score" best-score))
best-score))))
;; (print (do-puzzle lines-e))
;; (print (do-puzzle lines))
(defun do-puzzle (lines &key (permutation-depth 4))
(clrhash open-valves-in-order-memo)
(let* ((valvedef (valvedef/initialize lines))
(matrix (make-distance-matrix valvedef))
(non-zero-names (loop for v being each hash-value of valvedef
if (> (getf v :rate) 0)
collect (getf v :name))))
(print (list
"part 1"
(smart-find-best-permutation valvedef
matrix
non-zero-names
30
permutation-depth)))
(do-part2 valvedef matrix non-zero-names 26 permutation-depth)))
| 19,901 | Common Lisp | .lisp | 389 | 36.526992 | 111 | 0.543038 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 4885308523434f0b2ebcf0a86fa4ab120d7862d84a4b6246008947f03fdbb79a | 42,136 | [
-1
] |
42,137 | dijkstra.lisp | drvdputt_aoc-2022/src/dijkstra.lisp | ;; build up a distance matrix using dijktra's algorithm
(defun dijkstra/distance-matrix-init (num-nodes)
(make-array (list num-nodes num-nodes)
:element-type 'integer
:initial-element -1))
(defun dijkstra/set-distance (matrix i j d)
(setf (aref matrix i j) d)
(setf (aref matrix j i) d))
(defun dijkstra/update-distance (matrix i j d)
(let ((old-d (aref matrix i j)))
(when (or (= -1 old-d) (< d old-d))
(dijkstra/set-distance matrix i j d))))
;; trying to make this generic, so i can use it in the future
;; name-to-index-f: function that takes a string (node name) and translates it to an index in
;; the distance matrix
;; node-name: string
;; prev-node-names: list of strings, to backpropagate the distance info. nil at the entry point
;; of the recursion.
;; next-node-names-f: function that returns the nodes to explore next, given the current node
;; name and the previous node name
(defun dijkstra/recurse (matrix
i ;; node to explore
neighbor-indices-f ;; function that returns the indices of the neighbors
&key (j-prev nil)) ;; nodes previously explored in this path
(let ((neighbors (funcall neighbor-indices-f i))
(new-j-prev (cons i j-prev)))
;; (print (list "exploring from" i "which has neighbours" neighbors))
;; first, do the backpropagation to all previous nodes. Also takes care of setting neighbor
;; distance to 1, since the neighbor is always at index 1 in the (updated) history.
(loop for j in new-j-prev
for d from 0
do (dijkstra/update-distance matrix i j d))
;; then, figure out where to go next, and recurse
(loop for j in neighbors
if (dijkstra/needs-exploration matrix new-j-prev j)
do (dijkstra/recurse matrix
j
neighbor-indices-f
:j-prev new-j-prev))))
;; else
;; do (print (list "do not explore from" i "to" j)))))
;; does node j need exploration, given i-history
(defun dijkstra/needs-exploration (matrix j-prev j-next)
;; (print (list "needs-exploration" j-prev j-next))
(if (member j-next j-prev)
;; if number is already in history, stop exploring, we don't want loops
nil
;; ;; else, we keep exploring as long as the backpropagation keeps finding new distances
;; ;; (detours matter! we want the distance ALL pairs of points). Same deal if a shorter
;; ;; path between one of the points in the history and the next point would be found.
;; (loop for jp in j-prev
;; for d from 1
;; for old = (aref matrix j-next jp)
;; if (or (= -1 old) (< d old))
;; do (print (list "re-exploring would improve distance" j-next jp "from" old "to" d))
;; and return t
;; ;; if no improvement at all, no need to explore this path further
;; finally (return nil))))
;; scratch that. This is an optimization that only works for partial distance matrices
;; (those from a single starting point). If we want to fill the full distance matrix, then
;; we NEED to explore all paths (except loops). Otherwise, some connections can be missed.
;; There might be an optimization possible (e.g. if we already know the ideal distance
;; from C to F, then to know the distance from A to F we only need to traverse A, B and C,
;; and then add C to F (and fill in B to F, A to F).
;; let's just do everything. Seems the right thing to do if there's much branching.
t))
;; alternate method, where we don't need to now the distance. We only want to flag the points in
;; terms of being reachable from the given starting point. Will fill an array of length
;; num-nodes with 1 and -1, with 1 at position j meaning that a node j was reachable from node
;; i.
(defun dijkstra/visitor (num-nodes
;; starting node
i
;; function that returns the neighboring indices, given an index
neighbor-indices-f)
(let ((visited (make-array num-nodes
:element-type 'integer
:initial-element -1)))
(dijkstra/visitor-recurse visited i neighbor-indices-f)
visited))
(defun dijkstra/visitor-recurse (visit-vector
i
neighbor-indices-f)
;; register that we visited the current node
(setf (elt visit-vector i) 1)
(let ((neighbors (funcall neighbor-indices-f i)))
;; then visit the neighbors that still need a visit (recurse)
(loop for j in neighbors
if (= -1 (elt visit-vector j))
do (dijkstra/visitor-recurse visit-vector j neighbor-indices-f))))
| 5,030 | Common Lisp | .lisp | 90 | 45.322222 | 100 | 0.625886 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 185c836cdad54b9bf1643620f5e3f720cfd00b5ec254278fff0b785aa2bd7a5f | 42,137 | [
-1
] |
42,138 | file.lisp | drvdputt_aoc-2022/src/file.lisp | (require "asdf")
(defun read-lines (fn)
(uiop:read-file-lines fn))
(defun read-default-input (prefix)
(read-lines (pathname (concatenate 'string prefix "-input.txt"))))
(provide "file")
;; assumes that all lines are of equal length
;; supported read-in types: numbers, chars
(load "string.lisp")
(defun read-array (fn type)
(let* ((lines (read-lines fn))
(nrow (length lines))
(ncol (length (first lines)))
(a (make-array (list nrow ncol) :element-type type)))
(loop for l in lines
for i from 0
with f = (cond ((eq type 'char)
(lambda (line) (loop for c across line collect c)))
((eq type 'integer)
(function find-all-int)))
do (loop for x in (funcall f l)
for j from 0
do (setf (aref a i j) x)))
a))
| 794 | Common Lisp | .lisp | 24 | 28.208333 | 68 | 0.626144 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | fa3fe82368d3f45e665aca10208cf608f66b72ce06fd3eb519ed525ebe918215 | 42,138 | [
-1
] |
42,139 | six.lisp | drvdputt_aoc-2022/src/six.lisp | (load "string.lisp")
(defvar example "mjqjpqmgbljsphdztnvjfqwrcgsmlb")
(defvar input "djhjvjggdzznllvvrvggscgscsrrffgvfvllfclcrchhwzhzqqlhqhffsdsmmcffnggcttdpttwpwttjvtjvtvqqctcwcmcsswvwzzlnzlnnvbnbdnngmmhchrcrqqhbhllbtllmppgjjtvjvdjvvpcpjcjjfrfzfzzdvzdvvswvvjzzbpzbzzddbndbbgjbjjvpjpjtjqtjqjcjmcjjrtjrjrqqvtvpvwpprhphrhdrddpdhhfsspddqnqwnntrtnrrthrhtrtwtdttmmnvmnmppswsqwqjqwqbqrqbqdbqdqgdqgqtgqqgzzhpzzwswvwmvwwvvrzznwzzbsbhbfhhvcvwvvrzrgzzfhzhhlthlhqhgqgttlmljmlmqqjddtqtctbblplddnqnzqnnzrzjrrqwrqqcfffbdfffspsqswstspsvppqmppdmppdvvfrfddqhqzqddtjddfqfrfllnjjcnnjzjmzmtmddbvdvzvbzzcjzzffdbdbsswshhrwrrfggpccszzzgdgvvlflwwdbbhqhffngffdfdmdgdhdmhdmdsspssctstdtdmdhhzvvcbbrqqmrmwwjmjqjmqjmjjjrmrmlmmbppmgpgttmptpspmmrttcddtjjspsfsqqbhbbzzgbzzznwzzlddmtdmdgmmlnljlvjjtgjjggmmnnvqnqzzfhzhttvbbprpmpmrmlrmllwmlwmlwwsjjlffbgfggqmggqgvglvvrpvrpvpnpphmhnmhhbbqjbbrrvfrfwflldffzwzccscqsqppctctddqbbmggmccdbdvbdvvpdpdbdsdjsjbbwpwcpcbcbmmzdmmvtvqvpvphhlblwwfmwmvvdhdtdwwlblglhhvfvwwqggrnrttpddtvvwqvqmqhhwnnghhbpbvbnvvdqqrqdqwqppmwpwhppnmnjmnjmjttvhhcgchcssrlrwllpdpndpdtptzppqqpvvtffcwffjppvnnjvvjnnwcwnnhlhjhsjsnjjzfjfsfhsssvttvfvsvpspppwswmswwqmwwzvwzzvtzvvwddqqzhhqpqjjwrwlrrbcbvvqllqjllvzvgvmvhmmsppwvpwphhjnndjjtpjtthzzvrrcwcrczzmqmsssvtstqtrrgtrtvtwtccbwcwrwbbdbgbmmcsmcssvjsjqqsnqqtvvbgbfbdffhjffvnnzpplqppzzfwfrfnfcncqccgjjcffhshrhgrhghvhphccqtcqtccjzccdnncggftgttrppnpptlltztqtjqqvfvqvdvmvmjjgqqrqgrqqcggdvvpcvcjvjnnrjjmbmlbmmqvmqmfmwmpwwnhwdtmvhqfwlbpzjplfhfntjgmvqmmjqpbngpvjvpgzpqwjjwhvjwwplrtjhzmzqmdrppgbrspmctlggmflbjzzfcvvdqlrtvqvwhcpjnmlvfgwrwwtblpqstddnqntnmwsbgjfrbdrlnvqdrnttshjmvpmncmggfdbnndwzmswmdvhmmwtgpfglrzzhwcsgvhnnrrhmnhftvvqfdfrsphzbslgscmwsnrwbvqphhswvpvsbbstvnndclhfhdctlvwrmdgzfcfmjmznqvvqrddmdlqznvcsqsgnpcqqhbdwqntcnqljstqvrzhgvzqdltpwmnpvjmqrpjsfhqvhchjnwjnqpdqbdjqdpqsqhbwwmhfthzbrsjnhncpbjrhgqlzmrzlnvrrfvlrmflcqfmqjzjwscrflgzwtbchfrnvsrrtncvhjbnnmlmmfdjcbmbsmgdtwzwcwnthfbsnrgdfwqjncqsdmfnfqgtcwrhjprlnhvrnpmnnhlwstvqjrsprqhjzszzgfznmgwjqglvfrrwpdbptdrnbbwbzbcbhbtcchmfsgmvnmrbdqhqgmvtfmvpgvjzjpgjbdhcfrfhprgdzrprccnhbmzdfjsgldlgpgdrfhbhtmhdttdsbndgbdfccqhhwhqfmlsfhsbbbmdncrwzcnrdvcmhllfwtrgjpgngzwptnqtggtcjwrptffmsrgdpctsdjtpssngsdqwfsbhdbcqvbdrzlhzlsbbzhqthzhcwsftlhrmhgpfzljgcphjjvhpqjzsfnrztwrhlnlbmgcgmstrbbwclpvdtdpclzlhmmpmmpmppnwjglhwppprlbzbvwqwmpgtvvpgdthnwbtblwpwgvmbcbjwjbczlcmzfwzbqvzsvgcmspvrsblldscqlgghdwzbvhhvgcfwgnqwlngclbjfwrpwtdjvqmzwwjztwdjplhzpzfslbbvfdsnpggwcttzwdlzgqgmrnpnclhrlngtwcwblzdjmpgqvzsvsdmzdwlgcdlccnnlrcvtrvspcsmgmzzvwnlzwtznwtqtdjcnhwrqhqrmvqqhrpdtnsmfrlcgpjcnddsqzcppgrnhvwsdbjvvtmvbjdncpdnmzfswmtvzfbdpqfjvwvqlhptnpdfdnlwfrgstpvvmhsqfgggdrsfgldfzbcjzhqzvfwmzccwjrslhjwlbmrpqgzdfnfbhsmdpzwtqnqldtqvshvlvmlnnmqrqbpwvnhqhtcbfclhrcqlqzhsqplsnbczvrbzqwlfwjdtmstzdbswtrvlpzzlrfvgdmldbwcttztrvsgzjwhhpcrvtgzfzppdlrdwswbnjfqqpqfbcqlzdmjsgjtzmvhdzspwlqpdjnccmbtdhnnhfvwqclbzzgglfgmvvgrccdsbwfmpvqwqrhmdzfhhhgbgjgwmnzmnggfrpspchvzpcmcpsbzgldmgqjqqdcjpwwncwrwjbhgzdbbcmbzbbtvprsjrhfwgsppdrrlzvnmtmwrmmrhtlndvsvjvgqmmttbbnpdhnjhwgrvlrdtpbrtwpwvvpslcqnvnrlhpvgdwnrzjmhwmgvpndtrjlzqpfzfbrsgbzjjqcfgsfwchblzstdflblngtzbrzrrvsczqvfhjjdlffrghgqvqfdtstqlnzllsrnnrtvrzdphbhdfpmhlfncqbdtzjqqcfbzpvgzdcsvvbfdvqrfrncbrwmpdmhnlqdscwnvldzblpzfqcvnbzmmtbmwjbczsjvzmfthfpvjcpwftqcbgjwflfrbrggwnvwndtncljfrdfwqwhfbctpjghfvnjnntnrgbfbmhplgmpfvmgvfqjslgnnrnlgztlstpcjwtlhmwlljcfmptfwsphnlsrjwmgtghgqmsvwvqsmblwpdftbrwjcdlzjmjblghszznqhsnqrcmtccgdwrrlsmwswvrjltqmwsdwvpnzltllhrsdvmrntdhtwwbgrqmrffnqbqrczvzchbgmzwtjtfzwntsnlbwbgrlvqjsqmdnwjqlwrdpnfpggzrjvtrhqdbmmbtfmmblgwtrqccqbjnljqflhgtphvrgrgghgrpbgfgdztsmfwrfflsqmrwbfjwsmpfrnbqjwnwdqwcwzpwbsmngjwfmbwdmnprdjnjbmqgfcbvtcvcthpmnmvvzdzgqqbhtjqfcdvhfzwqgfsbtvnwbzpmmtswfntjjppsswgbfrjbrstltdgbmclmbfvlslghbhnqqbdlzgtctgsfnwvbpzbvnwfbjmbfqcpqqvgrzwcbrwzdbdsjsslcjlmtprntpsdmqldzwqlqztwqtqfqzmrnzbtpqlfnsdwfdgggfvmqmrdqmnffnzcwfzsrqfpvrmsfsrbnpbhnqvdglvglllpggpmwmngrhzwgpdlzrbsvjtqmshhnlzwwftdtqwrqwgbbnczqcwmsvcljqlscftwflhwwhgnqwztfchdzsllrzbhbqwcfztjnqtdmsfnlzlcwzfmtlcgwclzfhhldgrnfjvzthzqzmzvwcrnhpdcwswpddsbwtznwlcwsnfqnqwnntngplwnfgwrcnpvgffwrcrszzdbfvzjmrmlrjwcvdvbglgncjwcnnpdfnwsrzsvzgnjrlqmwhvtdgmpbqmjthmhhmzjhpvnbvrqnlspdbcgshwlnvwpvrbcmvbvcsdmgwtmsthqtcfmllsfwvqcrbmdgqtzjwrg")
(defun first-unique-substring (s num)
(loop for i from 0 to (- (length s) num)
for end = (+ num i)
when (not (has-duplicate-character (subseq s i end)))
return end))
(defun start-of-packet (s)
(first-unique-substring s 4))
(defun start-of-message (s)
(first-unique-substring s 14))
(print (start-of-packet input))
(print (start-of-message input))
| 4,549 | Common Lisp | .lisp | 14 | 322.642857 | 4,112 | 0.973057 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 211e5466414ff08e1e4eceb3ea44f43a4153880b7dab544623489f975ee6e785 | 42,139 | [
-1
] |
42,140 | twentytwo.lisp | drvdputt_aoc-2022/src/twentytwo.lisp | (load "file.lisp")
(load "num.lisp")
(defparameter lines (read-default-input "twentytwo"))
(defparameter lines-e (read-default-input "twentytwo-example"))
(defun parse-map (lines)
"Read map into char array from input lines."
(let* ((h (- (length lines) 2))
(w (apply #'max (mapcar #'length (subseq lines 0 (1+ h)))))
(a (make-array (list h w) :element-type 'character :initial-element #\ )))
(loop for l in lines
for i from 0 below h
do (loop for c across l
for j from 0
do (setf (aref a i j) c)))
;; (pprint-char-array a)
a))
(defun parse-instructions (lines)
"Get lists :moves and :turns
There is always one more move than turns.
These should be executed as move turn move turn move"
(let* ((l (car (last lines)))
(numbers (find-all-int l))
(letters (loop for c across l
if (or (char= c #\L) (char= c #\R))
collect c)))
(list :moves numbers
:turns letters)))
(defstruct wrapmap
char-a ;; the map data
;; start and end of wrap-around for each row
row-start-v
;; end is exclusive! (easier for modulo etc)
row-end-v
;; start and end of wrap-around for each column
col-start-v
col-end-v)
(defun wrapmap/init (a)
(let* ((nrow (array-dimension a 0))
(ncol (array-dimension a 1))
(wm (make-wrapmap :char-a a
:row-start-v (make-array nrow :element-type 'integer)
:row-end-v (make-array nrow :element-type 'integer)
:col-start-v (make-array ncol :element-type 'integer)
:col-end-v (make-array ncol :element-type 'integer))))
(loop for i from 0 below nrow
with start = nil
with end = nil
do (loop for j from 0 below ncol
for space-p = (char= (aref a i j) #\ )
with previous-inside-p = nil
;; if outside, and character is not space, we have now entered!
if (and (not previous-inside-p) (not space-p))
do (setq previous-inside-p t
start j)
;; if inside, and we encounter a space, we are outside again, so we can return
if (and previous-inside-p space-p)
do (setq end j)
and return nil
;; if no more spaces encountered
finally (setq end ncol))
do (setf (aref (wrapmap-row-start-v wm) i) start
(aref (wrapmap-row-end-v wm) i) end))
(loop for j from 0 below ncol
with start = nil
with end = nil
do (loop for i from 0 below nrow
for space-p = (char= (aref a i j) #\ )
with previous-inside-p = nil
;; if outside, and character is not space, we have now entered!
if (and (not previous-inside-p) (not space-p))
do (setq previous-inside-p t
start i)
;; if inside, and we encounter a space, we are outside again, so we can return
if (and previous-inside-p space-p)
do (setq end i)
and return nil
;; if no more spaces encountered
finally (setq end nrow))
do (setf (aref (wrapmap-col-start-v wm) j) start
(aref (wrapmap-col-end-v wm) j) end))
wm))
(defun wrap-generic (x start end)
"Force x to be within [start, end[ by wrapping."
(+ start (mod (- x start)
(- end start))))
(defun wrapmap/wrap-i (wm i j)
(let ((start (elt (wrapmap-col-start-v wm) j))
(end (elt (wrapmap-col-end-v wm) j)))
(wrap-generic i start end)))
(defun wrapmap/wrap-j (wm i j)
(let ((start (elt (wrapmap-row-start-v wm) i))
(end (elt (wrapmap-row-end-v wm) i)))
(wrap-generic j start end)))
(defun wrapmap/get-value-wrapped (wm i j direction)
"Wrap the given the given position if necessary, and return the map value."
(let* ((horizontal (or (= direction 0)
(= direction 2)))
(wi (if horizontal
i
(wrapmap/wrap-i wm i j)))
(wj (if horizontal
(wrapmap/wrap-j wm i j)
j)))
(aref (wrapmap-char-a wm) wi wj)))
;; direction is magic number 0, 1, 2, or 3 (with right = 0, and clockwise increase)
;; (defun do-turn (direction turn)
;; (mod (cond ((char= turn #\R)
;; (+ direction 1))
;; ((char= turn #\L)
;; (- direction 1)))
;; 4))
(defun do-move (wm i j direction distance)
"a is the map. I and j indicate position (row and column). Direction is magic number 0, 1, 2,
3 for r, d, l, u. Distance is int."
(let ((horizontal (or (= direction 0) (= direction 2)))
;; direction 0 (right) and 1 (down) have delta + 1
;; the other two - 1
(delta (if (< direction 2) 1 -1))
(a (wrapmap-char-a wm)))
(if horizontal
(loop for counter from 0 below distance
with j-cur = j
for j-next = (wrapmap/wrap-j wm i (+ j-cur delta))
do (print (list "moving j" j-cur j-next))
if (char= (aref a i j-next) #\#)
return (cons i j-cur)
else
do (setq j-cur j-next)
finally (return (cons i j-cur)))
;; else vertical
(loop for counter from 0 below distance
with i-cur = i
for i-next = (wrapmap/wrap-i wm (+ i-cur delta) j)
;; check for obstruction
if (char= (aref a i-next j) #\#)
return (cons i-cur j)
else
do (setq i-cur i-next)
finally (return (cons i-cur j))))))
(defun do-puzzle (lines)
(let* ((wm (wrapmap/init (parse-map lines)))
(i 0)
(j (aref (wrapmap-row-start-v wm) i))
(ij (cons i j))
(d 0)
(instructions (parse-instructions lines)))
(print wm)
;; do the first move
(print ij)
(setq ij (do-move wm (car ij) (cdr ij) d (first (getf instructions :moves))))
(print ij)
;; then (turn, move), (turn, move), ...
(loop for turn in (getf instructions :turns)
for move in (cdr (getf instructions :moves))
;; apply turn
do (setq d (do-turn d turn))
;; apply move
do (setq ij (do-move wm (car ij) (cdr ij) d move))
do (print (list turn move ij)))
(+ (* 1000 (1+ (car ij)))
(* 4 (1+ (cdr ij)))
d)))
(defun which-face (i j N)
"return which cube face we're in, depending on the coordinates. Only works for the big input
(it has a different unfolding layout than the example...
The returned numbers correspond to my drawing on paper.
Returns -1 if off the 2d map (useful for detecting if we have to change face."
(let ((ii (floor i N))
(jj (floor j N)))
(cond ((and (= ii 0) (= jj 1))
1)
((and (= ii 0) (= jj 2))
2)
((and (= ii 1) (= jj 1))
3)
((and (= ii 2) (= jj 0))
4)
((and (= ii 2) (= jj 1))
5)
((and (= ii 3) (= jj 0))
6)
(t
-1))))
(defun face-offset (f)
"sort of inverse function. Returns the top left corner of each face."
(cdr (assoc f '((1 . (0 . 1))
(2 . (0 . 2))
(3 . (1 . 1))
(4 . (2 . 0))
(5 . (2 . 1))
(6 . (3 . 0))))))
;; (defparameter cube-face-transform-type
;; ;; maps pairs of cubes to a label (function maybe?) indicating the type of coordinate
;; ;; transformation
;; (list '(1 2) 'horizontal
;; (cons '(1 3) 'vertical)
;; (cons '(
;; how all 4 sides of each face relate to another face, with a certain rotation given. Rotation:
;; 0 = plain, 1 = 90 anticlockwise, 2 upside-down, 3 270 anticlockwise
(defun cube-face-relation (f)
(cdr (assoc f
'((1 . (:r (2 . 0)
:d (3 . 0)
:l (4 . 2)
:u (6 . 1)))
(2 . (:r (5 . 2)
:d (3 . 1)
:l (1 . 0)
:u (6 . 0)))
(3 . (:r (2 . 3)
:d (5 . 0)
:l (4 . 3)
:u (1 . 0)))
(4 . (:r (5 . 0)
:d (6 . 0)
:l (1 . 2)
:u (3 . 1)))
(5 . (:r (2 . 2)
:d (6 . 1)
:l (4 . 0)
:u (3 . 0)))
(6 . (:r (5 . 3)
:d (2 . 0)
:l (1 . 3)
:u (4 . 0)))))))
(defun direction-legend (d)
(cdr (assoc d '((0 . :r)
(1 . :d)
(2 . :l)
(3 . :u)))))
(defun neighboring-face-and-rotation (f d)
"f = face integer
d = direction in which to roll over (integer again)
Returns (next-face . num-rotations)"
(getf (cube-face-relation f) (direction-legend d)))
(defun transform-point-to-rotated-face (i j N times)
"times = multiple of 90
Rotation matrix won't work because we want to rotate around center.
Straightforward on paper though.
Rotating the coordinate axes 90 anti-clockwise (= rotating the face of the cube while the
point stays the same), yields:
x' = N - y
y' = x
or i-new = j
j-new = N - 1 - i
"
(let ((i-new i)
(j-new j))
(loop repeat times
for i-temp = i-new
for j-temp = j-new
do (setq i-new j-temp
j-new (- N 1 i-temp)))
(cons i-new j-new)))
(defun transform-direction-to-rotated-face (d times)
(mod (+ d times) 4))
(defun step-on-cube (f i j d N)
"Return coordinates after making one step in the given direction.
(works with cube coordinates (f i j), with f referring to the current face. For every face,
the up direction (i = 0) is the same as in the 2d map. So every time we step over the
border of a face, we rotate i j, and update f."
(let* ((horizontal (or (= d 0) (= d 2)))
(delta (if (< d 2) 1 -1))
(next-i (if horizontal i (+ i delta)))
(next-j (if horizontal (+ j delta) j))
(next-f f)
(next-d d)
(change-f (or (< next-i 0)
(>= next-i N)
(< next-j 0)
(>= next-j N))))
(when change-f
;; we change face --> figure out which face is next and what transformation to apply
(let* ((face-and-rot (neighboring-face-and-rotation f d)))
;; change face and orientation
(setq next-f (car face-and-rot)
next-d (transform-direction-to-rotated-face d (cdr face-and-rot)))
;; roll over the coordinates
(setq next-i (mod next-i N)
next-j (mod next-j N))
(print (list "before rot" (list next-i next-j)))
(let ((rot-ij (transform-point-to-rotated-face next-i next-j N (cdr face-and-rot))))
(setq next-i (car rot-ij)
next-j (cdr rot-ij)))))
(list next-f next-i next-j next-d)))
(defun cube-to-2d (f i j N)
(let ((offset (face-offset f)))
(cons (+ i (* N (car offset)))
(+ j (* N (cdr offset))))))
(defun do-cube-move (a f i j N direction distance)
"Step one by one, along a direction, checking for obstructions by converting to 2d coords at
every step"
(loop for counter from 0 below distance
with f-cur = f
with i-cur = i
with j-cur = j
with d-cur = direction
for fijd-next = (step-on-cube f-cur i-cur j-cur d-cur N)
for f-next = (first fijd-next)
for i-next = (second fijd-next)
for j-next = (third fijd-next)
for d-next = (fourth fijd-next)
for ij-next-2d = (cube-to-2d f-next i-next j-next N)
do (print (list "moving-fijd" fijd-next ij-next-2d))
;; check for obstruction on the 2d map
if (char= (aref a (car ij-next-2d) (cdr ij-next-2d)) #\#)
return (list f-cur i-cur j-cur d-cur)
else
;; visualize path
do (setf (aref a (car ij-next-2d) (cdr ij-next-2d))
(cdr (assoc d-cur '((0 . #\>)
(1 . #\v)
(2 . #\<)
(3 . #\^)))))
and do (setq f-cur f-next
i-cur i-next
j-cur j-next
d-cur d-next)
finally (return (list f-cur i-cur j-cur d-cur))))
(defun do-part2 ()
(let* ((f 1)
(i 0)
(j 0)
(d 0)
(fijd (list f i j d))
(N 50)
(a (parse-map lines))
(instructions (parse-instructions lines)))
;; start at f = 1, i = 0, j = 0 (top left corner of face 1, maps to 0, 50 on the 2D map)
;; check if the conversion works fine
(print (cube-to-2d f i j N))
;; do the first move
(print fijd)
(setq fijd (do-cube-move a (first fijd) (second fijd) (third fijd) N (fourth fijd) (first (getf instructions :moves))))
(print fijd)
;; then (turn, move), (turn, move), ...
(loop for turn in (getf instructions :turns)
for move in (cdr (getf instructions :moves))
do (setq fijd (do-cube-move a
(first fijd) (second fijd) (third fijd) N (do-turn (fourth fijd) turn)
move))
do (print (list turn move fijd)))
;; print out the path
(pprint-char-array a)
;; convert back to 2d, and get final score
(let ((ij-2d (cube-to-2d (first fijd) (second fijd) (third fijd) N)))
(+ (* 1000 (1+ (car ij-2d)))
(* 4 (1+ (cdr ij-2d)))
(fourth fijd)))))
| 14,137 | Common Lisp | .lisp | 349 | 29.687679 | 123 | 0.503205 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | a1ec47db86f3d87820f8e34d8011ccfc88789706a1c80dcbe1b1fecdf420a429 | 42,140 | [
-1
] |
42,141 | thirteen.lisp | drvdputt_aoc-2022/src/thirteen.lisp | (defun ensure-list (i)
(if (numberp i)
(list i)
i))
;; trinary: -1 in order, 1 out of order, 0 undecided
(defun compare-any (v1 v2)
(cond
;; both integer. this is the exit of the recursion
((and (numberp v1) (numberp v2))
(compare-int v1 v2))
;; at least one list --> recurse
(t
(comp-lists (ensure-list v1) (ensure-list v2)))))
(defun compare-int (v1 v2)
(cond ((< v1 v2) -1)
((= v1 v2) 0)
(t 1)))
(defun comp-lists (v1 v2)
(loop for x1 in v1
for x2 in v2
with c = nil
do (setq c (compare-any x1 x2))
if (not (= c 0))
return c
finally
(return (compare-int (length v1) (length v2)))))
(load "file.lisp")
(defvar lines (read-default-input "thirteen"))
(load "string.lisp")
;; first character needs to be [, and last character needs to be ].
;; this will also be recursive: if another [ is encountered, we do
;; parse-packet on this [ ... ] sub-sequence. The exit for this
;; recursion, is when there's no more [. Then we can just collect all
;; integers.
(defun parse-packet (l)
(let ((num-brackets (length (index-of-all-matches l "\\["))))
(cond
;; single integer
((= num-brackets 0)
(parse-integer l))
;; list of integers
((= num-brackets 1)
(find-all-int l))
;; nested, use recursion
(t
(loop for sub in (split-packet l)
collect (parse-packet sub))))))
(defun split-packet (l)
;; ignore first and last character ([ and ]), so we have only the
;; comma separated values
(let ((csv (subseq l 1 (- (length l) 1))))
(loop for c across csv
for i from 0
with fragment-start = 0
with bracket-balance = 0
;; if bracket balance > 0, we are inside a sublist and we need
;; to keep accumulating characters for this substring
if (char= c #\[)
do (incf bracket-balance)
if (char= c #\])
do (decf bracket-balance)
;; if bracket balance = 0 and we encounter a comma, then
;; we collect the substring and set up for the next one
if (and (= bracket-balance 0) (char= c #\,))
collect (subseq csv fragment-start i) into fragments
and do (setq fragment-start (+ 1 i))
finally
;; at the end, there's no comma anymore, so we need to add
;; this part separately
(return (append fragments (list (subseq csv fragment-start)))))))
(defun do-puzzle (lines)
(let ((num-pairs (/ (+ 1 (length lines)) 3)))
(loop for i from 1 to num-pairs
for j = (* 3 (- i 1))
for v1 = (parse-packet (nth j lines))
for v2 = (parse-packet (nth (+ 1 j) lines))
if (= -1 (compare-any v1 v2))
collect i into ivals
and sum i into isum
finally (return (list ivals isum)))))
(defun do-part2 (lines)
(let* ((dividers (list (list (list 2))
(list (list 6))))
(packets (concatenate 'list
dividers
(loop for l in lines
if (not (string= l ""))
collect (parse-packet l))))
(sorted-packets (sort packets (lambda (v1 v2)
(= -1 (compare-any v1 v2))))))
(print sorted-packets)
(loop for p in sorted-packets
for i from 1
if (= 0 (compare-any p (first dividers)))
do (print (list "first divider at" i))
if (= 0 (compare-any p (second dividers)))
do (print (list "second divider at" i)))))
(defvar lines-e (read-lines "thirteen-example-input.txt"))
| 3,344 | Common Lisp | .lisp | 98 | 29.438776 | 71 | 0.622477 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 0400cf04f435e0d835d571f6fc7fda7e772d95b53e99cf917581a607030b6f0d | 42,141 | [
-1
] |
42,142 | two.lisp | drvdputt_aoc-2022/src/two.lisp | #!/usr/bin/sbcl --script
(load "file.lisp")
(defvar lines (read-lines #p"two-input.txt"))
(defun score (line hands-function)
(let ((hands (funcall hands-function line)))
;; remember that hands is (opponent me)
(+ (choice-score (nth 1 hands))
(apply (function win-score) hands))))
(defun choice-score (hand)
(cond ((char= hand #\A) 1)
((char= hand #\B) 2)
((char= hand #\C) 3)))
(defun win-score (opponent me)
(cond ((draw opponent me) 3)
((win opponent me) 6)
(t 0)))
(defun draw (opponent me)
(char= opponent me))
(defun win (opponent me)
(or (and (char= opponent #\A) (char= me #\B))
(and (char= opponent #\B) (char= me #\C))
(and (char= opponent #\C) (char= me #\A))))
;; return a pair of ABC
(defun original-rps-hands (line)
(let ((opponent (elt line 0))
(me (elt line 2)))
(list opponent (cond ((char= me #\X) #\A)
((char= me #\Y) #\B)
((char= me #\Z) #\C)))))
(defun draw-hand (opponent)
(identity opponent))
(defun win-hand (opponent)
(cond ((char= opponent #\A) #\B)
((char= opponent #\B) #\C)
((char= opponent #\C) #\A)))
(defun lose-hand (opponent)
(cond ((char= opponent #\A) #\C)
((char= opponent #\B) #\A)
((char= opponent #\C) #\B)))
;; part two of the problem, with strategy that depends on other hand
(defun updated-rps-hands (line)
(let ((opponent (elt line 0))
(me (elt line 2)))
(list opponent
(cond ((char= me #\X) (lose-hand opponent))
((char= me #\Y) (draw-hand opponent))
((char= me #\Z) (win-hand opponent))))))
(print (loop for l in lines
sum (score l (function original-rps-hands))))
(print (loop for l in lines
sum (score l (function updated-rps-hands))))
;; (print (loop for l in lines
;; sum (score l)))
| 1,770 | Common Lisp | .lisp | 53 | 29.830189 | 68 | 0.609569 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 4be2a567171d38c8bcdd7c7841e2a4a1dae241b2ec73462ac4e81fcb57653522 | 42,142 | [
-1
] |
42,143 | three.lisp | drvdputt_aoc-2022/src/three.lisp | #!/usr/bin/sbcl --script
(load "file.lisp")
(load "string.lisp")
(defvar lines (read-lines #p"three-example-input.txt"))
(defvar halves-example (string-halves (nth 0 lines)))
(print (nth 0 halves-example))
(print (nth 1 halves-example))
(print (apply (function common-character) (string-halves (nth 0 lines))))
(defun priority (c)
(let ((code (char-code c)))
(cond
;; A should map to 27. char code of A is 65
((and (<= 65 code) (< code 97))
(+ (- code 64) 26))
;; a should map to 1. char code of a is 97.
((<= 97 code)
(- code 96)))))
(defun rucksack-common-item (line)
(apply (function common-character) (string-halves line)))
(defun three-elves-common-item (line1 line2 line3)
(common-character-multi (list line1 line2 line3)))
(print (priority #\A))
(print (priority #\z))
(print (loop for l in (read-lines #p"three-input.txt")
;; do (print (list l (string-halves l) (rucksack-common-item l)))
sum (priority (rucksack-common-item l))))
(print (common-character-multi (subseq lines 0 3)))
(defvar all-lines (read-lines #p"three-input.txt"))
(print (loop for (line1 line2 line3) on all-lines
for i from 0 to (length all-lines)
when (= 0 (mod i 3))
;; collect (three-elves-common-item line1 line2 line3)))
sum (priority (three-elves-common-item line1 line2 line3))))
| 1,364 | Common Lisp | .lisp | 33 | 37.454545 | 73 | 0.660606 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | d90d33a2e8733885ede2375b9b74664022e73324caebe9ed58fad18975c8529c | 42,143 | [
-1
] |
42,144 | dfss.lisp | drvdputt_aoc-2022/src/dfss.lisp | (defun dfss/recurse (cur-state
end-p
state-score-f
next-states-f)
"Depth first state search.
STATE can be anything. This state (and evolutions of this state) will be passed to the
functions below. The details depend on these functions.
END-P should take a STATE as an argument, and return t when the state is considered final. It
is recommended that the STATE tracks the depth in some way, and cuts off the calculation at a
certain depth.
STATE-SCORE-F should return the score of a STATE. Will be called once END-PREDICATE-F returns
t, and we start going back up in the recursion. Branches will be chosen to maximize the
score. Best state is passed all the way upward.
NEXT-STATES-F should return a list of STATE objects, each one typically has a different type
of evolution step applied to them. Try to make this function as restrictive as possible, so
that the number of possible paths does not explode. If no options are returned, the branch
also ends and the score is returned."
(if (funcall end-p cur-state)
;; If the current state is past the end, just return the score. This breaks off the
;; recursion.
;; (funcall state-score-f cur-state)
cur-state
;; Else, get the possible next states, and recursively continue the state evolution. Once
;; the end of the branch returns the score, it will be passed up and returned here. The
;; maximum score of all t he sub-branches is passed up.
(let ((next-states (funcall next-states-f cur-state)))
(if next-states
(loop for next-state in next-states
with best-state = nil
with best-score = nil
do (let* ((return-state (dfss/recurse next-state
end-p
state-score-f
next-states-f))
(return-score (funcall state-score-f return-state)))
(when (or (not best-score) (> return-score best-score))
(setq best-score return-score
best-state return-state)))
finally (return best-state))
;; if no next states were possible, end the calculation by returning the current
;; state
cur-state))))
| 2,486 | Common Lisp | .lisp | 42 | 44.880952 | 96 | 0.605166 | drvdputt/aoc-2022 | 0 | 0 | 0 | GPL-3.0 | 9/19/2024, 11:51:06 AM (Europe/Amsterdam) | 665e47b11aa541a4c5fe0c14e7a69bd05b5a990b62c91f8d9c24d6bba5b8a19d | 42,144 | [
-1
] |