From 8b28cd478b5118fe0140a80b2b86813259281e8e Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Wed, 1 Dec 2021 21:49:42 -0800 Subject: [PATCH] Day 02: Move some work into parsing. --- src/02.rkt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/02.rkt b/src/02.rkt index dd295ca..da8290b 100644 --- a/src/02.rkt +++ b/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) \ No newline at end of file