Start of day 1.

This commit is contained in:
Matt Jadud
2025-12-22 07:28:44 -05:00
parent 975ab2e865
commit 3a937f6c0a

67
01/01.rkt Normal file
View File

@@ -0,0 +1,67 @@
#lang racket
(require "../lib/aoc-files.rkt")
;; https://adventofcode.com/2025/day/1
#|
The attached document (your puzzle input) contains a sequence of rotations, one per line, which tell you how to open the safe. A rotation starts with an L or R which indicates whether the rotation should be to the left (toward lower numbers) or to the right (toward higher numbers). Then, the rotation has a distance value which indicates how many clicks the dial should be rotated in that direction.
So, if the dial were pointing at 11, a rotation of R8 would cause the dial to point at 19. After that, a rotation of L19 would cause it to point at 0.
Because the dial is a circle, turning the dial left from 0 one click makes it point at 99. Similarly, turning the dial right from 99 one click makes it point at 0.
So, if the dial were pointing at 5, a rotation of L10 would cause it to point at 95. After that, a rotation of R5 could cause it to point at 0.
The dial starts by pointing at 50.
You could follow the instructions, but your recent required official North Pole secret entrance security training seminar taught you that the safe is actually a decoy. The actual password is the number of times the dial is left pointing at 0 after any rotation in the sequence.
|#
(define (day-01r1 ls)
(define (add0 n) n)
(define (day-01h ls ptr count)
(cond
[(empty? ls) count]
[else
(define operator (if (string-prefix? (first ls) "L") - +))
(define dist (string->number (string-trim (first ls)
(~a (string-ref (first ls) 0)))))
(define landing (operator ptr (modulo dist 100)))
(define result (cond
[(< landing 0) (+ landing 100)]
[(>= landing 100) (remainder landing 100)]
[else landing]))
(if (or (> result 100) (< result 0))
(error (format "Out of bounds: ~a~n" result))
(day-01h (rest ls) result (if (zero? result) (add1 count) count)))]))
(day-01h ls 50 0))
(define test-input (read-input 1 #:round 'test))
(day-01r1 test-input)
(define r1-input (read-input 1 #:round 'r1));
(day-01r1 r1-input)
(define (day-01r2 ls #:incr [incr (lambda (x) 1)])
(define (add0 n) n)
(define (day-01h ls ptr count)
(cond
[(empty? ls) count]
[else
(define operator (if (string-prefix? (first ls) "L") - +))
(define dist (string->number (string-trim (first ls)
(~a (string-ref (first ls) 0)))))
(define landing (operator ptr (modulo dist 100)))
(define result (cond
[(< landing 0) (cons (+ landing 100) (incr dist))]
[(>= landing 100) (cons (remainder landing 100) (incr dist))]
[(= landing 0) (cons 0 (incr dist))]
[else (cons landing 0)]))
(if (or (> (car result) 100) (< (car result) 0))
(error (format "Out of bounds: ~a~n" result))
(day-01h (rest ls) (car result) (if (zero? (car result)) (+ (cdr result) (add1 count)) count)))]))
(day-01h ls 50 0))
(day-01r2 test-input)
(day-01r2 r1-input)
(day-01r2 test-input #:incr (lambda (dist) (modulo dist 100)))
(day-01r2 r1-input #:incr (lambda (dist) (modulo dist 100)))