|
1 | 1 | package com.codingnagger.days;
|
2 | 2 |
|
3 |
| -import java.util.List; |
| 3 | +import java.math.BigDecimal; |
| 4 | +import java.util.*; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import java.util.stream.Stream; |
4 | 7 |
|
5 | 8 | public class Day4 implements Day {
|
6 | 9 | @Override
|
7 | 10 | public String partOne(List<String> input) {
|
8 |
| - return null; |
| 11 | + BingoGame game = parseGame(input); |
| 12 | + game.play(); |
| 13 | + return "" + game.getWinner().getScore(); |
9 | 14 | }
|
10 | 15 |
|
11 | 16 | @Override
|
12 | 17 | public String partTwo(List<String> input) {
|
13 |
| - return null; |
| 18 | + FudgedBingoGame game = parseFudgedGame(input); |
| 19 | + game.play(); |
| 20 | + return "" + game.getLastWinner().getScore(); |
| 21 | + } |
| 22 | + |
| 23 | + FudgedBingoGame parseFudgedGame(List<String> input) { |
| 24 | + List<BigDecimal> draw = getBigDecimalStream(input.get(0), ",").collect(Collectors.toList()); |
| 25 | + FudgedBingoGame game = new FudgedBingoGame(draw); |
| 26 | + return (FudgedBingoGame) parseGame(draw, game, input); |
| 27 | + } |
| 28 | + |
| 29 | + BingoGame parseGame(List<String> input) { |
| 30 | + List<BigDecimal> draw = getBigDecimalStream(input.get(0), ",").collect(Collectors.toList()); |
| 31 | + BingoGame game = new BingoGame(draw); |
| 32 | + return parseGame(draw, game, input); |
| 33 | + } |
| 34 | + |
| 35 | + BingoGame parseGame(List<BigDecimal> draw, BingoGame game, List<String> input) { |
| 36 | + BingoBoard board = new BingoBoard(); |
| 37 | + |
| 38 | + for (int i = 2; i < input.size(); i++) { |
| 39 | + String line = input.get(i); |
| 40 | + |
| 41 | + if (line.isBlank()) { |
| 42 | + game.addBoard(board); |
| 43 | + board = new BingoBoard(); |
| 44 | + } else { |
| 45 | + board.addLine(line); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + game.addBoard(board); |
| 50 | + |
| 51 | + return game; |
| 52 | + } |
| 53 | + |
| 54 | + public class FudgedBingoGame extends BingoGame { |
| 55 | + private BingoBoard lastWinner; |
| 56 | + private List<BingoBoard> winners; |
| 57 | + |
| 58 | + public FudgedBingoGame(List<BigDecimal> draw) { |
| 59 | + super(draw); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void playRound() { |
| 64 | + winners = new ArrayList<>(); |
| 65 | + |
| 66 | + super.playRound(); |
| 67 | + |
| 68 | + if (isOver()) { |
| 69 | + if (boards.size() > 1) { |
| 70 | + if (winners.size() > 0) { |
| 71 | + System.out.println("Last number" + lastWinner.lastNumber); |
| 72 | + System.out.println("===== Remove "+winners.size()+" winners (remaining: "+boards.size()+" ====="); |
| 73 | + boards.removeAll(winners); |
| 74 | + System.out.println("========================="); |
| 75 | + super.setWinner(null); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + void setWinner(BingoBoard b) { |
| 83 | + winners.add(b); |
| 84 | + lastWinner = b; |
| 85 | + super.setWinner(b); |
| 86 | + } |
| 87 | + |
| 88 | + public BingoBoard getLastWinner() { |
| 89 | + return lastWinner; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public class BingoGame { |
| 94 | + private int cursor; |
| 95 | + private final List<BigDecimal> draw; |
| 96 | + protected final List<BingoBoard> boards; |
| 97 | + private BingoBoard winner; |
| 98 | + |
| 99 | + public BingoGame(List<BigDecimal> draw) { |
| 100 | + this.cursor = 0; |
| 101 | + this.draw = draw; |
| 102 | + this.boards = new ArrayList<>(); |
| 103 | + } |
| 104 | + |
| 105 | + public void addBoard(BingoBoard board) { |
| 106 | + boards.add(board); |
| 107 | + } |
| 108 | + |
| 109 | + public boolean isOver() { |
| 110 | + return winner != null || cursor >= draw.size() ; |
| 111 | + } |
| 112 | + |
| 113 | + public void play() { |
| 114 | + while (!isOver()) { |
| 115 | + playRound(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + protected void playRound() { |
| 120 | + System.out.println(); |
| 121 | + System.out.println(); |
| 122 | + BigDecimal drawn = draw.get(cursor); |
| 123 | + System.out.println("======= Play Round - Drawn "+drawn+ " ======="); |
| 124 | + |
| 125 | + for (BingoBoard b : boards) { |
| 126 | + b.print(); |
| 127 | + b.numberFound(drawn); |
| 128 | + if (b.isWinner()) { |
| 129 | + setWinner(b); |
| 130 | + } |
| 131 | + System.out.println(); |
| 132 | + } |
| 133 | + |
| 134 | + System.out.println(); |
| 135 | + cursor++; |
| 136 | + } |
| 137 | + |
| 138 | + void setWinner(BingoBoard b) { |
| 139 | + winner = b; |
| 140 | + } |
| 141 | + |
| 142 | + public BingoBoard getWinner() { |
| 143 | + return winner; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private class BingoBoard { |
| 148 | + private final int BOARD_SIDE_LENGTH = 5; |
| 149 | + private final BigDecimal[][] numbers = new BigDecimal[BOARD_SIDE_LENGTH][]; |
| 150 | + private final boolean[][] found = new boolean[BOARD_SIDE_LENGTH][]; |
| 151 | + private int creationCursor = 0; |
| 152 | + private BigDecimal lastNumber; |
| 153 | + |
| 154 | + public void addLine(String line) { |
| 155 | + BigDecimal[] lineNumbers = getBigDecimalStream(line).toArray(BigDecimal[]::new); |
| 156 | + numbers[creationCursor] = lineNumbers; |
| 157 | + found[creationCursor] = new boolean[lineNumbers.length]; |
| 158 | + creationCursor++; |
| 159 | + } |
| 160 | + |
| 161 | + boolean isWinner() { |
| 162 | + for (int i = 0; i < BOARD_SIDE_LENGTH; i++) { |
| 163 | + boolean winner = true; |
| 164 | + |
| 165 | + for (int j = 0; j < BOARD_SIDE_LENGTH; j++) { |
| 166 | + winner &= found[i][j]; |
| 167 | + } |
| 168 | + |
| 169 | + if (winner) { |
| 170 | + return true; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + for (int i = 0; i < BOARD_SIDE_LENGTH; i++) { |
| 175 | + boolean winner = true; |
| 176 | + |
| 177 | + for (int j = 0; j < BOARD_SIDE_LENGTH; j++) { |
| 178 | + winner &= found[j][i]; |
| 179 | + } |
| 180 | + |
| 181 | + if (winner) { |
| 182 | + return true; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + public void numberFound(BigDecimal number) { |
| 190 | + outerloop:for (int i = 0; i < BOARD_SIDE_LENGTH; i++) { |
| 191 | + for (int j = 0; j < BOARD_SIDE_LENGTH; j++) { |
| 192 | + if (Objects.equals(numbers[i][j], number)) { |
| 193 | + System.out.println("Found "+number); |
| 194 | + found[i][j] = true; |
| 195 | + lastNumber = number; |
| 196 | + print(); |
| 197 | + break outerloop; |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + public BigDecimal getScore() { |
| 204 | + BigDecimal score = BigDecimal.ZERO; |
| 205 | + |
| 206 | + for (int i = 0; i < BOARD_SIDE_LENGTH; i++) { |
| 207 | + for (int j = 0; j < BOARD_SIDE_LENGTH; j++) { |
| 208 | + if (!found[i][j]) { |
| 209 | + score = score.add(numbers[i][j]); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + System.out.println("Score "+score+ " - last: "+lastNumber+ " - board:"); |
| 215 | + print(); |
| 216 | + return score.multiply(lastNumber); |
| 217 | + } |
| 218 | + |
| 219 | + public void print() { |
| 220 | + System.out.println("=== Board ==="); |
| 221 | + for (int i = 0; i < BOARD_SIDE_LENGTH; i++) { |
| 222 | + for (int j = 0; j < BOARD_SIDE_LENGTH; j++) { |
| 223 | + System.out.print(numbers[i][j]+"|"+found[i][j]+" \t"); |
| 224 | + } |
| 225 | + System.out.println(); |
| 226 | + } |
| 227 | + System.out.println(); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + private static Stream<BigDecimal> getBigDecimalStream(String s, String separator) { |
| 232 | + return Arrays.stream(s.split(separator)).map(String::trim).map(BigDecimal::new); |
| 233 | + } |
| 234 | + |
| 235 | + private static Stream<BigDecimal> getBigDecimalStream(String s) { |
| 236 | + return getBigDecimalStream(s.trim(), "[^\\d]+"); |
14 | 237 | }
|
15 | 238 | }
|
0 commit comments