Browse Source

get clapped

master
Hazel Levine 1 year ago
parent
commit
80274cd138
Signed by: hazel
GPG Key ID: 1884029A28789A62
  1. 1000
      data/day2.txt
  2. 44
      day2.rkt

1000
data/day2.txt

File diff suppressed because it is too large Load Diff

44
day2.rkt

@ -0,0 +1,44 @@
#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)))))
Loading…
Cancel
Save