Skip to content

Commit 339e2d4

Browse files
committed
Advent of Code 2021 - Day 4
1 parent 83b1322 commit 339e2d4

File tree

7 files changed

+864
-13
lines changed

7 files changed

+864
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ Last year I ran a [cruise log for my 25 days competing in the Advent of Code](ht
2121
- [The Advent of Code 2021 Day 1 log, starting slow](https://www.codingnagger.com/2021/12/01/the-advent-of-code-2021-day-1-log-starting-slow/)
2222
- [The Advent of Code 2021 Day 2 log, sneaky release](https://www.codingnagger.com/2021/12/02/the-advent-of-code-2021-day-2-log-sneaky-release/)
2323
- [The Advent of Code 2021 Day 3 log, sleepy brain](https://www.codingnagger.com/2021/12/03/the-advent-of-code-2021-day-3-log-sleepy-brain/)
24+
[The Advent of Code 2021 Day 4 log, playing myself](https://www.codingnagger.com/2021/12/04/the-advent-of-code-2021-day-4-log-playing-myself/)

pom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
<properties>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17-
<maven.compiler.source>1.8</maven.compiler.source>
18-
<maven.compiler.target>1.8</maven.compiler.target>
17+
<maven.compiler.source>11</maven.compiler.source>
18+
<maven.compiler.target>11</maven.compiler.target>
1919
</properties>
2020

2121
<dependencies>
@@ -85,5 +85,15 @@
8585
</plugin>
8686
</plugins>
8787
</pluginManagement>
88+
<plugins>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-compiler-plugin</artifactId>
92+
<configuration>
93+
<source>11</source>
94+
<target>11</target>
95+
</configuration>
96+
</plugin>
97+
</plugins>
8898
</build>
8999
</project>

src/main/java/com/codingnagger/App.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.codingnagger;
22

3-
import com.codingnagger.days.Day;
4-
import com.codingnagger.days.Day1;
5-
import com.codingnagger.days.Day2;
6-
import com.codingnagger.days.Day3;
3+
import com.codingnagger.days.*;
74
import com.codingnagger.utils.InputLoader;
85

96
import java.io.IOException;
@@ -16,9 +13,9 @@ public class App {
1613
public static void main(String[] args) throws IOException {
1714
System.out.println("Advent of Code 2021");
1815

19-
List<String> input = InputLoader.Load("day3.txt");
16+
List<String> input = InputLoader.Load("day4.txt");
2017

21-
Day day = new Day3();
18+
Day day = new Day4();
2219

2320
System.out.println("Part 1:");
2421
System.out.println(day.partOne(input));
Lines changed: 226 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,238 @@
11
package com.codingnagger.days;
22

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;
47

58
public class Day4 implements Day {
69
@Override
710
public String partOne(List<String> input) {
8-
return null;
11+
BingoGame game = parseGame(input);
12+
game.play();
13+
return "" + game.getWinner().getScore();
914
}
1015

1116
@Override
1217
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]+");
14237
}
15238
}

0 commit comments

Comments
 (0)