From 3a937f6c0a94895d7112878bfb4d10a374eca130 Mon Sep 17 00:00:00 2001 From: Matt Jadud Date: Mon, 22 Dec 2025 07:28:44 -0500 Subject: [PATCH] Start of day 1. --- 01/01.rkt | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 01/01.rkt diff --git a/01/01.rkt b/01/01.rkt new file mode 100644 index 0000000..bcaa61f --- /dev/null +++ b/01/01.rkt @@ -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))) \ No newline at end of file