forked from logicchains/LPATHBench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrkt.rkt
More file actions
54 lines (48 loc) · 2.1 KB
/
rkt.rkt
File metadata and controls
54 lines (48 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#lang typed/racket
(struct: route ([dest : Integer] [cost : Integer]) #:transparent)
(struct: node ([neighbours : (Listof route)]) #:transparent)
(: str->int (String -> Integer))
(define (str->int str)
(define n (string->number str))
(if n (numerator (inexact->exact (real-part n))) 0))
(: read-places (-> (Vectorof node)))
(define (read-places)
(define lines
(file->lines "agraph"))
(define num-lines (str->int (car lines)))
(define nodes (build-vector num-lines (lambda (n) (node `()))))
(let loop ([i : Integer 0])
(define nums (string-split (list-ref (cdr lines) i)))
(define len (length nums))
(when (and (> len 2) (> (length lines) (+ i 2)))
(let ([node-id (str->int (list-ref nums 0))]
[neighbour (str->int (list-ref nums 1))]
[cost (str->int (list-ref nums 2))])
(define new-node (node
(append (node-neighbours (vector-ref nodes node-id))
(list (route neighbour cost)))))
(vector-set! nodes node-id new-node)
(loop (+ i 1)))))
nodes)
(: get-longest-path ((Vectorof node) Integer (Vectorof Boolean) -> Integer))
(define (get-longest-path nodes node-id visited)
(vector-set! visited node-id #t)
(define sum
(foldr
(lambda ([neighbour : route] [max : Integer])
(if (not (vector-ref visited (route-dest neighbour)))
(let ([dist (+ (route-cost neighbour) (get-longest-path nodes (route-dest neighbour) visited))])
(if (> dist max)
dist
max))
max))
0
(node-neighbours (vector-ref nodes node-id))))
(vector-set! visited node-id #f)
sum)
(define nodes (read-places))
(define visited : (Vectorof Boolean) (build-vector (vector-length nodes) (lambda (n) #f)))
(define start (current-inexact-milliseconds))
(define len (get-longest-path nodes 0 visited))
(define duration (- (current-inexact-milliseconds) start))
(printf "~a LANGUAGE Racket ~a\n" len (inexact->exact (floor duration)))