From f1134f4b005fc1c24960a39a09432c3125f72133 Mon Sep 17 00:00:00 2001 From: cow Date: Wed, 28 Dec 2022 03:47:10 -0500 Subject: [PATCH] day 10 part 1 in common lisp --- common-lisp/day-10/example.txt | 1 + common-lisp/day-10/input.txt | 1 + common-lisp/day-10/solution.lisp | 60 +++++++++++++ input/day-10-example.txt | 146 +++++++++++++++++++++++++++++++ input/day-10.txt | 145 ++++++++++++++++++++++++++++++ 5 files changed, 353 insertions(+) create mode 120000 common-lisp/day-10/example.txt create mode 120000 common-lisp/day-10/input.txt create mode 100755 common-lisp/day-10/solution.lisp create mode 100644 input/day-10-example.txt create mode 100644 input/day-10.txt diff --git a/common-lisp/day-10/example.txt b/common-lisp/day-10/example.txt new file mode 120000 index 0000000..06b69fd --- /dev/null +++ b/common-lisp/day-10/example.txt @@ -0,0 +1 @@ +../../input/day-10-example.txt \ No newline at end of file diff --git a/common-lisp/day-10/input.txt b/common-lisp/day-10/input.txt new file mode 120000 index 0000000..88d1ca6 --- /dev/null +++ b/common-lisp/day-10/input.txt @@ -0,0 +1 @@ +../../input/day-10.txt \ No newline at end of file diff --git a/common-lisp/day-10/solution.lisp b/common-lisp/day-10/solution.lisp new file mode 100755 index 0000000..91ff4f2 --- /dev/null +++ b/common-lisp/day-10/solution.lisp @@ -0,0 +1,60 @@ +#!/usr/bin/env -S sbcl --script + +;; Copyright (C) 2022 +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +(require 'asdf) +(require 'uiop) +(require 'str) +(require 'iterate) + +(defpackage :advent-of-code-day-10 + (:use :cl :iterate) + (:import-from :uiop :read-file-lines)) + +(in-package :advent-of-code-day-10) + +(defun signal-strength-at-cycle (instructions cycle) + (iter (for instruction in instructions) + (for noop? = (not (consp instruction))) + (for counter initially 1 then (+ counter (if noop? 1 2))) + (for register initially 1 then (if noop? register (+ register (cdr instruction)))) + (for previous-counter previous counter) + (for previous-register previous register) + (when (> counter cycle) + (return (* cycle previous-register))))) + +(defun parse-input (lines) + (iter (for line in lines) + (collect (if (string= line "noop") + :noop + (cons :addx (parse-integer (cadr (str:split " " line)))))))) + +(let ((input (parse-input (read-file-lines "example.txt")))) + (assert (= 420 (signal-strength-at-cycle input 20))) + (assert (= 1140 (signal-strength-at-cycle input 60))) + (assert (= 1800 (signal-strength-at-cycle input 100))) + (assert (= 2940 (signal-strength-at-cycle input 140))) + (assert (= 2880 (signal-strength-at-cycle input 180))) + (assert (= 3960 (signal-strength-at-cycle input 220))) + (assert (= 13140 (iter (for cycle from 20 to 220 by 40) + (sum (signal-strength-at-cycle input cycle)))))) + +(let ((input (parse-input (read-file-lines "input.txt")))) + (assert (= 14320 (iter (for cycle from 20 to 240 by 40) + (sum (signal-strength-at-cycle input cycle)))))) + +;; Local Variables: +;; mode: lisp +;; End: diff --git a/input/day-10-example.txt b/input/day-10-example.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/input/day-10-example.txt @@ -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/input/day-10.txt b/input/day-10.txt new file mode 100644 index 0000000..485d25b --- /dev/null +++ b/input/day-10.txt @@ -0,0 +1,145 @@ +noop +noop +noop +addx 6 +noop +addx 30 +addx -26 +noop +addx 5 +noop +noop +noop +noop +addx 5 +addx -5 +addx 6 +addx 5 +addx -1 +addx 5 +noop +noop +addx -14 +addx -18 +addx 39 +addx -39 +addx 25 +addx -22 +addx 2 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +noop +addx 3 +addx 2 +addx 2 +noop +addx 3 +noop +addx 3 +addx 2 +addx 5 +addx 4 +addx -18 +addx 17 +addx -38 +addx 5 +addx 2 +addx -5 +addx 27 +addx -19 +noop +addx 3 +addx 4 +noop +noop +addx 5 +addx -1 +noop +noop +addx 4 +addx 5 +addx 2 +addx -4 +addx 5 +noop +addx -11 +addx 16 +addx -36 +noop +addx 5 +noop +addx 28 +addx -23 +noop +noop +noop +addx 21 +addx -18 +noop +addx 3 +addx 2 +addx 2 +addx 5 +addx 1 +noop +noop +addx 4 +noop +noop +noop +noop +noop +addx 8 +addx -40 +noop +addx 7 +noop +addx -2 +addx 5 +addx 2 +addx 25 +addx -31 +addx 9 +addx 5 +addx 2 +addx 2 +addx 3 +addx -2 +noop +addx 3 +addx 2 +noop +addx 7 +addx -2 +addx 5 +addx -40 +addx 20 +addx -12 +noop +noop +noop +addx -5 +addx 7 +addx 7 +noop +addx -1 +addx 1 +addx 5 +addx 3 +addx -2 +addx 2 +noop +addx 3 +addx 2 +noop +noop +noop +noop +addx 7 +noop +noop +noop +noop