From 9b8ae3acfcab1fa3925a0faed3d301dda4d8b1e4 Mon Sep 17 00:00:00 2001 From: cow Date: Wed, 14 Dec 2022 02:56:35 -0500 Subject: [PATCH] day 6 part 1 in scheme --- scheme/advent-of-code.scm | 34 +++++++++++++++++++++++++++++ scheme/day-6/input.txt | 1 + scheme/day-6/solution.scm | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 scheme/advent-of-code.scm create mode 120000 scheme/day-6/input.txt create mode 100755 scheme/day-6/solution.scm diff --git a/scheme/advent-of-code.scm b/scheme/advent-of-code.scm new file mode 100644 index 0000000..72344d9 --- /dev/null +++ b/scheme/advent-of-code.scm @@ -0,0 +1,34 @@ +;; 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 . + +(define-module (advent-of-code) + #:export (read-file + read-file-lines + should-equal)) + +(use-modules (ice-9 textual-ports)) + +(define (read-file file) + (with-input-from-file file + (lambda () + (get-string-all (current-input-port))) + #:encoding "utf-8")) + +(define (read-file-lines file) + (string-split (read-file file) #\newline)) + +(define (should-equal a b) + (unless (equal? a b) + (error (format #f "Expected ~A\nActual ~A\n" a b)))) diff --git a/scheme/day-6/input.txt b/scheme/day-6/input.txt new file mode 120000 index 0000000..58d542f --- /dev/null +++ b/scheme/day-6/input.txt @@ -0,0 +1 @@ +../../input/day-6.txt \ No newline at end of file diff --git a/scheme/day-6/solution.scm b/scheme/day-6/solution.scm new file mode 100755 index 0000000..07615ed --- /dev/null +++ b/scheme/day-6/solution.scm @@ -0,0 +1,45 @@ +#!/usr/bin/env -S guile -s +!# + +;; 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 . + +(use-modules (srfi srfi-1) + (ice-9 match) + (advent-of-code)) + +(define (all-different? list) + (match list + (() '()) + ((_) #t) + ((head . tail) (if (member head tail) + #f + (all-different? tail))))) + +(define (solution input marker-length) + (let loop ((position 0)) + (let ((marker (take (drop input position) marker-length))) + (if (all-different? marker) + (+ position marker-length) + (loop (+ position 1)))))) + +(should-equal (all-different? '(a b c d)) #t) +(should-equal (all-different? '(a a b c)) #f) +(should-equal 7 (solution (string->list "mjqjpqmgbljsphdztnvjfqwrcgsmlb") 4)) +(should-equal 5 (solution (string->list "bvwbjplbgvbhsrlpgdmjqwftvncz") 4)) +(should-equal 6 (solution (string->list "nppdvjthqldpwncqszvftbrmjlhg") 4)) +(should-equal 10 (solution (string->list "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg") 4)) +(should-equal 11 (solution (string->list "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw") 4)) +(should-equal 1794 (solution (string->list (read-file "input.txt")) 4))