1
0
Fork 0

Day 02: Move some work into parsing.

This commit is contained in:
Jonathan Chan 2021-12-01 21:49:42 -08:00 committed by Jonathan Chan
parent 66fb5ddd5b
commit 8b28cd478b
1 changed files with 8 additions and 8 deletions

View File

@ -5,13 +5,14 @@
(define input (define input
(for/list ([line (problem-input 2)]) (for/list ([line (problem-input 2)])
(match (string-split line " ") (match (string-split line " ")
[`(,dir ,val) (list (string->symbol dir) (string->number val))]))) [`("up" ,val) (list 'vert (negate (string->number val)))]
[`("down" ,val) (list 'vert (string->number val))]
[`("forward" ,val) (list 'horiz (string->number val))])))
(define part1 (define part1
(let ([horiz (sum (filter-map (match-lambda [`(forward ,n) n] [else #f]) input))] (let ([horiz (sum (filter-map (match-lambda [`(horiz ,n) n] [else #f]) input))]
[ups (sum (filter-map (match-lambda [`(up ,n) n] [else #f]) input))] [depth (sum (filter-map (match-lambda [`(vert ,n) n] [else #f]) input))])
[downs (sum (filter-map (match-lambda [`(down ,n) n] [else #f]) input))]) (* horiz depth)))
(* horiz (- downs ups))))
(define part2 (define part2
(for/fold ([aim 0] (for/fold ([aim 0]
@ -20,8 +21,7 @@
#:result (* horiz depth)) #:result (* horiz depth))
([dir-val input]) ([dir-val input])
(match dir-val (match dir-val
[`(forward ,val) (values aim (+ horiz val) (+ depth (* aim val)))] [`(horiz ,val) (values aim (+ horiz val) (+ depth (* aim val)))]
[`(up ,val) (values (- aim val) horiz depth)] [`(vert ,val) (values (+ aim val) horiz depth)])))
[`(down ,val) (values (+ aim val) horiz depth)])))
(show-solution part1 part2) (show-solution part1 part2)