Advent of Code 2021 https://adventofcode.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('') and can be up to 35 characters long.
83 lines
3.1 KiB
83 lines
3.1 KiB
#lang racket




(define (mapmap f lst) ; map . map


(map (λ (x) (map (λ (y) (f y)) x)) lst))




(define data


(mapmap stringsplit


(map (λ (l) (stringsplit l "  "))


(file>lines "day8.txt"))))




(define (iseasy? d)


(let ([len (stringlength d)])


(or (= len 2) (= len 3) (= len 4) (= len 7))))




(define part1


(let* ([justoutput (map second data)]


[justeasydigits (map (λ (x) (filter iseasy? x)) justoutput)])


(length (flatten justeasydigits))))




part1




; part 2




; Can we just count the number of times a segment appears?


; No. Damn. But it gets us close. Naming the segments A  G


;


; A has 8 instances and IS NOT in 1


; B has 6 instances


; C has 8 instances and IS in 1


; D has 7 instances and IS in 4


; E has 4 instances


; F has 9 instances


; G has 7 instances and IS NOT in 4




(define (numinstances letter lst)


(length (filter (λ (s) (stringcontains? s letter)) lst)))




(define letters '("a" "b" "c" "d" "e" "f" "g"))




(define (assignsegments lst) ; returns a vector with each position corresponing to a segment's letter. e.g. vect[0] = c, vect[1] = g, etc.


(let ([one (findf (λ (s) (= (stringlength s) 2)) lst)]


[four (findf (λ (s) (= (stringlength s) 4)) lst)])


(vector


(findf (λ (l) (and (= (numinstances l lst) 8) (not (stringcontains? one l)))) letters) ; A has 8 instances and isn't in 1


(findf (λ (l) (= (numinstances l lst) 6)) letters) ; B


(findf (λ (l) (and (= (numinstances l lst) 8) (stringcontains? one l))) letters) ; C


(findf (λ (l) (and (= (numinstances l lst) 7) (stringcontains? four l))) letters) ; D


(findf (λ (l) (= (numinstances l lst) 4)) letters) ; E


(findf (λ (l) (= (numinstances l lst) 9)) letters) ; F


(findf (λ (l) (and (= (numinstances l lst) 7) (not (stringcontains? four l)))) letters)))) ; G




(define (allsegmentsinstr? key segments str)


(foldl (λ (seg all) (and all (stringcontains? str (vectorref key seg)))) true segments))




(define (segments>number lst key) ; takes some segments ("bc"), the key, and returns the corresponding number


(cond


[(and (= (stringlength lst) 6) (allsegmentsinstr? key '(0 1 2 4 5 6) lst)) 0]


[(= (stringlength lst) 2) 1]


[(and (= (stringlength lst) 5) (allsegmentsinstr? key '(0 2 3 4 6) lst)) 2]


[(and (= (stringlength lst) 5) (allsegmentsinstr? key '(0 2 3 5 6) lst)) 3]


[(= (stringlength lst) 4) 4]


[(and (= (stringlength lst) 5) (allsegmentsinstr? key '(0 1 3 5 6) lst)) 5]


[(and (= (stringlength lst) 6) (allsegmentsinstr? key '(0 1 3 4 5 6) lst)) 6]


[(= (stringlength lst) 3) 7]


[(= (stringlength lst) 7) 8]


[(and (= (stringlength lst) 6) (allsegmentsinstr? key '(0 1 2 3 5 6) lst)) 9]))






(define (numlist>number lst) ; '(1 2 3) > 123


(string>number


(apply stringappend


(map number>string lst))))




(define (line>number line)


(let* ([key (assignsegments (first line))]


[output (second line)]


[numlist (map (λ (s) (segments>number s key)) output)])


(numlist>number numlist)))




(define part2


(foldl (λ (l acc) (+ acc (line>number l))) 0 data))




part2
