Day 10
This commit is contained in:
parent
c00a6d2779
commit
5d0921f1fd
|
@ -0,0 +1,138 @@
|
|||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 29
|
||||
addx -28
|
||||
addx 5
|
||||
addx -1
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 12
|
||||
addx -6
|
||||
noop
|
||||
addx 4
|
||||
addx -1
|
||||
addx 1
|
||||
addx 5
|
||||
addx -31
|
||||
addx 32
|
||||
addx 4
|
||||
addx 1
|
||||
noop
|
||||
addx -38
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx -32
|
||||
addx 33
|
||||
addx -20
|
||||
addx 27
|
||||
addx -39
|
||||
addx 1
|
||||
noop
|
||||
addx 5
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx -2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -16
|
||||
addx 21
|
||||
addx -1
|
||||
addx 1
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx -22
|
||||
addx 26
|
||||
addx -39
|
||||
noop
|
||||
addx 5
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 23
|
||||
noop
|
||||
addx -18
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx -27
|
||||
addx 28
|
||||
addx 5
|
||||
addx -11
|
||||
addx -27
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 27
|
||||
addx -26
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 4
|
||||
addx -3
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx -33
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 31
|
||||
addx -26
|
||||
addx 6
|
||||
noop
|
||||
noop
|
||||
addx -1
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx 3
|
||||
noop
|
||||
addx -1
|
||||
addx 5
|
||||
addx 1
|
||||
addx -12
|
||||
addx 17
|
||||
addx -1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
8
lib.rkt
8
lib.rkt
|
@ -91,6 +91,14 @@
|
|||
(define (show-solution part1 part2)
|
||||
(printf "Part 1: ~a\nPart 2: ~a\n" part1 part2))
|
||||
|
||||
;; show-solution* : (-> a) -> (-> b) -> void
|
||||
;; Like show-solution, but parts 1 and 2 are thunks.
|
||||
(define (show-solution* part1 part2)
|
||||
(display "Part 1: ")
|
||||
(displayln (part1))
|
||||
(display "Part 2: ")
|
||||
(displayln (part2)))
|
||||
|
||||
|
||||
;; String helpers ;;
|
||||
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
#lang curly-fn racket
|
||||
|
||||
(require "../lib.rkt")
|
||||
|
||||
(define input
|
||||
(map (∘ (match-lambda
|
||||
['("noop") 'noop]
|
||||
[`("addx" ,n) (list 'addx (string->number n))])
|
||||
#{string-split %1 " "})
|
||||
(problem-input 10)))
|
||||
|
||||
(define (part1)
|
||||
(define strengths 0)
|
||||
(define (update! cycle X)
|
||||
(when (zero? (% (- cycle 20) 40))
|
||||
(set! strengths (+ strengths (* cycle X)))))
|
||||
(for/fold ([X 1] [cycle 1]
|
||||
#:result strengths)
|
||||
([instr input])
|
||||
(match instr
|
||||
['noop
|
||||
(update! cycle X)
|
||||
(values X (add1 cycle))]
|
||||
[`(addx ,n)
|
||||
(update! cycle X)
|
||||
(update! (add1 cycle) X)
|
||||
(values (+ X n) (+ cycle 2))])))
|
||||
|
||||
(define (part2)
|
||||
(define CRT (make-vector 240))
|
||||
(define (update! pos X)
|
||||
(if (<= (sub1 X) (% pos 40) (add1 X))
|
||||
(vector-set! CRT pos #\█)
|
||||
(vector-set! CRT pos #\ )))
|
||||
(for/fold ([X 1] [pos 0])
|
||||
([instr input])
|
||||
(match instr
|
||||
['noop
|
||||
(update! pos X)
|
||||
(values X (add1 pos))]
|
||||
[`(addx ,n)
|
||||
(update! pos X)
|
||||
(update! (add1 pos) X)
|
||||
(values (+ X n) (+ pos 2))]))
|
||||
(displayln "See below.")
|
||||
(for ([s (in-slice 40 CRT)])
|
||||
(displayln (list->string s))))
|
||||
|
||||
(show-solution* part1 part2)
|
Loading…
Reference in New Issue