Skip to content

Commit

Permalink
Day 24: The celebration night.
Browse files Browse the repository at this point in the history
  • Loading branch information
ythirion committed Dec 24, 2023
1 parent 9bee113 commit 708e6dd
Show file tree
Hide file tree
Showing 12 changed files with 1,144 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Here are the different challenges :
- [Day 21: Refactor the tests and production code to Output-Based tests.](exercise/day21/docs/challenge.md)
- [Day 22: Design a diamond program using T.D.D and Property-Based Testing.](exercise/day22/docs/challenge.md)
- [Day 23: Refactor the code after putting it under test.](exercise/day23/docs/challenge.md)
- [Day 24: Write the most complicated code you can.](exercise/day24/docs/challenge.md)

### Solutions
A solution proposal will be published here every day during the `Advent Of Craft` containing `the code` and a `step by step` guide.
Expand Down
Binary file added exercise/day24/docs/cdd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions exercise/day24/docs/challenge.md
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)
Binary file added exercise/day24/docs/snippet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions exercise/day24/pom.xml
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>
8 changes: 8 additions & 0 deletions exercise/day24/src/main/java/M/Instruction.java
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]));
}
}
11 changes: 11 additions & 0 deletions exercise/day24/src/main/java/M/Position.java
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);
}
}
33 changes: 33 additions & 0 deletions exercise/day24/src/main/java/M/Submarine.java
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;
}
}
19 changes: 19 additions & 0 deletions exercise/day24/src/test/java/M/FileUtils.java
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());
}
}
33 changes: 33 additions & 0 deletions exercise/day24/src/test/java/M/SubmarineTests.java
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();
}
}
Loading

0 comments on commit 708e6dd

Please sign in to comment.