Quest 2: From Complex to Clarity

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 3 points 1 year ago

I was stuck for a while on part 2. I thought I was running into precision errors, so I re-implemented everything using exact integers, then I re-implemented it in python, but it turns out that I just had an off-by-1 error. It just so happened that my off-by-1 algorithm gave the correct solution to the sample input. Part 3 takes a while to compute.

Scheme/Guile

(use-modules (ice-9 rdelim))
(use-modules (ice-9 format))
(use-modules (srfi srfi-1))

(define (parse-file file-name)
  (let* ((p (open-input-file file-name))
        (equality-split (string-split (read-line p) #\=))
        (complex-str (list-ref equality-split 1))
        (complex-parts (map string->number (string-split
          (substring/read-only complex-str 1 (- (string-length complex-str) 1))
          #\,))))
    (+ (car complex-parts) (* 0+1i (list-ref complex-parts 1)))))
(define (special-div numerator divisor)
  (+
    (truncate/ (real-part numerator) (real-part divisor))
    (* 0+1i (truncate/ (imag-part numerator) (imag-part divisor)))))

(let* ((A (parse-file "notes/everybody_codes_e2025_q02_p1.txt")))
  (format #t "Input is ~a\n" A)
  (let loop ((R 0+0i) (i 3))
    (if (> i 0)
      (loop (+ (special-div (* R R) 10+10i) A) (- i 1))
      (format #t "P1 Answer: [~d,~a]\n\n"
              (inexact->exact (real-part R))
              (inexact->exact (imag-part R))))))


(define (check-engrave-range value)
  (and-map (lambda (x) (<= (abs x) 1000000)) (list (real-part value) (imag-part value))))
(define (check-engrave point)
  (let loop ((result 0+0i) (i 100))
    (if (and (> i 0) (check-engrave-range result))
      (loop (+ point (special-div (* result result) 100000+100000i)) (- i 1))
      (check-engrave-range result))))
(let* ((origin (parse-file "notes/everybody_codes_e2025_q02_p2.txt"))
       (engrave-count 0))
  (format #t "Input is ~a\n" origin)
  (let loop ((i 0))
    (if (<= i 1000) (begin
      (let loop ((j 0))
        (if (<= j 1000) (begin
          (set! engrave-count (+ engrave-count
                                 (if (check-engrave (+ origin (+ i (* j 0+1i)))) 1 0)))
          (loop (+ j 10)))))
      (loop (+ i 10)))))
  (format #t "P2 answer: ~a\n\n" engrave-count))


(let* ((origin (parse-file "notes/everybody_codes_e2025_q02_p3.txt"))
       (engrave-count 0))
  (format #t "Input is ~a\n" origin)
  (let loop ((i 0))
    (if (<= i 1000) (begin
      (let loop ((j 0))
        (if (<= j 1000) (begin
          (set! engrave-count
                (+ engrave-count (if (check-engrave (+ origin (+ i (* j 0+1i)))) 1 0)))
          (loop (+ j 1)))))
      (loop (+ i 1)))))
  (format #t "P3 answer: ~a\n\n" engrave-count))
  • source