aoc2020/9.rkt

45 lines
1.1 KiB
Racket
Raw Normal View History

2020-12-09 05:17:51 +00:00
#lang curly-fn racket
(require "scripts/aoc.rkt")
;; solution for day 9
;; helper functions here
(define (part1 input)
(define vec (list->vector input))
(let/ec exit
(for ([i (in-range 25 (vector-length vec))])
(define valid?
(for*/first ([a (in-range (- i 25) i)] [b (in-range a i)]
#:when (= (vector-ref vec i) (+ (vector-ref vec a) (vector-ref vec b)))) #t))
(unless valid?
(exit (vector-ref vec i))))))
(define (part2 input)
(define pt1 10884537)
(define pt1-i 501)
(let/ec exit
(define vec (list->vector input))
(define (cb args)
(when (= pt1 (apply + args))
(exit (+ (apply min args) (apply max args)))))
(for* ([ct (in-range 3 100)] [i (in-range 0 (- pt1-i ct))])
(define sub (vector-copy vec i (+ i ct)))
(cb (vector->list sub)))))
(module+ test
(require rackunit)
;; tests here
(displayln "no tests :("))
(module+ main
(define input (file->list "inputs/9"))
;; part 1
(answer 9 1 (part1 input))
;; part 2
(answer 9 2 (part2 input))
(displayln "meow"))