Although I mentioned Scratch earlier, I actually have no interest in it. Maybe a bit more in Snap (mostly because Brian Harvey's connection to Snap).I solved days 8 through 14 from Advent of Code 2020 in Scratch on a Pi.
viewtopic.php?p=1784564#p1784564
The main difficulty I had was no local variables. Since function arguments are read-write it was possible to add dummy arguments in recursive blocks that need local variables.
Working with two dimensional arrays were also difficult in Scratch. One of the interesting things about Chapel is making the domain of an array a first-class object that can be modified during a run and which is separate from the array reference.
Scratch was fun in part because there were no paradigmatic ways to write the algorithms used in the puzzle solutions. Since no one uses Scratch for such things, I felt free to write in any style that led to a working solution.
With Chapel I keep feeling I'm just doing it wrong. Then I spend extra time trying to figure out how the language is supposed to be used. A nagging irritation right now is finding a paradigmatic way to express a byte-sized literal as a single ASCII character. The expression b"."[0] means the first character '.' of a sequence of one bytes. It seems clumsy compared to simply writing '.' as in C and C-derived languages.
When I ask for help with Chapel all Fido does is howl about how FidoBasic is better.
BTW: this mentioning Brian Harvey is because he is actually the guilty party who have started me on a Lisp (Scheme, Logo) journey.
I was a regular C programmer (read that as imperative programmer) some 30+ years ago when my son started learning Logo in school. I got interested in it, but something was obviously wrong. Okay, Logo had turtles (which my son loved), but the rest of what he was being taught looked like BASIC to me. Then I found Brian Harvey's UCB Logo and his 3 Logo books. I read those 3, and his Simply Scheme book after that, and there was no way back for me.
Now back to --- Day 17: Chronospatial Computer ---
Part1 of day17 is exactly the same as day24 part1 (crossed wires).
Solution is almost ridiculously easy using the same approach -- using all functions.
The only difference is that names of wires in day24 were strings so I had to use hashtables. Here all names (Opcodes, Operands, program) are numbers so I can use vectors.
The main advantage this approach gives me is a very elegant code. Elegant, as in almost no IFs. The whole program has only 2. One to stop the run, after the pointer is at the end of program, and one in "jnz" (opcode 3) instruction to check for zero in register A. There are no other decisions to be made in the whole program.
Code:
;;; Advent of code 2024 - day17, part1 on BPI-F3 RISC-V;;; Chez code --- Chronospatial Computer ---(load "utils.so")(define A #f)(define B #f)(define C #f)(define pointer 0)(define out "")(define operandv (make-vector 7))(define opcodev (make-vector 8))(define programv (make-vector 16))(define (init-operands o) (case o ((0 1 2 3) (vector-set! operandv o (lambda () o))) ((4) (vector-set! operandv o (lambda () A))) ((5) (vector-set! operandv o (lambda () B))) ((6) (vector-set! operandv o (lambda () C))) (else (error 'init-operands "illegal operand"))))(define (init-opcode opc) (case opc ((0) (vector-set! opcodev opc (lambda (o)(set! A (quotient A (expt 2 ((vector-ref operandv o)))))(jump 2))))......hrvoje@BPI-F3:~/Projects/advent-of-code/2024/day-17/Chez$ scheme day17.soChez Scheme Version 10.1.0Copyright 1984-2024 Cisco Systems, Inc.> (time (lambda () (day17 "input.txt")))"7,4,2,0,5,0,5,3,7"#<time-duration 0.000530926>
Statistics: Posted by hrvoje064 — Thu Jan 16, 2025 2:49 am