Day 02: Move some work into parsing.
This commit is contained in:
parent
f94d7d40a8
commit
6ba50cce23
16
src/02.rkt
16
src/02.rkt
|
@ -5,13 +5,14 @@
|
|||
(define input
|
||||
(for/list ([line (problem-input 2)])
|
||||
(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
|
||||
(let ([horiz (sum (filter-map (match-lambda [`(forward ,n) n] [else #f]) input))]
|
||||
[ups (sum (filter-map (match-lambda [`(up ,n) n] [else #f]) input))]
|
||||
[downs (sum (filter-map (match-lambda [`(down ,n) n] [else #f]) input))])
|
||||
(* horiz (- downs ups))))
|
||||
(let ([horiz (sum (filter-map (match-lambda [`(horiz ,n) n] [else #f]) input))]
|
||||
[depth (sum (filter-map (match-lambda [`(vert ,n) n] [else #f]) input))])
|
||||
(* horiz depth)))
|
||||
|
||||
(define part2
|
||||
(for/fold ([aim 0]
|
||||
|
@ -20,8 +21,7 @@
|
|||
#:result (* horiz depth))
|
||||
([dir-val input])
|
||||
(match dir-val
|
||||
[`(forward ,val) (values aim (+ horiz val) (+ depth (* aim val)))]
|
||||
[`(up ,val) (values (- aim val) horiz depth)]
|
||||
[`(down ,val) (values (+ aim val) horiz depth)])))
|
||||
[`(horiz ,val) (values aim (+ horiz val) (+ depth (* aim val)))]
|
||||
[`(vert ,val) (values (+ aim val) horiz depth)])))
|
||||
|
||||
(show-solution part1 part2)
|
Loading…
Reference in New Issue