40 lines
1.2 KiB
Racket
40 lines
1.2 KiB
Racket
#lang curly-fn racket
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
;; solution for day 3
|
|
|
|
(define prios (string->list "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
|
|
|
|
(define (part1 input)
|
|
(for/sum ([line (in-list input)])
|
|
(define len (string-length line))
|
|
(define h1 (substring line 0 (/ len 2)))
|
|
(define h2 (substring line (/ len 2) len))
|
|
(define g1 (list->set (string->list h1)))
|
|
(define g2 (list->set (string->list h2)))
|
|
(define both (set-intersect g1 g2))
|
|
(index-of prios (first (set->list both)))))
|
|
|
|
|
|
(define (part2 input)
|
|
(displayln (length input))
|
|
(for/sum ([i (in-range 0 (length input) 3)])
|
|
(define l1 (list-ref input (+ i 0)))
|
|
(define l2 (list-ref input (+ i 1)))
|
|
(define l3 (list-ref input (+ i 2)))
|
|
(define g1 (list->set (string->list l1)))
|
|
(define g2 (list->set (string->list l2)))
|
|
(define g3 (list->set (string->list l3)))
|
|
(define both (set-intersect (set-intersect g1 g2 ) g3))
|
|
(index-of prios (first (set->list both)))))
|
|
|
|
(define (parse fname)
|
|
(file->lines fname))
|
|
|
|
(module+ main
|
|
(define input (parse "inputs/3"))
|
|
(answer 3 1 (time (part1 input)))
|
|
(answer 3 2 (time (part2 input)))
|
|
(displayln "meow"))
|