-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## Day 24: The celebration night. | ||
|
||
Your last battle was a full victory, and you are ready to celebrate with | ||
your kingdom. A big celebration is being prepared. | ||
|
||
Today is about indulgence, excess and feast. | ||
|
||
On today's challenge, the last thing you want is to think about your craft and | ||
as such, the point of the exercise is to increase complexity of the code. | ||
|
||
Yes, you heard right. | ||
|
||
Using the [`Crappy-Driven Development`](https://github.com/ythirion/crappy-driven-development) technique, you have to make your | ||
code the crappiest possible. | ||
|
||
> "The secret art of making yourself indispensable by writing crappy code !!!" | ||
> **Challenge of day 24: Write the most complicated code you can.** | ||
- <u>💡HINT:</u> Your IDE can also help you write difficult code. | ||
|
||
![Crappy Driven Development](cdd.png) | ||
![Snippet](snippet.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.advent-of-craft</groupId> | ||
<artifactId>advent-of-craft2023</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>dive</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package M; | ||
|
||
public record Instruction(String text, int x) { | ||
public static Instruction fromText(String text) { | ||
var split = text.split(" "); | ||
return new Instruction(split[0], Integer.parseInt(split[1])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package M; | ||
|
||
public record Position(int horizontal, int depth) { | ||
public Position changeDepth(int newDepth) { | ||
return new Position(horizontal, newDepth); | ||
} | ||
|
||
public Position moveHorizontally(int newHorizontal) { | ||
return new Position(newHorizontal, depth); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package M; | ||
|
||
import java.util.List; | ||
|
||
public class Submarine { | ||
private final Position position; | ||
|
||
private Submarine(Position position) { | ||
this.position = position; | ||
} | ||
|
||
public Submarine(int horizontal, int depth) { | ||
this(new Position(horizontal, depth)); | ||
} | ||
|
||
public Submarine move(List<Instruction> instructions) { | ||
return instructions | ||
.stream() | ||
.reduce(this, Submarine::move, (p, n) -> p); | ||
} | ||
|
||
private Submarine move(Instruction instruction) { | ||
return new Submarine(switch (instruction.text()) { | ||
case "down" -> position.changeDepth(position.depth() + instruction.x()); | ||
case "up" -> position.changeDepth(position.depth() - instruction.x()); | ||
default -> position.moveHorizontally(position.horizontal() + instruction.x()); | ||
}); | ||
} | ||
|
||
public Position getPosition() { | ||
return position; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package M; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
import static java.lang.System.lineSeparator; | ||
|
||
public class FileUtils { | ||
public static String getInputAsString(String input) throws URISyntaxException, IOException { | ||
return Files.readString(Path.of(Objects.requireNonNull(FileUtils.class.getClassLoader().getResource(input)).toURI())); | ||
} | ||
|
||
public static String[] getInputAsSeparatedLines(String input) throws URISyntaxException, IOException { | ||
return getInputAsString(input).split(lineSeparator()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package M; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
// Original specifications available here : https://adventofcode.com/2021/day/2 | ||
class SubmarineTests { | ||
@Test | ||
void should_move_on_given_instructions() throws URISyntaxException, IOException { | ||
var instructions = loadInstructions(); | ||
var submarine = new Submarine(0, 0) | ||
.move(instructions); | ||
|
||
assertThat(calculateResult(submarine)) | ||
.isEqualTo(1_690_020); | ||
} | ||
|
||
private int calculateResult(Submarine submarine) { | ||
return submarine.getPosition().depth() * submarine.getPosition().horizontal(); | ||
} | ||
|
||
private List<Instruction> loadInstructions() throws URISyntaxException, IOException { | ||
return Arrays.stream(FileUtils.getInputAsSeparatedLines("submarine.txt")) | ||
.map(Instruction::fromText) | ||
.toList(); | ||
} | ||
} |
Oops, something went wrong.