Browse Source

pain

master
Hazel Levine 1 year ago
parent
commit
252148b8af
Signed by: hazel
GPG Key ID: 1884029A28789A62
  1. 1
      data/day6.txt
  2. 98
      day6.rkt
  3. 2
      tmpl.rkt

1
data/day6.txt

@ -0,0 +1 @@
1,1,5,2,1,1,5,5,3,1,1,1,1,1,1,3,4,5,2,1,2,1,1,1,1,1,1,1,1,3,1,1,5,4,5,1,5,3,1,3,2,1,1,1,1,2,4,1,5,1,1,1,4,4,1,1,1,1,1,1,3,4,5,1,1,2,1,1,5,1,1,4,1,4,4,2,4,4,2,2,1,2,3,1,1,2,5,3,1,1,1,4,1,2,2,1,4,1,1,2,5,1,3,2,5,2,5,1,1,1,5,3,1,3,1,5,3,3,4,1,1,4,4,1,3,3,2,5,5,1,1,1,1,3,1,5,2,1,3,5,1,4,3,1,3,1,1,3,1,1,1,1,1,1,5,1,1,5,5,2,1,5,1,4,1,1,5,1,1,1,5,5,5,1,4,5,1,3,1,2,5,1,1,1,5,1,1,4,1,1,2,3,1,3,4,1,2,1,4,3,1,2,4,1,5,1,1,1,1,1,3,4,1,1,5,1,1,3,1,1,2,1,3,1,2,1,1,3,3,4,5,3,5,1,1,1,1,1,1,1,1,1,5,4,1,5,1,3,1,1,2,5,1,1,4,1,1,4,4,3,1,2,1,2,4,4,4,1,2,1,3,2,4,4,1,1,1,1,4,1,1,1,1,1,4,1,5,4,1,5,4,1,1,2,5,5,1,1,1,5

98
day6.rkt

@ -0,0 +1,98 @@
#lang racket
(require "lib/common.rkt"
fancy-app)
(define (generate-initial-hash fish)
(define res (make-hash))
(for ([f (in-list fish)])
(hash-update! res f add1 0))
res)
(define (simulate-day! hsh)
(define copy (hash-copy hsh))
(for ([(f k) (in-hash copy)])
(cond [(zero? f)
;; add k 8s
(hash-update! hsh 8 (+ k _) 0)
;; add k 6s
(hash-update! hsh 6 (+ k _) 0)
;; reset zero
(hash-set! hsh 0 0)]
[else
;; move it down
(hash-update! hsh (- f 1) (+ k _) 0)
(hash-update! hsh f (- _ k) 0)])))
(define (day6a fish)
(define count-hash (generate-initial-hash fish))
(for ([_ (in-range 80)])
(simulate-day! count-hash))
(foldl + 0 (hash-values count-hash)))
(define (day6b fish)
(define count-hash (generate-initial-hash fish))
(for ([_ (in-range 256)])
(simulate-day! count-hash))
(foldl + 0 (hash-values count-hash)))
(module+ main
(call-with-input-file "data/day6.txt"
(λ (prt)
(define fish
(~> (port->string prt)
string-split
first
(string-split ",")
(map string->number _)))
(answer 6 1 (day6a fish))
(answer 6 2 (day6b fish)))))
(module+ test
(require rackunit)
(define test-fish '(3 4 3 1 2))
(define init-hash (generate-initial-hash test-fish))
(check-equal? (hash->list init-hash)
(hash->list (hash 1 1
2 1
3 2
4 1)))
(simulate-day! init-hash)
(check-equal? (sort #:key car (hash->list init-hash) <)
(sort #:key car
(hash->list (hash 0 1
1 1
2 2
3 1
4 0))
<))
(simulate-day! init-hash)
(check-equal? (sort #:key car (hash->list init-hash) <)
(sort #:key car
(hash->list (hash 0 1
1 2
2 1
3 0
4 0
6 1
8 1))
<))
(simulate-day! init-hash)
(check-equal? (sort #:key car (hash->list init-hash) <)
(sort #:key car
(hash->list (hash 0 2
1 1
2 0
3 0
4 0
5 1
6 1
7 1
8 1))
<))
(check-equal? (day6a test-fish) 5934)
(check-equal? (day6b test-fish) 26984457539))

2
tmpl.rkt

@ -9,7 +9,7 @@
(module+ main
(call-with-input-file "data/dayN.txt"
(lambda (prt)
(λ (prt)
(define lines (port->lines prt))
(answer N 1 (dayNa lines))
(answer N 2 (dayNb lines)))))

Loading…
Cancel
Save