don't break puzzles with leading spaces

This commit is contained in:
mehbark 2025-04-28 17:57:11 -04:00
parent b9ec9518db
commit 8d7babec4d

View file

@ -1,6 +1,8 @@
#! /usr/bin/env guile -e main -s #! /usr/bin/env guile -e main -s
!# !#
;; TODO: make this a module
(use-modules (srfi srfi-1) (use-modules (srfi srfi-1)
(srfi srfi-9 gnu) (srfi srfi-9 gnu)
(srfi srfi-11) (srfi srfi-11)
@ -107,9 +109,23 @@
;; board = (player-pos (wall goal box)-bitsets) ;; board = (player-pos (wall goal box)-bitsets)
(define (string->board s) (define (string->board s)
;; string-trim-both instead of filter (compose not string-null?) keeps internal ;; string-trim alone doesn't work because e.g.
;; blanks while removing (almost always) useless leading and trailing blanks ;; ###
(define lines (list->vector (string-split (string-trim-both s) #\newline))) ;; ###.#
;; #@ $#
;; # #
;; #####
;; becomes
;; ###
;; ###.#
;; #@ $#
;; # #
;; #####
;; which is just no good
(define lines (list->vector
(string-split (string-trim-both s (lambda (c) (eqv? c #\newline)))
#\newline)))
(define widths (vector-map (lambda (_ line) (string-length line)) lines)) (define widths (vector-map (lambda (_ line) (string-length line)) lines))
(define width (apply max (vector->list widths))) (define width (apply max (vector->list widths)))
(define height (vector-length lines)) (define height (vector-length lines))
@ -276,9 +292,6 @@
")) "))
(define (main . args) (define (main args)
(define-values (cost node) (min-steps (string->board (get-string-all (current-input-port))))) (define-values (cost node) (min-steps (string->board (get-string-all (current-input-port)))))
(format #t "~a\n~a\n" cost node)) (format #t "~a\n~a\n" cost node))
;; TODO: lol i have immutable board movement, so just make this actually playable
;; with super easy undo lol