From e309f81f40a2d01513060f7bd07daaeb5ae78ad6 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 2 Dec 2024 08:53:23 -0300 Subject: Add 2 day of 2024 (part1) --- 2024/aoc-utils.scm | 8 +++++++- 2024/day02/main.scm | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100755 2024/day02/main.scm diff --git a/2024/aoc-utils.scm b/2024/aoc-utils.scm index 6b57aa5..7fafcc6 100644 --- a/2024/aoc-utils.scm +++ b/2024/aoc-utils.scm @@ -3,9 +3,15 @@ #:use-module (srfi srfi-9) #:use-module (ice-9 regex) #:use-module (ice-9 rdelim) - #:export (file->list)) + #:export (file->list + lines->number-list)) (define (file->list filename) (let ((lines (string-split (call-with-input-file filename read-string) #\Newline))) ; Remove EOF token (take lines (1- (length lines))))) + +(define (lines->number-list lines) + (map (lambda (line) + (map string->number (string-split line #\Space))) + lines)) diff --git a/2024/day02/main.scm b/2024/day02/main.scm new file mode 100755 index 0000000..310f72e --- /dev/null +++ b/2024/day02/main.scm @@ -0,0 +1,54 @@ +#!/usr/bin/guile -s +!# + +(add-to-load-path (format #f "~a/2024" (getcwd))) +(add-to-load-path (format #f "~a/.." (dirname (current-filename)))) + +(use-modules + (aoc-utils) + (srfi srfi-1) + (srfi srfi-9) + (ice-9 regex)) + +(define (all-decresing? report) + (equal? report (sort report >))) + +(define (all-incrising? report) + (equal? report (sort report <))) + +(define (safe-head? report) + (if (>=(length report) 2) + (let ((diff (abs (- (first report) (second report))))) + (and (<= diff 3) (>= diff 1))) + #t)) + +(define (safe-difference? report) + (cond ((not (safe-head? report)) #f) + ((null? report) #t) + (else (safe-difference? (cdr report))))) + +(define (safe-report? report) + (and (or (all-incrising? report) + (all-decresing? report)) + (safe-difference? report))) + +(define (part1 lines) + (let ((reports (lines->number-list lines))) + (fold + 0 (map (lambda (report) (if (safe-report? report) + 1 + 0)) reports)))) + +(define (part2 lines) + (let ((reports (lines->number-list lines))) + 0)) + +(define (main) + (let ((lines (file->list "input.txt"))) + (display "Part1: ") + (display (part1 lines)) + (newline) + (display "Part2: ") + (display (part2 lines)) + (newline))) + +(main) -- cgit v1.2.3-70-g09d2