Advertisement
alefhidalgo

TheRiddle

Jun 20th, 2011
1,540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.IOException;
  3.  
  4. import javax.imageio.ImageIO;
  5. import javax.script.ScriptEngine;
  6. import javax.script.ScriptEngineManager;
  7. import javax.script.ScriptException;
  8.  
  9. /**
  10.  * Tuenti Programming Contest
  11.  * Challenge 18: The Riddle
  12.  * @author alefhidalgo [at] gmail [dot] com
  13.  */
  14. public class TheRiddle {
  15.    
  16.     /* Secret text constants*/
  17.     private static final String HIDDEN_BEGIN_STRING =  "you can also answer this simple question to solve the challenge: how much is";
  18.     private static final String HIDDEN_END_STRING =  "? Crack!";
  19.     private BufferedImage image;
  20.    
  21.     /**
  22.      * Constructor
  23.      * @param image
  24.      */
  25.     public TheRiddle(BufferedImage image){
  26.         this.image = image;
  27.     }
  28.        
  29.     /**
  30.      * Get Hidden solution from image
  31.      *  - Read binary value for all stripes pixels until red begin (skipping green background)
  32.      *  - Convert binary string to legible ascii grouping in bytes
  33.      *  - Search the problem statement ( A operation B)
  34.      *  
  35.      * @return The solution for the given expression.
  36.      */
  37.     public String getHiddenSolution(){
  38.         int x = image.getWidth();
  39.         int y = image.getHeight();
  40.         StringBuilder sbDecoded = new StringBuilder();
  41.     f1: for (int i = 0; i < x; i++) {
  42.             for (int j = 0; j < y; j++) {
  43.                 int pixel = image.getRGB(i, j); //Get pixel value      
  44.                 int r = (pixel >> 16) & 0xff;          
  45.                 int g  =(pixel >> 8) & 0xff;
  46.                 int b = (pixel) & 0xff;        
  47.                 if(r==255) break f1; /* until red */
  48.                 if(g!=255){ /* skip green */
  49.                     //append 8byte -> ascii chars
  50.                     sbDecoded.append((char)r);
  51.                     sbDecoded.append((char)g);
  52.                     sbDecoded.append((char)b);                                                 
  53.                 }            
  54.             }
  55.        
  56.         }      
  57.         //Get secret expression to solve (A operation B)
  58.         int indexBeginCode= sbDecoded.indexOf(TheRiddle.HIDDEN_BEGIN_STRING) + TheRiddle.HIDDEN_BEGIN_STRING.length();
  59.         int indexEndCode = sbDecoded.indexOf(TheRiddle.HIDDEN_END_STRING);     
  60.         return evalExpression(sbDecoded.substring(indexBeginCode,indexEndCode).trim());    
  61.     }  
  62.    
  63.     /**
  64.      * evalExpression
  65.      *  Eval expression using JS engine
  66.      *      A + B
  67.      *      A - C
  68.      *      A / C
  69.      *      ...
  70.      * @param expression to solve
  71.      * @return solved expression value
  72.      */
  73.     private String evalExpression(String expression){
  74.         ScriptEngineManager manager = new ScriptEngineManager();
  75.         ScriptEngine engine = manager.getEngineByName("js");
  76.         Double ret = null;
  77.         try {
  78.             ret = (Double) engine.eval(expression);
  79.         } catch (ScriptException e) {
  80.             /* Never should happen */
  81.             return "NOT A VALID EXPRESSION";           
  82.         }
  83.         return String.valueOf(ret.intValue());
  84.     }
  85.    
  86.    
  87.     public static void main(String[] s) {      
  88.         BufferedImage image= null;
  89.         BASE64DecoderStream decoder = new BASE64DecoderStream(System.in);
  90.         try {
  91.             image = ImageIO.read(decoder);
  92.         } catch (IOException e) {
  93.             /* Never should happen */
  94.         }
  95.         TheRiddle theRiddle = new TheRiddle(image);
  96.         System.out.println(theRiddle.getHiddenSolution());
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement