From 99505278327f387fc3e64392d46e32b3a23f9845 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sun, 11 Dec 2022 01:10:43 -0500 Subject: [PATCH] day10: Add solution for pt 1 and 2 --- 2022/10/input | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2022/10/sample | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2022/10/sample2 | 3 +++ 2022/10/sol.scm | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 344 insertions(+) create mode 100644 2022/10/input create mode 100644 2022/10/sample create mode 100644 2022/10/sample2 create mode 100755 2022/10/sol.scm diff --git a/2022/10/input b/2022/10/input new file mode 100644 index 0000000..5edd3b8 --- /dev/null +++ b/2022/10/input @@ -0,0 +1,138 @@ +addx 1 +noop +addx 2 +noop +addx 3 +addx 3 +addx 1 +addx 5 +addx 1 +noop +noop +addx 4 +noop +noop +addx -9 +addx 16 +addx -1 +noop +addx 5 +addx -2 +addx 4 +addx -35 +addx 2 +addx 28 +noop +addx -23 +addx 3 +addx -2 +addx 2 +addx 5 +addx -8 +addx 19 +addx -8 +addx 2 +addx 5 +addx 5 +addx -14 +addx 12 +addx 2 +addx 5 +addx 2 +addx -13 +addx -23 +noop +addx 1 +addx 5 +addx -1 +addx 2 +addx 4 +addx -9 +addx 10 +noop +addx 6 +addx -11 +addx 12 +addx 5 +addx -25 +addx 30 +addx -2 +addx 2 +addx -5 +addx 12 +addx -37 +noop +noop +noop +addx 24 +addx -17 +noop +addx 33 +addx -32 +addx 3 +addx 1 +noop +addx 6 +addx -13 +addx 17 +noop +noop +noop +addx 12 +addx -4 +addx -2 +addx 2 +addx 3 +addx 4 +addx -35 +addx -2 +noop +addx 20 +addx -13 +addx -2 +addx 5 +addx 2 +addx 23 +addx -18 +addx -2 +addx 17 +addx -10 +addx 17 +noop +addx -12 +addx 3 +addx -2 +addx 2 +noop +addx 3 +addx 2 +noop +addx -13 +addx -20 +noop +addx 1 +addx 2 +addx 5 +addx 2 +addx 5 +noop +noop +noop +noop +noop +addx 1 +addx 2 +addx -18 +noop +addx 26 +addx -1 +addx 6 +noop +noop +noop +addx 4 +addx 1 +noop +noop +noop +noop diff --git a/2022/10/sample b/2022/10/sample new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/2022/10/sample @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop diff --git a/2022/10/sample2 b/2022/10/sample2 new file mode 100644 index 0000000..f428b7b --- /dev/null +++ b/2022/10/sample2 @@ -0,0 +1,3 @@ +noop +addx 3 +addx -5 diff --git a/2022/10/sol.scm b/2022/10/sol.scm new file mode 100755 index 0000000..a78e5f7 --- /dev/null +++ b/2022/10/sol.scm @@ -0,0 +1,57 @@ +#!/usr/local/bin/chicken-csi -ss +(import (chicken string) + (chicken io)) + +(define (parse-line l) + (let ((parts (string-split l))) + (cond ((equal? (car parts) "noop") + (list (string->symbol (car parts)))) + ((equal? (car parts) "addx") + (list (string->symbol (car parts)) + (string->number (cadr parts)))) + (else "Error: bad line" l)))) + +(define (simul cmds) + (let loop ((cmds cmds) (x 1)) + (if (null? cmds) + '() + (let ((cmd (car cmds))) + (cond ((eq? (car cmd) 'noop) + (cons x (loop (cdr cmds) x))) + ((eq? (car cmd) 'addx) + ; takes 2 counts, so append x THEN append x + val + (let ((x2 (+ (cadr cmd) x))) + (cons x (cons x2 (loop (cdr cmds) x2))))) + (else "Bad cmd " cmd)))))) + +; x is 3-wide, covers (x - 1, x, x + 1), (x + 39 , x + 40, x + 41), ... +(define (overlaps? x c) + ; NOTE: need to add 1 to X since cycle is 1 indexed + (>= 1 (abs (- (add1 x) (modulo c 40))))) + +(define (part-1 sim) + (let loop ((i 1) (sim sim) (count 0)) + ; (print (list i (car sim) count)) + (if (null? sim) + count + (if (= 0 (modulo (+ i 20) 40)) + (loop (add1 i) (cdr sim) (+ count (* i (car sim)))) + (loop (add1 i) (cdr sim) count))))) + +(define (part-2 sim) + (let loop ((i 0) (sim sim)) + (if (null? sim) + 'done + (begin + (and (= 0 (modulo i 40)) (newline)) + (if (overlaps? (car sim) (add1 i)) + (display "#") + (display " ")) + (loop (add1 i) (cdr sim)))))) + +(define (main args) + (let* ((lines (read-lines)) + (cmds (map parse-line lines)) + (x-vals (cons 1 (simul cmds)))) ; NOTE: need first val + (print (part-1 x-vals)) + (part-2 x-vals))) -- libgit2 1.8.1