Skip to content

Instantly share code, notes, and snippets.

@aaronjeline
Created December 1, 2021 21:46
Show Gist options
  • Save aaronjeline/aa6e2eeab2deaa79d0be62de6fba308b to your computer and use it in GitHub Desktop.
Save aaronjeline/aa6e2eeab2deaa79d0be62de6fba308b to your computer and use it in GitHub Desktop.
Solution for day 1 of aoc
#lang racket
(require threading)
(module+ test
(require rackunit))
;; Parsing
(define src
(~>
(with-input-from-file "total_input"
(lambda () (port->lines (current-input-port))))
(map string->number _)
(list->vector)))
(struct
batch
(finished in-progress)
#:transparent)
(define (batch-update-finished b f)
(batch (f (batch-finished b))
(batch-in-progress b)))
(define (batch-update-in-progress b f)
(batch (batch-finished b)
(f (batch-in-progress b))))
(module+ test
(check-equal?
(batch-update-in-progress (fresh-batch)
(lambda (lst) (cons 1 lst)))
(batch '() (cons 1 '()))))
(define (fresh-batch)
(batch '() '()))
(define (split-list p lst)
(values
(filter p lst)
(filter-not p lst)))
(module+ test
(define-values (x y) (split-list even? '(1 2 3 4)))
(check-equal? x '(2 4))
(check-equal? y '(1 3))
(define-values (done not-done) (split-list finished? '((3 2 1) (2 1))))
(check-equal? done '((3 2 1)))
(check-equal? not-done '((2 1))))
(define WINDOW 3)
(define (update b n)
(~>
(mark-finished b)
(add-new _ n)))
(define (add-new b n)
(batch-update-in-progress b
(lambda (in-progress)
(cons (list n)
(map (lambda (lst) (cons n lst)) in-progress)))))
(module+ test
(check-equal?
(add-new (fresh-batch) 1)
(batch '() '((1))))
(define b (batch '() '((2 1) (2))))
(check-equal?
(add-new b 3)
(batch '() '((3) (3 2 1) (3 2)))))
(define (mark-finished b)
(define-values (newly-finished still-in-progress)
(split-list finished? (batch-in-progress b)))
(batch
(append (map sum newly-finished) (batch-finished b))
still-in-progress))
(module+ test
(define b1 (batch '() '((3 2 1) (5 6))))
(check-equal?
(mark-finished b1)
(batch '(6) '((5 6)))))
(define (sum lst) (apply + lst))
(define (finished? lst)
(= (length lst) WINDOW))
(define (compute-windows)
(define batches (fresh-batch))
(define (update! n)
(set! batches (update batches n)))
(for [(i (in-range (vector-length src)))]
(define n (vector-ref src i))
(update! n))
(update! 0)
(~>
(batch-finished batches)
reverse
list->vector))
(define (compute-increases windows)
(define s 0)
(define (inc!)
(set! s (add1 s)))
(batch
(append (map sum newly-finished) (batch-finished b))
still-in-progress))
(module+ test
(define b1 (batch '() '((3 2 1) (5 6))))
(check-equal?
(mark-finished b1)
(batch '(6) '((5 6)))))
(define (sum lst) (apply + lst))
(define (finished? lst)
(= (length lst) WINDOW))
(define (compute-windows)
(define batches (fresh-batch))
(define (update! n)
(set! batches (update batches n)))
(for [(i (in-range (vector-length src)))]
(define n (vector-ref src i))
(update! n))
(update! 0)
(~>
(batch-finished batches)
reverse
list->vector))
(define (compute-increases windows)
(define s 0)
(define (inc!)
(set! s (add1 s)))
(define (get n)
(vector-ref windows n))
(for [(i (in-range 1 (vector-length windows)))]
(define this (get i))
(define last (get (sub1 i)))
(when (> this last)
(inc!)))
s)
(define (main)
(define windows (compute-windows))
(displayln (compute-increases windows)))
(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment