воскресенье, 15 апреля 2012 г.

Qualification Round 2012. Problem A. Speaking in Tongues

Первый раз немножко поучаствовал в Google Code Jam. К сожаленью плотный график из разных дел не разрешил весь уикенд потратить на решение головоломок от гугла, но очень понравилось. Вот так решил первую задачку:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class Main {

 public static void main(String[] args) throws Exception {

  TreeMap<Character, Character> map = getMap();

  File inputFile = new File("A-small-attempt0.in");
  FileInputStream fileInputStream = new FileInputStream(inputFile);
  BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));

  File outputFile = new File("out.txt");
  FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileOutputStream));

  Integer t = Integer.valueOf(reader.readLine());

  String line = null;
  for (int i = 1; (line = reader.readLine()) != null && i <= t && i <= 30; i++) {
   if (i != 1) {
    writer.newLine();
   }

   writer.write("Case #" + i + ": ");

   for (int j = 0; j < line.length() && j <= 100; j++) {
    Character c = line.charAt(j);
    writer.write(map.get(c));
   }
  }

  reader.close();
  writer.close();
 }

 private static TreeMap<Character, Character> getMap() {
  TreeMap<Character, Character> map = new TreeMap<Character, Character>();
  map.put('a', 'y');
  map.put('o', 'e');
  map.put('z', 'q');
  map.put('q', 'z');

  String i = "ejp mysljylc kd kxveddknmc re jsicpdrysirbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcdde kr kd eoya kw aej tysr re ujdr lkgc jv";
  String o = "our language is impossible to understandthere are twenty six factorial possibilitiesso it is okay if you want to just give up";

  for (int n = 0; n < i.length(); n++) {
   map.put(i.charAt(n), o.charAt(n));
  }

  return map;
 }

 @SuppressWarnings("unused")
 private static void printMap(Map<Character, Character> map) {
  Iterator<Entry<Character, Character>> iterator = map.entrySet().iterator();
  int i = 0;
  while (iterator.hasNext()) {
   Entry<Character, Character> entry = iterator.next();
   System.out.println(++i + ". " + entry.getKey() + " -> " + entry.getValue());
  }
 }
}

надеюсь, они меня возьмут на работу :D