diff options
author | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-12-12 09:57:37 -0500 |
---|---|---|
committer | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-12-12 09:57:37 -0500 |
commit | 591488cf7c85e4b8bfdbebdce2187bb23c41fa0d (patch) | |
tree | 64c38cc9607ae537e07fa8f63fc9339956bfdc87 /2022/day-10/day-10.rkt | |
parent | 2c50f6aefe5c385672312e8907fc1e6724e215d1 (diff) | |
parent | 69de3d8e95b8d66609e07dffcb2022dde901d837 (diff) | |
download | gleam_aoc-591488cf7c85e4b8bfdbebdce2187bb23c41fa0d.tar.gz gleam_aoc-591488cf7c85e4b8bfdbebdce2187bb23c41fa0d.zip |
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode
Diffstat (limited to '2022/day-10/day-10.rkt')
-rw-r--r-- | 2022/day-10/day-10.rkt | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/2022/day-10/day-10.rkt b/2022/day-10/day-10.rkt new file mode 100644 index 0000000..70c80d3 --- /dev/null +++ b/2022/day-10/day-10.rkt @@ -0,0 +1,43 @@ +#lang racket + +(require advent-of-code + threading + fancy-app + (only-in algorithms chunks-of)) + +(define/match (process-instruction _) + [((list "noop")) (list 'noop)] + [((list "addx" (app string->number val))) (list 'noop (cons 'addx val))]) + +(define instructions + (~> (fetch-aoc-input (find-session) 2022 10) + (string-split "\n") + (map (λ~> string-split process-instruction) _) + (apply append _))) + +;; part 1 +(define interesting-times (inclusive-range 20 220 40)) + +(define/match (evaluate-instruction _op acc) + [('noop _) acc] + [((cons 'addx n) _) (+ acc n)]) + +(for/fold ([acc 1] [interesting-strengths 0] #:result interesting-strengths) + ([inst (in-list instructions)] [i (in-naturals 1)]) + (define new-interesting + (if (member i interesting-times) (+ interesting-strengths (* i acc)) interesting-strengths)) + (values (evaluate-instruction inst acc) new-interesting)) + +;; part 2 +(for/fold ([acc 1] [pixels '()] #:result (~> pixels reverse (chunks-of 40) (map (apply string _) _))) + ([inst (in-list instructions)] [i (in-naturals)]) + (define new-pixel (if (member (modulo i 40) (list (sub1 acc) acc (add1 acc))) #\█ #\space)) + (values (evaluate-instruction inst acc) (cons new-pixel pixels))) + +; for my data set, +; '("███ ████ ████ █ █ ████ ████ █ █ ██ " +; "█ █ █ █ █ █ █ █ █ █ █ █ " +; "█ █ █ ███ ██ ███ ███ ████ █ █ " +; "███ █ █ █ █ █ █ █ █ ████ " +; "█ █ █ █ █ █ █ █ █ █ █ █ " +; "█ █ ████ ████ █ █ ████ █ █ █ █ █ ") |