Permalink
Browse files

Merge pull request #40 from dimdim2/master

퀴즈3번문제 풀이 추가
  • Loading branch information...
2 parents 236fee9 + d662698 commit 6982971078248946e75ffaa845b3239cf61d7fc2 @miracle0k miracle0k committed May 16, 2012
View
71 src/quize/number03/daewon/DaewonAlwaysTurnLeft.java
@@ -0,0 +1,71 @@
+package quize.number03.daewon;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+public class DaewonAlwaysTurnLeft {
+
+ public static final char FORWARD = 'W';
+ public static final char TURN_LEFT = 'L';
+ public static final char TURN_RIGHT = 'R';
+
+ public String solve(String input) {
+ Maze maze = new Maze();
+
+ String[] paths = input.split(" ");
+ String entrance2Exit = paths[0];
+ String exit2Entrance = paths[1];
+
+ Mouse mouse = new Mouse();
+ navigateMaze(maze, mouse, entrance2Exit);
+ mouse.reverse();
+ navigateMaze(maze, mouse, exit2Entrance);
+
+ return maze.toString();
+ }
+
+ private void navigateMaze(Maze maze, Mouse mouse, String path) {
+ for (int i = 0; i < path.length(); i++) {
+ char action = path.charAt(i);
+ switch (action) {
+ case FORWARD:
+ mouse.forward();
+ int row = mouse.getRow();
+ if (row == 0 || i == (path.length() - 1))
+ break;
+
+ int col = mouse.getColumn();
+ Direction direction = mouse.getDirection().reverse();
+ MazeCell cell = maze.getCell(row, col);
+ cell.open(direction);
+ break;
+
+ case TURN_LEFT:
+ mouse.turnLeft();
+ break;
+
+ case TURN_RIGHT:
+ mouse.turnRight();
+ break;
+ }
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+ FileWriter writer = new FileWriter("/B-large-practice.out");
+ BufferedReader reader = new BufferedReader(new FileReader("/B-large-practice.in"));
+
+ DaewonAlwaysTurnLeft atl = new DaewonAlwaysTurnLeft();
+ int inputCount = Integer.parseInt(reader.readLine());
+ for (int i = 0; i < inputCount; i++) {
+ String result = atl.solve(reader.readLine());
+ writer.write(String.format("Case #%d:\n%s", i + 1, result));
+ }
+
+ writer.close();
+ reader.close();
+
+ }
+}
View
46 src/quize/number03/daewon/Direction.java
@@ -0,0 +1,46 @@
+package quize.number03.daewon;
+
+public enum Direction {
+
+ NORTH(1), SOUTH(2), WEST(4), EAST(8);
+
+ final int value;
+
+ private Direction(int value) {
+ this.value = value;
+ }
+
+ public Direction reverse() {
+ switch (this) {
+ case NORTH:
+ return SOUTH;
+ case SOUTH:
+ return NORTH;
+ case EAST:
+ return WEST;
+ case WEST:
+ return EAST;
+ default:
+ return null;
+ }
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public Direction valueOf(int value) {
+ switch (value) {
+ case 1:
+ return NORTH;
+ case 2:
+ return SOUTH;
+ case 4:
+ return WEST;
+ case 8:
+ return EAST;
+ default:
+ throw new IllegalArgumentException(String.format("Invalid Value [Expected:1,2,4,8] [Arg:%d]", value));
+ }
+ }
+}
View
67 src/quize/number03/daewon/Maze.java
@@ -0,0 +1,67 @@
+package quize.number03.daewon;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class Maze {
+ int columnSize;
+ int rowSize;
+ List<MazeRow> rows = new ArrayList<MazeRow>();
+
+ public void setRow(int rowIdx, MazeRow row) {
+ rows.set(rowIdx, row);
+ }
+
+ public void setCell(int row, int col, MazeCell cell) {
+ rows.get(row).setColumn(col, cell);
+ }
+
+ public MazeRow getRow(int row) {
+ int addRowCount = row - rows.size();
+ for (int i = 0; i < addRowCount; i++) {
+ MazeRow mazeRow = new MazeRow();
+ rows.add(mazeRow);
+ }
+
+ return rows.get(row);
+ }
+
+ public MazeCell getCell(int row, int col) {
+ int addRowCount = row - rows.size();
+ for (int i = 0; i < addRowCount; i++) {
+ MazeRow mazeRow = new MazeRow();
+ rows.add(mazeRow);
+ }
+
+ return rows.get(row - 1).getCol(col);
+ }
+
+ public int getRowSize() {
+ return rows.size();
+ }
+
+ public String toString() {
+ StringBuilder strBuf = new StringBuilder(columnSize * rowSize);
+ for (MazeRow row : rows) {
+ Map<Integer, MazeCell> columns = row.getColumns();
+
+ Set<Integer> keySet = columns.keySet();
+ List<Integer> idxs = new ArrayList<Integer>();
+ for (Integer colIdx : keySet) {
+ idxs.add(colIdx);
+ }
+
+ Collections.sort(idxs);
+ for (Integer colIdx : idxs) {
+ strBuf.append(columns.get(colIdx).getHex());
+ }
+ strBuf.append("\n");
+ }
+
+ return strBuf.toString();
+ }
+
+}
View
42 src/quize/number03/daewon/MazeCell.java
@@ -0,0 +1,42 @@
+package quize.number03.daewon;
+
+public class MazeCell {
+
+ int value = 0x0;
+
+ public MazeCell() {
+ super();
+ }
+
+ public MazeCell(int value) {
+ this.value = value;
+ }
+
+ public int getNumber() {
+ return value;
+ }
+
+ public String getHex() {
+ return String.format("%x", value);
+ }
+
+ public void open(Direction direction) {
+ value = value | direction.getValue();
+ }
+
+ public void openNorth() {
+ open(Direction.NORTH);
+ }
+
+ public void openSouth() {
+ open(Direction.SOUTH);
+ }
+
+ public void openWest() {
+ open(Direction.WEST);
+ }
+
+ public void openEast() {
+ open(Direction.EAST);
+ }
+}
View
33 src/quize/number03/daewon/MazeRow.java
@@ -0,0 +1,33 @@
+package quize.number03.daewon;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class MazeRow {
+ Map<Integer, MazeCell> columns = new HashMap<Integer, MazeCell>();
+
+ public MazeRow() {}
+
+ public void setColumn(int col, MazeCell cell) {
+ columns.put(col, cell);
+ }
+
+ public MazeCell getCol(int col) {
+ MazeCell cell = columns.get(col);
+ if (cell == null) {
+ cell = new MazeCell();
+ columns.put(col, cell);
+ }
+
+ return cell;
+ }
+
+ public Map<Integer, MazeCell> getColumns() {
+ return columns;
+ }
+
+ public int getColumnSize() {
+ return columns.size();
+ }
+
+}
View
111 src/quize/number03/daewon/Mouse.java
@@ -0,0 +1,111 @@
+package quize.number03.daewon;
+
+public class Mouse {
+
+ private Direction direction = Direction.SOUTH;
+ private int numOfColumn = 0;
+ private int numOfRow = 0;
+ private int row = 0;
+ private int column = 0;
+
+ public Mouse() {}
+
+ public Mouse(Direction direction) {
+ this.direction = direction;
+ }
+
+ public void forward() {
+ switch (direction) {
+ case NORTH:
+ row--;
+ break;
+ case EAST:
+ column++;
+ break;
+ case SOUTH:
+ row++;
+ break;
+ case WEST:
+ column--;
+ break;
+ default:
+ break;
+ }
+
+ numOfColumn = Math.max(Math.abs(column), numOfColumn);
+ numOfRow = Math.max(Math.abs(row), numOfRow);
+ }
+
+ public int getRow() {
+ return row;
+ }
+
+ public void setRow(int row) {
+ this.row = row;
+ }
+
+ public int getColumn() {
+ return column;
+ }
+
+ public void setColumn(int column) {
+ this.column = column;
+ }
+
+ public Direction reverse() {
+ turnLeft();
+ return turnLeft();
+ }
+
+ public Direction turnLeft() {
+ if (direction == Direction.NORTH) {
+ this.direction = Direction.WEST;
+
+ } else if (direction == Direction.WEST) {
+ this.direction = Direction.SOUTH;
+
+ } else if (direction == Direction.SOUTH) {
+ this.direction = Direction.EAST;
+
+ } else if (direction == Direction.EAST) {
+ this.direction = Direction.NORTH;
+
+ }
+
+ return this.direction;
+ }
+
+ public Direction turnRight() {
+ if (direction == Direction.NORTH) {
+ this.direction = Direction.EAST;
+
+ } else if (direction == Direction.EAST) {
+ this.direction = Direction.SOUTH;
+
+ } else if (direction == Direction.SOUTH) {
+ this.direction = Direction.WEST;
+
+ } else if (direction == Direction.WEST) {
+ this.direction = Direction.NORTH;
+ }
+
+ return this.direction;
+ }
+
+ public Direction getDirection() {
+ return direction;
+ }
+
+ public void setDirection(Direction direction) {
+ this.direction = direction;
+ }
+
+ public int getNumOfRow() {
+ return numOfRow;
+ }
+
+ public int getNumOfColumn() {
+ return numOfColumn;
+ }
+
+}
View
67 src/quize/number04/DaewonSpeakingInTongues.java
@@ -0,0 +1,67 @@
+package quize.number04;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class DaewonSpeakingInTongues {
+ static Map<String, String> languageMaps = new HashMap<String, String>(27);
+ static {
+ languageMaps.put(" ", " ");
+ languageMaps.put("a", "y");
+ languageMaps.put("b", "h");
+ languageMaps.put("c", "e");
+ languageMaps.put("d", "s");
+ languageMaps.put("e", "o");
+ languageMaps.put("f", "c");
+ languageMaps.put("g", "v");
+ languageMaps.put("h", "x");
+ languageMaps.put("i", "d");
+ languageMaps.put("j", "u");
+ languageMaps.put("k", "i");
+ languageMaps.put("l", "g");
+ languageMaps.put("m", "l");
+ languageMaps.put("n", "b");
+ languageMaps.put("o", "k");
+ languageMaps.put("p", "r");
+ languageMaps.put("q", "z");
+ languageMaps.put("r", "t");
+ languageMaps.put("s", "n");
+ languageMaps.put("t", "w");
+ languageMaps.put("u", "j");
+ languageMaps.put("v", "p");
+ languageMaps.put("w", "f");
+ languageMaps.put("x", "m");
+ languageMaps.put("y", "a");
+ languageMaps.put("z", "q");
+ }
+
+ public String solve(String readLine) {
+ StringBuilder result = new StringBuilder(readLine.length());
+ for(int i = 0; i < readLine.length(); i++) {
+ result.append(languageMaps.get(readLine.charAt(i)+""));
+ }
+
+ return result.toString();
+ }
+
+ public static void main(String[] args) throws IOException {
+ FileWriter writer = new FileWriter("/A-small-practice.out");
+ BufferedReader reader = new BufferedReader(new FileReader("/A-small-practice.in"));
+
+ DaewonSpeakingInTongues solution = new DaewonSpeakingInTongues();
+ int inputCount = Integer.parseInt(reader.readLine());
+ for (int i = 0; i < inputCount; i++) {
+ String result = solution.solve(reader.readLine());
+ writer.write(String.format("Case #%d: %s\n", i + 1, result));
+ }
+
+ writer.close();
+ reader.close();
+
+ }
+
+}
View
66 src/quize/number05/DaewonDancingWithGooglers.java
@@ -0,0 +1,66 @@
+package quize.number05;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+public class DaewonDancingWithGooglers {
+
+ public static final int NUM_OF_JUDGE = 3;
+
+ public int solve(String input) {
+
+ int result = 0;
+
+ String[] datas = input.split(" ");
+ int numOfGoogler = Integer.parseInt(datas[0]);
+ int numOfSuprising = Integer.parseInt(datas[1]);
+ int minOfBest = Integer.parseInt(datas[2]);
+
+ for (int i = 0; i < numOfGoogler; i++) {
+ int sumOfScore = Integer.parseInt(datas[i + 3]);
+ int avg = sumOfScore / NUM_OF_JUDGE;
+ int mod =sumOfScore % NUM_OF_JUDGE;
+
+ if (avg >= minOfBest) {
+ result++;
+
+ } else if (avg == (minOfBest - 1)) {
+ if (mod != 0) {
+ result++;
+
+ } else if (sumOfScore > 0 && numOfSuprising > 0) {
+ result++;
+ numOfSuprising--;
+ }
+
+ } else if (avg == (minOfBest - 2) && mod == 2 && numOfSuprising > 0) {
+ result++;
+ numOfSuprising--;
+ }
+ }
+
+ return result;
+ }
+
+ public static void main(String[] args) throws IOException {
+ FileWriter writer = new FileWriter("/B-large-practice.out");
+ BufferedReader reader = new BufferedReader(new FileReader("/B-large-practice.in"));
+
+ long startTime = System.currentTimeMillis();
+ System.out.println("Start to solve google code jam problem");
+ DaewonDancingWithGooglers solution = new DaewonDancingWithGooglers();
+ int inputCount = Integer.parseInt(reader.readLine());
+ for (int i = 0; i < inputCount; i++) {
+ int result = solution.solve(reader.readLine());
+ writer.write(String.format("Case #%d: %s\n", i + 1, result));
+ }
+ System.out.printf("End to solve google code jam problem [ElapsedTime:%d]\n", (System.currentTimeMillis() - startTime));
+
+ writer.close();
+ reader.close();
+
+ }
+
+}
View
19 test/quize/number03/daewon/DaewonAlwaysTurnLeftTest.java
@@ -0,0 +1,19 @@
+package quize.number03.daewon;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class DaewonAlwaysTurnLeftTest {
+
+ DaewonAlwaysTurnLeft atl = new DaewonAlwaysTurnLeft();
+
+ @Test
+ public void test() {
+ String result = atl.solve("WRWWLWWLWWLWLWRRWRWWWRWWRWLW WWRRWLWLWWLWWLWWRWWRWWLW");
+ Assert.assertEquals("ac5\n386\n9c7\ne43\n9c5\n", result);
+
+ result = atl.solve("WW WW");
+ Assert.assertEquals("3\n", result);
+ }
+}
View
55 test/quize/number04/DaewonSpeakingInTonguesTest.java
@@ -0,0 +1,55 @@
+package quize.number04;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import quize.number04.DaewonSpeakingInTongues;
+
+public class DaewonSpeakingInTonguesTest {
+
+ @Test
+ public void test() {
+ DaewonSpeakingInTongues solution = new DaewonSpeakingInTongues();
+ String input = "ejp mysljylc kd kxveddknmc re jsicpdrysi";
+ String output = "our language is impossible to understand";
+ Assert.assertEquals(output, solution.solve(input));
+
+ input = "rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd";
+ output = "there are twenty six factorial possibilities";
+ Assert.assertEquals(output, solution.solve(input));
+
+ input = "de kr kd eoya kw aej tysr re ujdr lkgc jv";
+ output = "so it is okay if you want to just give up";
+ Assert.assertEquals(output, solution.solve(input));
+
+ }
+
+ public static void main(String[] args) {
+
+ String googleres = "ejp mysljylc kd kxveddknmc re jsicpdrysi" +
+ "rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd" +
+ "de kr kd eoya kw aej tysr re ujdr lkgc jv";
+
+ String english = "our language is impossible to understand" +
+ "there are twenty six factorial possibilities" +
+ "so it is okay if you want to just give up";
+
+ // googleres : english
+ Map<String, String> g2eMaps = new HashMap<String, String>();
+ for (int i = 0; i < googleres.length(); i++) {
+ g2eMaps.put(googleres.charAt(i) + "", english.charAt(i) + "");
+ }
+
+ Set<String> keySet = g2eMaps.keySet();
+ Object[] googleresChars = keySet.toArray();
+ Arrays.sort(googleresChars);
+ for (Object gChar : googleresChars) {
+ System.out.println(gChar + ":" + g2eMaps.get(gChar));
+ }
+ }
+}
View
18 test/quize/number05/DaewonDancingWithGooglersTest.java
@@ -0,0 +1,18 @@
+package quize.number05;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DaewonDancingWithGooglersTest {
+
+ @Test
+ public void test() {
+ DaewonDancingWithGooglers solution = new DaewonDancingWithGooglers();
+
+ Assert.assertEquals(3, solution.solve("3 1 5 15 13 11"));
+ Assert.assertEquals(2, solution.solve("3 0 8 23 22 21"));
+ Assert.assertEquals(1, solution.solve("2 1 1 8 0"));
+ Assert.assertEquals(3, solution.solve("6 2 8 29 20 8 18 18 21"));
+ Assert.assertEquals(1, solution.solve("2 2 3 5 4"));
+ }
+}

0 comments on commit 6982971

Please sign in to comment.