2021/src/02.rkt

27 lines
842 B
Racket
Raw Normal View History

2021-12-02 05:37:35 +00:00
#lang racket
(require "../lib.rkt")
(define input
(for/list ([line (problem-input 2)])
(match (string-split line " ")
2021-12-02 05:49:42 +00:00
[`("up" ,val) (list 'vert (negate (string->number val)))]
[`("down" ,val) (list 'vert (string->number val))]
[`("forward" ,val) (list 'horiz (string->number val))])))
2021-12-02 05:37:35 +00:00
(define part1
2021-12-02 05:49:42 +00:00
(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)))
2021-12-02 05:37:35 +00:00
(define part2
(for/fold ([aim 0]
[horiz 0]
[depth 0]
#:result (* horiz depth))
([dir-val input])
(match dir-val
2021-12-02 05:49:42 +00:00
[`(horiz ,val) (values aim (+ horiz val) (+ depth (* aim val)))]
[`(vert ,val) (values (+ aim val) horiz depth)])))
2021-12-02 05:37:35 +00:00
(show-solution part1 part2)