Posts

Showing posts with the label Code Golf

Binary Puzzle Solver

Answer : Brachylog, 34 bytes {ℕ<2}ᵐ²&≜{d?ọᵐctᵐ=&{ḅlᵐ⌉<3}ᵐ}&\↰₂& Try it online! This is pretty damn slow, so the test case on TIO is 4x4. I am currently running the 6x6 test case on my computer to see how much time it takes. This takes a list of lists as input. The unknown values should be indicated with variables, that is with all-uppercase strings (and they should all be different, as otherwise you would be indicating that some cells must have the same value) Explanation We constrain the values to be in {0,1} , then we try instantiations of the variables until one respects all 3 rules. This is why this is so slow (because it will try all of them until finding one; and because in that case Brachylog is not implemented well enough so that constraints can be imposed before trying a possible matrix). & Output = Input { }ᵐ² Map two levels on the Input (i.e. each cell): ℕ<2 ...

Bring Out The Inner Llama Of A Sentence

Answer : Perl, 52 bytes The solution is provided as function that takes the string as argument and returns a list of positions. One-based positions, case-sensitive search, without newlines: 52 bytes sub l{pop=~/(l).*?(l).*?(a).*?(m).*?(a)/;@+[1..$#+]} The case-sensitive search returns an empty array in the example of the question, because after matching the first three letters the lowercase letter m is missing in the input text. Support of newlines: + 1 byte = 53 bytes sub l{pop=~/(l).*?(l).*?(a).*?(m).*?(a)/s;@+[1..$#+]} The text can now span several lines. Case-insensitive search: + 1 byte = 54 bytes sub l{pop=~/(l).*?(l).*?(a).*?(m).*?(a)/si;@+[1..$#+]} Now the example in the question reports a list of index positions, they are one-based numbers: [45 68 77 106 115] Zero-based positions: + 9 bytes = 63 bytes sub l{pop=~/(l).*?(l).*?(a).*?(m).*?(a)/si;map{$_-1}@+[1..$#+]} Result for the example in the question: [44 67 76 105 114] Ungolfed: The l...

Can You Beat The British Intelligence? (Nonogram Solver)

Answer : Haskell, 242 230 201 199 177 163 160 149 131 bytes import Data.Lists m=map a#b=[x|x<-m(chunk$length b).mapM id$[0,1]<$(a>>b),g x==a,g(transpose x)==b] g=m$list[0]id.m sum.wordsBy(<1) Finally under 200 bytes, credit to @Bergi. Huge thanks to @nimi for helping almost halving the size. Wow. Almost at half size now, partly because of me but mainly because of @nimi. The magic function is (#) . It finds all solutions of a given nonogram. This is able to solve all cases, but may be super slow, since it's complexity is about O(2^(len a * len b)) . A quick benchmark revealed 86GB allocated for a 5x5 nonogram. Fun fact: It works for all nonograms, not only square ones. How it works: a#b : Given lists of lists of integers which represent the number of squares, generate all grids ( map(chunk$length b).mapM id$a>>b>>[[0,1]] ) and filter the results to keep only the valid ones. g : Given a potential nonogram it sums the runs of 1's...

BASKETBALL FRVR?

Answer : JavaScript (ES6), 73 72 bytes Prints all unique, lexically sorted combinations. f=(n,p=0,s='')=>n?n>1&&f(n-2,0,s+'S')&f(n-(p-9?p+=3:9),p,s+'V'):alert(s) Test cases In this snippet, alert() has been replaced by console.log() for user-friendliness. f=(n,p=0,s='')=>n?n>1&&f(n-2,0,s+'S')&f(n-(p-9?p+=3:9),p,s+'V'):console.log(s) ;[2, 3, 4, 12, 16] .forEach(n => { console.log('[' + n + ']'); f(n); }) Jelly, 30 bytes o2,5ṁo8‘ Hṗ@€⁾VSẎðOḂŒgÇ€FS=ðÐf A monadic link returning a list of the strings (lists of characters). Try it online! - The footer calls the link and separates the entries by newlines since a full program's implicit output would smash them together. How? o2,5ṁo8‘ - Link 1, helper to form shot scores: list of shots grouped by type (S=1 and V=0) - e.g. [[1,1],[0,0,0,0,0],[1,1],[0],[1,1]] 2,5 ...

Build A Killer Sudoku Solver

Answer : GolfScript, 138 characters n%~[~]:N;1/:P.&:L;9..*?{(.[{.9%)\9/}81*;]:§;L{.`{\~@@=*}+[P§]zip%{+}*\L?N==}%§9/..zip+\3/{{3/}%zip{{+}*}%}%{+}*+{.&,9=}%+1-,!{§puts}*.}do; This is a killer sudoku solver in GolfScript. It expects input on STDIN in two rows as given in the example above. Please note: Since the puzzle description does not make any restrictions on execution time I preferred small code size over speed. The code tests all 9^81 grid configurations for a solution which may take some time on a slow computer ;-) R - 378 characters Assuming x="AABBBCDEFGGHHCCDEFGGIICJKKFLMMINJKOFLPPQNJOORSPTQNUVVRSTTQWUUXXSYZWWaaXXSYZWbbbcc" y="3 15 22 4 16 15 25 17 9 8 20 6 14 17 17 13 20 12 27 6 20 6 10 14 8 16 15 13 17" 378 characters: z=strsplit v=sapply R=rep(1:9,9) C=rep(1:9,e=9) N=1+(R-1)%/%3+3*(C-1)%/%3 G=z(x,"")[[1]] M=as.integer(z(y," ")[[1]])[order(unique(G))] s=c(1,rep(NA,80)) i=1 repeat if({n=function(g)!any(v(split(s,...