-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Lisp with Advent of Code 2023 🎄 #556
Conversation
Part 1: (load "/lib/lisp/core.lsp")
(def (contains? x lst)
(if (nil? lst) false
(if (eq? x (head lst)) true
(contains? c (tail lst)))))
(def (two-digits line) (do
(var digits
(filter
(fun (c)
(contains? c (chars "0123456789")))
(chars line)))
(str->num (str.join (list (first digits) (last digits)) ""))))
(print (reduce +
(map two-digits (lines (read (first args)))))) The Edit: I forgot about it but we already had it, it was just missing from the doc |
We can boot directly into the Lisp interpreter by removing the login and the shell in vga set palette "/ini/palettes/gruvbox-dark.csv"
vga set font "/ini/fonts/zap-light-8x16.psf"
# read "/ini/banner.txt"
# user login
# shell
lisp For the AoC we can also increase the memory from 32MB to 256MB:
|
I used a dictionary to solve both parts of the second day challenge in Ruby (https://github.com/vinc/advent-of-code/tree/master/ruby/2023/02) but we don't have that in MOROS Lisp yet so I will not complete today's challenge: (var line "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green")
(var games (str.split (second (str.split line ": ")) "; "))
(map (fun (game) (map words (str.split game ", "))) games) |
Typing One could also use it to load |
Part 1: (load "/lib/lisp/core.lsp")
(def (aoc-count line) (do
(var nums (str.split (second (str.split line ": ")) " | "))
(var win (reject empty? (words (nth nums 0))))
(var own (reject empty? (words (nth nums 1))))
(var n (len (intersection win own)))
(if (> n 0) (^ 2 (- n 1)) 0)))
(print (reduce + (map aoc-count (lines (read (first args)))))) I added |
Part 1: (load "/lib/lisp/core.lsp")
(var puzzle (dict "red" 12 "green" 13 "blue" 14))
(def (aoc-cube s)
(<= (str->num (first (words s))) (get puzzle (second (words s)))))
(def (aoc-cubes s)
(map aoc-cube (str.split s ", ")))
(def (aoc-game s) (do
(var game (str.split s ": "))
(var id (str->num (second (words (first game)))))
(var res (map aoc-cubes (str.split (second game) "; ")))
(if (contains? (reduce concat res) false) 0 id)))
(print (reduce +
(map aoc-game (lines (read (first args)))))) I added a Those functions can also work with lists and strings where I'll change the signature of |
A few examples: (get "Hello" 0) # => "H"
(get "Hello" 6) # => ()
(get (list 1 2 3) 0) # => 1
(get (list 1 2 3) 3) # => ()
(get (dict "a" 1 "b" 2 "c" 3) "a") # => 1
(get (dict "a" 1 "b" 2 "c" 3) "d") # => ()
(put "Heo" 2 "ll") # => "Hello"
(put (list 1 3) 1 2) # => (1 2 3)
(put (dict "a" 1 "b" 2) "c" 3) # => (dict "a" 1 "b" 2 "c" 3)
(push "Hell" "o") # => "Hello"
(push (list 1 2) 3) # => (1 2 3) |
Previously we were comparing the condition of
The macro
|
The previous commits are now allowing us to do this:
Instead of this:
|
With the latest commit we get this:
Instead of that:
Which is the default because |
Functions in a namespace like |
A new script will document the functions available:
|
This year again I'll do the easiest parts of the Advent of Code in Lisp to improve the language with practical examples.
\b
and\e
escape sequencesdict
type (implemented withBTreeMap
)empty?
function to test empty stringsreject
function as the inverse offilter
put
function to insert an element into adict
,list
, andstring
push
function to insert an element at the end of alist
orstring
host
function to resolve hostnames into IP addressessystem
function toshell
(aliased tosh
)nth
function toget
and make it work withdict
(load "/lib/lisp/core.lsp")
in REPL automaticallyif
andwhile