Browse Source

day 13

canon
Hazel Levine 8 months ago
parent
commit
cd8e887653
Signed by: hazel GPG Key ID: 1884029A28789A62
  1. 2
      data/day13.txt
  2. 102
      day12.rkt
  3. 41
      day13.rkt

2
data/day13.txt

@ -0,0 +1,2 @@
1000390
13,x,x,41,x,x,x,x,x,x,x,x,x,997,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,x,x,x,19,x,x,x,x,x,x,x,x,x,29,x,619,x,x,x,x,x,37,x,x,x,x,x,x,x,x,x,x,17

102
day12.rkt

@ -1,75 +1,67 @@
#lang racket
(require "lib/common.rkt")
(provide (all-defined-out))
;; broken rn
(struct action (act val) #:transparent)
(struct state (dir x y) #:transparent)
(struct waypoint (east north) #:transparent)
(struct posn (x y) #:transparent)
(define (posn-+ p1 p2)
(posn (+ (posn-x p1) (posn-x p2))
(+ (posn-y p1) (posn-y p2))))
(define (posn-scalar-* p v)
(posn (* v (posn-x p))
(* v (posn-y p))))
(define (preprocess prt)
(for/list ([line (in-list (port->lines prt))])
(action (string-ref line 0)
(string->number (substring line 1)))))
(define (execute/a act st)
(match-define (state dir x y) st)
(match act
[(action #\N val) (struct-copy state st [x (+ x val)])]
[(action #\S val) (struct-copy state st [x (- x val)])]
[(action #\E val) (struct-copy state st [y (+ y val)])]
[(action #\W val) (struct-copy state st [y (- y val)])]
[(action #\L val) (struct-copy state st [dir (- dir val)])]
[(action #\R val) (struct-copy state st [dir (+ dir val)])]
[(action #\F val)
; start facing east
(match (modulo dir 360) ; coterminal
[0 (struct-copy state st [y (+ y val)])]
[90 (struct-copy state st [x (- x val)])]
[180 (struct-copy state st [y (- y val)])]
[270 (struct-copy state st [x (+ x val)])])]))
(define (rotate x y d)
(define hyp (sqrt (+ (sqr x) (sqr y))))
(define ang (+ (degrees->radians d) (atan y x)))
(cons (* hyp (cos ang))
(* hyp (sin ang))))
(define (execute/b act wp st)
(match-define (state _ x y) st)
(match-define (waypoint east north) wp)
(match act
[(action #\N val) (cons (struct-copy waypoint wp [north (+ north val)]) st)]
[(action #\S val) (cons (struct-copy waypoint wp [north (- north val)]) st)]
[(action #\E val) (cons (struct-copy waypoint wp [east (+ east val)]) st)]
[(action #\W val) (cons (struct-copy waypoint wp [east (- east val)]) st)]
[(action #\L val)
(match-define (cons new-east new-north) (rotate east north val))
(cons (waypoint new-east new-north) st)]
[(action #\R val)
(match-define (cons new-east new-north) (rotate east north (- val)))
(cons (waypoint new-east new-north) st)]
[(action #\F val)
(cons wp (state 0 (+ x (* val east)) (+ y (* val north))))]))
(define (rotate p d)
(define (posn->complex p)
(make-rectangular (posn-x p) (posn-y p)))
(define (complex->posn c)
(posn (real-part c) (imag-part c)))
(complex->posn (* (posn->complex p)
(make-polar 1 (degrees->radians d)))))
(define (manhattan x1 x2 y1 y2)
(+ (abs (- x1 x2))
(abs (- y1 y2))))
(define (manhattan p)
(inexact->exact
(round (+ (abs (posn-x p))
(abs (posn-y p))))))
(define (day12a input)
(for/fold ([st (state 0 0 0)]
#:result (manhattan 0 (state-x st)
0 (state-y st)))
(for/fold ([dir 0]
[psn (posn 0 0)]
#:result (manhattan psn))
([act (in-list input)])
(execute/a act st)))
(match-define (posn x y) psn)
(match-define (action ltr val) act)
(match ltr
[#\N (values dir (posn x (+ y val)))]
[#\S (values dir (posn x (- y val)))]
[#\E (values dir (posn (+ x val) y))]
[#\W (values dir (posn (- x val) y))]
[#\L (values (- dir val) psn)]
[#\R (values (+ dir val) psn)]
[#\F (values dir (posn-+ psn (rotate (posn 0 val) dir)))])))
(define (day12b input)
(for/fold ([wp-st (cons (waypoint 10 1) (state 0 0 0))]
#:result (inexact->exact
(round (manhattan 0 (state-x (cdr wp-st))
0 (state-y (cdr wp-st))))))
(for/fold ([wp (posn 10 1)]
[psn (posn 0 0)]
#:result (manhattan psn))
([act (in-list input)])
(match-define (cons wp st) wp-st)
(execute/b act wp st)))
(match-define (posn x y) wp)
(match-define (action ltr val) act)
(match ltr
[#\N (values (posn x (+ y val)) psn)]
[#\S (values (posn x (- y val)) psn)]
[#\E (values (posn (+ x val) y) psn)]
[#\W (values (posn (- x val) y) psn)]
[#\L (values (rotate wp (- val)) psn)]
[#\R (values (rotate wp val) psn)]
[#\F (values wp (posn-+ psn (posn-scalar-* wp val)))])))
(module+ main
(call-with-input-file "data/day12.txt"

41
day13.rkt

@ -0,0 +1,41 @@
#lang racket
(require "lib/common.rkt"
math/number-theory)
(provide (all-defined-out))
(define (preprocess prt)
(match-define (list (app string->number tm) ids) (port->lines prt))
(define ids/parsed
(for/list ([id (in-list (string-split ids ","))])
(define num (string->number id))
(if num num 'empty)))
(values tm ids/parsed))
(define (in-bus id)
(in-range 0 +inf.0 id))
(define (day13a tm ids)
(define ids/f (filter (lambda (v) (not (eqv? v 'empty))) ids))
(define lst
(for/list ([id (in-list ids/f)])
(cons id (for/first ([dep (in-bus id)]
#:when (> dep tm))
dep))))
(match-define (cons id dep) (argmin cdr lst))
(* id (- dep tm)))
(define (day13b ids)
(define eqns
(for/fold ([eqns '()])
([id (in-list ids)]
[i (in-naturals)]
#:unless (equal? id 'empty))
(cons (cons (- id i) id) eqns)))
(solve-chinese (map car eqns) (map cdr eqns)))
(module+ main
(call-with-input-file "data/day13.txt"
(lambda (prt)
(define-values (tm ids) (preprocess prt))
(answer 13 1 (day13a tm ids))
(answer 13 2 (day13b ids)))))
Loading…
Cancel
Save