diff options
author | Matias Linares <matias.linares@comprandoengrupo.net> | 2024-12-03 10:19:28 -0300 |
---|---|---|
committer | Matias Linares <matias.linares@comprandoengrupo.net> | 2024-12-03 10:19:28 -0300 |
commit | 789d38b8594cf73a8f6ed306b8ed90529f9c1210 (patch) | |
tree | 5560e25c3f1a5a5eea67fa759d4736c97fe369af | |
parent | 2e633775d14eaa246af901b0d64542192f3c2810 (diff) | |
download | advent-of-code-789d38b8594cf73a8f6ed306b8ed90529f9c1210.tar.gz |
-rw-r--r-- | 2024/aoc-utils.scm | 4 | ||||
-rwxr-xr-x | 2024/day03/main.scm | 52 |
2 files changed, 56 insertions, 0 deletions
diff --git a/2024/aoc-utils.scm b/2024/aoc-utils.scm index 98e83cd..5deb269 100644 --- a/2024/aoc-utils.scm +++ b/2024/aoc-utils.scm @@ -4,6 +4,7 @@ #:use-module (ice-9 regex) #:use-module (ice-9 rdelim) #:export (file->list + file->str lines->number-list)) (define (file->list filename) @@ -11,6 +12,9 @@ ; Remove EOF token (take lines (1- (length lines))))) +(define (file->str filename) + (call-with-input-file filename read-string)) + (define (lines->number-list lines) (map (lambda (line) (map string->number (string-split line #\Space))) diff --git a/2024/day03/main.scm b/2024/day03/main.scm new file mode 100755 index 0000000..1a8505d --- /dev/null +++ b/2024/day03/main.scm @@ -0,0 +1,52 @@ +#!/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 mul-rx (make-regexp "mul\\(([0-9]+),([0-9]+)\\)")) + +(define (mul str) + (let ((match (regexp-exec mul-rx str))) + (* (string->number (match:substring match 1)) + (string->number (match:substring match 2))))) + +(define (not-corrupted-mults memory) + (map match:substring (list-matches mul-rx memory))) + +(define (part1 memory) + (apply + (map mul (not-corrupted-mults memory)))) + +(define or-rx (make-regexp "do\\(\\)|don't\\(\\)|mul\\([0-9]+,[0-9]+\\)")) + +(define (part2 memory) + (let ((tokens (map match:substring (list-matches or-rx memory))) + (enabled? #t)) + (apply + (map (lambda (match) + (cond ((string= "do()" match) (begin + (set! enabled? #t) + 0)) + ((string= "don't()" match) (begin + (set! enabled? #f) + 0)) + (else (if enabled? + (mul match) + 0)))) + tokens)))) + +(define (main) + (let ((memory (file->str "input.txt"))) + (display "Part1: ") + (display (part1 memory)) + (newline) + (display "Part2: ") + (display (part2 memory)) + (newline))) + +(main) |