clone1's recent activity

  1. Comment on Kyle Rittenhouse, Project Veritas, and the inability to think in terms of principles in ~news

    clone1
    Link Parent
    I don't think that you understand their point. The view that the people running the trial are pure evil and the view that the result of the trial is legally correct aren't contradictory.

    I don't think that you understand their point. The view that the people running the trial are pure evil and the view that the result of the trial is legally correct aren't contradictory.

    3 votes
  2. Comment on Microsoft Edge’s new ‘buy now, pay later’ feature is the definition of bloatware in ~tech

    clone1
    Link
    It doesn't seem fundamentally different than Firefox's built in pocket integrations. I guess a finance extension is more predatory than a read later extension.

    It doesn't seem fundamentally different than Firefox's built in pocket integrations. I guess a finance extension is more predatory than a read later extension.

    7 votes
  3. Comment on Element One - All of Matrix, WhatsApp, Signal and Telegram in one place in ~tech

    clone1
    Link Parent
    All methods of secure communication require that you trust the other party. Even if the person you're talking to is using signal they can just give their phone to the cia and let them have a look

    All methods of secure communication require that you trust the other party. Even if the person you're talking to is using signal they can just give their phone to the cia and let them have a look

    4 votes
  4. Comment on Can data die? Why one of the internet's oldest images lives on without its subject's consent in ~tech

    clone1
    Link Parent
    Yeah it looked cool and it was nice that they could sync the graph with the text, but it wasn't comfortable to read at all.

    Yeah it looked cool and it was nice that they could sync the graph with the text, but it wasn't comfortable to read at all.

    3 votes
  5. Comment on As of October 2021 what is in your opinion the best Reddit alternative and why? What are its best qualities? in ~tech

    clone1
    Link Parent
    How do you use so many servers effectively? On reddit I was in more subreddits than that but they all showed up on my front page. I can't imagine looking through 100s of discord servers

    How do you use so many servers effectively? On reddit I was in more subreddits than that but they all showed up on my front page. I can't imagine looking through 100s of discord servers

    4 votes
  6. Comment on Valve has been secretly building a Switch-like portable PC designed to run a large number of games on the Steam PC platform via Linux in ~games

    clone1
    Link Parent
    Don't you get matched with other people on the same platform for those games, though?

    Don't you get matched with other people on the same platform for those games, though?

    2 votes
  7. Comment on Valve has been secretly building a Switch-like portable PC designed to run a large number of games on the Steam PC platform via Linux in ~games

    clone1
    Link Parent
    I doubt people would want to play competitive PC fps games on a switch like device, though.

    I doubt people would want to play competitive PC fps games on a switch like device, though.

    4 votes
  8. Comment on Deepfake lips are coming to dubbed films in ~tech

    clone1
    Link Parent
    With that logic, can you really see Parasite if you don't know Korean? There are always going to be cultural references, figures of speech, tones and implications that don't translate. I feel like...

    With that logic, can you really see Parasite if you don't know Korean? There are always going to be cultural references, figures of speech, tones and implications that don't translate. I feel like "Korean" -> "English subtitles" is a bigger jump away from the creators original intent than "English subs" -> "English dubs" is.

    7 votes
  9. Comment on Urbit: A Personal Identity Server in ~tech

    clone1
    Link Parent
    I'm not sure if the logo is a sig rune. Rune seems to be what the two symbol pairs are called, not the symbols themselves. That could be a retcon though.

    I'm not sure if the logo is a sig rune. Rune seems to be what the two symbol pairs are called, not the symbols themselves. That could be a retcon though.

    3 votes
  10. Comment on Illinois rep. wants to ban ‘all violent video games’ to curb violence in ~games

  11. Comment on Anyone using a lightweight browser with Linux? in ~comp

    clone1
    Link Parent
    +1 for webkit. I'm using suckless surf on my pinebook pro and it works pretty well. I had to add -fsigned-char to the compile flags to get scroll up keybinds to work right on it, so OP might have...

    +1 for webkit. I'm using suckless surf on my pinebook pro and it works pretty well. I had to add -fsigned-char to the compile flags to get scroll up keybinds to work right on it, so OP might have to do that on their chrombook.

    2 votes
  12. Comment on Why is Apple's M1 chip so fast? in ~comp

    clone1
    Link Parent
    Apple's biggest strength really seems to be the ability to go "My way or the highway"

    Apple's biggest strength really seems to be the ability to go "My way or the highway"

    10 votes
  13. Comment on Day 11: Seating System in ~comp

    clone1
    Link
    Mit scheme. Went with vectors today. Part 1 & 2 (define (get-lines parse-line f) (with-input-from-file f (lambda () (let loop ((line (read-line)) (lines '())) (if (eof-object? line) (reverse...

    Mit scheme. Went with vectors today.

    Part 1 & 2
    (define (get-lines parse-line f)
      (with-input-from-file f
        (lambda ()
          (let loop ((line (read-line))
                     (lines '()))
            (if (eof-object? line)
                (reverse lines)
                (loop (read-line)
                      (cons (parse-line line) lines)))))))
    
    (define (2d-vector-ref vec x y)
      (vector-ref (vector-ref vec x) y))
    
    (define (vector-map-with-index proc vect)
      (let* ((length (vector-length vect))
             (new-vect (make-vector length)))
        (for-each (lambda (i)
                    (vector-set! new-vect
                                 i
                                 (proc i (vector-ref vect i))))
                  (iota length))
        new-vect))
    
    (define (board seats count-nearby limit)
      (vector-map-with-index
       (lambda (y row)
         (vector-map-with-index
          (lambda (x seat)
            (if (eq? seat #\.) #\.
                (let ((nearby (count-nearby seats y x)))
                  (cond ((= nearby 0) #\#)
                        ((>= nearby limit) #\L)
                        (else seat)))))
          row))
       seats))
    
    (define (count-filled seats)
      (count (lambda (seat) (eq? seat #\#))
             (append-map (lambda (row) (vector->list row))
                         (vector->list seats))))
    
    (define (board-loop seats count-nearby seat-limit)
      (let loop ((seats seats))
        (let ((new-seats (board seats count-nearby seat-limit)))
          (if (equal? seats new-seats) (count-filled new-seats)
              (loop new-seats)))))
    
    (define (make-in-range seats)
      (let ((h (vector-length seats))
            (w (vector-length (vector-first seats))))
        (lambda (row col)
          (and (<= 0 row (- h 1))
               (<= 0 col (- w 1))))))
    
    (define directions '((-1 . -1) (0 . -1) (1 . -1) (-1 . 0) (1 . 0) (-1 . 1) (0 . 1) (1 . 1)))
    
    (define (one seats)
      (define (count-nearby seats row col)
        (let ((in-range? (make-in-range seats)))
          (define (adjacent-seats)
            (filter-map (lambda (direction)
                          (let ((row (+ row (car direction)))
                                (col (+ col (cdr direction))))
                            (and (in-range? row col)
                                 (2d-vector-ref seats row col))))
                        directions))
          (count (lambda (seat) (eq? seat #\#)) (adjacent-seats))))
      (board-loop seats count-nearby 4))
    
    (define (two seats)
      (define (count-nearby seats row col)
        (let ((in-range? (make-in-range seats)))
          (define (seat-in-direction? direction)
            (let ((drow (car direction))
                  (dcol (cdr direction)))
              (let loop ((row (+ row drow))
                         (col (+ col dcol)))
                (if (not (in-range? row col)) #f
                    (let ((seat (2d-vector-ref seats row col)))
                      (case seat
                        ((#\L) #f)
                        ((#\#) #t)
                        (else (loop (+ row drow) (+ col dcol)))))))))
          (count seat-in-direction? directions)))
      (board-loop seats count-nearby 5))
    
    (define (run lines)
      (display "One: ") (display (one lines)) (newline)
      (display "Two: ") (display (two lines)) (newline))
    
    (run (list->vector (get-lines string->vector "input")))
    
    3 votes
  14. Comment on Day 10: Adapter Array in ~comp

    clone1
    Link
    Mit scheme. Part 1 & 2 For part two I started out with a recursive memoized approach but then after messing with it I realized only that last three values could matter, so I keep a list of three...

    Mit scheme.

    Part 1 & 2 For part two I started out with a recursive memoized approach but then after messing with it I realized only that last three values could matter, so I keep a list of three values corresponding to the adapters (-3, -2, -1) relative of the current adapter. I shift it at the beginning of each iteration to correct it if the difference between the current and previous adapter is greater than one, and at the end by one.
    (define (get-lines parse-line f)
      (with-input-from-file f
        (lambda ()
          (let loop ((line (read-line))
                     (lines '()))
            (if (eof-object? line)
                (reverse lines)
                (loop (read-line)
                      (cons (parse-line line) lines)))))))
    
    (define (one adapters)
      (let loop ((3-count 1)
                 (1-count 0)
                 (prev 0)
                 (remaining adapters))
        (if (null? remaining) (* 3-count 1-count)
            (let* ((x (car remaining))
                   (remaining (cdr remaining))
                   (diff (- x prev)))
              (case diff
                ((1) (loop 3-count (+ 1-count 1) x remaining))
                ((3) (loop (+ 3-count 1) 1-count x remaining))
                (else (loop 3-count 1-count x remaining)))))))
    
    (define (cdr-n l n)
      (if (= n 0) l
          (cdr-n (cdr l) (- n 1))))
    
    (define (sum lst)
      (fold + 0 lst))
    
    (define (two adapters)
      (let loop ((adapters adapters)
                 ;; List of the amount of paths for the last 3 adapters
                 (prev-list '(0 0 1))
                 (prev-val 0))
        (if (null? adapters)
            ;; The plug is 3 away so the answer is the last item in the list
            (caddr prev-list)
            (let* ((x (car adapters))
                   (shift-val (- x prev-val 1))
                   (prev-list (cdr-n prev-list shift-val))
                   (x-val (sum prev-list)))
              (loop (cdr adapters)
                    (append (cdr prev-list)
                            (iota shift-val 0 0)
                            (list x-val))
                    x)))))
    
    (define (run lines)
      (let ((lines (sort lines <)))
        (display "One: ") (display (one lines)) (newline)
        (display "Two: ") (display (two lines)) (newline)))
    
    (run (get-lines string->number "input"))
    
    4 votes
  15. Comment on Day 9: Encoding Error in ~comp

    clone1
    (edited )
    Link
    MIT Scheme. Could have made part one a lot more efficient but decided to just use my two sum function. Part 1 & 2 (define (get-lines parse-line file) (with-input-from-file file (lambda () (let...

    MIT Scheme. Could have made part one a lot more efficient but decided to just use my two sum function.

    Part 1 & 2
    (define (get-lines parse-line file)
      (with-input-from-file file
        (lambda ()
          (let loop ((line (read-line))
                     (lines '()))
            (if (eof-object? line)
                (reverse lines)
                (loop (read-line)
                      (cons (parse-line line) lines)))))))
    
    (define (2sum target numbers)
      (let ((passed (make-weak-eq-hash-table)))
        (let loop ((numbers numbers))
          (if (null? numbers) #f
              (let* ((x (car numbers))
                     (complement (- target x)))
                (if (and (>= complement 0)
                         (hash-table/get passed complement #f))
                    (cons x complement)
                    (begin
                      (hash-table/put! passed x #t)
                      (loop (cdr numbers)))))))))
    
    (define (with-index iter proc list)
      (iter (lambda (pair) (proc (car pair) (cdr pair)))
            (map cons
                 (iota (length list))
                 list)))
    
    (define (one nums pre-size)
      (cdr (with-index find (lambda (i x)
                              (let ((res (2sum x (sublist nums
                                                          i
                                                          (+ i pre-size)))))
                                (and (not res)
                                     x)))
                       (drop nums pre-size))))
    
    (define (two target nums)
      (let loop ((nums nums)
                 (seq '())
                 (sum 0))
        (cond ((= sum target) (+ (apply min seq)
                                 (apply max seq)))
              ((> sum target) (loop nums
                                    (cdr seq)
                                    (- sum (car seq))))
              (else (loop (cdr nums)
                          (append seq (list (car nums)))
                          (+ sum (car nums)))))))
    
    (define (run lines)
      (let ((one-res (one lines 25)))
        (display "One: ") (display one-res) (newline)
        (display "Two: ")  (display (two one-res lines)) (newline)))
    
    (run (get-lines string->number "input"))
    
    Part 1 alt Re-implemented part one to be 3 times faster and 9 times uglier.
    (define (one-f nums prev-size)
      (let ((prev (make-weak-eq-hash-table)))
        (for-each (lambda (x) (hash-table-set! prev x #t))
                  (take nums prev-size))
        ;; Pairs of the current number to check and the next number to remove from the prev num hash
        ;; We're doing it this way because I couldn't think of how to make an efficient queue
        (let* ((x-remove-pairs (map cons
                                    (drop nums prev-size)
                                    (take nums (- (length nums)
                                                  prev-size)))))
          (car
           (find (lambda (x-rem-p)
                   (let ((x (car x-rem-p))
                         (rem (cdr x-rem-p)))
                     (or (not (find (lambda (y) (hash-table-ref/default prev (- x y) #f))
                                    (hash-table-keys prev)))
                         (begin (hash-table-delete! prev rem)
                                (hash-table-set! prev x #t)
                                #f))))
                 x-remove-pairs)))))
    
    2 votes
  16. Comment on Day 8: Handheld Halting in ~comp

    clone1
    Link
    Mit scheme Part 1 & 2 (define (get-lines parse-line file) (with-input-from-file file (lambda () (let loop ((line (read-line)) (lines '())) (if (eof-object? line) (reverse lines) (loop (read-line)...

    Mit scheme

    Part 1 & 2
    (define (get-lines parse-line file)
      (with-input-from-file file
        (lambda ()
          (let loop ((line (read-line))
                     (lines '()))
            (if (eof-object? line)
                (reverse lines)
                (loop (read-line)
                      (cons (parse-line line) lines)))))))
    
    (define (make-ins op val) (cons op val))
    (define (get-op ins) (car ins))
    (define (get-val ins) (cdr ins))
    
    (define (parse-ins line)
      (let* ((split ((string-splitter) line))
             (op (string->symbol (car split)))
             (val (string->number (cadr split))))
        (make-ins op val)))
    
    (define (execute ins-lst)
      (let loop ((acc 0)
                 (counter 0)
                 (visited '()))
        (cond ((member counter visited) (cons 'loop acc))
              ((= counter (length ins-lst)) (cons 'ok acc))
              (else (let* ((ins (list-ref ins-lst counter))
                           (op (get-op ins))
                           (val (get-val ins)))
                      (case op
                        ((nop) (loop acc
                                     (+ counter 1)
                                     (cons counter visited)))
                        ((acc) (loop (+ acc val)
                                     (+ counter 1)
                                     (cons counter visited)))
                        ((jmp) (loop acc
                                     (+ counter val)
                                     (cons counter visited)))))))))
    
    (define (one ins-lst)
      (cdr (execute ins-lst)))
    
    (define (flip-ins ins)
      (let ((op (get-op ins))
            (val (get-val ins)))
        (make-ins (if (eq? op 'jmp) 'nop 'jmp)
                  val)))
    
    (define (replace-n lst val n)
      (cond ((null? lst) '())
            ((= n 0) (cons val (cdr lst)))
            (else (cons (car lst)
                        (replace-n (cdr lst)
                                   val
                                   (- n 1))))))
    
    (define (two ins-lst)
      (let loop ((ins-change-pairs
                  (filter-map (lambda (i ins)
                                (and (or (eq? (get-op ins) 'jmp)
                                         (eq? (get-op ins) 'nop))
                                     (cons i (flip-ins ins))))
                              (iota (length ins-lst))
                              ins-lst)))
        (let* ((rep-index (caar ins-change-pairs))
               (rep-ins (cdar ins-change-pairs))
               (ex-res (execute (replace-n ins-lst rep-ins rep-index))))
          (if (eq? (car ex-res) 'ok)
              (cdr ex-res)
              (loop (cdr ins-change-pairs))))))
    
    (define (run lines)
        (display "One: ")
        (display (one lines))
        (newline)
        (display "Two: ")
        (display (two lines))
        (newline))
    
    (run (get-lines parse-ins "input"))
    
    3 votes
  17. Comment on Day 7: Handy Haversacks in ~comp

    clone1
    (edited )
    Link
    MIT scheme Part 1 & 2 Part one is very inefficient but it works. I guess the way to improve it would be to memoize or make an association of bags to their containing bags. (define (get-lines...

    MIT scheme

    Part 1 & 2 Part one is very inefficient but it works. I guess the way to improve it would be to memoize or make an association of bags to their containing bags.
    (define (get-lines parse-line)
      (with-input-from-file "input"
        (lambda ()
          (let loop ((line (read-line))
                     (lines '()))
            (if (eof-object? line)
                (reverse lines)
                (loop (read-line)
                      (cons (parse-line line) lines)))))))
    
    (define (try-match-exact pattern string)
      (regsexp-match-string (compile-regsexp
                             (append '(seq (line-start))
                                     pattern
                                     '((line-end))))
                            string))
    
    (define (bag-str->symbol str)
      (string->symbol (string-replace (string-downcase str)
                                      #\space
                                      #\-)))
    
    (define (parse-bag line)
      (let* ((bag-name '(seq (* (char-in alphabetic)) #\space (* (char-in alphabetic))))
             (matches (try-match-exact
                       `((group k ,bag-name)
                         #\space
                         "bag" (? #\s)
                         " contain"
                         (alt " no other bags."
                              (* (seq #\space (group n (* (char-in numeric)))
                                      #\space (group b ,bag-name)
                                      #\space "bag" (? #\s) (alt #\, #\.)))))
                       line)))
        (if (not matches) (begin (display "Parse failed: ") (display line) (newline))
            (let* ((groups (cddr matches))
                   (bag (bag-str->symbol (cdar groups)))
                   (inner (cdr groups)))
              (cons bag
                    (if (null? inner) '()
                        (map (lambda (n b)
                               (cons (string->number (cdr n))
                                     (bag-str->symbol (cdr b))))
                             (filter (lambda (x) (eq? (car x) 'n)) inner)
                             (filter (lambda (x) (eq? (car x) 'b)) inner))))))))
    
    (define (contains? alst bag target)
      (if (eq? bag target) #t
          (any (lambda (n-bag-pair) (contains? alst (cdr n-bag-pair) target))
               (cdr (assq bag alst)))))
    
    (define (one lines)
      (- (count (lambda (bag) (contains? lines (car bag) 'shiny-gold))
                lines)
         1))
    
    (define (sum lst)
      (fold + 0 lst))
    
    (define (bags-in alst bag)
      (let ((contents (cdr (assq bag alst))))
        (if (null? contents) 1
            (+ (sum (map (lambda (n-bag-pair)
                           (* (car n-bag-pair)
                              (bags-in alst (cdr n-bag-pair))))
                         contents))
               1))))
    
    (define (two lines)
      (- (bags-in lines 'shiny-gold)
         1))
    
    (define (run lines)
        (display "One: ")
        (display (one lines))
        (newline)
        (display "Two: ")
        (display (two lines))
        (newline))
    
    (run (get-lines parse-bag))
    
    Part 1 memoized Went ahead and memoized part one with a hash table and a closure. Went from .5sec to 0sec
    (define (make-memoized-contains? alst target)
      (let ((memo-table (make-strong-eq-hash-table)))
        (define (contains? bag)
          (if (eq? bag target) #t
              (let ((memo-val (hash-table/get memo-table bag '())))
                (if (not (null? memo-val)) memo-val
                    (let ((result (any (lambda (n-bag-pair) (contains? (cdr n-bag-pair)))
                                       (cdr (assq bag alst)))))
                      (hash-table/put! memo-table bag result)
                      result)))))
        contains?))
    
    (define (one lines)
      (let ((contains? (make-memoized-contains? lines 'shiny-gold))
            (bag-list (filter (lambda (bag) (not (eq? bag 'shiny-gold)))
                              (map car lines))))
        (count contains? bag-list)))
    
    2 votes
  18. Comment on Day 6: Custom Customs in ~comp

    clone1
    Link
    Posting python today because I like how it turned out. Part 1 & 2 def parse(x): return x.rstrip() def split(lines): res = [] lin = [] for line in lines: if line == "": res.append(lin) lin = []...

    Posting python today because I like how it turned out.

    Part 1 & 2
    def parse(x):
        return x.rstrip()
    
    def split(lines):
        res = []
        lin = []
        for line in lines:
            if line == "":
                res.append(lin)
                lin = []
            else:
                lin.append(set([x for x in line]))
        res.append(lin)
        return res
    
    def one(lines):
        return sum(map(lambda g: len(set.union(*g)), lines))
    
    def two(lines):
        return sum(map(lambda g: len(set.intersection(*g)), lines))
    
    
    f = open("input", "r")
    
    lines = [parse(x) for x in f]
    lines = split(lines)
    
    print("One: " + str(one(lines)))
    print("Two: " + str(two(lines)))
    
    6 votes
  19. Comment on Day 5: Binary Boarding in ~comp

    clone1
    Link
    MIT scheme solution. Part 1 & 2 Pretty simple if you can find the trick. I wonder if scheme has a better way to do part b. (define (get-lines parse-line) (with-input-from-file "input" (lambda ()...

    MIT scheme solution.

    Part 1 & 2 Pretty simple if you can find the trick. I wonder if scheme has a better way to do part b.
    (define (get-lines parse-line)
      (with-input-from-file "input"
        (lambda ()
          (let loop ((line (read-line))
                     (lines '()))
            (if (eof-object? line)
                (reverse lines)
                (loop (read-line)
                      (cons (parse-line line) lines)))))))
    
    (define (string-replace-all str pairs)
      (fold (lambda (pair str)
              (string-replace str (car pair) (cdr pair)))
            str pairs))
    
    (define (parse-seat-id str)
      (let* ((str (string-replace-all str
                                      '((#\B . #\1)
                                        (#\F . #\0)
                                        (#\R . #\1)
                                        (#\L . #\0))))
             (row (string->number (substring str 0 7)
                                  2))
             (col (string->number (substring str 7)
                                  2)))
        (+ (* row 8)
           col)))
    
    (define (one lines)
      (apply max lines))
    
    (define (two lines)
      (let loop ((ids (sort lines <))
                 (last-id '()))
        (if (null? ids) #f
            (let ((id (car ids))
                  (ids (cdr ids)))
              (if (or (null? last-id)
                      (= (- id last-id)
                         1))
                  (loop ids id)
                  (+ last-id 1))))))
    
    (define (run lines)
      (display "One: ")
      (display (one lines))
      (newline)
      (display "Two: ")
      (display (two lines))
      (newline))
    
    (run (get-lines parse-seat-id))
    
    4 votes
  20. Comment on Day 4: Passport Processing in ~comp

    clone1
    (edited )
    Link
    I "solved" part one with python in about 10 minutes... Then spent 30 minutes debugging to find that I was dropping the last passport during parsing... Writing my scheme solution was cathartic. I...

    I "solved" part one with python in about 10 minutes... Then spent 30 minutes debugging to find that I was dropping the last passport during parsing... Writing my scheme solution was cathartic. I really need to learn scheme regex because some of my validations (hgt) are pretty messy.

    Part 1 & 2
    (define (make-field key val)
      (cons key val))
    
    (define (make-field-from-str field-str)
      (let ((split-str ((string-splitter 'delimiter #\:) field-str)))
        (make-field (string->symbol (car split-str))
                    (cadr split-str))))
    
    (define get-key car)
    (define get-val cdr)
    
    (define (get-passports)
      (define (add-passport passports lines)
        (if (= (length lines) 0) passports
            (cons (map make-field-from-str
                       (append-map (string-splitter) lines))
                  passports)))
      (with-input-from-file "input"
        (lambda ()
          (let loop ((line (read-line))
                     (passport-lines '())
                     (passports '()))
            (cond ((eof-object? line)
                   (reverse (add-passport passports passport-lines)))
                  ((= (string-length line) 0)
                   (loop (read-line) '() (add-passport passports passport-lines)))
                  (else (loop (read-line)
                              (cons line passport-lines)
                              passports)))))))
    
    (define (has-field? passport key)
      (member key (map get-key passport)))
    
    (define (has-all-fields? passport)
      (let ((required '(byr ecl eyr hcl hgt iyr pid)))
        (equal? (sort (remove (lambda (key) (eq? key 'cid))
                              (map get-key passport))
                      symbol<?)
                required)))
    
    (define (field-valid? field)
      (define (string-between? min max str)
        (let ((num (string->number str)))
          (and num (<= min num max))))
      (let ((key (get-key field))
            (val (get-val field)))
        (cond ((eq? key 'byr) (string-between? 1920 2002 val))
              ((eq? key 'iyr) (string-between? 2010 2020 val))
              ((eq? key 'eyr) (string-between? 2020 2030 val))
              ;; Probably should learn how regex works in scheme
              ((eq? key 'hgt) (let ((split-i (string-find-first-index
                                              char-alphabetic? val)))
                                (if (not split-i) #f
                                    (let ((num (substring val 0 split-i))
                                          (unit (substring val split-i)))
                                      (cond ((string=? unit "cm")
                                             (string-between? 150 193 num))
                                            ((string=? unit "in")
                                             (string-between? 59 76 num))
                                            (else #f))))))
              ((eq? key 'hcl) (and (= (string-length val) 7)
                                   (eq? (string-ref val 0) #\#)
                                   (string-every (lambda (c) (or (char-numeric? c)
                                                            (char-set-member?
                                                             (string->char-set "abcdef")
                                                             c)))
                                                 (substring val 1))))
              ((eq? key 'ecl) (member val '("amb" "blu" "brn" "gry" "grn" "hzl" "oth")))
              ((eq? key 'pid) (and (string->number val)
                                   (= (string-length val) 9)))
              ((eq? key 'cid) #t)
              (else (display "Unknown field: ") (display key) (newline) #t))))
    
    (define (all-fields-valid? passport)
      (every field-valid? passport))
    
    (define (one lines)
      (count has-all-fields? lines))
    
    (define (two lines)
      (count (lambda (passport)
               (and (has-all-fields? passport)
                    (all-fields-valid? passport)))
             lines))
    
    (define (run lines)
      (display "One: ")
      (display (one lines))
      (newline)
      (display "Two: ")
      (display (two lines))
      (newline))
    
    (run (get-passports))
    

    Update:
    Scheme regex are weird. I think it's worse

    Regex changes
    (define (try-match-exact pattern string)
      (regsexp-match-string (compile-regsexp (append '(seq (line-start))
                                                     pattern
                                                     '((line-end))))
                            string))
    
    (define (get-group matches group)
      (cdr (find (lambda (x) (and (pair? x) (eq? (car x) group)))
                 matches)))
    
    (define (field-valid? field)
      (define (string-between? min max str)
        (let ((num (string->number str)))
          (and num (<= min num max))))
      (let ((key (get-key field))
            (val (get-val field)))
        (cond ((eq? key 'hgt) (let ((matches
                                     (try-match-exact
                                      '((group num (* (char-in numeric)))
                                        (group unit (* (char-in alphabetic))))
                                      val)))
                                (if (not matches) #f
                                    (let ((num (get-group matches 'num))
                                          (unit (get-group matches 'unit)))
                                      (cond ((string=? unit "cm")
                                             (string-between? 150 193 num))
                                            ((string=? unit "in")
                                             (string-between? 59 76 num))
                                            (else #f))))))
              ((eq? key 'hcl) (try-match-exact '(#\# (** 6 (char-in numeric "abcdef")))
                                               val))
              ((eq? key 'pid) (try-match-exact '((** 9 (char-in numeric)))
                                               val))
              (else (display "Unknown field: ") (display key) (newline) #t))))
    
    4 votes