From 4f1d1944873c949b6d474f63e16098804c588a33 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 24 Sep 2017 23:31:57 -0700 Subject: 4006 --- html/nqueens.mu.html | 97 ++++++++++++++++++++++++++-------------------------- nqueens.mu | 9 +++-- 2 files changed, 52 insertions(+), 54 deletions(-) diff --git a/html/nqueens.mu.html b/html/nqueens.mu.html index 6180c160..bb65102c 100644 --- a/html/nqueens.mu.html +++ b/html/nqueens.mu.html @@ -97,7 +97,7 @@ if ('onhashchange' in window) { 36 ¦ { 37 ¦ ¦ curr-conflicts?:bool <- conflict? curr, queens 38 ¦ ¦ break-if curr-conflicts? - 39 ¦ ¦ queens:&:list:square <- push curr, queens + 39 ¦ ¦ queens <- push curr, queens 40 ¦ ¦ sub-result:num <- nqueens n, queens 41 ¦ ¦ result <- add result, sub-result 42 ¦ ¦ queens <- rest queens @@ -114,54 +114,53 @@ if ('onhashchange' in window) { 53 def conflict? curr:square, queens:&:list:square -> result:bool [ 54 local-scope 55 load-ingredients - 56 result1:bool <- conflicting-file? curr, queens - 57 return-if result1, result1 - 58 result2:bool <- conflicting-diagonal? curr, queens - 59 return result2 - 60 ] - 61 - 62 def conflicting-file? curr:square, queens:&:list:square -> result:bool [ - 63 local-scope - 64 load-ingredients - 65 curr-file:num <- get curr, file:offset - 66 { - 67 ¦ break-unless queens - 68 ¦ q:square <- first queens - 69 ¦ qfile:num <- get q, file:offset - 70 ¦ file-match?:bool <- equal curr-file, qfile - 71 ¦ return-if file-match?, 1/conflict-found - 72 ¦ queens <- rest queens - 73 ¦ loop - 74 } - 75 return 0/no-conflict-found - 76 ] - 77 - 78 def conflicting-diagonal? curr:square, queens:&:list:square -> result:bool [ - 79 local-scope - 80 load-ingredients - 81 curr-rank:num <- get curr, rank:offset - 82 curr-file:num <- get curr, file:offset - 83 { - 84 ¦ break-unless queens - 85 ¦ q:square <- first queens - 86 ¦ qrank:num <- get q, rank:offset - 87 ¦ qfile:num <- get q, file:offset - 88 ¦ rank-delta:num <- subtract qrank, curr-rank - 89 ¦ file-delta:num <- subtract qfile, curr-file - 90 ¦ rank-delta <- abs rank-delta - 91 ¦ file-delta <- abs file-delta - 92 ¦ diagonal-match?:bool <- equal rank-delta, file-delta - 93 ¦ return-if diagonal-match?, 1/conflict-found - 94 ¦ queens <- rest queens - 95 ¦ loop - 96 } - 97 return 0/no-conflict-found - 98 ] - 99 -100 def main [ -101 nqueens 4 -102 $dump-trace [app] -103 ] + 56 result:bool <- conflicting-file? curr, queens + 57 return-if result + 58 result:bool <- conflicting-diagonal? curr, queens + 59 ] + 60 + 61 def conflicting-file? curr:square, queens:&:list:square -> result:bool [ + 62 local-scope + 63 load-ingredients + 64 curr-file:num <- get curr, file:offset + 65 { + 66 ¦ break-unless queens + 67 ¦ q:square <- first queens + 68 ¦ qfile:num <- get q, file:offset + 69 ¦ file-match?:bool <- equal curr-file, qfile + 70 ¦ return-if file-match?, 1/conflict-found + 71 ¦ queens <- rest queens + 72 ¦ loop + 73 } + 74 return 0/no-conflict-found + 75 ] + 76 + 77 def conflicting-diagonal? curr:square, queens:&:list:square -> result:bool [ + 78 local-scope + 79 load-ingredients + 80 curr-rank:num <- get curr, rank:offset + 81 curr-file:num <- get curr, file:offset + 82 { + 83 ¦ break-unless queens + 84 ¦ q:square <- first queens + 85 ¦ qrank:num <- get q, rank:offset + 86 ¦ qfile:num <- get q, file:offset + 87 ¦ rank-delta:num <- subtract qrank, curr-rank + 88 ¦ file-delta:num <- subtract qfile, curr-file + 89 ¦ rank-delta <- abs rank-delta + 90 ¦ file-delta <- abs file-delta + 91 ¦ diagonal-match?:bool <- equal rank-delta, file-delta + 92 ¦ return-if diagonal-match?, 1/conflict-found + 93 ¦ queens <- rest queens + 94 ¦ loop + 95 } + 96 return 0/no-conflict-found + 97 ] + 98 + 99 def main [ +100 nqueens 4 +101 $dump-trace [app] +102 ] diff --git a/nqueens.mu b/nqueens.mu index 87ce90b6..51495009 100644 --- a/nqueens.mu +++ b/nqueens.mu @@ -36,7 +36,7 @@ def nqueens n:num, queens:&:list:square -> result:num, queens:&:list:square [ { curr-conflicts?:bool <- conflict? curr, queens break-if curr-conflicts? - queens:&:list:square <- push curr, queens + queens <- push curr, queens sub-result:num <- nqueens n, queens result <- add result, sub-result queens <- rest queens @@ -53,10 +53,9 @@ def nqueens n:num, queens:&:list:square -> result:num, queens:&:list:square [ def conflict? curr:square, queens:&:list:square -> result:bool [ local-scope load-ingredients - result1:bool <- conflicting-file? curr, queens - return-if result1, result1 - result2:bool <- conflicting-diagonal? curr, queens - return result2 + result:bool <- conflicting-file? curr, queens + return-if result + result:bool <- conflicting-diagonal? curr, queens ] def conflicting-file? curr:square, queens:&:list:square -> result:bool [ -- cgit 1.4.1-2-gfad0