aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Marín Noguera <juan@mnpi.eu>2024-12-05 13:24:52 +0100
committerJuan Marín Noguera <juan@mnpi.eu>2024-12-05 13:24:52 +0100
commit84c986dffe7a85c87b80e6d736b3b36e460ff4dd (patch)
tree026e817a43a6a55e4fae4c1193e3945eb43e7b53
parent06b1f2b0f5d84958897d39e85d099e4e8cce4546 (diff)
Update instructions to apply to Guile
Also rename `main` to `main.lua` and `main.scm` to `main`.
-rw-r--r--README.md12
-rwxr-xr-xmain64
-rwxr-xr-xmain.lua29
-rwxr-xr-xmain.scm35
4 files changed, 73 insertions, 67 deletions
diff --git a/README.md b/README.md
index e2c5cc5..0c29b03 100644
--- a/README.md
+++ b/README.md
@@ -1,16 +1,22 @@
# Advent of code 2024
-These are my solutions for Advent of Code in 2024. This year I'm using Lua as the programming language. To launch the program for a given day, run
+These are my solutions for Advent of Code in 2024. This year I'm using Guile
+Scheme as the programming language. To launch the program for a given day, run
```sh
./main <<ID>> [<<FILE>>]
```
-where `<<ID>>` is the day number followed by `a` for the first part or `b` for the second part. If the file is not specified, it defaults to the number of the day with two digits and extension `.in`, i.e. `01.in`. One can use the name of a file that doesn't exist, e.g. `-`, to read from standard input.
+where `<<ID>>` is the day number followed by `a` for the first part or `b` for
+the second part. If the file is not specified, it defaults to the number of the
+day with two digits and extension `.in`, i.e. `01.in`. To read from standard
+input, use `-` as the file name.
Example:
```sh
-./main 01b input
+./main 05b -
```
+Some of the first days are written in Lua rather than Guile Scheme. To run
+them, simply replace `main` with `main.lua`.
diff --git a/main b/main
index 38189ee..39f8da9 100755
--- a/main
+++ b/main
@@ -1,29 +1,35 @@
-#!/usr/bin/lua
-
-if #arg < 1 or #arg > 2 then
- error ("Syntax: %s <DAY><a|b> [inputfile]"):format(arg[0])
- os.exit(1)
-end
-
-local num, letter = arg[1]:match("^(%d+)([ab])$")
-if not num then
- error ('Expected <DAY><a|b> as first argument (e.g. "01b"), got: %s'):format(arg[1])
- os.exit(1)
-end
-num = tonumber(num)
-
-local script = ("%02d.lua"):format(num)
-local infile = arg[2] or ("%02d.in"):format(num)
-local input_stream = io.open(infile)
-if input_stream then
- io.input(input_stream)
-else
- print "Reading from standard input."
-end
-
-local parsed = loadfile(script)()
-if letter == 'a' then
- parsed:part1()
-else
- parsed:part2()
-end
+#!/usr/bin/env guile
+!#
+
+(use-modules (ice-9 format) (ice-9 regex))
+
+(define args (program-arguments))
+
+(unless (<= 2 (length args) 3)
+ (error (format #f "Syntax: ~a <DAY><a|b> [INFILE]\n" (car args)))
+ (exit 1))
+
+(define mtch (string-match "^([0-9]+)(a|b)$" (list-ref args 1)))
+
+(unless mtch
+ (error (format #f "Expected day and part (like \"03a\" or \"02b\"), got: ~a"
+ (list-ref args 1))))
+
+(define day (format #f "~2,'0d" (string->number (match:substring mtch 1))))
+(define part (if (string= (match:substring mtch 2) "a") 'part1 'part2))
+
+(set! %load-path (cons "." %load-path))
+
+(define day-module
+ (resolve-module (list (string->symbol (string-append "day" day)))))
+(define func (module-ref day-module part))
+
+(define input-file
+ (cond ((= (length args) 2) (string-append day ".in"))
+ ((string= (list-ref args 2) "-") #f)
+ (else (list-ref args 2))))
+
+(display (if input-file
+ (call-with-input-file input-file func)
+ (func (current-input-port))))
+(newline)
diff --git a/main.lua b/main.lua
new file mode 100755
index 0000000..38189ee
--- /dev/null
+++ b/main.lua
@@ -0,0 +1,29 @@
+#!/usr/bin/lua
+
+if #arg < 1 or #arg > 2 then
+ error ("Syntax: %s <DAY><a|b> [inputfile]"):format(arg[0])
+ os.exit(1)
+end
+
+local num, letter = arg[1]:match("^(%d+)([ab])$")
+if not num then
+ error ('Expected <DAY><a|b> as first argument (e.g. "01b"), got: %s'):format(arg[1])
+ os.exit(1)
+end
+num = tonumber(num)
+
+local script = ("%02d.lua"):format(num)
+local infile = arg[2] or ("%02d.in"):format(num)
+local input_stream = io.open(infile)
+if input_stream then
+ io.input(input_stream)
+else
+ print "Reading from standard input."
+end
+
+local parsed = loadfile(script)()
+if letter == 'a' then
+ parsed:part1()
+else
+ parsed:part2()
+end
diff --git a/main.scm b/main.scm
deleted file mode 100755
index 39f8da9..0000000
--- a/main.scm
+++ /dev/null
@@ -1,35 +0,0 @@
-#!/usr/bin/env guile
-!#
-
-(use-modules (ice-9 format) (ice-9 regex))
-
-(define args (program-arguments))
-
-(unless (<= 2 (length args) 3)
- (error (format #f "Syntax: ~a <DAY><a|b> [INFILE]\n" (car args)))
- (exit 1))
-
-(define mtch (string-match "^([0-9]+)(a|b)$" (list-ref args 1)))
-
-(unless mtch
- (error (format #f "Expected day and part (like \"03a\" or \"02b\"), got: ~a"
- (list-ref args 1))))
-
-(define day (format #f "~2,'0d" (string->number (match:substring mtch 1))))
-(define part (if (string= (match:substring mtch 2) "a") 'part1 'part2))
-
-(set! %load-path (cons "." %load-path))
-
-(define day-module
- (resolve-module (list (string->symbol (string-append "day" day)))))
-(define func (module-ref day-module part))
-
-(define input-file
- (cond ((= (length args) 2) (string-append day ".in"))
- ((string= (list-ref args 2) "-") #f)
- (else (list-ref args 2))))
-
-(display (if input-file
- (call-with-input-file input-file func)
- (func (current-input-port))))
-(newline)