Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4919

Teaching and learning resources • Re: Advent of Code 2024

$
0
0
Day 24 Crossed Wires - only part1 for now.

Code:

;;; Advent of code 2024 - day24, part1 & part2, on BPI-F3 RISC-V;;; Chez code(load "utils.so")(define table (make-hashtable string-hash string=? 256))(define (make-function fs lw rw)  (cond   ((string=? fs "AND")    (letrec ((AND      (lambda ()(let ((l (hashtable-ref table lw #f))      (r (hashtable-ref table rw #f)))  (if (and (number? l) (number? r))      (logand l r) AND)))))      AND))    ((string=? fs "XOR")     (letrec ((XOR       (lambda () (let ((l (hashtable-ref table lw #f))       (r (hashtable-ref table rw #f)))   (if (and (number? l) (number? r))       (logxor l r) XOR)))))       XOR))    (else     (letrec ((OR       (lambda () (let ((l (hashtable-ref table lw #f))       (r (hashtable-ref table rw #f)))   (if (and (number? l) (number? r))       (logor l r) OR)))))       OR))))

Code:

hrvoje@BPI-F3:~/Projects/advent-of-code/2024/day-24/Chez$ scheme day24.soChez Scheme Version 10.1.0Copyright 1984-2024 Cisco Systems, Inc.> (time (lambda () (day24 "input.txt")))63168299811048#<time-duration 0.013824233>
This was written very fast, just to test if it will work. The function "make-function" can be reduced to something much more elegant:

Code:

(define (make-function fs lw rw)  (let ((LF (case fs      (("AND") logand)      (("XOR") logxor)      (else logor))))    (letrec ((func      (lambda ()(let ((l (hashtable-ref table lw #f))      (r (hashtable-ref table rw #f)))  (if (and (number? l) (number? r))      (LF l r) func)))))      func)))

Statistics: Posted by hrvoje064 — Sat Jan 04, 2025 3:17 am



Viewing all articles
Browse latest Browse all 4919

Trending Articles