Skip to content

Instantly share code, notes, and snippets.

@phaniram
Created December 29, 2011 00:43
Show Gist options
  • Save phaniram/1530765 to your computer and use it in GitHub Desktop.
Save phaniram/1530765 to your computer and use it in GitHub Desktop.
Gild.com puzzle - Fightsopa
package com.gild.puzzles;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Fightsopa {
public void process(String input,int n)
{
int abcd=n;
if(abcd<0)
{
abcd=(26-((0-abcd)%26));
}
int rot=26-abcd%26;
char out=0;
for(char ch : input.toCharArray())
{
if(ch>=65 && ch<=90)
{
if(ch+rot<=90)
{
out=(char) (ch+rot);
System.out.print(out);
}
else
{
out=(char) (ch+rot-26);
System.out.print(out);
}
}else if(ch>=97 && ch<=122)
{
if(ch+rot<=122)
{
out=(char) (ch+rot);
System.out.print(out);
}
else
{
out=(char) (ch+rot-26);
System.out.print(out);
}
}else
{
System.out.print(ch);
}
}
}
public void handler(File fFile) throws FileNotFoundException
{
Scanner scanner = new Scanner(new FileReader(fFile));
int rot=0;
int count=0;
while (scanner.hasNextLine()) {
if(count==0)
{
rot=scanner.nextInt();
scanner.nextLine();
}else
{
String wordp = scanner.nextLine();
process(wordp,rot);
System.out.println();
}
count++;
}
scanner.close();
}
public static void main(String args[]) throws FileNotFoundException
{
if (args.length == 0) {
File fFile = new File("D:\\a.in");
Fightsopa fsopa=new Fightsopa();
fsopa.handler(fFile);
}
}
}
@phaniram
Copy link
Author

Puzzle Description
At Gild we believe in Internet freedom and we think SOPA is simply wrong. For each user who solves this challenge (up to 10k users), Gild will donate $5 to EFF to fight SOPA.
An anonymous source has sent us a collection of encrypted cablegrams. These are a collection of intercepted communications between entertainment industry lobbyists and politicians. Unfortunately, the data is encrypted, so we need your help by decoding them to unveil the global conspiracy!
After spending time looking at the encrypted text, you exclaim, "I got it!"
Write a program that takes as input a single argument on the command line. This argument will be a file name which contains the encrypted message. The program must decipher the message and print the clear text as the result on standard output.
Input specifications

Your submission will be tested against an input file that contains ASCII characters. The input file starts at the very first line with a N-digits positive or negative integer number that is your cipher, followed by a new-line character. The second line starts with the payload of the encrypted text until the end of file.
Note that only words (alphanumeric sequences) are encrypted and that every other character (i.e. punctuation) must not be processed by your algorithm and must also be copied ‘as is’ to the standard output.
Example input file:
87
Qxuuhfxxm bynjtrwp: juu hxda lxwcnwc jan knuxwp cx db! Yjbb cqrb kruu xa uron fruu nwm!
Output Specifications

The expected output must be the decrypted message. The starting number must not be included in the output.
Example output:
Hollywood speaking: all of your content belongs to us! Pass this bill or life will end!
SOPA could pass anytime soon, so hurry up!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment