Compare commits
3 Commits
98336be2d7
...
7662c00cf9
| Author | SHA1 | Date | |
|---|---|---|---|
| 7662c00cf9 | |||
| 0388455b85 | |||
| bdc9004df6 |
@@ -1,8 +1,5 @@
|
||||
(import (srfi :43)) ; vector extensions
|
||||
|
||||
; returns numbers 0 to 9 from ascii character
|
||||
(define (char->number c)
|
||||
(- (char->integer c) 48)) ; 48 is ASCII's number zero
|
||||
(load "../common.scm")
|
||||
|
||||
; reads a numnber from input
|
||||
; also consumes the next non-number character in the file!
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
; get list of lines from a file
|
||||
(define (get-lines file)
|
||||
(let loop [(lines '())]
|
||||
(if (eof-object? (peek-char file))
|
||||
lines
|
||||
(loop (cons (get-line file) lines)))))
|
||||
(load "../common.scm")
|
||||
|
||||
; check if character can close chunk
|
||||
(define (closing? c)
|
||||
|
||||
64
12-passage-pathing/code.scm
Normal file
64
12-passage-pathing/code.scm
Normal file
@@ -0,0 +1,64 @@
|
||||
(load "../common.scm")
|
||||
|
||||
; splits string into two at first separator character encountered
|
||||
; returns #f if there is given separator is not present
|
||||
(define (split-string str separator)
|
||||
(let [(len (string-length str))]
|
||||
(let loop [(i 0)]
|
||||
(if (= i len)
|
||||
#f
|
||||
(if (char=? (string-ref str i) separator)
|
||||
(list (substring str 0 i)
|
||||
(substring str (+ i 1) len))
|
||||
(loop (+ i 1)))))))
|
||||
|
||||
(define (add-edge nodes start end)
|
||||
(hashtable-update! nodes start
|
||||
(lambda (node)
|
||||
(if (member end node)
|
||||
node
|
||||
(cons end node)))
|
||||
'()))
|
||||
|
||||
(define (make-graph file)
|
||||
(let [(lines (get-lines file))
|
||||
(nodes (make-hashtable string-hash string=? 20))]
|
||||
(for-each
|
||||
(lambda (line)
|
||||
(let [(edge (split-string line #\-))]
|
||||
(let [(start (car edge))
|
||||
(end (cadr edge))]
|
||||
(add-edge nodes start end)
|
||||
(add-edge nodes end start))))
|
||||
lines)
|
||||
nodes))
|
||||
|
||||
(define (is-big? node)
|
||||
(char-upper-case? (string-ref node 0)))
|
||||
|
||||
(define (find-paths graph)
|
||||
(let [(paths '())]
|
||||
(let visit [(node "start")
|
||||
(path '())
|
||||
(visited-small '())]
|
||||
(assert (< (length visited-small) 20))
|
||||
(if (string=? node "end")
|
||||
(set! paths (cons (cons node path) paths))
|
||||
(for-each
|
||||
(lambda (child)
|
||||
(when (or (is-big? child)
|
||||
(not (member child visited-small)))
|
||||
(visit child
|
||||
(cons node path)
|
||||
(if (is-big? node)
|
||||
visited-small
|
||||
(cons node visited-small)))))
|
||||
(hashtable-ref graph node '()))))
|
||||
paths))
|
||||
|
||||
(call-with-input-file
|
||||
"input"
|
||||
(lambda (file)
|
||||
(let [(graph (make-graph file))]
|
||||
(printf "part 1:~% ~a paths with at most 1 visit to small caves.~%"
|
||||
(length (find-paths graph))))))
|
||||
22
12-passage-pathing/input
Normal file
22
12-passage-pathing/input
Normal file
@@ -0,0 +1,22 @@
|
||||
zi-end
|
||||
XR-start
|
||||
zk-zi
|
||||
TS-zk
|
||||
zw-vl
|
||||
zk-zw
|
||||
end-po
|
||||
ws-zw
|
||||
TS-ws
|
||||
po-TS
|
||||
po-YH
|
||||
po-xk
|
||||
zi-ws
|
||||
zk-end
|
||||
zi-XR
|
||||
XR-zk
|
||||
vl-TS
|
||||
start-zw
|
||||
vl-start
|
||||
XR-zw
|
||||
XR-vl
|
||||
XR-ws
|
||||
@@ -1,5 +1,12 @@
|
||||
(import (srfi :43)) ; vector extensions
|
||||
|
||||
; get list of lines from a file
|
||||
(define (get-lines file)
|
||||
(let loop [(lines '())]
|
||||
(if (eof-object? (peek-char file))
|
||||
lines
|
||||
(loop (cons (get-line file) lines)))))
|
||||
|
||||
; returns a list of numbers parsed from the first line of the file,
|
||||
; separated by commas
|
||||
(define (read-comma-separated-numbers file)
|
||||
|
||||
Reference in New Issue
Block a user