From a88e8da779cd09c398723e4e87e79d945af0994e Mon Sep 17 00:00:00 2001 From: brian Date: Sat, 3 Aug 2024 13:33:43 -0700 Subject: initial commit to day 6 --- .gitignore | 1 + day1.scm | 22 ++++++++++++++++ day2.scm | 53 +++++++++++++++++++++++++++++++++++++ day3.scm | 55 ++++++++++++++++++++++++++++++++++++++ day4.scm | 35 ++++++++++++++++++++++++ day5.scm | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ day6.scm | 24 +++++++++++++++++ 7 files changed, 279 insertions(+) create mode 100644 .gitignore create mode 100755 day1.scm create mode 100755 day2.scm create mode 100755 day3.scm create mode 100755 day4.scm create mode 100755 day5.scm create mode 100755 day6.scm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b9a1e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +inputs/ diff --git a/day1.scm b/day1.scm new file mode 100755 index 0000000..ca79a09 --- /dev/null +++ b/day1.scm @@ -0,0 +1,22 @@ +#!/usr/bin/env gosh +(use file.util) + +(define (read-input filename) + (map string->number (file->string-list filename))) + +(define (part1 input) + (fold + 0 (map (lambda (x) (- (floor (/ x 3)) 2)) input))) + +(define (calculate-fuel weight acc) + (if (< weight 9) + acc + (let ((fuel-weight (- (floor (/ weight 3)) 2))) + (calculate-fuel fuel-weight (+ acc fuel-weight))))) + +(define (part2 input) + (fold + 0 (map (lambda (x) (calculate-fuel x 0)) input))) + +(define (main args) + (let ((input (read-input "inputs/day1.txt"))) + (print (part1 input)) + (print (part2 input)))) diff --git a/day2.scm b/day2.scm new file mode 100755 index 0000000..bb6571f --- /dev/null +++ b/day2.scm @@ -0,0 +1,53 @@ +#!/usr/bin/env gosh +(use file.util) + +(define (read-input filename) + (map string->number (string-split (file->string filename) ","))) + +(define (run-opcode vec pos) + (let ((opcode (vector-ref vec pos))) + (cond + ((= opcode 1) + (vector-set! + vec + (vector-ref vec (+ pos 3)) + (+ (vector-ref vec (vector-ref vec (+ pos 1))) + (vector-ref vec (vector-ref vec (+ pos 2))))) + (run-opcode vec (+ pos 4))) + ((= opcode 2) + (vector-set! + vec + (vector-ref vec (+ pos 3)) + (* (vector-ref vec (vector-ref vec (+ pos 1))) + (vector-ref vec (vector-ref vec (+ pos 2))))) + (run-opcode vec (+ pos 4))) + ((= opcode 99) vec)))) + +(define (run-program input noun verb) + (let ((input-vector (list->vector input))) + (vector-set! input-vector 1 noun) + (vector-set! input-vector 2 verb) + (vector-ref (run-opcode input-vector 0) 0))) + +(define (part1 input) + (run-program input 12 2)) + +(define cartesian-product (lambda (xs ys) + (if (or (zero? (length xs)) (zero? (length ys))) + '() + (fold append '() (map (lambda (x) (map (lambda (y) (cons x y)) ys)) xs))))) + +(define (part2 input) + (letrec ((nouns (iota 100)) + (verbs (iota 100)) + (output (find + (lambda (x) + (= 19690720 + (run-program input (car x) (cdr x)))) + (cartesian-product nouns verbs)))) + (+ (cdr output) (* 100 (car output))))) + +(define (main args) + (let ((input (read-input "inputs/day2.txt"))) + (print (part1 input)) + (print (part2 input)))) \ No newline at end of file diff --git a/day3.scm b/day3.scm new file mode 100755 index 0000000..21fadf0 --- /dev/null +++ b/day3.scm @@ -0,0 +1,55 @@ +#!/usr/bin/env gosh +(use file.util) +(use util.match) +(use scheme.set) +(use gauche.sequence) + +(define (read-input filename) + (let ((parse-entry + (lambda (x) + (cons (string-ref x 0) + (string->number (substring x 1 (string-length x))))))) + (map (lambda (x) (map parse-entry x)) + (map (lambda (x) (string-split x ",")) + (file->string-list filename))))) + +(define (get-path steps x y acc) + (if (equal? '() steps) + acc + (match (car steps) + [`(#\U . ,distance) + (get-path (cdr steps) (+ x distance) y (append acc (map (lambda (dx) (cons (+ x dx) y) ) (iota distance 1))))] + [`(#\D . ,distance) + (get-path (cdr steps) (- x distance) y (append acc (map (lambda (dx) (cons (- x dx) y) ) (iota distance 1))))] + [`(#\L . ,distance) + (get-path (cdr steps) x (- y distance) (append acc (map (lambda (dy) (cons x (- y dy)) ) (iota distance 1))))] + [`(#\R . ,distance) + (get-path (cdr steps) x (+ y distance) (append acc (map (lambda (dy) (cons x (+ y dy)) ) (iota distance 1))))]))) + +(define (intersections steps) + (set->list (apply set-intersection + (map (lambda (list) (list->set equal-comparator (get-path list 0 0 '()))) + steps)))) + +(define (part1 input) + (letrec ((manhattan-distance + (lambda (x) + (+ (abs (car x)) (abs (cdr x)))))) + (car (sort (map manhattan-distance (intersections input)))))) + +(define (part2 input) + (letrec ((first-wire + (get-path (car input) 0 0 '())) + (second-wire + (get-path (cadr input) 0 0 '())) + (linear-distance + (lambda (x) + (+ (find-index (lambda (y) (equal? x y)) first-wire) + (find-index (lambda (y) (equal? x y)) second-wire) + 2)))) + (car (sort (map linear-distance (intersections input)))))) + +(define (main args) + (let ((input (read-input "inputs/day3.txt"))) + (print (part1 input)) + (print (part2 input)))) \ No newline at end of file diff --git a/day4.scm b/day4.scm new file mode 100755 index 0000000..29eefb0 --- /dev/null +++ b/day4.scm @@ -0,0 +1,35 @@ +#!/usr/bin/env gosh +(use scheme.set) + +(define max-num 746325) +(define min-num 264360) +(define num-range (iota (- max-num min-num -1) min-num)) + +(define (digit-list number) (map digit->integer (string->list (number->string number)))) + +(define (valid-pw? number) + (letrec ((num-list (digit-list number)) + (sorted-num-list (sort num-list)) + (num-set (list->set eq-comparator num-list))) + (and + (> (length num-list) (set-size num-set)) + (equal? sorted-num-list num-list)))) + +(define part1 + (length (filter valid-pw? num-range))) + +(define (valid-count? number) + (letrec ((num-list (digit-list number)) + (counts + (fold + (lambda (element hashtable) (hash-table-update! hashtable element (cut + 1 <>) 0) hashtable) + (make-hash-table eq-comparator) + num-list))) + (any (lambda (x) (= x 2)) (hash-table-values counts)))) + +(define part2 + (length (filter (lambda (x) (and (valid-pw? x) (valid-count? x))) num-range))) + +(define (main args) + (print part1) + (print part2)) diff --git a/day5.scm b/day5.scm new file mode 100755 index 0000000..3c35310 --- /dev/null +++ b/day5.scm @@ -0,0 +1,89 @@ +#!/usr/bin/env gosh +(use file.util) + +(define (read-input filename) + (map string->number (string-split (file->string filename) ","))) + +(define (run-opcode vec pos) + (letrec ((instruction (vector-ref vec pos)) + (opcode (mod instruction 100)) + (mode1 (= 1 (div (mod instruction 1000) 100))) + (mode2 (= 1 (div (mod instruction 10000) 1000))) + (mode3 (= 1 (div instruction 10000))) + (parameter + (lambda (x) + (cond + ((= x 1) + (if mode1 + (vector-ref vec (+ pos 1)) + (vector-ref vec (vector-ref vec (+ pos 1))))) + ((= x 2) + (if mode2 + (vector-ref vec (+ pos 2)) + (vector-ref vec (vector-ref vec (+ pos 2))))) + ((= x 3) + (if mode3 + (vector-ref vec (+ pos 3)) + (vector-ref vec (vector-ref vec (+ pos 3))))))))) + (print "opcode: " opcode) + (print "real params: " + (vector-ref vec (+ pos 1)) " " + (vector-ref vec (+ pos 2)) " " + (vector-ref vec (+ pos 3))) + (print "resolved params: " + (parameter 1) " " + (parameter 2) " " + (parameter 3)) + (print vec) + (cond + ((= opcode 1) + (vector-set! + vec + (vector-ref vec (+ pos 3)) + (+ (parameter 1) + (parameter 2))) + (run-opcode vec (+ pos 4))) + ((= opcode 2) + (vector-set! + vec + (vector-ref vec (+ pos 3)) + (* (parameter 1) + (parameter 2))) + (run-opcode vec (+ pos 4))) + ((= opcode 3) + (vector-set! + vec + (vector-ref vec (+ pos 1)) + (string->number (read-line))) + (run-opcode vec (+ pos 2))) + ((= opcode 4) + (print (parameter 1)) + (run-opcode vec (+ pos 2))) + ((= opcode 5) + (if (= (parameter 1) 0) + (run-opcode vec (+ pos 3)) + (run-opcode vec (parameter 2)))) + ((= opcode 6) + (if (= (parameter 1) 0) + (run-opcode vec (parameter 2)) + (run-opcode vec (+ pos 3)))) + ((= opcode 7) + (if (< (parameter 1) (parameter 2)) + (vector-set! vec (parameter 3) 1) + (vector-set! vec (parameter 3) 0)) + (run-opcode vec (+ pos 4))) + ((= opcode 8) + (if (= (parameter 1) (parameter 2)) + (vector-set! vec (parameter 3) 1) + (vector-set! vec (parameter 3) 0)) + (run-opcode vec (+ pos 4))) + ((= opcode 99) + vec) + (else (print "this shouldn't happen"))))) + +(define (part1 input) + (run-opcode input 0)) + +(define (main args) + (let ((input (read-input "inputs/day5test2.txt"))) + (part1 (list->vector input)))) \ No newline at end of file diff --git a/day6.scm b/day6.scm new file mode 100755 index 0000000..419d687 --- /dev/null +++ b/day6.scm @@ -0,0 +1,24 @@ +#!/usr/bin/env gosh +(use file.util) + +(define (read-input filename) + (let ((tree (make-hash-table string-comparator))) + (for-each + (lambda (x) (hash-table-push! tree (car x) (cadr x))) + (map + (cut string-split <> ")") + (file->string-list filename))) + tree)) + +(define (count-branch-length mapping key length) + (if (hash-table-contains? mapping key) + (fold + 0 (map (cut count-branch-length mapping <> (+ length 1)) + (hash-table-ref mapping key))) + (fold + 0 (iota length 1)))) + +(define (part1 input) + (count-branch-length input "COM" 0)) + +(define (main args) + (let ((input (read-input "inputs/day6test.txt"))) + (print (part1 input)))) \ No newline at end of file -- cgit 1.4.1-2-gfad0