Posts

Showing posts with the label Code Golf

Create A Roman Numeral Calculator

Answer : JavaScript (ES6), 238 c=s=>{X={M:1e3,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1} n=eval('W='+s.replace(/[\w]+/g,n=>(o=0,n.replace(/[MDLV]|C[MD]?|X[CL]?|I[XV]?/g,d=>o+=X[d]), o+';W=W')));o='';for(i in X)while(n>=X[i])o+=i,n-=X[i];return o} Usage: c("XIX + LXXX") > "XCIX" c('XCIX + I / L * D + IV') > "MIV" Annotated version: /** * Process basic calculation for roman numerals. * * @param {String} s The calculation to perform * @return {String} The result in roman numerals */ c = s => { // Create a lookup table. X = { M: 1e3, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 }; // Do the calculation. // // The evaluated string is instrumented to as below: // 99+1/50*500+4 -> W=99;W=W+1;W=W/50;W=W*500;W=W+4;W=W // -> 1004 n = eval('W=' + s.replace( // Match all ro...

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...

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,...

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...

Country Name Mashup Generator

Answer : Jelly, 74 73 bytes Já¹–XṬk⁸ḢḢFṪ;ƲƭF)jṪḢƭ€á¹€$$ ḢṖ; ṪḢṪ;ÆŠá¹­ Fe€Ã˜cá¹–TXṬkḢḢṪƭ) e€⁾ -k)ẈỊḄ‘ƲĿ Ḣ,2KƊÇE? Try it online! A full program that takes a list of two strings as its argument and implicitly outputs the mashed up country name. The handling of hyphens is relatively costly, particularly since they are included whichever side of the split they fall. Explanation Helper link 1 Handles case where both countries have multiple words ) | For each country: J | - Sequence along words á¹– | - Remove last X | - Pick one at random Ṭ | - Convert to a boolean list with a 1 at that index k⁸ | - Split list of words after that point Æ­ | - Alternate between: Ḣ | - Head (first set of words for the first country) Ʋ | - Following as a monad (for the second country) Ḣ | - Head (f...