Commit 912d538b authored by Gilles Coremans's avatar Gilles Coremans
Browse files

Add Day 12 solutions and input

parent 19aee355
#lang racket
(struct moon (xpos ypos zpos xvel yvel zvel) #:transparent)
(define (read-moons)
(let ([regexres (regexp-match #rx"<x=(-?[0-9]+), y=(-?[0-9]+), z=(-?[0-9]+)>" (current-input-port))])
(if (not regexres)
empty
(let ([coords (map (compose string->number bytes->string/utf-8) (cdr regexres))])
(cons(moon (list-ref coords 0) (list-ref coords 1) (list-ref coords 2) 0 0 0) (read-moons))))))
(define (sign n)
(if (n . > . 0)
1
(if (n . = . 0)
0
-1)))
(define (velchange subject cause)
(list
(sign ((moon-xpos cause) . - . (moon-xpos subject)))
(sign ((moon-ypos cause) . - . (moon-ypos subject)))
(sign ((moon-zpos cause) . - . (moon-zpos subject)))))
(define (vecsum l r)
(if (empty? l)
empty
(cons ((car l) . + . (car r)) (vecsum (cdr l) (cdr r)))))
(define (move-moon subject a)
(moon (+ (moon-xpos subject) (moon-xvel subject) (list-ref a 0))
(+ (moon-ypos subject) (moon-yvel subject) (list-ref a 1))
(+ (moon-zpos subject) (moon-zvel subject) (list-ref a 2))
(+ (moon-xvel subject) (list-ref a 0))
(+ (moon-yvel subject) (list-ref a 1))
(+ (moon-zvel subject) (list-ref a 2))))
(define (step-moons moons)
(for/list ([moon moons])
(move-moon moon
(foldl (lambda (other accel) (vecsum accel (velchange moon other)))
'(0 0 0) moons))))
(define (timestep moons steps)
(if (= 0 steps)
moons
(timestep (step-moons moons) (sub1 steps))))
(define (energy moons)
(if (empty? moons)
0
(let ([m (car moons)])
(+ (* (+ (abs (moon-xpos m)) (abs (moon-ypos m)) (abs (moon-zpos m)))
(+ (abs (moon-xvel m)) (abs (moon-yvel m)) (abs (moon-zvel m))))
(energy (cdr moons))))))
(energy (timestep (read-moons) 1000))
\ No newline at end of file
#lang racket
(struct moon (xpos ypos zpos xvel yvel zvel) #:transparent)
(define (read-moons)
(let ([regexres (regexp-match #rx"<x=(-?[0-9]+), y=(-?[0-9]+), z=(-?[0-9]+)>" (current-input-port))])
(if (not regexres)
empty
(let ([coords (map (compose string->number bytes->string/utf-8) (cdr regexres))])
(cons(moon (list-ref coords 0) (list-ref coords 1) (list-ref coords 2) 0 0 0) (read-moons))))))
(define (sign n)
(if (n . > . 0)
1
(if (n . = . 0)
0
-1)))
(define (accel subject cause)
(sign ((car cause) . - . (car subject))))
(define (move-moon subject a)
(cons (+ (car subject) (cdr subject) a)
(+ (cdr subject) a)))
(define (step-moons moons)
(for/list ([m moons])
(move-moon m
(foldl (lambda (other a) (+ a (accel m other)))
0 moons))))
(define (timestep moons init steps)
(if (equal? moons init)
steps
(timestep (step-moons moons) init (add1 steps))))
(let* ([moons (read-moons)]
[xaxis (map (lambda (m) (cons (moon-xpos m) (moon-xvel m))) moons)]
[yaxis (map (lambda (m) (cons (moon-ypos m) (moon-yvel m))) moons)]
[zaxis (map (lambda (m) (cons (moon-zpos m) (moon-zvel m))) moons)])
(lcm
(timestep (step-moons xaxis) xaxis 1)
(timestep (step-moons yaxis) yaxis 1)
(timestep (step-moons zaxis) zaxis 1)))
\ No newline at end of file
<x=-4, y=-9, z=-3>
<x=-13, y=-11, z=0>
<x=-17, y=-7, z=15>
<x=-16, y=4, z=2>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment