From 2e633775d14eaa246af901b0d64542192f3c2810 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 2 Dec 2024 10:18:15 -0300 Subject: Add part2 of day2 2024 --- 2024/aoc-utils.scm | 12 ++++++++++++ 2024/day02/main.scm | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/2024/aoc-utils.scm b/2024/aoc-utils.scm index 7fafcc6..98e83cd 100644 --- a/2024/aoc-utils.scm +++ b/2024/aoc-utils.scm @@ -15,3 +15,15 @@ (map (lambda (line) (map string->number (string-split line #\Space))) lines)) + +(define (remove-nth l n) + "Remove the `N' element from `L', returning a new list." + (append (list-head l n) (list-tail l (1+ n)))) + +(define (indexes lst) + "Get the indexes for `lst'." + (let ((retval '())) + (do ((i (1- (length lst)) (1- i))) + ((< i 0)) + (set! retval (cons i retval))) + retval)) diff --git a/2024/day02/main.scm b/2024/day02/main.scm index 310f72e..07f0a4d 100755 --- a/2024/day02/main.scm +++ b/2024/day02/main.scm @@ -38,9 +38,26 @@ 1 0)) reports)))) +(define (indexes report) + (let ((retval '())) + (do ((i (1- (length report)) (1- i))) + ((< i 0)) + (set! retval (cons i retval))) + retval)) + +(define (gen-reports report) + (cons report (map (lambda (idx) + (remove-nth report idx)) (indexes report)))) + +(define (safe-report-with-toleration? report) + (any safe-report? (gen-reports report))) + (define (part2 lines) (let ((reports (lines->number-list lines))) - 0)) + (apply + (map (lambda (r) + (if (safe-report-with-toleration? r) + 1 + 0)) reports)))) (define (main) (let ((lines (file->list "input.txt"))) -- cgit v1.2.3-70-g09d2