You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.5 KiB
44 lines
1.5 KiB
#lang racket |
|
(require "lib/common.rkt") |
|
|
|
(struct forward (n) #:transparent) |
|
(struct down (n) #:transparent) |
|
(struct up (n) #:transparent) |
|
|
|
(define (parse lines) |
|
(for/list ([line (in-list lines)]) |
|
(match-define (list instr num) (string-split line)) |
|
(match instr |
|
["forward" (forward (string->number num))] |
|
["down" (down (string->number num))] |
|
["up" (up (string->number num))]))) |
|
|
|
(define (day2a lst) |
|
(for/fold ([current-horiz 0] |
|
[current-depth 0] |
|
#:result (* current-horiz current-depth)) |
|
([instr (in-list lst)]) |
|
(match instr |
|
[(forward num) (values (+ current-horiz num) current-depth)] |
|
[(down num) (values current-horiz (+ current-depth num))] |
|
[(up num) (values current-horiz (- current-depth num))]))) |
|
|
|
(define (day2b lst) |
|
(for/fold ([current-horiz 0] |
|
[current-depth 0] |
|
[current-aim 0] |
|
#:result (* current-horiz current-depth)) |
|
([instr (in-list lst)]) |
|
(match instr |
|
[(forward num) (values (+ current-horiz num) |
|
(+ (* current-aim num) current-depth) |
|
current-aim)] |
|
[(down num) (values current-horiz current-depth (+ current-aim num))] |
|
[(up num) (values current-horiz current-depth (- current-aim num))]))) |
|
|
|
(module+ main |
|
(call-with-input-file "data/day2.txt" |
|
(lambda (prt) |
|
(define lines (parse (port->lines prt))) |
|
(answer 2 1 (day2a lines)) |
|
(answer 2 2 (day2b lines)))))
|
|
|