aboutsummaryrefslogtreecommitdiff
path: root/aoc2022/day-10/day-10.rkt
blob: 70c80d310ac741a6f016edfacb8058cadab7d9a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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,
; '("███  ████ ████ █  █ ████ ████ █  █  ██  "
;   "█  █    █ █    █ █  █    █    █  █ █  █ "
;   "█  █   █  ███  ██   ███  ███  ████ █  █ "
;   "███   █   █    █ █  █    █    █  █ ████ "
;   "█ █  █    █    █ █  █    █    █  █ █  █ "
;   "█  █ ████ ████ █  █ ████ █    █  █ █  █ ")