Advertisement
alefhidalgo

TheUnknownProblem

Jun 20th, 2011
1,566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.IOException;
  3. import javax.imageio.ImageIO;
  4.  
  5. /**
  6.  * Tuenti Programming Contest
  7.  * Challenge 17: The ¿? Problem
  8.  * @author alefhidalgo [at] gmail [dot] com
  9.  */
  10. public class TheUnknownProblem {
  11.    
  12.     /* Secret text */
  13.     private static final String HIDDEN_STRING = "Good job crack! You have done well :) To finish this challenge you have to write the program that submits the following code: ";
  14.     /* Secret code length */
  15.     private static final int LONG_SECRET_CODE = 40;
  16.     private BufferedImage image;
  17.    
  18.     /**
  19.      * Constructor
  20.      * @param image
  21.      */
  22.     public TheUnknownProblem(BufferedImage image){
  23.         this.image = image;
  24.     }
  25.    
  26.     /**
  27.      * Get Hidden Code from image
  28.      * @return
  29.      */
  30.     public String getHiddenCode(){
  31.         int x = image.getWidth();
  32.         int y = image.getHeight();
  33.         StringBuilder sbLSB = new StringBuilder();
  34.         for (int i = 0; i < y; i++) {
  35.             for (int j = 0; j < x; j++) {
  36.                 int pixel = image.getRGB(j, i);
  37.                
  38.                 int r = (pixel >> 16) & 0xff;          
  39.                 int g  =(pixel >> 8) & 0xff;
  40.                 int b = (pixel) & 0xff;
  41.                 String sr = Integer.toBinaryString(r);
  42.                 String sg = Integer.toBinaryString(g);
  43.                 String sb = Integer.toBinaryString(b);
  44.                 /* Get Less Significative Bit and append in order BLUE,GREEN,REED*/            
  45.                 sbLSB.append(sb.substring(sb.length()-1, sb.length()));
  46.                 sbLSB.append(sg.substring(sg.length()-1, sg.length()));
  47.                 sbLSB.append(sr.substring(sr.length()-1, sr.length()));
  48.             }
  49.         }      
  50.         StringBuilder decoded = new StringBuilder();       
  51.         for(int i=0; i<sbLSB.length();i+=8) {            
  52.          decoded.append((char)Integer.parseInt(sbLSB.substring(i,i+8), 2));
  53.         }
  54.         //get secret CODE
  55.         int indexCode= decoded.indexOf(TheUnknownProblem.HIDDEN_STRING) + TheUnknownProblem.HIDDEN_STRING.length();    
  56.         return decoded.substring( indexCode ,indexCode + LONG_SECRET_CODE);    
  57.     }  
  58.    
  59.     public static void main(String[] s) {      
  60.         BufferedImage image= null;
  61.         BASE64DecoderStream decoder = new BASE64DecoderStream(System.in);
  62.         try {
  63.             image = ImageIO.read(decoder);
  64.         } catch (IOException e) {
  65.             /* Never should happen */
  66.         }
  67.         TheUnknownProblem theUnknowProblem = new TheUnknownProblem(image); 
  68.         System.out.println(theUnknowProblem.getHiddenCode());          
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement