-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding solution for Day 8 * Upgrading table * Upgrading cursor rules
- Loading branch information
Showing
9 changed files
with
724 additions
and
8 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
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
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 info.jab.aoc.day8; | ||
|
||
import info.jab.aoc.Day; | ||
|
||
/** | ||
* https://adventofcode.com/2015/day/8 | ||
*/ | ||
public class Day8 implements Day<Integer> { | ||
|
||
@Override | ||
public Integer getPart1Result(String fileName) { | ||
return new StringLiteralCalculator().solvePartOne(fileName); | ||
} | ||
|
||
@Override | ||
public Integer getPart2Result(String fileName) { | ||
return new StringLiteralCalculator().solvePartTwo(fileName); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
2015/src/main/java/info/jab/aoc/day8/StringLiteralCalculator.java
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,119 @@ | ||
package info.jab.aoc.day8; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import com.putoet.resources.ResourceLines; | ||
|
||
import info.jab.aoc.Solver; | ||
|
||
class StringLiteralCalculator implements Solver<Integer> { | ||
|
||
private int calculateCodeLength(String input) { | ||
return input.length(); | ||
} | ||
|
||
private int calculateMemoryLength(String input) { | ||
// Remove quotes from start and end | ||
String content = input.substring(1, input.length() - 1); | ||
|
||
int length = 0; | ||
for (int i = 0; i < content.length(); i++) { | ||
if (content.charAt(i) == '\\') { | ||
if (i + 1 < content.length()) { | ||
char nextChar = content.charAt(i + 1); | ||
if (nextChar == '\\' || nextChar == '"') { | ||
// For \\ or \" | ||
length++; | ||
i++; | ||
} else if (nextChar == 'x' && i + 3 < content.length()) { | ||
// For \x followed by two hexadecimal characters | ||
length++; | ||
i += 3; | ||
} | ||
} | ||
} else { | ||
length++; | ||
} | ||
} | ||
return length; | ||
} | ||
|
||
public int calculateTotalDifference(List<String> strings) { | ||
int totalCode = strings.stream() | ||
.mapToInt(this::calculateCodeLength) | ||
.sum(); | ||
|
||
int totalMemory = strings.stream() | ||
.mapToInt(this::calculateMemoryLength) | ||
.sum(); | ||
|
||
return totalCode - totalMemory; | ||
} | ||
|
||
// Part 2 | ||
|
||
//Imperative version | ||
private String encodeString(String input) { | ||
StringBuilder encoded = new StringBuilder(); | ||
encoded.append("\""); // Add opening quote | ||
|
||
for (char c : input.toCharArray()) { | ||
switch (c) { | ||
case '\"': | ||
encoded.append("\\\""); | ||
break; | ||
case '\\': | ||
encoded.append("\\\\"); | ||
break; | ||
default: | ||
encoded.append(c); | ||
} | ||
} | ||
encoded.append("\""); // Add closing quote | ||
return encoded.toString(); | ||
} | ||
|
||
private String encodeString2(String input) { | ||
final Map<Character, String> ENCODING_RULES = Map.of( | ||
'\"', "\\\"", | ||
'\\', "\\\\" | ||
); | ||
|
||
return "\"" + | ||
input.chars() | ||
.mapToObj(c -> (char) c) | ||
.map(c -> ENCODING_RULES.getOrDefault(c, String.valueOf(c))) | ||
.collect(Collectors.joining()) + | ||
"\""; | ||
} | ||
|
||
private int calculateEncodedLength(String input) { | ||
return encodeString2(input).length(); | ||
} | ||
|
||
public int calculateTotalDifference2(List<String> strings) { | ||
int encodedLength = strings.stream() | ||
.mapToInt(this::calculateEncodedLength) | ||
.sum(); | ||
|
||
int originalLength = strings.stream() | ||
.mapToInt(String::length) | ||
.sum(); | ||
|
||
return encodedLength - originalLength; | ||
} | ||
|
||
@Override | ||
public Integer solvePartOne(String fileName) { | ||
var lines = ResourceLines.list(fileName); | ||
return calculateTotalDifference(lines); | ||
} | ||
|
||
@Override | ||
public Integer solvePartTwo(String fileName) { | ||
var lines = ResourceLines.list(fileName); | ||
return calculateTotalDifference2(lines); | ||
} | ||
} |
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,69 @@ | ||
Feature: String Literal Space Calculator | ||
As Santa | ||
I need to calculate the difference between code representation and memory representation of strings | ||
So that I can determine how much space my digital list will take up | ||
|
||
Background: | ||
Given I have a file containing string literals | ||
And each string literal is on a separate line | ||
And the file uses standard escape sequences | ||
| Escape Sequence | Representation | | ||
| \\ | single backslash | | ||
| \" | double-quote character | | ||
| \x[0-9A-F]{2} | ASCII character (hex code) | | ||
|
||
Rule: String literals must be properly counted both in code and memory representation | ||
|
||
Scenario Outline: Calculate space difference for various string literals | ||
Given I have a string literal "<code_string>" | ||
When I count the characters in code representation | ||
And I count the characters in memory representation | ||
Then the code character count should be <code_count> | ||
And the memory character count should be <memory_count> | ||
|
||
Examples: | ||
| code_string | code_count | memory_count | description | | ||
| "" | 2 | 0 | empty string | | ||
| "abc" | 5 | 3 | simple string | | ||
| "aaa\"aaa" | 10 | 7 | string with escaped quote | | ||
| "\x27" | 6 | 1 | string with hex escape | | ||
|
||
Scenario: Calculate total difference for multiple strings | ||
Given I have the following string literals in my file: | ||
| String | | ||
| "" | | ||
| "abc" | | ||
| "aaa\"aaa"| | ||
| "\x27" | | ||
When I calculate the total characters of code | ||
And I calculate the total characters in memory | ||
Then the total code character count should be 23 | ||
And the total memory character count should be 11 | ||
And the difference should be 12 | ||
|
||
Rule: Special escape sequences must be properly processed | ||
|
||
Scenario Outline: Handle escape sequences correctly | ||
Given I have a string with escape sequence "<escape_sequence>" | ||
When I process the escape sequence | ||
Then it should be interpreted as "<interpretation>" | ||
|
||
Examples: | ||
| escape_sequence | interpretation | | ||
| \\ | single backslash | | ||
| \" | double-quote | | ||
| \x27 | apostrophe character | | ||
|
||
Rule: Whitespace should be ignored in calculations | ||
|
||
Scenario: Ignore whitespace in file | ||
Given I have a file with string literals separated by whitespace: | ||
""" | ||
"" | ||
"abc" | ||
"aaa\"aaa" | ||
"\x27" | ||
""" | ||
When I calculate the total difference | ||
Then the result should be the same as without whitespace | ||
And the difference should be 12 |
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,30 @@ | ||
Feature: String encoding calculator | ||
As a programmer | ||
I want to calculate the difference between encoded and original characters | ||
To solve day 8 puzzle part 2 | ||
|
||
Background: | ||
Given a list of literal text strings | ||
|
||
Scenario Outline: Calculate additional characters after encoding | ||
When I encode the string "<original_string>" | ||
Then the encoded string should be "<encoded_string>" | ||
And the character increase should be <increase> | ||
|
||
Examples: | ||
| original_string | encoded_string | increase | | ||
| "" | "\"\"" | 4 | | ||
| "abc" | "\"abc\"" | 4 | | ||
| "aaa\"aaa" | "\"aaa\\\"aaa\"" | 6 | | ||
| "\x27" | "\"\\x27\"" | 5 | | ||
|
||
Scenario: Calculate total difference | ||
Given the following strings: | ||
| "" | | ||
| "abc" | | ||
| "aaa\"aaa" | | ||
| "\x27" | | ||
When I encode all strings | ||
Then the total encoded length should be 42 | ||
And the original length should be 23 | ||
And the total difference should be 19 |
Oops, something went wrong.