From dafff9e6e6f9c56537ab403cdb18256b06f2f691 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Fri, 4 Jul 2025 16:44:21 +0900 Subject: [PATCH 01/10] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB138?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 190 +++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 kkanazawa/src/SpiralDecoder.java diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java new file mode 100644 index 0000000..8ecb5e8 --- /dev/null +++ b/kkanazawa/src/SpiralDecoder.java @@ -0,0 +1,190 @@ +package paiza.src; + +import java.lang.reflect.Array; +import java.util.Scanner; + +/** + * 受け取った二次元の文字リストをうずまき型で解読する + * + * @author 金澤継心 + */ +public class SpiralDecoder { + + public static void main(final String[] args) { + Scanner scanner = new Scanner(System.in); + final int lineAmount = scanner.nextInt(); // 入力値の行の数 + final int columnAmount = scanner.nextInt(); // 入力値の列の数; + final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス + final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス + + // もととなる二次元の文字リストのを入力から受け取る + final String[][] cipher = inputCipher(scanner, lineAmount, columnAmount); + scanner.close(); + + // 入力値の二次元の文字リストの周りをNULLで埋める + final String[][] paddedCipher = nullPaddingCipher(cipher); + // 復号処理 + final String selectedDecode = decodingCipher(lineAmount, columnAmount, startIndex, endIndex, + paddedCipher); + System.out.println(selectedDecode); + } + + /** + * もととなる文字列を入力から受け取る + * + * @param scanner 入力を受け取るスキャナー + * @param lineAmount 文字リストの行数 + * @param columnAmount 文字リストの列数 + */ + public static String[][] inputCipher(final Scanner scanner, final int lineAmount, final int columnAmount) { + String[][] cipher = new String[lineAmount][columnAmount]; + for (int i = 0; i < lineAmount; i++) { + String inputLine = scanner.next(); + cipher[i] = inputLine.split(""); + } + return cipher; + } + + /** + * 文字リストの周りをNULLで埋める + * + * @param originCipher もととなる文字リスト + */ + public static String[][] nullPaddingCipher(final String[][] originCipher) { + final int lineAmount = Array.getLength(originCipher); + final int columnAmount = Array.getLength(originCipher[0]); + String[][] paddedCipher = new String[lineAmount + 2][columnAmount + 2]; + + // 元の文字リストをNULLしかない文字リストの中央にコピーする + for (int i = 0; i < lineAmount; i++) { + System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, columnAmount); + } + return paddedCipher; + } + + /** + * 周りをNULLで埋めた文字リストの解読 NULLにぶつかったら方向転換するイメージでうずまき状に要素を移動する + * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える + * + * @param lineAmount 文字列リストの行数 + * @param columnAmount 文字列リストの列数 + * @param startIndex 解読後読み始める文字の順番 + * @param endIndex 解読後読み終える文字の順番 + */ + public static String decodingCipher(final int lineAmount, final int columnAmount, final int startIndex, + final int endIndex, final String[][] cipher) { + // 現在いる要素を示すカーソルとして使うクラス + DecodeCursor cursor = new DecodeCursor(); + // NULLでかこっているため、始まりのインデックスは行、列ともに1から + cursor.setLineIndex(1); + cursor.setColumnIndex(1); + // 移動する方向を表すインスタンス変数 + cursor.setDirection(0); + // 現在移し終えた文字数のカウンタ + int charCounter = 0; + // この配列に文字移して最終的には復号した順番に文字が並ぶ + String[] decordResult = new String[lineAmount * columnAmount]; + + while (charCounter < lineAmount * columnAmount) { + // NULLの要素に移動した際、カーソルの位置を戻す処理 + cursor.modifying(charCounter); + // NULLのぶつかるまで一方向に要素を読み取り続ける + while (cipher[cursor.getLineIndex()][cursor.getColumnIndex()] != null) { + decordResult[charCounter] = cipher[cursor.getLineIndex()][cursor.getColumnIndex()]; + cipher[cursor.getLineIndex()][cursor.getColumnIndex()] = null; + charCounter++; + // カーソルを現在進むべき方向に移動させる + cursor.shifting(); + } + // 方向転換の処理 + cursor.changeDirection(); + } + return String.join("", decordResult).substring(startIndex - 1, endIndex); + } + +} + + +/** + * 現在読み取っている要素を示すカーソルとして使うクラス + * + */ +class DecodeCursor { + private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している + private int lineIndex;// 現在の行の位置 + private int columnIndex;// 現在の列の位置 + + public int getDirection() { + return direction; + } + + public void setDirection(int direction) { + this.direction = direction; + } + + public void changeDirection() { + this.direction++; + } + + public int getLineIndex() { + return lineIndex; + } + + public void setLineIndex(int lineIndex) { + this.lineIndex = lineIndex; + } + + public int getColumnIndex() { + return columnIndex; + } + + public void setColumnIndex(int columnIndex) { + this.columnIndex = columnIndex; + } + + // NULLの要素に移動した際、カーソルの位置を戻す処理 + // 4つの方向はdirectionを4で割った剰余として区別している + // 0:右 1:下 2:左 3:上 + public void modifying(final int charCounter) { + switch (direction % 4) { + case 0: + if (charCounter != 0) { + this.columnIndex++; + this.lineIndex++; + } + break; + case 1: + this.columnIndex--; + this.lineIndex++; + break; + case 2: + this.columnIndex--; + this.lineIndex--; + break; + case 3: + this.columnIndex++; + this.lineIndex--; + break; + } + } + + // カーソルを現在進むべき方向に移動させる + // 4つの方向はdirectionを4で割った剰余として区別している + // 0:右 1:下 2:左 3:上 + public void shifting() { + switch (this.direction % 4) { + case 0: + this.columnIndex++; + break; + case 1: + this.lineIndex++; + break; + case 2: + this.columnIndex--; + break; + case 3: + this.lineIndex--; + break; + } + } +} -- GitLab From a9235b5fba8dc52b4e5898f3b0a88d08a4ef01d8 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Mon, 7 Jul 2025 16:04:29 +0900 Subject: [PATCH 02/10] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB156?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 181 ++++++++++++++++++------------- 1 file changed, 105 insertions(+), 76 deletions(-) diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java index 8ecb5e8..be0dc1b 100644 --- a/kkanazawa/src/SpiralDecoder.java +++ b/kkanazawa/src/SpiralDecoder.java @@ -1,6 +1,7 @@ package paiza.src; import java.lang.reflect.Array; +import java.util.Arrays; import java.util.Scanner; /** @@ -12,86 +13,104 @@ public class SpiralDecoder { public static void main(final String[] args) { Scanner scanner = new Scanner(System.in); - final int lineAmount = scanner.nextInt(); // 入力値の行の数 - final int columnAmount = scanner.nextInt(); // 入力値の列の数; - final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス - final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス + final int ROW_COUNT = scanner.nextInt(); // 入力値の行の数 + final int COLUMN_COUNT = scanner.nextInt(); // 入力値の列の数; + final int START_INDEX = scanner.nextInt(); // 読み始まる文字のインデックス + final int END_INDEX = scanner.nextInt(); // 読み終える文字のインデックス + final String BARRIR_CHAR = "@"; // もととなる二次元の文字リストのを入力から受け取る - final String[][] cipher = inputCipher(scanner, lineAmount, columnAmount); + final String[][] CIPHER = inputCipher(scanner, ROW_COUNT, COLUMN_COUNT); scanner.close(); // 入力値の二次元の文字リストの周りをNULLで埋める - final String[][] paddedCipher = nullPaddingCipher(cipher); - // 復号処理 - final String selectedDecode = decodingCipher(lineAmount, columnAmount, startIndex, endIndex, - paddedCipher); + final String[][] PADDED_CIPHER = paddingCipher(CIPHER, BARRIR_CHAR); + + // 解読処理 + final String selectedDecode = decodingCipher(ROW_COUNT, COLUMN_COUNT, START_INDEX, END_INDEX, + PADDED_CIPHER, BARRIR_CHAR); System.out.println(selectedDecode); } /** * もととなる文字列を入力から受け取る * - * @param scanner 入力を受け取るスキャナー - * @param lineAmount 文字リストの行数 - * @param columnAmount 文字リストの列数 + * @param SCANNER 入力を受け取るスキャナー + * @param ROW_COUNT 文字リストの行数 + * @param COLUMN_COUNT 文字リストの列数 */ - public static String[][] inputCipher(final Scanner scanner, final int lineAmount, final int columnAmount) { - String[][] cipher = new String[lineAmount][columnAmount]; - for (int i = 0; i < lineAmount; i++) { - String inputLine = scanner.next(); - cipher[i] = inputLine.split(""); + public static String[][] inputCipher(final Scanner SCANNER, final int ROW_COUNT, + final int COLUMN_COUNT) { + String[][] cipher = new String[ROW_COUNT][COLUMN_COUNT]; + for (int i = 0; i < ROW_COUNT; i++) { + String inputRow = SCANNER.next(); + cipher[i] = inputRow.split(""); } return cipher; } /** - * 文字リストの周りをNULLで埋める + * 文字リストの周りを@で埋める * - * @param originCipher もととなる文字リスト + * @param ORIGIN_CIPHER もととなる文字リスト */ - public static String[][] nullPaddingCipher(final String[][] originCipher) { - final int lineAmount = Array.getLength(originCipher); - final int columnAmount = Array.getLength(originCipher[0]); - String[][] paddedCipher = new String[lineAmount + 2][columnAmount + 2]; - - // 元の文字リストをNULLしかない文字リストの中央にコピーする - for (int i = 0; i < lineAmount; i++) { - System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, columnAmount); + public static String[][] paddingCipher(final String[][] ORIGIN_CIPHER, final String BARRIR_CHAR) { + final int ROW_COUNT = Array.getLength(ORIGIN_CIPHER); + final int COLUMN_COUNT = Array.getLength(ORIGIN_CIPHER[0]); + String[][] paddedCipher = new String[ROW_COUNT + 2][COLUMN_COUNT + 2]; + + for (String[] o : paddedCipher) { + Arrays.fill(o, BARRIR_CHAR); + } + + // 元の文字リストを"@"しかない文字リストの中央にコピーする + for (int i = 0; i < ROW_COUNT; i++) { + System.arraycopy(ORIGIN_CIPHER[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); } return paddedCipher; } /** - * 周りをNULLで埋めた文字リストの解読 NULLにぶつかったら方向転換するイメージでうずまき状に要素を移動する + * 周りをNULLで埋めた文字リストの解読 @にぶつかったら方向転換するイメージでうずまき状に要素を移動する * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える * - * @param lineAmount 文字列リストの行数 - * @param columnAmount 文字列リストの列数 - * @param startIndex 解読後読み始める文字の順番 - * @param endIndex 解読後読み終える文字の順番 + * @param ROW_COUNT 文字列リストの行数 + * @param COLUMN_COUNT 文字列リストの列数 + * @param START_INDEX 解読後読み始める文字の順番 + * @param END_INDEX 解読後読み終える文字の順番 */ - public static String decodingCipher(final int lineAmount, final int columnAmount, final int startIndex, - final int endIndex, final String[][] cipher) { + public static String decodingCipher(final int ROW_COUNT, final int COLUMN_COUNT, + final int START_INDEX, + final int END_INDEX, final String[][] CIPHER, final String BARRIR_CHAR) { // 現在いる要素を示すカーソルとして使うクラス DecodeCursor cursor = new DecodeCursor(); // NULLでかこっているため、始まりのインデックスは行、列ともに1から - cursor.setLineIndex(1); + cursor.setRowIndex(1); cursor.setColumnIndex(1); // 移動する方向を表すインスタンス変数 cursor.setDirection(0); // 現在移し終えた文字数のカウンタ int charCounter = 0; // この配列に文字移して最終的には復号した順番に文字が並ぶ - String[] decordResult = new String[lineAmount * columnAmount]; - - while (charCounter < lineAmount * columnAmount) { - // NULLの要素に移動した際、カーソルの位置を戻す処理 - cursor.modifying(charCounter); - // NULLのぶつかるまで一方向に要素を読み取り続ける - while (cipher[cursor.getLineIndex()][cursor.getColumnIndex()] != null) { - decordResult[charCounter] = cipher[cursor.getLineIndex()][cursor.getColumnIndex()]; - cipher[cursor.getLineIndex()][cursor.getColumnIndex()] = null; + String[] decodedResult = new String[ROW_COUNT * COLUMN_COUNT]; + + // 初期位置から初めて@にぶつかるまでの解読処理 + while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { + decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; + CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; + charCounter++; + cursor.shifting(); + } + cursor.changeDirection(); + + // 初めて@にぶつかってから解読し終わるまでの処理 + while (charCounter < ROW_COUNT * COLUMN_COUNT) { + // @の要素に移動した際、カーソルの位置を戻す処理 + cursor.modifying(); + // @のぶつかるまで一方向に要素を読み取り続ける + while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { + decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; + CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; charCounter++; // カーソルを現在進むべき方向に移動させる cursor.shifting(); @@ -99,7 +118,7 @@ public class SpiralDecoder { // 方向転換の処理 cursor.changeDirection(); } - return String.join("", decordResult).substring(startIndex - 1, endIndex); + return String.join("", decodedResult).substring(START_INDEX - 1, END_INDEX); } } @@ -111,59 +130,63 @@ public class SpiralDecoder { */ class DecodeCursor { private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している - private int lineIndex;// 現在の行の位置 + private int rowIndex;// 現在の行の位置 private int columnIndex;// 現在の列の位置 public int getDirection() { return direction; } - public void setDirection(int direction) { - this.direction = direction; + public void setDirection(final int DIRECTION) { + this.direction = DIRECTION; } public void changeDirection() { this.direction++; } - public int getLineIndex() { - return lineIndex; + public int getRowIndex() { + return rowIndex; } - public void setLineIndex(int lineIndex) { - this.lineIndex = lineIndex; + public void setRowIndex(final int ROW_INDEX) { + this.rowIndex = ROW_INDEX; } public int getColumnIndex() { return columnIndex; } - public void setColumnIndex(int columnIndex) { - this.columnIndex = columnIndex; + public void setColumnIndex(final int COLUMN_INDEX) { + this.columnIndex = COLUMN_INDEX; } - // NULLの要素に移動した際、カーソルの位置を戻す処理 - // 4つの方向はdirectionを4で割った剰余として区別している + // NULL(要検討)の要素に移動した際、カーソルの位置を戻す処理 + // 4つの方向はdirectionを4で割った剰余として区別する // 0:右 1:下 2:左 3:上 - public void modifying(final int charCounter) { - switch (direction % 4) { - case 0: - if (charCounter != 0) { - this.columnIndex++; - this.lineIndex++; - } + public void modifying() { + final int DIRECTION_NUM = this.direction % 4; + final int RIGHT_NUM = 0; + final int DOWN_NUM = 1; + final int LEFT_NUM = 2; + final int UP_NUM = 3; + + switch (DIRECTION_NUM) { + case RIGHT_NUM: + this.columnIndex++; + this.rowIndex++; break; - case 1: + case DOWN_NUM: this.columnIndex--; - this.lineIndex++; + this.rowIndex++; break; - case 2: + case LEFT_NUM: this.columnIndex--; - this.lineIndex--; + this.rowIndex--; break; - case 3: + case UP_NUM: this.columnIndex++; - this.lineIndex--; + this.rowIndex--; break; } } @@ -172,18 +195,24 @@ class DecodeCursor { // 4つの方向はdirectionを4で割った剰余として区別している // 0:右 1:下 2:左 3:上 public void shifting() { - switch (this.direction % 4) { - case 0: + final int DIRECTION_NUM = this.direction % 4; + final int RIGHT_NUM = 0; + final int DOWN_NUM = 1; + final int LEFT_NUM = 2; + final int UP_NUM = 3; + + switch (DIRECTION_NUM) { + case RIGHT_NUM: this.columnIndex++; break; - case 1: - this.lineIndex++; + case DOWN_NUM: + this.rowIndex++; break; - case 2: + case LEFT_NUM: this.columnIndex--; break; - case 3: - this.lineIndex--; + case UP_NUM: + this.rowIndex--; break; } } -- GitLab From cad12ab177baec88577281987f99500f6fe0c6b4 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Mon, 7 Jul 2025 17:44:16 +0900 Subject: [PATCH 03/10] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB156?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 119 +++++++++++++++---------------- 1 file changed, 58 insertions(+), 61 deletions(-) diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java index be0dc1b..45c2046 100644 --- a/kkanazawa/src/SpiralDecoder.java +++ b/kkanazawa/src/SpiralDecoder.java @@ -13,37 +13,37 @@ public class SpiralDecoder { public static void main(final String[] args) { Scanner scanner = new Scanner(System.in); - final int ROW_COUNT = scanner.nextInt(); // 入力値の行の数 - final int COLUMN_COUNT = scanner.nextInt(); // 入力値の列の数; - final int START_INDEX = scanner.nextInt(); // 読み始まる文字のインデックス - final int END_INDEX = scanner.nextInt(); // 読み終える文字のインデックス - final String BARRIR_CHAR = "@"; + final int rowCount = scanner.nextInt(); // 入力値の行の数 + final int columnCount = scanner.nextInt(); // 入力値の列の数; + final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス + final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス + final String barrirchar = "@"; // もととなる二次元の文字リストのを入力から受け取る - final String[][] CIPHER = inputCipher(scanner, ROW_COUNT, COLUMN_COUNT); + final String[][] cipher = inputCipher(scanner, rowCount, columnCount); scanner.close(); // 入力値の二次元の文字リストの周りをNULLで埋める - final String[][] PADDED_CIPHER = paddingCipher(CIPHER, BARRIR_CHAR); + final String[][] paddedCipher = paddingCipher(cipher, barrirchar); // 解読処理 - final String selectedDecode = decodingCipher(ROW_COUNT, COLUMN_COUNT, START_INDEX, END_INDEX, - PADDED_CIPHER, BARRIR_CHAR); + final String selectedDecode = decodingCipher(rowCount, columnCount, startIndex, endIndex, + paddedCipher, barrirchar); System.out.println(selectedDecode); } /** * もととなる文字列を入力から受け取る * - * @param SCANNER 入力を受け取るスキャナー - * @param ROW_COUNT 文字リストの行数 - * @param COLUMN_COUNT 文字リストの列数 + * @param scanner 入力を受け取るスキャナー + * @param rowCount 文字リストの行数 + * @param columnCount 文字リストの列数 */ - public static String[][] inputCipher(final Scanner SCANNER, final int ROW_COUNT, - final int COLUMN_COUNT) { - String[][] cipher = new String[ROW_COUNT][COLUMN_COUNT]; - for (int i = 0; i < ROW_COUNT; i++) { - String inputRow = SCANNER.next(); + public static String[][] inputCipher(final Scanner scanner, final int rowCount, + final int columnCount) { + String[][] cipher = new String[rowCount][columnCount]; + for (int i = 0; i < rowCount; i++) { + String inputRow = scanner.next(); cipher[i] = inputRow.split(""); } return cipher; @@ -52,20 +52,20 @@ public class SpiralDecoder { /** * 文字リストの周りを@で埋める * - * @param ORIGIN_CIPHER もととなる文字リスト + * @param originCipher もととなる文字リスト */ - public static String[][] paddingCipher(final String[][] ORIGIN_CIPHER, final String BARRIR_CHAR) { - final int ROW_COUNT = Array.getLength(ORIGIN_CIPHER); - final int COLUMN_COUNT = Array.getLength(ORIGIN_CIPHER[0]); + public static String[][] paddingCipher(final String[][] originCipher, final String barrirchar) { + final int ROW_COUNT = Array.getLength(originCipher); + final int COLUMN_COUNT = Array.getLength(originCipher[0]); String[][] paddedCipher = new String[ROW_COUNT + 2][COLUMN_COUNT + 2]; for (String[] o : paddedCipher) { - Arrays.fill(o, BARRIR_CHAR); + Arrays.fill(o, barrirchar); } // 元の文字リストを"@"しかない文字リストの中央にコピーする for (int i = 0; i < ROW_COUNT; i++) { - System.arraycopy(ORIGIN_CIPHER[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); + System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); } return paddedCipher; } @@ -74,14 +74,14 @@ public class SpiralDecoder { * 周りをNULLで埋めた文字リストの解読 @にぶつかったら方向転換するイメージでうずまき状に要素を移動する * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える * - * @param ROW_COUNT 文字列リストの行数 - * @param COLUMN_COUNT 文字列リストの列数 - * @param START_INDEX 解読後読み始める文字の順番 - * @param END_INDEX 解読後読み終える文字の順番 + * @param rowCount 文字列リストの行数 + * @param columnCount 文字列リストの列数 + * @param startIndex 解読後読み始める文字の順番 + * @param endIndex 解読後読み終える文字の順番 */ - public static String decodingCipher(final int ROW_COUNT, final int COLUMN_COUNT, - final int START_INDEX, - final int END_INDEX, final String[][] CIPHER, final String BARRIR_CHAR) { + public static String decodingCipher(final int rowCount, final int columnCount, + final int startIndex, + final int endIndex, final String[][] cipher, final String barrirchar) { // 現在いる要素を示すカーソルとして使うクラス DecodeCursor cursor = new DecodeCursor(); // NULLでかこっているため、始まりのインデックスは行、列ともに1から @@ -92,25 +92,25 @@ public class SpiralDecoder { // 現在移し終えた文字数のカウンタ int charCounter = 0; // この配列に文字移して最終的には復号した順番に文字が並ぶ - String[] decodedResult = new String[ROW_COUNT * COLUMN_COUNT]; + String[] decodedResult = new String[rowCount * columnCount]; // 初期位置から初めて@にぶつかるまでの解読処理 - while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { - decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; - CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; + while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { + decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; + cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; charCounter++; cursor.shifting(); } cursor.changeDirection(); // 初めて@にぶつかってから解読し終わるまでの処理 - while (charCounter < ROW_COUNT * COLUMN_COUNT) { + while (charCounter < rowCount * columnCount) { // @の要素に移動した際、カーソルの位置を戻す処理 cursor.modifying(); // @のぶつかるまで一方向に要素を読み取り続ける - while (CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] != BARRIR_CHAR) { - decodedResult[charCounter] = CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()]; - CIPHER[cursor.getRowIndex()][cursor.getColumnIndex()] = BARRIR_CHAR; + while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { + decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; + cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; charCounter++; // カーソルを現在進むべき方向に移動させる cursor.shifting(); @@ -118,7 +118,7 @@ public class SpiralDecoder { // 方向転換の処理 cursor.changeDirection(); } - return String.join("", decodedResult).substring(START_INDEX - 1, END_INDEX); + return String.join("", decodedResult).substring(startIndex - 1, endIndex); } } @@ -132,6 +132,10 @@ class DecodeCursor { private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している private int rowIndex;// 現在の行の位置 private int columnIndex;// 現在の列の位置 + final static int RIGHT = 0; + final static int DOWN = 1; + final static int LEFT = 2; + final static int UP = 3; public int getDirection() { return direction; @@ -165,26 +169,22 @@ class DecodeCursor { // 4つの方向はdirectionを4で割った剰余として区別する // 0:右 1:下 2:左 3:上 public void modifying() { - final int DIRECTION_NUM = this.direction % 4; - final int RIGHT_NUM = 0; - final int DOWN_NUM = 1; - final int LEFT_NUM = 2; - final int UP_NUM = 3; - - switch (DIRECTION_NUM) { - case RIGHT_NUM: + final int directionNum = this.direction % 4; + + switch (directionNum) { + case RIGHT: this.columnIndex++; this.rowIndex++; break; - case DOWN_NUM: + case DOWN: this.columnIndex--; this.rowIndex++; break; - case LEFT_NUM: + case LEFT: this.columnIndex--; this.rowIndex--; break; - case UP_NUM: + case UP: this.columnIndex++; this.rowIndex--; break; @@ -195,23 +195,20 @@ class DecodeCursor { // 4つの方向はdirectionを4で割った剰余として区別している // 0:右 1:下 2:左 3:上 public void shifting() { - final int DIRECTION_NUM = this.direction % 4; - final int RIGHT_NUM = 0; - final int DOWN_NUM = 1; - final int LEFT_NUM = 2; - final int UP_NUM = 3; - - switch (DIRECTION_NUM) { - case RIGHT_NUM: + final int directionNum= this.direction % 4; + + + switch (directionNum) { + case RIGHT: this.columnIndex++; break; - case DOWN_NUM: + case DOWN: this.rowIndex++; break; - case LEFT_NUM: + case LEFT: this.columnIndex--; break; - case UP_NUM: + case UP: this.rowIndex--; break; } -- GitLab From 72ac6f64abff7648a28da76d14f4de58c3780580 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Tue, 8 Jul 2025 15:55:56 +0900 Subject: [PATCH 04/10] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB128?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/SpiralDecoder.java | 216 ------------------ .../src/TwoDimensionBarcodeConversion.java | 173 ++++++++++++++ 2 files changed, 173 insertions(+), 216 deletions(-) delete mode 100644 kkanazawa/src/SpiralDecoder.java create mode 100644 kkanazawa/src/TwoDimensionBarcodeConversion.java diff --git a/kkanazawa/src/SpiralDecoder.java b/kkanazawa/src/SpiralDecoder.java deleted file mode 100644 index 45c2046..0000000 --- a/kkanazawa/src/SpiralDecoder.java +++ /dev/null @@ -1,216 +0,0 @@ -package paiza.src; - -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.Scanner; - -/** - * 受け取った二次元の文字リストをうずまき型で解読する - * - * @author 金澤継心 - */ -public class SpiralDecoder { - - public static void main(final String[] args) { - Scanner scanner = new Scanner(System.in); - final int rowCount = scanner.nextInt(); // 入力値の行の数 - final int columnCount = scanner.nextInt(); // 入力値の列の数; - final int startIndex = scanner.nextInt(); // 読み始まる文字のインデックス - final int endIndex = scanner.nextInt(); // 読み終える文字のインデックス - final String barrirchar = "@"; - - // もととなる二次元の文字リストのを入力から受け取る - final String[][] cipher = inputCipher(scanner, rowCount, columnCount); - scanner.close(); - - // 入力値の二次元の文字リストの周りをNULLで埋める - final String[][] paddedCipher = paddingCipher(cipher, barrirchar); - - // 解読処理 - final String selectedDecode = decodingCipher(rowCount, columnCount, startIndex, endIndex, - paddedCipher, barrirchar); - System.out.println(selectedDecode); - } - - /** - * もととなる文字列を入力から受け取る - * - * @param scanner 入力を受け取るスキャナー - * @param rowCount 文字リストの行数 - * @param columnCount 文字リストの列数 - */ - public static String[][] inputCipher(final Scanner scanner, final int rowCount, - final int columnCount) { - String[][] cipher = new String[rowCount][columnCount]; - for (int i = 0; i < rowCount; i++) { - String inputRow = scanner.next(); - cipher[i] = inputRow.split(""); - } - return cipher; - } - - /** - * 文字リストの周りを@で埋める - * - * @param originCipher もととなる文字リスト - */ - public static String[][] paddingCipher(final String[][] originCipher, final String barrirchar) { - final int ROW_COUNT = Array.getLength(originCipher); - final int COLUMN_COUNT = Array.getLength(originCipher[0]); - String[][] paddedCipher = new String[ROW_COUNT + 2][COLUMN_COUNT + 2]; - - for (String[] o : paddedCipher) { - Arrays.fill(o, barrirchar); - } - - // 元の文字リストを"@"しかない文字リストの中央にコピーする - for (int i = 0; i < ROW_COUNT; i++) { - System.arraycopy(originCipher[i], 0, paddedCipher[i + 1], 1, COLUMN_COUNT); - } - return paddedCipher; - } - - /** - * 周りをNULLで埋めた文字リストの解読 @にぶつかったら方向転換するイメージでうずまき状に要素を移動する - * 移動した順に要素を復号用の配列に移していき、元の配列の要素はNULLに置き換える - * - * @param rowCount 文字列リストの行数 - * @param columnCount 文字列リストの列数 - * @param startIndex 解読後読み始める文字の順番 - * @param endIndex 解読後読み終える文字の順番 - */ - public static String decodingCipher(final int rowCount, final int columnCount, - final int startIndex, - final int endIndex, final String[][] cipher, final String barrirchar) { - // 現在いる要素を示すカーソルとして使うクラス - DecodeCursor cursor = new DecodeCursor(); - // NULLでかこっているため、始まりのインデックスは行、列ともに1から - cursor.setRowIndex(1); - cursor.setColumnIndex(1); - // 移動する方向を表すインスタンス変数 - cursor.setDirection(0); - // 現在移し終えた文字数のカウンタ - int charCounter = 0; - // この配列に文字移して最終的には復号した順番に文字が並ぶ - String[] decodedResult = new String[rowCount * columnCount]; - - // 初期位置から初めて@にぶつかるまでの解読処理 - while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { - decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; - cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; - charCounter++; - cursor.shifting(); - } - cursor.changeDirection(); - - // 初めて@にぶつかってから解読し終わるまでの処理 - while (charCounter < rowCount * columnCount) { - // @の要素に移動した際、カーソルの位置を戻す処理 - cursor.modifying(); - // @のぶつかるまで一方向に要素を読み取り続ける - while (cipher[cursor.getRowIndex()][cursor.getColumnIndex()] != barrirchar) { - decodedResult[charCounter] = cipher[cursor.getRowIndex()][cursor.getColumnIndex()]; - cipher[cursor.getRowIndex()][cursor.getColumnIndex()] = barrirchar; - charCounter++; - // カーソルを現在進むべき方向に移動させる - cursor.shifting(); - } - // 方向転換の処理 - cursor.changeDirection(); - } - return String.join("", decodedResult).substring(startIndex - 1, endIndex); - } - -} - - -/** - * 現在読み取っている要素を示すカーソルとして使うクラス - * - */ -class DecodeCursor { - private int direction;// 方向を示す 4つの方向はこの値を4で割った剰余として区別している - private int rowIndex;// 現在の行の位置 - private int columnIndex;// 現在の列の位置 - final static int RIGHT = 0; - final static int DOWN = 1; - final static int LEFT = 2; - final static int UP = 3; - - public int getDirection() { - return direction; - } - - public void setDirection(final int DIRECTION) { - this.direction = DIRECTION; - } - - public void changeDirection() { - this.direction++; - } - - public int getRowIndex() { - return rowIndex; - } - - public void setRowIndex(final int ROW_INDEX) { - this.rowIndex = ROW_INDEX; - } - - public int getColumnIndex() { - return columnIndex; - } - - public void setColumnIndex(final int COLUMN_INDEX) { - this.columnIndex = COLUMN_INDEX; - } - - // NULL(要検討)の要素に移動した際、カーソルの位置を戻す処理 - // 4つの方向はdirectionを4で割った剰余として区別する - // 0:右 1:下 2:左 3:上 - public void modifying() { - final int directionNum = this.direction % 4; - - switch (directionNum) { - case RIGHT: - this.columnIndex++; - this.rowIndex++; - break; - case DOWN: - this.columnIndex--; - this.rowIndex++; - break; - case LEFT: - this.columnIndex--; - this.rowIndex--; - break; - case UP: - this.columnIndex++; - this.rowIndex--; - break; - } - } - - // カーソルを現在進むべき方向に移動させる - // 4つの方向はdirectionを4で割った剰余として区別している - // 0:右 1:下 2:左 3:上 - public void shifting() { - final int directionNum= this.direction % 4; - - - switch (directionNum) { - case RIGHT: - this.columnIndex++; - break; - case DOWN: - this.rowIndex++; - break; - case LEFT: - this.columnIndex--; - break; - case UP: - this.rowIndex--; - break; - } - } -} diff --git a/kkanazawa/src/TwoDimensionBarcodeConversion.java b/kkanazawa/src/TwoDimensionBarcodeConversion.java new file mode 100644 index 0000000..8d85be2 --- /dev/null +++ b/kkanazawa/src/TwoDimensionBarcodeConversion.java @@ -0,0 +1,173 @@ +package paiza.src; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Scanner; +/** + * 受け取った数字を二次元の簡易バーコードに変換して出力する + * + * @author 金澤継心 + */ +public class TwoDimensionBarcodeConversion { + //入力値を一桁ごとに分ける際に使用 + final static int DIVISOR = 10; + + public static void main(final String[] args) { + + int inputNumber = inputNumber(); + + ArrayList allNumber = divideThreeEach(inputNumber); + + for (ThreeNumbers threeNum : allNumber) { + threeNum.outputDecodeResult(); + } + } + /** + * 整数値を入力して返す + * 整数の入力は以下の条件を満たす + * ・100 ≦ N ≦ 999,999,999 + * ・(N の桁数) = 3 の倍数 + */ + public static int inputNumber() { + int inputNum = 0; + + try(Scanner scanner = new Scanner(System.in)){ + inputNum = scanner.nextInt(); + }catch(Exception e) { + System.out.println("エラー発生"); + } + return inputNum; + } + /** + * 引数の整数値を1桁ごとに分け、それを三つずつまとめたリストを返す + * + * @param inputNumber 入力された整数値 + */ + public static ArrayList divideThreeEach(final int inputNumber){ + ArrayList splitNum = divideOneEach(inputNumber); + ArrayList allNumbers = new ArrayList<>(); + + //一桁ごとに分けられた数字を3つずつにまとめてリストに格納 + for (int count = 0; count < splitNum.size(); count += 2) { + ThreeNumbers three = new ThreeNumbers(); + three.setNumber(splitNum.get(count), splitNum.get(count + 1), splitNum.get(count + 2)); + allNumbers.add(three); + count++; + } + return allNumbers; + } + /** + * 引数の整数値を一桁ごとに分けてリストとして返す + * + * @param inputNumber 入力された整数値 + */ + public static ArrayList divideOneEach(final int inputNumber){ + int originNumber = inputNumber; + ArrayList splitNum = new ArrayList<>(); + while (originNumber > 0) { + splitNum.add(originNumber % DIVISOR); + originNumber /= DIVISOR; + } + Collections.reverse(splitNum); + + return splitNum; + } +} + +/** + * 入力値において並んだ三つの桁の値を保存するクラス + * + */ +class ThreeNumbers { + private int leftNumber; + private int centerNumber; + private int rightNumber; + + public int getLeftNumber() { + return leftNumber; + } + + public void setNumber(final int leftNumber, final int centerNumber, final int rightNumber) { + this.leftNumber = leftNumber; + this.centerNumber = centerNumber; + this.rightNumber = rightNumber; + } + + public void setLeftNumber(int leftNumber) { + this.leftNumber = leftNumber; + } + + public int getCenterNumber() { + return centerNumber; + } + + public void setCenterNumber(int centerNumber) { + this.centerNumber = centerNumber; + } + + public int getRightNumber() { + return rightNumber; + } + + public void setRightNumber(int rightNumber) { + this.rightNumber = rightNumber; + } + + public void outputDecodeResult() { + for(StringBuilder decodeNum : decodeThreeNum()) { + System.out.println(decodeNum); + } + } + /** + * 三つの数を二次元バーコードに変換して返す. + * decodeNum()で一つ一つの数字をバーコードに変換した後、 + * それらを横並びにつなげる + * + */ + public ArrayList decodeThreeNum() { + int left = leftNumber; + int center = centerNumber; + int right = rightNumber; + ArrayList leftDecode = decodeNum(left); + ArrayList centerDecode = decodeNum(center); + ArrayList rightDecode = decodeNum(right); + + ArrayList decodedThree = new ArrayList<>(); + //左、中央、右の数のバーコードを表す文字列で + //同じcount行目の文字列をつなげる + for (int count = 0; count < 3; count++) { + StringBuilder row = new StringBuilder(); + row.append(leftDecode.get(count)); + row.append(centerDecode.get(count)); + row.append(rightDecode.get(count)); + decodedThree.add(row); + } + return decodedThree; + } + /** + * 一つの数字を二次元バーコードに変換して返す. + * 二次元バーコードを三行の文字列として扱う. + */ + public ArrayList decodeNum(final int number) { + int num = number; + //decodedNumに1行目、2行目、3行目の文字列をそれぞれ入れる + ArrayList decodedNum = new ArrayList<>(); + + for (int count1 = 0; count1 < 3; count1++) { + //rowに1行分の文字列をを入れる + StringBuilder row = new StringBuilder(); + //1行文の文字列を作成 + for (int count2 = 0; count2 < 3; count2++) { + if (0 < num) { + row.append("#"); + } else { + row.append("."); + } + num--; + } + decodedNum.add(row); + } + return decodedNum; + } + +} -- GitLab From e33dab918063d5d944c51a770155d3f3bef8ac03 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Tue, 8 Jul 2025 16:21:03 +0900 Subject: [PATCH 05/10] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB128?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/TwoDimensionBarcodeConversion.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/kkanazawa/src/TwoDimensionBarcodeConversion.java b/kkanazawa/src/TwoDimensionBarcodeConversion.java index 8d85be2..f40db7b 100644 --- a/kkanazawa/src/TwoDimensionBarcodeConversion.java +++ b/kkanazawa/src/TwoDimensionBarcodeConversion.java @@ -14,9 +14,9 @@ public class TwoDimensionBarcodeConversion { public static void main(final String[] args) { - int inputNumber = inputNumber(); + final int inputNumber = inputNumber(); - ArrayList allNumber = divideThreeEach(inputNumber); + final ArrayList allNumber = divideThreeEach(inputNumber); for (ThreeNumbers threeNum : allNumber) { threeNum.outputDecodeResult(); @@ -44,7 +44,7 @@ public class TwoDimensionBarcodeConversion { * @param inputNumber 入力された整数値 */ public static ArrayList divideThreeEach(final int inputNumber){ - ArrayList splitNum = divideOneEach(inputNumber); + final ArrayList splitNum = divideOneEach(inputNumber); ArrayList allNumbers = new ArrayList<>(); //一桁ごとに分けられた数字を3つずつにまとめてリストに格納 @@ -125,12 +125,9 @@ class ThreeNumbers { * */ public ArrayList decodeThreeNum() { - int left = leftNumber; - int center = centerNumber; - int right = rightNumber; - ArrayList leftDecode = decodeNum(left); - ArrayList centerDecode = decodeNum(center); - ArrayList rightDecode = decodeNum(right); + ArrayList leftDecode = decodeNum(leftNumber); + ArrayList centerDecode = decodeNum(centerNumber); + ArrayList rightDecode = decodeNum(rightNumber); ArrayList decodedThree = new ArrayList<>(); //左、中央、右の数のバーコードを表す文字列で -- GitLab From 20bf65f60263573c4d54a017d2b09053b76528a6 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Tue, 8 Jul 2025 17:12:08 +0900 Subject: [PATCH 06/10] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB128?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...java => TwoDimensionBarcodeConverter.java} | 64 ++++++++----------- 1 file changed, 26 insertions(+), 38 deletions(-) rename kkanazawa/src/{TwoDimensionBarcodeConversion.java => TwoDimensionBarcodeConverter.java} (67%) diff --git a/kkanazawa/src/TwoDimensionBarcodeConversion.java b/kkanazawa/src/TwoDimensionBarcodeConverter.java similarity index 67% rename from kkanazawa/src/TwoDimensionBarcodeConversion.java rename to kkanazawa/src/TwoDimensionBarcodeConverter.java index f40db7b..43fbd51 100644 --- a/kkanazawa/src/TwoDimensionBarcodeConversion.java +++ b/kkanazawa/src/TwoDimensionBarcodeConverter.java @@ -2,13 +2,14 @@ package paiza.src; import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Scanner; /** * 受け取った数字を二次元の簡易バーコードに変換して出力する * * @author 金澤継心 */ -public class TwoDimensionBarcodeConversion { +public class TwoDimensionBarcodeConverter { //入力値を一桁ごとに分ける際に使用 final static int DIVISOR = 10; @@ -16,7 +17,7 @@ public class TwoDimensionBarcodeConversion { final int inputNumber = inputNumber(); - final ArrayList allNumber = divideThreeEach(inputNumber); + final List allNumber = divideThreeEach(inputNumber); for (ThreeNumbers threeNum : allNumber) { threeNum.outputDecodeResult(); @@ -43,14 +44,13 @@ public class TwoDimensionBarcodeConversion { * * @param inputNumber 入力された整数値 */ - public static ArrayList divideThreeEach(final int inputNumber){ - final ArrayList splitNum = divideOneEach(inputNumber); - ArrayList allNumbers = new ArrayList<>(); + public static List divideThreeEach(final int inputNumber){ + final List splitNum = divideOneEach(inputNumber); + List allNumbers = new ArrayList<>(); //一桁ごとに分けられた数字を3つずつにまとめてリストに格納 for (int count = 0; count < splitNum.size(); count += 2) { - ThreeNumbers three = new ThreeNumbers(); - three.setNumber(splitNum.get(count), splitNum.get(count + 1), splitNum.get(count + 2)); + ThreeNumbers three = new ThreeNumbers(splitNum.get(count),splitNum.get(count+1),splitNum.get(count+2)); allNumbers.add(three); count++; } @@ -61,9 +61,9 @@ public class TwoDimensionBarcodeConversion { * * @param inputNumber 入力された整数値 */ - public static ArrayList divideOneEach(final int inputNumber){ + public static List divideOneEach(final int inputNumber){ int originNumber = inputNumber; - ArrayList splitNum = new ArrayList<>(); + List splitNum = new ArrayList<>(); while (originNumber > 0) { splitNum.add(originNumber % DIVISOR); originNumber /= DIVISOR; @@ -82,37 +82,25 @@ class ThreeNumbers { private int leftNumber; private int centerNumber; private int rightNumber; - - public int getLeftNumber() { - return leftNumber; - } - - public void setNumber(final int leftNumber, final int centerNumber, final int rightNumber) { + + public ThreeNumbers(int leftNumber, int centerNumber, int rightNumber) { this.leftNumber = leftNumber; this.centerNumber = centerNumber; this.rightNumber = rightNumber; } - public void setLeftNumber(int leftNumber) { - this.leftNumber = leftNumber; + public int getLeftNumber() { + return leftNumber; } public int getCenterNumber() { return centerNumber; } - public void setCenterNumber(int centerNumber) { - this.centerNumber = centerNumber; - } - public int getRightNumber() { return rightNumber; } - public void setRightNumber(int rightNumber) { - this.rightNumber = rightNumber; - } - public void outputDecodeResult() { for(StringBuilder decodeNum : decodeThreeNum()) { System.out.println(decodeNum); @@ -124,12 +112,12 @@ class ThreeNumbers { * それらを横並びにつなげる * */ - public ArrayList decodeThreeNum() { - ArrayList leftDecode = decodeNum(leftNumber); - ArrayList centerDecode = decodeNum(centerNumber); - ArrayList rightDecode = decodeNum(rightNumber); + public List decodeThreeNum() { + List leftDecode = decodeSingleNum(leftNumber); + List centerDecode = decodeSingleNum(centerNumber); + List rightDecode = decodeSingleNum(rightNumber); - ArrayList decodedThree = new ArrayList<>(); + List decodedThreeNum = new ArrayList<>(); //左、中央、右の数のバーコードを表す文字列で //同じcount行目の文字列をつなげる for (int count = 0; count < 3; count++) { @@ -137,24 +125,24 @@ class ThreeNumbers { row.append(leftDecode.get(count)); row.append(centerDecode.get(count)); row.append(rightDecode.get(count)); - decodedThree.add(row); + decodedThreeNum.add(row); } - return decodedThree; + return decodedThreeNum; } /** * 一つの数字を二次元バーコードに変換して返す. * 二次元バーコードを三行の文字列として扱う. */ - public ArrayList decodeNum(final int number) { + public List decodeSingleNum(final int number) { int num = number; //decodedNumに1行目、2行目、3行目の文字列をそれぞれ入れる - ArrayList decodedNum = new ArrayList<>(); - - for (int count1 = 0; count1 < 3; count1++) { - //rowに1行分の文字列をを入れる + List decodedNum = new ArrayList<>(); + //上から1行目、2行目、3行目の順に文字列を作成 + for (int rowindex = 0; rowindex < 3; rowindex++) { + //rowに1行分の文字列を入れる StringBuilder row = new StringBuilder(); //1行文の文字列を作成 - for (int count2 = 0; count2 < 3; count2++) { + for (int columnIndex = 0; columnIndex < 3; columnIndex++) { if (0 < num) { row.append("#"); } else { -- GitLab From cf397ef97ff249e6c80b61d9226a76fa38128ba5 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Wed, 23 Jul 2025 16:35:20 +0900 Subject: [PATCH 07/10] =?UTF-8?q?paizaB153=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/Katori.java | 236 +++++++++++++++++++++++++++++++++ kkanazawa/test/KatoriTest.java | 204 ++++++++++++++++++++++++++++ 2 files changed, 440 insertions(+) create mode 100644 kkanazawa/src/Katori.java create mode 100644 kkanazawa/test/KatoriTest.java diff --git a/kkanazawa/src/Katori.java b/kkanazawa/src/Katori.java new file mode 100644 index 0000000..c3dd0f4 --- /dev/null +++ b/kkanazawa/src/Katori.java @@ -0,0 +1,236 @@ +package paiza.src; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +/** + * + * + */ +public class Katori { + + public static void main(final String[] args) { + Scanner scanner = new Scanner(System.in); + final int sideLength = scanner.nextInt(); // 入力値の行,列の数 + + // もととなる二次元配列を入力から受け取る + final int[][] coil = KatoriIO.inputCoil(scanner, sideLength); + scanner.close(); + + // 入力値の二次元配列の周りを-1で埋める + final int[][] paddedCoil = CalcKatori.paddingCoil(coil, sideLength); + + // 二次元配列を渦巻き状に読み取り一次元リストに保存 + final List decodedKatori = CalcKatori.decodeKatori(sideLength, paddedCoil); + + // 渦巻き状に読み取ったリストから1/4, 2/4, 3/4, すべて燃えきる,までにかかる時間を計算 + final List fourTimes = CalcKatori.calcurateTime(decodedKatori); + + // 計算結果の表示 + KatoriIO.outputTimes(fourTimes); + } + +} + + +/** + * 入出力を行うクラス + * + */ +final class KatoriIO { + /** + * 蚊取り線香のブロックごとに燃える時間を二次元配列として入力する + * + * @param scanner + * @param sideLength 文字リストの行数、列数 + */ + public static int[][] inputCoil(final Scanner scanner, final int sideLength) { + int[][] coil = new int[sideLength][sideLength]; + for (int row = 0; row < sideLength; row++) { + for (int column = 0; column < sideLength; column++) { + coil[row][column] = scanner.nextInt(); + } + } + return coil; + } + + /** + * 蚊取り線香が1/4, 2/4, 3/4, すべて燃えきる,までにかかる時間を表示する + * + */ + public static void outputTimes(List fourTimes) { + for (Integer time : fourTimes) { + System.out.println(time); + } + + } +} + + +final class CalcKatori { + // 二次元配列を囲う値 + final static int BARRIER_NUM = -1; + + /** + * 二次元配列の周りを"-1"で埋める + * + * @param originCipher もととなる文字リスト + */ + public static int[][] paddingCoil(final int[][] originCoil, final int sideLength) { + + int[][] paddedCoil = new int[sideLength + 2][sideLength + 2]; + + for (int[] o : paddedCoil) { + Arrays.fill(o, BARRIER_NUM); + } + // 元リストを"-1"しかない文字リストの中央にコピーする + for (int row = 0; row < sideLength; row++) { + System.arraycopy(originCoil[row], 0, paddedCoil[row + 1], 1, sideLength); + } + return paddedCoil; + } + + /** + * 周りを"-1"で埋めた文字リストの解読 "-1"にぶつかったら方向転換するイメージでうずまき状に要素を移動する 移動した順に二次元配列の要素を1次元の別のリストに移していく + * 読み取った二次元配列の要素は"-1"に置き換える + * + * @param sideLength 文字列リストの行数、列数 + * @param coil 二次元配列 + */ + public static List decodeKatori(final int sideLength, final int[][] coil) { + + // "-1"でかこっているため、始まりのインデックスは行、列ともに1から + IndexPointer pointer = new IndexPointer(1, 1, Direction.RIGHT); + + // 現在移し終えた要素のカウンタ + int brockCounter = 0; + // この配列に要素を移して最終的にはうずまきの順番に値が並ぶ + List brockTimes = new ArrayList<>(); + + // 初期位置から初めて-1にぶつかるまでの解読処理 + while (coil[pointer.getRowIndex()][pointer.getColumnIndex()] != BARRIER_NUM) { + brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); + coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; + brockCounter++; + pointer.shifting(); + } + + // 初めて-1にぶつかってから解読し終わるまでの処理 + while (brockCounter < sideLength * sideLength) { + // -1の要素に移動した際、カーソルの位置を戻す処理 + pointer.modifying(); + // -1にぶつかるまで一方向に要素を読み取り続ける + while (coil[pointer.getRowIndex()][pointer.getColumnIndex()] != BARRIER_NUM) { + brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); + coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; + brockCounter++; + // カーソルを現在進んでいる方向に移動させる + pointer.shifting(); + } + } + + return brockTimes; + + } + + public static List calcurateTime(List brockTimes) { + // 読み取った値を全体の1/4, 2/4, 3/4, 4/4、ごとに和をとり、リストに格納 + List fourTimes = new ArrayList<>(); + fourTimes.add(brockTimes.subList(0, brockTimes.size() / 4).stream().reduce(Integer::sum).get()); + fourTimes.add(brockTimes.subList(0, brockTimes.size() / 2).stream().reduce(Integer::sum).get()); + fourTimes + .add(brockTimes.subList(0, brockTimes.size() * 3 / 4).stream().reduce(Integer::sum).get()); + fourTimes.add(brockTimes.subList(0, brockTimes.size()).stream().reduce(Integer::sum).get()); + return fourTimes; + } +} + + +/** + * 要素を読み取る方向 + */ +enum Direction { + RIGHT, + DOWN, + LEFT, + UP, +} + + +/** + * 現在読み取っている位置を示すポインターのクラス + * + */ +final class IndexPointer { + + private int rowIndex;// 現在の行の位置 + private int columnIndex;// 現在の列の位置 + private Direction direction; + + public IndexPointer(int rowIndex, int columnIndex, Direction direction) { + this.rowIndex = rowIndex; + this.columnIndex = columnIndex; + this.direction = direction; + } + + public Direction getDirection() { + return direction; + } + + public int getRowIndex() { + return rowIndex; + } + + public int getColumnIndex() { + return columnIndex; + } + + // -1の要素に移動した際、カーソルの位置を戻す処理 + // 時計周りに移動方向を変える + public void modifying() { + + switch (direction) { + case RIGHT: + this.columnIndex--; + this.rowIndex++; + direction = Direction.DOWN; + break; + case DOWN: + this.columnIndex--; + this.rowIndex--; + direction = Direction.LEFT; + break; + case LEFT: + this.columnIndex++; + this.rowIndex--; + direction = Direction.UP; + break; + case UP: + this.columnIndex++; + this.rowIndex++; + direction = Direction.RIGHT; + break; + } + } + + // カーソルを現在進む方向に移動させる + public void shifting() { + + switch (direction) { + case RIGHT: + this.columnIndex++; + break; + case DOWN: + this.rowIndex++; + break; + case LEFT: + this.columnIndex--; + break; + case UP: + this.rowIndex--; + break; + } + } +} diff --git a/kkanazawa/test/KatoriTest.java b/kkanazawa/test/KatoriTest.java new file mode 100644 index 0000000..3e0c1cd --- /dev/null +++ b/kkanazawa/test/KatoriTest.java @@ -0,0 +1,204 @@ +package paiza.src; + +import static org.junit.Assert.*; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +public class KatoriTest { + + //二次元配列の周りを囲う値 + final static int BARRIER_NUM = -1; + //二次元配列の行数、列数 + final static int SIDE_LENGTH = 4; + + public static class KatoriIOTest { + + + @Test + public void OutputTimesに12_39_57_82_を格納したリストを渡すと各要素が改行を入れて出力される() { + String expected = 12 + + System.lineSeparator() + + + 39 + + System.lineSeparator() + + + 57 + + System.lineSeparator() + + + 82 + +System.lineSeparator(); + + List fourTimes = new ArrayList<>(); + fourTimes.add(12); + fourTimes.add(39); + fourTimes.add(57); + fourTimes.add(82); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + KatoriIO.outputTimes(fourTimes); + + assertEquals(expected,out.toString()); + } + } + + public static class CalckatoriTest { + int[][] coil; + int[][] paddingCoil; + List decodedKatori; + List times; + + @Before + public void setUp() { + coil = new int[][] { + { + 3, 4, 3, 2 + }, { + 7, 9, 3, 7 + }, { + 1, 9, 4, 6 + }, { + 3, 7, 8, 6 + } + }; + + paddingCoil = new int[][] { + { + -1, -1, -1, -1, -1, -1 + }, { + -1, 3, 4, 3, 2, -1 + }, { + -1, 7, 9, 3, 7, -1 + }, { + -1, 1, 9, 4, 6, -1 + }, { + -1, 3, 7, 8, 6, -1 + }, { + -1, -1, -1, -1, -1, -1 + } + }; + + decodedKatori = new ArrayList<>(); + decodedKatori.add(3); + decodedKatori.add(4); + decodedKatori.add(3); + decodedKatori.add(2); + decodedKatori.add(7); + decodedKatori.add(6); + decodedKatori.add(6); + decodedKatori.add(8); + decodedKatori.add(7); + decodedKatori.add(3); + decodedKatori.add(1); + decodedKatori.add(7); + decodedKatori.add(9); + decodedKatori.add(3); + decodedKatori.add(4); + decodedKatori.add(9); + + times = new ArrayList<>(); + times.add(12); + times.add(39); + times.add(57); + times.add(82); + } + + @Test + public void PaddingCoilに4行4列の二次元配列coilを渡すとその二次元配列の周りをBARRIER_NUMで囲った二次元配列を返す() { + int[][] result = CalcKatori.paddingCoil(coil, SIDE_LENGTH); + for (int row = 0; row < SIDE_LENGTH + 2; row++) { + for (int column = 0; column < SIDE_LENGTH + 2; column++) { + assertEquals(paddingCoil[row][column], result[row][column]); + } + } + } + + @Test + public void decodedKatoriに二次元配列paddingCoilを渡すとうずまき状に要素を読み取ったリスト返す() { + List result = CalcKatori.decodeKatori(SIDE_LENGTH, paddingCoil); + assertEquals(decodedKatori, result); + } + + @Test + public void calcurateTimeに1次元リストdecodedKatoriを渡すと四分の一_四分の二_四分の三_四分の四ごとに和をとった値のリストを返す() { + List result = CalcKatori.calcurateTime(decodedKatori); + assertEquals(times, result); + } + } + + public static class IndexPointerTest { + + @Test + public void コンストラクタで設定した値がゲッターで取得できる() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.RIGHT); + assertEquals(1, pointer.getRowIndex()); + assertEquals(5, pointer.getColumnIndex()); + assertEquals(Direction.RIGHT, pointer.getDirection()); + + } + + @Test + public void modifyingでRIGHTから時計回りに1周してRIGHTに戻ってくる() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.RIGHT); + assertEquals(Direction.RIGHT, pointer.getDirection()); + pointer.modifying(); + assertEquals(Direction.DOWN, pointer.getDirection()); + pointer.modifying(); + assertEquals(Direction.LEFT, pointer.getDirection()); + pointer.modifying(); + assertEquals(Direction.UP, pointer.getDirection()); + pointer.modifying(); + assertEquals(Direction.RIGHT, pointer.getDirection()); + + } + + @RunWith(Theories.class) + public static class shiftingのテスト { + @DataPoints + public static Fixture[] PARAMS = { + new Fixture(1, 5, Direction.RIGHT, 1, 6), + new Fixture(1, 5, Direction.DOWN, 2, 5), + new Fixture(1, 5, Direction.LEFT, 1, 4), + new Fixture(1, 5, Direction.UP, 0, 5), + }; + @Theory + //RIGHT:行インデックス+0 列インデックス+1 + //DOWN:行インデックス+1 列インデックス+0 + //LEFT:行インデックス+0 列インデックスー1 + //UP:行インデックスー1 列インデックス+0 + public void RIGHT_DOWN_LEFT_UPに対してそれぞれインデックスが適切に変動(Fixture params) throws Exception { + IndexPointer pointer = new IndexPointer(params.indexRow, params.indexColumn, params.direction); + pointer.shifting(); + assertEquals(params.expectedRow, pointer.getRowIndex()); + assertEquals(params.expectedColumn, pointer.getColumnIndex()); + } + + //パラメータ化用のクラス + static class Fixture { + int indexRow; + int indexColumn; + Direction direction; + int expectedRow; + int expectedColumn; + + Fixture(int indexRow, int indexColumn, Direction direction, int expectedRow, int expectedColumn) { + this.indexRow = indexRow; + this.indexColumn = indexColumn; + this.direction = direction; + this.expectedRow = expectedRow; + this.expectedColumn = expectedColumn; + } + } + + } + + + } +} -- GitLab From 74b415c4803ec66ce0f5332a366745b3ecaea207 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Wed, 23 Jul 2025 16:38:47 +0900 Subject: [PATCH 08/10] =?UTF-8?q?paizaB153=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/TwoDimensionBarcodeConverter.java | 158 ------------------ 1 file changed, 158 deletions(-) delete mode 100644 kkanazawa/src/TwoDimensionBarcodeConverter.java diff --git a/kkanazawa/src/TwoDimensionBarcodeConverter.java b/kkanazawa/src/TwoDimensionBarcodeConverter.java deleted file mode 100644 index 43fbd51..0000000 --- a/kkanazawa/src/TwoDimensionBarcodeConverter.java +++ /dev/null @@ -1,158 +0,0 @@ -package paiza.src; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Scanner; -/** - * 受け取った数字を二次元の簡易バーコードに変換して出力する - * - * @author 金澤継心 - */ -public class TwoDimensionBarcodeConverter { - //入力値を一桁ごとに分ける際に使用 - final static int DIVISOR = 10; - - public static void main(final String[] args) { - - final int inputNumber = inputNumber(); - - final List allNumber = divideThreeEach(inputNumber); - - for (ThreeNumbers threeNum : allNumber) { - threeNum.outputDecodeResult(); - } - } - /** - * 整数値を入力して返す - * 整数の入力は以下の条件を満たす - * ・100 ≦ N ≦ 999,999,999 - * ・(N の桁数) = 3 の倍数 - */ - public static int inputNumber() { - int inputNum = 0; - - try(Scanner scanner = new Scanner(System.in)){ - inputNum = scanner.nextInt(); - }catch(Exception e) { - System.out.println("エラー発生"); - } - return inputNum; - } - /** - * 引数の整数値を1桁ごとに分け、それを三つずつまとめたリストを返す - * - * @param inputNumber 入力された整数値 - */ - public static List divideThreeEach(final int inputNumber){ - final List splitNum = divideOneEach(inputNumber); - List allNumbers = new ArrayList<>(); - - //一桁ごとに分けられた数字を3つずつにまとめてリストに格納 - for (int count = 0; count < splitNum.size(); count += 2) { - ThreeNumbers three = new ThreeNumbers(splitNum.get(count),splitNum.get(count+1),splitNum.get(count+2)); - allNumbers.add(three); - count++; - } - return allNumbers; - } - /** - * 引数の整数値を一桁ごとに分けてリストとして返す - * - * @param inputNumber 入力された整数値 - */ - public static List divideOneEach(final int inputNumber){ - int originNumber = inputNumber; - List splitNum = new ArrayList<>(); - while (originNumber > 0) { - splitNum.add(originNumber % DIVISOR); - originNumber /= DIVISOR; - } - Collections.reverse(splitNum); - - return splitNum; - } -} - -/** - * 入力値において並んだ三つの桁の値を保存するクラス - * - */ -class ThreeNumbers { - private int leftNumber; - private int centerNumber; - private int rightNumber; - - public ThreeNumbers(int leftNumber, int centerNumber, int rightNumber) { - this.leftNumber = leftNumber; - this.centerNumber = centerNumber; - this.rightNumber = rightNumber; - } - - public int getLeftNumber() { - return leftNumber; - } - - public int getCenterNumber() { - return centerNumber; - } - - public int getRightNumber() { - return rightNumber; - } - - public void outputDecodeResult() { - for(StringBuilder decodeNum : decodeThreeNum()) { - System.out.println(decodeNum); - } - } - /** - * 三つの数を二次元バーコードに変換して返す. - * decodeNum()で一つ一つの数字をバーコードに変換した後、 - * それらを横並びにつなげる - * - */ - public List decodeThreeNum() { - List leftDecode = decodeSingleNum(leftNumber); - List centerDecode = decodeSingleNum(centerNumber); - List rightDecode = decodeSingleNum(rightNumber); - - List decodedThreeNum = new ArrayList<>(); - //左、中央、右の数のバーコードを表す文字列で - //同じcount行目の文字列をつなげる - for (int count = 0; count < 3; count++) { - StringBuilder row = new StringBuilder(); - row.append(leftDecode.get(count)); - row.append(centerDecode.get(count)); - row.append(rightDecode.get(count)); - decodedThreeNum.add(row); - } - return decodedThreeNum; - } - /** - * 一つの数字を二次元バーコードに変換して返す. - * 二次元バーコードを三行の文字列として扱う. - */ - public List decodeSingleNum(final int number) { - int num = number; - //decodedNumに1行目、2行目、3行目の文字列をそれぞれ入れる - List decodedNum = new ArrayList<>(); - //上から1行目、2行目、3行目の順に文字列を作成 - for (int rowindex = 0; rowindex < 3; rowindex++) { - //rowに1行分の文字列を入れる - StringBuilder row = new StringBuilder(); - //1行文の文字列を作成 - for (int columnIndex = 0; columnIndex < 3; columnIndex++) { - if (0 < num) { - row.append("#"); - } else { - row.append("."); - } - num--; - } - decodedNum.add(row); - } - return decodedNum; - } - -} -- GitLab From dd12a8a3991662353ff2f62694578181699a5ecd Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Mon, 28 Jul 2025 12:34:09 +0900 Subject: [PATCH 09/10] =?UTF-8?q?Paiza=E3=81=AE=E5=95=8F=E9=A1=8C=E3=81=AE?= =?UTF-8?q?B153=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/Katori.java | 117 ++++++++++++++------------ kkanazawa/test/KatoriTest.java | 145 +++++++++++++++++++-------------- 2 files changed, 148 insertions(+), 114 deletions(-) diff --git a/kkanazawa/src/Katori.java b/kkanazawa/src/Katori.java index c3dd0f4..c5d2dc0 100644 --- a/kkanazawa/src/Katori.java +++ b/kkanazawa/src/Katori.java @@ -26,7 +26,7 @@ public class Katori { final List decodedKatori = CalcKatori.decodeKatori(sideLength, paddedCoil); // 渦巻き状に読み取ったリストから1/4, 2/4, 3/4, すべて燃えきる,までにかかる時間を計算 - final List fourTimes = CalcKatori.calcurateTime(decodedKatori); + final List fourTimes = CalcKatori.getQuartileSums(decodedKatori); // 計算結果の表示 KatoriIO.outputTimes(fourTimes); @@ -102,7 +102,7 @@ final class CalcKatori { public static List decodeKatori(final int sideLength, final int[][] coil) { // "-1"でかこっているため、始まりのインデックスは行、列ともに1から - IndexPointer pointer = new IndexPointer(1, 1, Direction.RIGHT); + var pointer = new IndexPointer(1, 1, Direction.RIGHT); // 現在移し終えた要素のカウンタ int brockCounter = 0; @@ -114,29 +114,85 @@ final class CalcKatori { brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; brockCounter++; - pointer.shifting(); + pointer = shifting(pointer); } // 初めて-1にぶつかってから解読し終わるまでの処理 while (brockCounter < sideLength * sideLength) { // -1の要素に移動した際、カーソルの位置を戻す処理 - pointer.modifying(); + pointer = modifying(pointer); // -1にぶつかるまで一方向に要素を読み取り続ける while (coil[pointer.getRowIndex()][pointer.getColumnIndex()] != BARRIER_NUM) { brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; brockCounter++; // カーソルを現在進んでいる方向に移動させる - pointer.shifting(); + pointer = shifting(pointer); } } return brockTimes; } + + // -1の要素に移動した際、カーソルの位置を戻す処理 + // 時計周りに移動方向を変える + public static IndexPointer modifying(final IndexPointer pointer) { + + + final Direction direction = pointer.getDirection(); + final int rowIndex = pointer.getRowIndex(); + final int columnIndex = pointer.getColumnIndex(); + + switch (direction) { + case RIGHT: + return new IndexPointer(rowIndex + 1, columnIndex - 1, Direction.DOWN); + + case DOWN: + return new IndexPointer(rowIndex - 1, columnIndex - 1, Direction.LEFT); + + case LEFT: + return new IndexPointer(rowIndex - 1, columnIndex + 1, Direction.UP); + + case UP: + return new IndexPointer(rowIndex + 1, columnIndex + 1, Direction.RIGHT); + + default: + throw new NullPointerException("Unknown operation: " + + direction); + } + + } + + // カーソルを現在進む方向に移動させる + public static IndexPointer shifting(final IndexPointer pointer) { + + final Direction direction = pointer.getDirection(); + final int rowIndex = pointer.getRowIndex(); + final int columnIndex = pointer.getColumnIndex(); + + switch (direction) { + case RIGHT: + return new IndexPointer(rowIndex, columnIndex + 1, Direction.RIGHT); + + case DOWN: + return new IndexPointer(rowIndex + 1, columnIndex, Direction.DOWN); + + case LEFT: + return new IndexPointer(rowIndex, columnIndex - 1, Direction.LEFT); + + case UP: + return new IndexPointer(rowIndex - 1, columnIndex, Direction.UP); - public static List calcurateTime(List brockTimes) { - // 読み取った値を全体の1/4, 2/4, 3/4, 4/4、ごとに和をとり、リストに格納 + default: + throw new NullPointerException("Unknown operation: " + + direction); + + } + } + + // 読み取った値を全体の四分位ごとに和をとり、リストに格納 + public static List getQuartileSums(List brockTimes) { List fourTimes = new ArrayList<>(); fourTimes.add(brockTimes.subList(0, brockTimes.size() / 4).stream().reduce(Integer::sum).get()); fourTimes.add(brockTimes.subList(0, brockTimes.size() / 2).stream().reduce(Integer::sum).get()); @@ -187,50 +243,5 @@ final class IndexPointer { return columnIndex; } - // -1の要素に移動した際、カーソルの位置を戻す処理 - // 時計周りに移動方向を変える - public void modifying() { - - switch (direction) { - case RIGHT: - this.columnIndex--; - this.rowIndex++; - direction = Direction.DOWN; - break; - case DOWN: - this.columnIndex--; - this.rowIndex--; - direction = Direction.LEFT; - break; - case LEFT: - this.columnIndex++; - this.rowIndex--; - direction = Direction.UP; - break; - case UP: - this.columnIndex++; - this.rowIndex++; - direction = Direction.RIGHT; - break; - } - } - - // カーソルを現在進む方向に移動させる - public void shifting() { - - switch (direction) { - case RIGHT: - this.columnIndex++; - break; - case DOWN: - this.rowIndex++; - break; - case LEFT: - this.columnIndex--; - break; - case UP: - this.rowIndex--; - break; - } - } + } diff --git a/kkanazawa/test/KatoriTest.java b/kkanazawa/test/KatoriTest.java index 3e0c1cd..351dcea 100644 --- a/kkanazawa/test/KatoriTest.java +++ b/kkanazawa/test/KatoriTest.java @@ -7,10 +7,6 @@ import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; -import org.junit.experimental.theories.DataPoints; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; public class KatoriTest { @@ -109,6 +105,8 @@ public class KatoriTest { times.add(57); times.add(82); } + + @Test public void PaddingCoilに4行4列の二次元配列coilを渡すとその二次元配列の周りをBARRIER_NUMで囲った二次元配列を返す() { @@ -127,10 +125,90 @@ public class KatoriTest { } @Test - public void calcurateTimeに1次元リストdecodedKatoriを渡すと四分の一_四分の二_四分の三_四分の四ごとに和をとった値のリストを返す() { - List result = CalcKatori.calcurateTime(decodedKatori); + public void getQuartileSumsに1次元リストdecodedKatoriを渡すと四分位ごとに和をとった値のリストを返す() { + List result = CalcKatori.getQuartileSums(decodedKatori); assertEquals(times, result); } + + + //modifyingのテスト + @Test + public void modifyingにDOWN_行1_列5をコンストラクタ設定したIndexPointerを渡すとLEFT_行0_列4のオブジェクトを返す() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.DOWN); + IndexPointer expect = new IndexPointer(0, 4, Direction.LEFT); + IndexPointer actual = CalcKatori.modifying(pointer); + assertEquals(expect.getRowIndex(), actual.getRowIndex()); + assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); + assertEquals(expect.getDirection(), actual.getDirection()); + } + @Test + public void modifyingにLEFT_行1_列5をコンストラクタ設定したIndexPointerを渡すとUP_行0_列6のオブジェクトを返す() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.LEFT); + IndexPointer expect = new IndexPointer(0, 6, Direction.UP); + IndexPointer actual = CalcKatori.modifying(pointer); + assertEquals(expect.getRowIndex(), actual.getRowIndex()); + assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); + assertEquals(expect.getDirection(), actual.getDirection()); + } + @Test + public void modifyingにUP_行1_列5をコンストラクタ設定したIndexPointerを渡すとRIGHT_行2_列6オブジェクトを返す() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.UP); + IndexPointer expect = new IndexPointer(2, 6, Direction.RIGHT); + IndexPointer actual = CalcKatori.modifying(pointer); + assertEquals(expect.getRowIndex(), actual.getRowIndex()); + assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); + assertEquals(expect.getDirection(), actual.getDirection()); + } + @Test(expected = NullPointerException.class) + public void modifyingにnull_行1_列5をコンストラクタ設定したIndexPointerを渡すとNullPointerExceptionを返す() { + IndexPointer pointer = new IndexPointer(1, 5,null); + CalcKatori.modifying(pointer); + } + + + //shiftingのテスト + @Test + public void shiftigにRIGHT_行1_列5をコンストラクタ設定したIndexPointerを渡すとRIGHT_行1_列6オブジェクトを返す() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.RIGHT); + IndexPointer expect = new IndexPointer(1, 6, Direction.RIGHT); + IndexPointer actual = CalcKatori.shifting(pointer); + assertEquals(expect.getRowIndex(), actual.getRowIndex()); + assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); + assertEquals(expect.getDirection(), actual.getDirection()); + } + @Test + public void shiftigにDOWN_行1_列5をコンストラクタ設定したIndexPointerを渡すとDOWN_行2_列5オブジェクトを返す() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.DOWN); + IndexPointer expect = new IndexPointer(2, 5, Direction.DOWN); + IndexPointer actual = CalcKatori.shifting(pointer); + assertEquals(expect.getRowIndex(), actual.getRowIndex()); + assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); + assertEquals(expect.getDirection(), actual.getDirection()); + } + @Test + public void shiftigにLEFT_行1_列5をコンストラクタ設定したIndexPointerを渡すとLEFT_行1_列4オブジェクトを返す() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.LEFT); + IndexPointer expect = new IndexPointer(1, 4, Direction.LEFT); + IndexPointer actual = CalcKatori.shifting(pointer); + assertEquals(expect.getRowIndex(), actual.getRowIndex()); + assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); + assertEquals(expect.getDirection(), actual.getDirection()); + } + @Test + public void shiftigにUP_行1_列5をコンストラクタ設定したIndexPointerを渡すとUP_行0_列5オブジェクトを返す() { + IndexPointer pointer = new IndexPointer(1, 5, Direction.UP); + IndexPointer expect = new IndexPointer(0, 5, Direction.UP); + IndexPointer actual = CalcKatori.shifting(pointer); + assertEquals(expect.getRowIndex(), actual.getRowIndex()); + assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); + assertEquals(expect.getDirection(), actual.getDirection()); + } + @Test(expected = NullPointerException.class) + public void shiftigにnull_行1_列5をコンストラクタ設定したIndexPointerを渡すとNullPointerExceptionを返す() { + IndexPointer pointer = new IndexPointer(1, 5, null); + CalcKatori.shifting(pointer); + } + } public static class IndexPointerTest { @@ -144,61 +222,6 @@ public class KatoriTest { } - @Test - public void modifyingでRIGHTから時計回りに1周してRIGHTに戻ってくる() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.RIGHT); - assertEquals(Direction.RIGHT, pointer.getDirection()); - pointer.modifying(); - assertEquals(Direction.DOWN, pointer.getDirection()); - pointer.modifying(); - assertEquals(Direction.LEFT, pointer.getDirection()); - pointer.modifying(); - assertEquals(Direction.UP, pointer.getDirection()); - pointer.modifying(); - assertEquals(Direction.RIGHT, pointer.getDirection()); - - } - - @RunWith(Theories.class) - public static class shiftingのテスト { - @DataPoints - public static Fixture[] PARAMS = { - new Fixture(1, 5, Direction.RIGHT, 1, 6), - new Fixture(1, 5, Direction.DOWN, 2, 5), - new Fixture(1, 5, Direction.LEFT, 1, 4), - new Fixture(1, 5, Direction.UP, 0, 5), - }; - @Theory - //RIGHT:行インデックス+0 列インデックス+1 - //DOWN:行インデックス+1 列インデックス+0 - //LEFT:行インデックス+0 列インデックスー1 - //UP:行インデックスー1 列インデックス+0 - public void RIGHT_DOWN_LEFT_UPに対してそれぞれインデックスが適切に変動(Fixture params) throws Exception { - IndexPointer pointer = new IndexPointer(params.indexRow, params.indexColumn, params.direction); - pointer.shifting(); - assertEquals(params.expectedRow, pointer.getRowIndex()); - assertEquals(params.expectedColumn, pointer.getColumnIndex()); - } - - //パラメータ化用のクラス - static class Fixture { - int indexRow; - int indexColumn; - Direction direction; - int expectedRow; - int expectedColumn; - - Fixture(int indexRow, int indexColumn, Direction direction, int expectedRow, int expectedColumn) { - this.indexRow = indexRow; - this.indexColumn = indexColumn; - this.direction = direction; - this.expectedRow = expectedRow; - this.expectedColumn = expectedColumn; - } - } - - } - } } -- GitLab From c2dc1051c58c64fe420a06acd23444a8f0105ec6 Mon Sep 17 00:00:00 2001 From: kkanazawa Date: Mon, 28 Jul 2025 15:48:59 +0900 Subject: [PATCH 10/10] =?UTF-8?q?Paiza=E3=81=AE=E5=95=8F=E9=A1=8C=E3=81=AE?= =?UTF-8?q?B153=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kkanazawa/src/Katori.java | 142 ++++++++++++++++++--------------- kkanazawa/test/KatoriTest.java | 53 +++++------- 2 files changed, 99 insertions(+), 96 deletions(-) diff --git a/kkanazawa/src/Katori.java b/kkanazawa/src/Katori.java index c5d2dc0..1f79e59 100644 --- a/kkanazawa/src/Katori.java +++ b/kkanazawa/src/Katori.java @@ -114,82 +114,27 @@ final class CalcKatori { brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; brockCounter++; - pointer = shifting(pointer); + pointer.getDirection().shift(pointer); } // 初めて-1にぶつかってから解読し終わるまでの処理 while (brockCounter < sideLength * sideLength) { // -1の要素に移動した際、カーソルの位置を戻す処理 - pointer = modifying(pointer); + pointer.getDirection().modify(pointer); // -1にぶつかるまで一方向に要素を読み取り続ける while (coil[pointer.getRowIndex()][pointer.getColumnIndex()] != BARRIER_NUM) { brockTimes.add(coil[pointer.getRowIndex()][pointer.getColumnIndex()]); coil[pointer.getRowIndex()][pointer.getColumnIndex()] = BARRIER_NUM; brockCounter++; // カーソルを現在進んでいる方向に移動させる - pointer = shifting(pointer); + pointer.getDirection().shift(pointer); } } return brockTimes; } - - // -1の要素に移動した際、カーソルの位置を戻す処理 - // 時計周りに移動方向を変える - public static IndexPointer modifying(final IndexPointer pointer) { - - - final Direction direction = pointer.getDirection(); - final int rowIndex = pointer.getRowIndex(); - final int columnIndex = pointer.getColumnIndex(); - - switch (direction) { - case RIGHT: - return new IndexPointer(rowIndex + 1, columnIndex - 1, Direction.DOWN); - - case DOWN: - return new IndexPointer(rowIndex - 1, columnIndex - 1, Direction.LEFT); - - case LEFT: - return new IndexPointer(rowIndex - 1, columnIndex + 1, Direction.UP); - - case UP: - return new IndexPointer(rowIndex + 1, columnIndex + 1, Direction.RIGHT); - - default: - throw new NullPointerException("Unknown operation: " - + direction); - } - - } - - // カーソルを現在進む方向に移動させる - public static IndexPointer shifting(final IndexPointer pointer) { - - final Direction direction = pointer.getDirection(); - final int rowIndex = pointer.getRowIndex(); - final int columnIndex = pointer.getColumnIndex(); - - switch (direction) { - case RIGHT: - return new IndexPointer(rowIndex, columnIndex + 1, Direction.RIGHT); - - case DOWN: - return new IndexPointer(rowIndex + 1, columnIndex, Direction.DOWN); - - case LEFT: - return new IndexPointer(rowIndex, columnIndex - 1, Direction.LEFT); - - case UP: - return new IndexPointer(rowIndex - 1, columnIndex, Direction.UP); - default: - throw new NullPointerException("Unknown operation: " - + direction); - - } - } // 読み取った値を全体の四分位ごとに和をとり、リストに格納 public static List getQuartileSums(List brockTimes) { @@ -208,10 +153,68 @@ final class CalcKatori { * 要素を読み取る方向 */ enum Direction { - RIGHT, - DOWN, - LEFT, - UP, + // modify + // -1の要素に移動した際、カーソルの位置を戻す処理 + // 時計周りに移動方向を変える + + //shift + // カーソルを現在進む方向に移動させる + RIGHT { + @Override + void shift(IndexPointer pointer) { + pointer.setColumnIndex(pointer.getColumnIndex() + 1); + } + + @Override + void modify(IndexPointer pointer) { + pointer.setRowIndex(pointer.getRowIndex() + 1); + pointer.setColumnIndex(pointer.getColumnIndex() - 1); + pointer.setDirection(Direction.DOWN); + } + }, + DOWN { + @Override + void shift(IndexPointer pointer) { + pointer.setRowIndex(pointer.getRowIndex() + 1); + } + + @Override + void modify(IndexPointer pointer) { + pointer.setRowIndex(pointer.getRowIndex() - 1); + pointer.setColumnIndex(pointer.getColumnIndex() - 1); + pointer.setDirection(Direction.LEFT); + } + }, + LEFT { + @Override + void shift(IndexPointer pointer) { + pointer.setColumnIndex(pointer.getColumnIndex() - 1); + } + + @Override + void modify(IndexPointer pointer) { + pointer.setRowIndex(pointer.getRowIndex() - 1); + pointer.setColumnIndex(pointer.getColumnIndex() + 1); + pointer.setDirection(Direction.UP); + } + }, + UP { + @Override + void shift(IndexPointer pointer) { + pointer.setRowIndex(pointer.getRowIndex() - 1); + } + + @Override + void modify(IndexPointer pointer) { + pointer.setRowIndex(pointer.getRowIndex() + 1); + pointer.setColumnIndex(pointer.getColumnIndex() + 1); + pointer.setDirection(Direction.RIGHT); + } + }; + + abstract void shift(IndexPointer pointer); + + abstract void modify(IndexPointer pointer); } @@ -225,7 +228,7 @@ final class IndexPointer { private int columnIndex;// 現在の列の位置 private Direction direction; - public IndexPointer(int rowIndex, int columnIndex, Direction direction) { + public IndexPointer(final int rowIndex, final int columnIndex, final Direction direction) { this.rowIndex = rowIndex; this.columnIndex = columnIndex; this.direction = direction; @@ -243,5 +246,16 @@ final class IndexPointer { return columnIndex; } - + public void setRowIndex(final int rowIndex) { + this.rowIndex = rowIndex; + } + + public void setColumnIndex(final int columnIndex) { + this.columnIndex = columnIndex; + } + + public void setDirection(final Direction direction) { + this.direction = direction; + } + } diff --git a/kkanazawa/test/KatoriTest.java b/kkanazawa/test/KatoriTest.java index 351dcea..48ae7c6 100644 --- a/kkanazawa/test/KatoriTest.java +++ b/kkanazawa/test/KatoriTest.java @@ -133,82 +133,71 @@ public class KatoriTest { //modifyingのテスト @Test - public void modifyingにDOWN_行1_列5をコンストラクタ設定したIndexPointerを渡すとLEFT_行0_列4のオブジェクトを返す() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.DOWN); + public void modifyにDOWN_行1_列5をコンストラクタ設定したIndexPointerを渡すとLEFT_行0_列4に変更する() { + IndexPointer actual = new IndexPointer(1, 5, Direction.DOWN); IndexPointer expect = new IndexPointer(0, 4, Direction.LEFT); - IndexPointer actual = CalcKatori.modifying(pointer); + actual.getDirection().modify(actual); assertEquals(expect.getRowIndex(), actual.getRowIndex()); assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); assertEquals(expect.getDirection(), actual.getDirection()); } @Test - public void modifyingにLEFT_行1_列5をコンストラクタ設定したIndexPointerを渡すとUP_行0_列6のオブジェクトを返す() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.LEFT); + public void modifyにLEFT_行1_列5をコンストラクタ設定したIndexPointerを渡すとUP_行0_列6に変更する() { + IndexPointer actual = new IndexPointer(1, 5, Direction.LEFT); IndexPointer expect = new IndexPointer(0, 6, Direction.UP); - IndexPointer actual = CalcKatori.modifying(pointer); + actual.getDirection().modify(actual); assertEquals(expect.getRowIndex(), actual.getRowIndex()); assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); assertEquals(expect.getDirection(), actual.getDirection()); } @Test - public void modifyingにUP_行1_列5をコンストラクタ設定したIndexPointerを渡すとRIGHT_行2_列6オブジェクトを返す() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.UP); + public void modifyにUP_行1_列5をコンストラクタ設定したIndexPointerを渡すとRIGHT_行2_列6に変更する() { + IndexPointer actual = new IndexPointer(1, 5, Direction.UP); IndexPointer expect = new IndexPointer(2, 6, Direction.RIGHT); - IndexPointer actual = CalcKatori.modifying(pointer); + actual.getDirection().modify(actual); assertEquals(expect.getRowIndex(), actual.getRowIndex()); assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); assertEquals(expect.getDirection(), actual.getDirection()); } - @Test(expected = NullPointerException.class) - public void modifyingにnull_行1_列5をコンストラクタ設定したIndexPointerを渡すとNullPointerExceptionを返す() { - IndexPointer pointer = new IndexPointer(1, 5,null); - CalcKatori.modifying(pointer); - } //shiftingのテスト @Test - public void shiftigにRIGHT_行1_列5をコンストラクタ設定したIndexPointerを渡すとRIGHT_行1_列6オブジェクトを返す() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.RIGHT); + public void shiftにRIGHT_行1_列5をコンストラクタ設定したIndexPointerを渡すとRIGHT_行1_列6に変更する() { + IndexPointer actual = new IndexPointer(1, 5, Direction.RIGHT); IndexPointer expect = new IndexPointer(1, 6, Direction.RIGHT); - IndexPointer actual = CalcKatori.shifting(pointer); + actual.getDirection().shift(actual); assertEquals(expect.getRowIndex(), actual.getRowIndex()); assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); assertEquals(expect.getDirection(), actual.getDirection()); } @Test - public void shiftigにDOWN_行1_列5をコンストラクタ設定したIndexPointerを渡すとDOWN_行2_列5オブジェクトを返す() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.DOWN); + public void shiftigにDOWN_行1_列5をコンストラクタ設定したIndexPointerを渡すとDOWN_行2_列5に変更する() { + IndexPointer actual = new IndexPointer(1, 5, Direction.DOWN); IndexPointer expect = new IndexPointer(2, 5, Direction.DOWN); - IndexPointer actual = CalcKatori.shifting(pointer); + actual.getDirection().shift(actual); assertEquals(expect.getRowIndex(), actual.getRowIndex()); assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); assertEquals(expect.getDirection(), actual.getDirection()); } @Test - public void shiftigにLEFT_行1_列5をコンストラクタ設定したIndexPointerを渡すとLEFT_行1_列4オブジェクトを返す() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.LEFT); + public void shiftigにLEFT_行1_列5をコンストラクタ設定したIndexPointerを渡すとLEFT_行1_列4に変更する() { + IndexPointer actual = new IndexPointer(1, 5, Direction.LEFT); IndexPointer expect = new IndexPointer(1, 4, Direction.LEFT); - IndexPointer actual = CalcKatori.shifting(pointer); + actual.getDirection().shift(actual); assertEquals(expect.getRowIndex(), actual.getRowIndex()); assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); assertEquals(expect.getDirection(), actual.getDirection()); } @Test - public void shiftigにUP_行1_列5をコンストラクタ設定したIndexPointerを渡すとUP_行0_列5オブジェクトを返す() { - IndexPointer pointer = new IndexPointer(1, 5, Direction.UP); + public void shiftigにUP_行1_列5をコンストラクタ設定したIndexPointerを渡すとUP_行0_列5に変更する() { + IndexPointer actual = new IndexPointer(1, 5, Direction.UP); IndexPointer expect = new IndexPointer(0, 5, Direction.UP); - IndexPointer actual = CalcKatori.shifting(pointer); + actual.getDirection().shift(actual); assertEquals(expect.getRowIndex(), actual.getRowIndex()); assertEquals(expect.getColumnIndex(), actual.getColumnIndex()); assertEquals(expect.getDirection(), actual.getDirection()); } - @Test(expected = NullPointerException.class) - public void shiftigにnull_行1_列5をコンストラクタ設定したIndexPointerを渡すとNullPointerExceptionを返す() { - IndexPointer pointer = new IndexPointer(1, 5, null); - CalcKatori.shifting(pointer); - } - } public static class IndexPointerTest { -- GitLab