diff options
| author | Juan Marín Noguera <juan@mnpi.eu> | 2024-12-05 13:24:52 +0100 |
|---|---|---|
| committer | Juan Marín Noguera <juan@mnpi.eu> | 2024-12-05 13:24:52 +0100 |
| commit | 84c986dffe7a85c87b80e6d736b3b36e460ff4dd (patch) | |
| tree | 026e817a43a6a55e4fae4c1193e3945eb43e7b53 /main | |
| parent | 06b1f2b0f5d84958897d39e85d099e4e8cce4546 (diff) | |
Update instructions to apply to Guile
Also rename `main` to `main.lua` and `main.scm` to `main`.
Diffstat (limited to 'main')
| -rwxr-xr-x | main | 64 |
1 files changed, 35 insertions, 29 deletions
@@ -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) |
