parent
39dde4ab6a
commit
adb0515460
@ -1,2 +1,12 @@ |
|||||||
# AdventOfCode-2023-functional |
# AdventOfCode-2023-functional |
||||||
|
|
||||||
|
Solutions to https://adventofcode.com/2023/ |
||||||
|
|
||||||
|
This is my first experience with Lisp, so the code is probably terrible. |
||||||
|
|
||||||
|
Requirements: Guile 3 in `/usr/bin`. |
||||||
|
|
||||||
|
In a folder for a specific day, |
||||||
|
|
||||||
|
* To run (assuming NuShell): `open sample.in | ./main.scm`; |
||||||
|
* To run (assuming bash): `./main.scm < sample.in`. |
||||||
|
@ -0,0 +1,61 @@ |
|||||||
|
#!/usr/bin/guile -s |
||||||
|
!# |
||||||
|
|
||||||
|
(use-modules (ice-9 rdelim)) |
||||||
|
|
||||||
|
(define (read-lines) |
||||||
|
(let loop ((line (read-line))) |
||||||
|
(if |
||||||
|
(eof-object? line) |
||||||
|
'() |
||||||
|
(cons line (loop (read-line)))))) |
||||||
|
|
||||||
|
(define (map list mapper) |
||||||
|
(if |
||||||
|
(null? list) |
||||||
|
'() |
||||||
|
(cons (mapper (car list)) (map (cdr list) mapper)))) |
||||||
|
|
||||||
|
(define (first-last-combine current rest) |
||||||
|
(if |
||||||
|
(null? current) |
||||||
|
rest |
||||||
|
(if |
||||||
|
(null? rest) |
||||||
|
current |
||||||
|
(cons (car current) (cdr rest))))) |
||||||
|
|
||||||
|
(define (first-last list predicate) |
||||||
|
(if |
||||||
|
(null? list) |
||||||
|
'() |
||||||
|
(first-last-combine |
||||||
|
(if |
||||||
|
(predicate (car list)) |
||||||
|
(cons (car list) (cons (car list) '())) |
||||||
|
'()) |
||||||
|
(first-last (cdr list) predicate)))) |
||||||
|
|
||||||
|
(define (sum numbers) |
||||||
|
(if |
||||||
|
(null? numbers) |
||||||
|
0 |
||||||
|
(+ (car numbers) (sum (cdr numbers))) |
||||||
|
)) |
||||||
|
|
||||||
|
(define (solve-line line) |
||||||
|
( |
||||||
|
#!(lambda (first-last-result) (sum (map first-last-result (lambda (char) (string->number (string char))))))!# |
||||||
|
(lambda (first-last-result) (string->number (list->string first-last-result))) |
||||||
|
(first-last |
||||||
|
(string->list line) |
||||||
|
(lambda (char) (if (char-numeric? char) #t #f))))) |
||||||
|
|
||||||
|
(define (solve-all lines) |
||||||
|
(if |
||||||
|
(null? lines) |
||||||
|
0 |
||||||
|
(+ (solve-line (car lines)) (solve-all (cdr lines))) |
||||||
|
)) |
||||||
|
|
||||||
|
(display (solve-all (read-lines))) |
@ -0,0 +1,4 @@ |
|||||||
|
1abc2 |
||||||
|
pqr3stu8vwx |
||||||
|
a1b2c3d4e5f |
||||||
|
treb7uchet |
Loading…
Reference in new issue