summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2024-06-09 22:02:45 -0600
committerbd <bdunahu@operationnull.com>2024-06-09 22:02:45 -0600
commite61508e4764d69a4ea0073f419b89ff0a06e813e (patch)
treeb2f49eeced70c552c73e4030c87406f47a0a8f5e
parentaec326408dd6ad16347b5661112895e60335893a (diff)
AoC 2015.1 p1+2
-rw-r--r--not-quite-lisp/README.org20
-rw-r--r--not-quite-lisp/input.txt1
-rw-r--r--not-quite-lisp/main.scm20
-rw-r--r--not-quite-lisp/nql-test.scm51
-rw-r--r--not-quite-lisp/nql.scm33
5 files changed, 125 insertions, 0 deletions
diff --git a/not-quite-lisp/README.org b/not-quite-lisp/README.org
new file mode 100644
index 0000000..d701543
--- /dev/null
+++ b/not-quite-lisp/README.org
@@ -0,0 +1,20 @@
+See: https://adventofcode.com/2015/day/1
+** Part 1
+*** Purpose
+Given a stream of parenthesis:
+
+#+begin_example
+))(((((
+#+end_example
+
+We subtract the number of "((" parens from the number of ")))".
+
+*** Method
+The solution to this is simple: count the number of each, and perform the subtraction. Given that I already solved part on of this problem before, I will instead immediately design a solution to the problem that also works for part 2. In my attempt months ago, I made the "mistake" of not using a loop :):
+
+** Part 2
+*** Purpose
+Now, we need to calculate the index of the input stream where the cumulative number is first zero.
+
+*** Method
+The above information reveals that a solution that would work for both parts is a simple loop structure.
diff --git a/not-quite-lisp/input.txt b/not-quite-lisp/input.txt
new file mode 100644
index 0000000..f7beb58
--- /dev/null
+++ b/not-quite-lisp/input.txt
@@ -0,0 +1 @@
+((((()(()(((((((()))(((()((((()())(())()(((()((((((()((()(()(((()(()((())))()((()()())))))))))()((((((())((()))(((((()(((((((((()()))((()(())()((())((()(()))((()))()))()(((((()(((()()))()())((()((((())()())()((((())()(()(()(((()(())(()(())(((((((())()()(((())(()(()(()(())))(()((((())((()))(((()(()()(((((()()(()(((()(((((())()))()((()(()))()((()((((())((((())(()(((())()()(()()()()()(())((((())((())(()()))()((((())))((((()())()((((())((()())((())(())(((((()((((()(((()((((())(()(((()()))()))((((((()((())()())))(((()(()))(()()(()(((()(()))((()()()())((()()()(((())())()())())())((()))(()(()))(((((()(()(())((()(())(())()((((()())()))((((())(())((())())((((()(((())(())((()()((((()((((((()(())()()(()(()()((((()))(())()())()))(())))(())))())()()(())(()))()((()(()(())()()))(()())))))(()))(()()))(())(((((()(()(()()((())()())))))((())())((())(()(())((()))(())(((()((((((((()()()(()))()()(((()))()((()()(())(())())()(()(())))(((((()(())(())(()))))())()))(()))()(()(((((((()((((())))())())())())()((((((((((((((()()((((((()()()())())()())())())(())(())))())((()())((()(()))))))()))))))))))))))))())((())((())()()))))))(((()((()(()()))((())(()()))()()())))(())))))))(()(((())))())()())))()()(())()))()(()))())((()()))))(()))))()))(()()(())))))))()(((()))))()(()))(())())))))()))((()))((()))())(())))))))))((((())()))()))()))())(())()()(())))())))(()())()))((()()(())))(())((((((()(())((()(((()(()()(())))()))))))()))()(()((()))()(()))(()(((())((((())())(())(()))))))))())))))))())())))))())))))()()(((())()(()))))))))())))))(())()()()))()))()))(()(())()()())())))))))())()(()(()))))()()()))))())(()))))()()))))()())))))(((())()()))(()))))))))))()()))))()()()))))(()())())()()())()(()))))()(()))(())))))))(((((())(())())()()))()()))(())))))()(()))))(())(()()))()())()))()))()))()))))())()()))())())))(()))(()))))))())()(((())()))))))))()))()())))())))())))()))))))))))()()))(()()))))))(())()(()))))())(()))))(()))))(()())))))())())()()))))())()))))))))(()))))()))))))()(()())))))))()))())))())))())))())))))))())(()()))))))(()())())))()())()))))))))))))))())))()(())))()))())()()(())(()()))(())))())()())(()(()(()))))())))))))))))())(()))()))()))))(())()())()())))))))))))()()))))))))))))())())))))(()())))))))))))())(())))()))))))))())())(()))()))(())))()))()()(())()))))))()((((())()))())())))))()))()))))((()())()))))())))(())))))))))))))))))()))))()()())()))()()))))())()))((()())))())))(()))(()())))))))()))()))))(())))))))(())))))())()()(()))())()))()()))))())()()))))())()))())))))))(()))))()())()))))))))(()))())))(()))()))))(())()))())())(())())())))))))((((())))))()))()))()())()(())))()))()))()())(()())()()(()())()))))())())))))(()))()))))())(()()(())))))(())()()((())())))))(())(())))))))())))))))))()(())))))))()())())())()(()))))))))(()))))))))())()()))()(()))))))()))))))())))))))(())))()()(())()())))))(((())))()((())()))())))(()()))())(())())))()(((()())))))()(()()())))()()(()()(()()))())()(()()()))())()()))()())(()))))())))))())))(())()()))))(()))))(())(()))(())))))()()))()))))())()))()()(())())))((()))())()))))))()()))))((()(()))))()()))))))())))))())(()((()())))))))))))()())())))()))(()))))))(()))(())()())))(()))))))))())()()()()))))(()())))))))((())))()))(()))(())(())()())()))))))))(())))())))(()))()()))(()()))(()))())))()(())))())((()((()(())))((())))()))))((((())())()())))(())))()))))))())(()()((())))())()(()())))))(()())()))())))))))((())())))))))(()(()))())()()(()()(((()(((()())))))()))))))()(())(()()((()()(())()()))())()())()))()())())())))))))(((())))))))()()))))))(((())()))(()()))(()()))))(()(()()((((())()())((()()))))(()(())))))()((()()()())()()((()((()()))(()))(((()()()))(((())))()(((())()))))))((()(())())))(()())(((((()(()))(()((()))(()())()))))(()(()))()(()))(())(((())(()()))))()()))(((()))))(()()()()))())))((()()()(())()))()))))()()))()))))))((((((()()()))))())((()()(((()))))(()(())(()()())())())))()(((()()))(())((())))(()))(()()()())((())())())(()))))()))()((()(())()(()()(())(()))(())()))(())(()))))(())(())())(()()(()((()()((())))((()))()((())))(((()()()()((((()))(()()))()()()(((())((())())(()()(()()()))()((())(())()))())(((()()(())))()((()()())()())(()(())())(((())(())())((())(())()(((()()))(())))((())(()())())(())((()()()((((((())))((()(((((())()))()))(())(()()))()))(())()()))(())((()()())()()(()))())()((())))()((()()())((((()())((())())())((()((()))()))((())((()()(()((()()(((())(()()))))((()((())()(((())(()((())())((())(()((((((())())()(()())()(())(((())((((((()(())(()((()()()((()()(()()()())))()()(((((()()))()((((((()))()(()(()(()(((()())((()))())()((()))(())))()))()()))())()()))())((((())(()(()))(((((((())(((()(((((()(((()()((((())(((())())))(()()()(()(()))()))((((((()))((()(((()(())((()((((()((((((())(((((())))(((()(()))))(((()(((())()((())(()((()))(((()()(((())((((()(()(((((()))(((()(((((((()(()()()(()(()(()()())(())(((((()(())())()())(()(()(()))()(()()()())(()()(()((()))()((())())()(()))((())(()))()(()))()(((()(()(()((((((()()()()())()(((((()()(((()()()((()(((((()))((((((((()()()(((((()))))))(()()()(())(()))(()()))))(())()))(((((()(((((()()(()(()())(((()))((((()((()(()(()((()(()((())))()(((()((()))((()))(((((((((()((()((()(())))()((((()((()()))((())(((()(((((()()(()(()()((()(()()()(((((((())())()())))))((((()()(()))()))(()((())()(()(((((((((()()(((()(()())(()((()())((())())((((()(((()(((()((((()((()((((()(()((((((())((((((((((((()()(()()((((((((((((((()((()()))()((((((((((((())((((()(()())((()(()(()))()(((((()()(((()()))()())(())((()(((((()((())(((((()((()(((((()))()()((((())()((((())(((((((((()(())(()(())))())(()((())(((())(())(())())(()(()(())()()((()((())()(((()(((((()(())))()(((()((())))((()()()(((()(((()((()(()(())(()((()())(()(()(((()(((((((((())(()((((()()))(()((((()()()()(((()((((((((()(()()((((((()(()()(()((()((((((((((()()(((((((()())(())))(((()()))(((((()((()()())(()()((((())((()((((()))))(())((()(()()(((()(()(((()((((()(((((()))())())(()((())()))(((()())((())((())((((()((()((((((())(()((((()()))((((((())()(()))((()(((())((((((((((()()(((((()(((((()((()()()((((())))(()))()((()(())()()((()((((((((((()((())(())(((((()(()(()()))((((()((((()()((()(((()(((((((((()(()((()((()))((((((()(((())()()((()(((((((()())))()()(()((()((()()(((()(()()()()((((()((())((((()(((((((((()(((()()(((()(()(((()(((()((())()(()((()(()(()(()))()(((()))(()((((()((())((((())((((((())(()))(()((((())((()(()((((((((()()((((((()(()(()()()(())((()((()()(((()(((((((()()((()(((((((()))(((((()(((()(()()()(()(((()((()()((())(()(((((((((()(()((()((((((()()((())()))(((((()((())()())()(((((((((((()))((((()()()()())(()()(()(()()))()))(()))(()(((()()))())(()(()))()()((())(()())()())()(()))()))(()()(()((((((())((()(((((((((((()(())()((()(()((()((()(()((()((((((((((()()())((())()(())))((())()())()(((((()(()())((((()((()(())(()))(((())()((()))(((((())(()))()()(()))(((())((((()((((()(())))(((((((()))))())()())(())((())()(()()((()(()))()(()()(()()((()())((())((()()))((((()))()()))(()()(())()()(((((()(())((()((((()))()))(()())())(((()()(()()))(())))))(()))((())(((((()((((()))()((((()))()((())(((())))(((()())))((()(()()(( \ No newline at end of file
diff --git a/not-quite-lisp/main.scm b/not-quite-lisp/main.scm
new file mode 100644
index 0000000..2d54c54
--- /dev/null
+++ b/not-quite-lisp/main.scm
@@ -0,0 +1,20 @@
+;; -*- compile-command: "guile -L . -e main -s main.scm < input.txt"; -*-
+(use-modules (nql)
+ ((ice-9 rdelim))
+ (ice-9 binary-ports))
+
+
+(define (stdin-to-str)
+ (let loop ((result ""))
+ (let ((line (read-line)))
+ (if (eof-object? line)
+ result
+ (loop (string-append result line "\n"))))))
+
+(define (main args)
+ (let ((result
+ (if (null? (cdr args))
+ (nql (stdin-to-str) #t)
+ (nql (stdin-to-str) #f))))
+ (display result))
+ (newline))
diff --git a/not-quite-lisp/nql-test.scm b/not-quite-lisp/nql-test.scm
new file mode 100644
index 0000000..101ae36
--- /dev/null
+++ b/not-quite-lisp/nql-test.scm
@@ -0,0 +1,51 @@
+;; -*- compile-command: "guile -L . nql-test.scm"; -*-
+(use-modules (srfi srfi-64)
+ (nql))
+
+(test-begin "harness")
+
+
+(test-equal "( is 1"
+ 1
+ (paren->number #\())
+
+(test-equal ") is -1"
+ -1
+ (paren->number #\)))
+
+
+(test-equal "? is 0"
+ 0
+ (paren->number #\?))
+
+
+(test-equal "count all even"
+ 0
+ (nql "(())" #t))
+
+(test-equal "count all 3"
+ 3
+ (nql "(((" #t))
+
+(test-equal "count all 5-2"
+ 3
+ (nql "(()(()(" #t))
+
+(test-equal "count all -2+1"
+ -1
+ (nql "())" #t))
+
+(test-equal "first-negative never"
+ #f
+ (nql "(" #f))
+
+(test-equal "first-negative immediate"
+ 1
+ (nql ")" #f))
+
+(test-equal "first-negative five"
+ 5
+ (nql "()())" #f))
+
+
+(test-end "harness")
diff --git a/not-quite-lisp/nql.scm b/not-quite-lisp/nql.scm
new file mode 100644
index 0000000..3863f02
--- /dev/null
+++ b/not-quite-lisp/nql.scm
@@ -0,0 +1,33 @@
+(define-module (nql)
+ #:use-module (srfi srfi-13)
+ #:export (nql
+ paren->number))
+
+
+(define (nql str count-all?)
+ "GIven string of parens STR,
+calculates '(' - ')' when
+COUNT-ALL is #t..
+
+If COUNT-ALL? is #f, returns the
+index of the first time a negative
+number is reached."
+ (let loop ((floor 0) (iteration 0) (lst (map paren->number
+ (string->list str))))
+ (cond
+ ((and (not count-all?)
+ (> 0 floor)) iteration)
+ ((null? lst) (and count-all? floor))
+ (#t (loop (+ (car lst) floor)
+ (1+ iteration)
+ (cdr lst))))))
+
+(define (paren->number char)
+ "Given a character representing a
+parenthesis, returns 1 for '(' and
+-1 for ')'.
+Returns zero for all other characters."
+ (cond
+ ((char=? char #\() 1)
+ ((char=? char #\)) -1)
+ (#t 0)))