Skip to content

Commit

Permalink
some more aoc 2017
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Nov 21, 2023
1 parent cf7dd8d commit 39f1775
Show file tree
Hide file tree
Showing 29 changed files with 2,397 additions and 1 deletion.
18 changes: 17 additions & 1 deletion adventofcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,57 +406,73 @@
[20162402tests]: src/test/java/org/ck/adventofcode/year2016/day24/Part2Test.java
[20162501tests]: src/test/java/org/ck/adventofcode/year2016/day25/Part1Test.java

# 2017 (11/11)
# 2017 (15/15)

| # | Name | Solution | Test |
|---------:|---------------------------------------------------------------------|:------------------------------------:|:---------------------------------:|
| 20170101 | [Day 1: Inverse Captcha][20170101] | ✅[💾][20170101solution] | ✅[💾][20170101tests] |
| 20170102 | [Day 1: Inverse Captcha - Part 2][20170102] | ✅[💾][20170102solution] | ✅[💾][20170102tests] |
| 20170201 | [Day 2: Corruption Checksum][20170201] | ✅[💾][20170201solution] | ✅[💾][20170201tests] |
| 20170202 | [Day 2: Corruption Checksum - Part 2][20170202] | ✅[💾][20170202solution] | ✅[💾][20170202tests] |
| 20170301 | [Day 3: Spiral Memory][20170301] | ✅[💾][20170301solution] | ✅[💾][20170301tests] |
| 20170302 | [Day 3: Spiral Memory - Part 2][20170302] | ✅[💾][20170302solution] | ✅[💾][20170302tests] |
| 20170401 | [Day 4: High-Entropy Passphrases][20170401] | ✅[💾][20170401solution] | ✅[💾][20170401tests] |
| 20170402 | [Day 4: High-Entropy Passphrases - Part 2][20170402] | ✅[💾][20170402solution] | ✅[💾][20170402tests] |
| 20170501 | [Day 5: A Maze of Twisty Trampolines, All Alike][20170501] | ✅[💾][20170501solution] | ✅[💾][20170501tests] |
| 20170502 | [Day 5: A Maze of Twisty Trampolines, All Alike - Part 2][20170502] | ✅[💾][20170502solution] | ✅[💾][20170502tests] |
| 20170601 | [Day 6: Memory Reallocation][20170601] | ✅[💾][20170601solution] | ✅[💾][20170601tests] |
| 20170602 | [Day 6: Memory Reallocation - Part 2][20170602] | ✅[💾][20170602solution] | ✅[💾][20170602tests] |
| 20170701 | [Day 7: Recursive Circus][20170701] | ✅[💾][20170701solution] | ✅[💾][20170701tests] |
| 20170801 | [Day 8: I Heard You Like Registers][20170801] | ✅[💾][20170801solution] | ✅[💾][20170801tests] |
| 20170802 | [Day 8: I Heard You Like Registers - Part 2][20170802] | ✅[💾][20170802solution] | ✅[💾][20170802tests] |

[20170101]: https://adventofcode.com/2017/day/1
[20170102]: https://adventofcode.com/2017/day/1#part2
[20170201]: https://adventofcode.com/2017/day/2
[20170202]: https://adventofcode.com/2017/day/2#part2
[20170301]: https://adventofcode.com/2017/day/3
[20170302]: https://adventofcode.com/2017/day/3#part2
[20170401]: https://adventofcode.com/2017/day/4
[20170402]: https://adventofcode.com/2017/day/4#part2
[20170501]: https://adventofcode.com/2017/day/5
[20170502]: https://adventofcode.com/2017/day/5#part2
[20170601]: https://adventofcode.com/2017/day/6
[20170602]: https://adventofcode.com/2017/day/6#part2
[20170701]: https://adventofcode.com/2017/day/7
[20170801]: https://adventofcode.com/2017/day/8
[20170802]: https://adventofcode.com/2017/day/8#part2

[20170101solution]: src/main/java/org/ck/adventofcode/year2017/day01/Part1.java
[20170102solution]: src/main/java/org/ck/adventofcode/year2017/day01/Part2.java
[20170201solution]: src/main/java/org/ck/adventofcode/year2017/day02/Part1.java
[20170202solution]: src/main/java/org/ck/adventofcode/year2017/day02/Part2.java
[20170301solution]: src/main/java/org/ck/adventofcode/year2017/day03/Part1.java
[20170302solution]: src/main/java/org/ck/adventofcode/year2017/day03/Part2.java
[20170401solution]: src/main/java/org/ck/adventofcode/year2017/day04/Part1.java
[20170402solution]: src/main/java/org/ck/adventofcode/year2017/day04/Part2.java
[20170501solution]: src/main/java/org/ck/adventofcode/year2017/day05/Part1.java
[20170502solution]: src/main/java/org/ck/adventofcode/year2017/day05/Part2.java
[20170601solution]: src/main/java/org/ck/adventofcode/year2017/day06/Part1.java
[20170602solution]: src/main/java/org/ck/adventofcode/year2017/day06/Part2.java
[20170701solution]: src/main/java/org/ck/adventofcode/year2017/day07/Part1.java
[20170801solution]: src/main/java/org/ck/adventofcode/year2017/day08/Part1.java
[20170802solution]: src/main/java/org/ck/adventofcode/year2017/day08/Part2.java

[20170101tests]: src/test/java/org/ck/adventofcode/year2017/day01/Part1Test.java
[20170102tests]: src/test/java/org/ck/adventofcode/year2017/day01/Part2Test.java
[20170201tests]: src/test/java/org/ck/adventofcode/year2017/day02/Part1Test.java
[20170202tests]: src/test/java/org/ck/adventofcode/year2017/day02/Part2Test.java
[20170301tests]: src/test/java/org/ck/adventofcode/year2017/day03/Part1Test.java
[20170302tests]: src/test/java/org/ck/adventofcode/year2017/day03/Part2Test.java
[20170401tests]: src/test/java/org/ck/adventofcode/year2017/day04/Part1Test.java
[20170402tests]: src/test/java/org/ck/adventofcode/year2017/day04/Part2Test.java
[20170501tests]: src/test/java/org/ck/adventofcode/year2017/day05/Part1Test.java
[20170502tests]: src/test/java/org/ck/adventofcode/year2017/day05/Part2Test.java
[20170601tests]: src/test/java/org/ck/adventofcode/year2017/day06/Part1Test.java
[20170602tests]: src/test/java/org/ck/adventofcode/year2017/day06/Part2Test.java
[20170701tests]: src/test/java/org/ck/adventofcode/year2017/day07/Part1Test.java
[20170801tests]: src/test/java/org/ck/adventofcode/year2017/day08/Part1Test.java
[20170802tests]: src/test/java/org/ck/adventofcode/year2017/day08/Part2Test.java

# 2019 (29/30)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.ck.adventofcode.year2017.day03;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20170301,
name = "Day 3: Spiral Memory",
url = "https://adventofcode.com/2017/day/3",
category = "2017")
public class Part1 {
public static void main(String[] args) {
final Map<Coordinates, Long> memory = new HashMap<>();

try (Scanner in = new Scanner(System.in)) {
final long maxCell = in.nextInt();

int x = 0;
int y = 0;
Direction direction = Direction.RIGHT;

memory.put(new Coordinates(x, y), 1L);
for (long cell = 2; cell <= maxCell; ++cell) {
x += direction.getdX();
y += direction.getdY();

memory.put(new Coordinates(x, y), cell);

if (!memory.containsKey(
new Coordinates(x + direction.getNext().getdX(), y + direction.getNext().getdY()))) {
direction = direction.getNext();
}
}

System.out.println(Math.abs(x) + Math.abs(y));
}
}

private record Coordinates(int x, int y) {}

private enum Direction {
UP(0, 1),
DOWN(0, -1),
RIGHT(1, 0),
LEFT(-1, 0);

private final int dX;
private final int dY;

Direction(final int dX, final int dY) {
this.dX = dX;
this.dY = dY;
}

public int getdX() {
return dX;
}

public int getdY() {
return dY;
}

public Direction getNext() {
return switch (this) {
case UP -> LEFT;
case DOWN -> RIGHT;
case RIGHT -> UP;
case LEFT -> DOWN;
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.ck.adventofcode.year2017.day03;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20170302,
name = "Day 3: Spiral Memory - Part 2",
url = "https://adventofcode.com/2017/day/3#part2",
category = "2017")
public class Part2 {
public static void main(String[] args) {
final Map<Coordinates, Long> memory = new HashMap<>();

try (Scanner in = new Scanner(System.in)) {
final long maxCell = in.nextInt();

int x = 0;
int y = 0;

Direction direction = Direction.RIGHT;

memory.put(new Coordinates(x, y), 1L);
while (true) {
x += direction.getdX();
y += direction.getdY();

Long newValue = getValue(memory, x, y);
memory.put(new Coordinates(x, y), newValue);

if (newValue > maxCell) {
break;
}

if (!memory.containsKey(
new Coordinates(x + direction.getNext().getdX(), y + direction.getNext().getdY()))) {
direction = direction.getNext();
}
}

System.out.println(memory.get(new Coordinates(x, y)));
}
}

private static Long getValue(final Map<Coordinates, Long> memory, final int x, final int y) {
return memory.getOrDefault(new Coordinates(x - 1, y - 1), 0L)
+ memory.getOrDefault(new Coordinates(x - 1, y), 0L)
+ memory.getOrDefault(new Coordinates(x - 1, y + 1), 0L)
+ memory.getOrDefault(new Coordinates(x, y - 1), 0L)
+ memory.getOrDefault(new Coordinates(x, y + 1), 0L)
+ memory.getOrDefault(new Coordinates(x + 1, y - 1), 0L)
+ memory.getOrDefault(new Coordinates(x + 1, y), 0L)
+ memory.getOrDefault(new Coordinates(x + 1, y + 1), 0L);
}

private record Coordinates(int x, int y) {}

private enum Direction {
UP(0, 1),
DOWN(0, -1),
RIGHT(1, 0),
LEFT(-1, 0);

private final int dX;
private final int dY;

Direction(final int dX, final int dY) {
this.dX = dX;
this.dY = dY;
}

public int getdX() {
return dX;
}

public int getdY() {
return dY;
}

public Direction getNext() {
return switch (this) {
case UP -> LEFT;
case DOWN -> RIGHT;
case RIGHT -> UP;
case LEFT -> DOWN;
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.ck.adventofcode.year2017.day08;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20170801,
name = "Day 8: I Heard You Like Registers",
url = "https://adventofcode.com/2017/day/8",
category = "2017")
public class Part1 {
private static final Pattern LINE_PATTERN =
Pattern.compile(
"(?<register>\\w+) (?<command>\\w+) (?<value>-?\\d+) if"
+ " (?<checkRegister>\\w+) (?<condition>[><=!]+) (?<checkValue>-?\\d+)");

public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
Map<String, Long> registers = new HashMap<>();

while (in.hasNextLine()) {
final Matcher matcher = LINE_PATTERN.matcher(in.nextLine());

if (matcher.find()) {
String register = matcher.group("register");
String command = matcher.group("command");
long value = Long.parseLong(matcher.group("value"));

String checkRegister = matcher.group("checkRegister");
String condition = matcher.group("condition");
long checkValue = Long.parseLong(matcher.group("checkValue"));

if (conditionIsValid(registers, checkRegister, condition, checkValue)) {
switch (command) {
case "inc" -> registers.put(register, registers.getOrDefault(register, 0L) + value);
case "dec" -> registers.put(register, registers.getOrDefault(register, 0L) - value);
default -> throw new IllegalArgumentException();
}
}
}
}

System.out.println(registers.values().stream().mapToLong(x -> x).max().getAsLong());
}
}

private static boolean conditionIsValid(
final Map<String, Long> registers,
final String checkRegister,
final String condition,
final long checkValue) {
long actualValue = registers.getOrDefault(checkRegister, 0L);

return switch (condition) {
case "==" -> actualValue == checkValue;
case "!=" -> actualValue != checkValue;
case ">=" -> actualValue >= checkValue;
case "<=" -> actualValue <= checkValue;
case ">" -> actualValue > checkValue;
case "<" -> actualValue < checkValue;
default -> throw new IllegalArgumentException();
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.ck.adventofcode.year2017.day08;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ck.codechallengelib.annotation.Solution;

@Solution(
id = 20170802,
name = "Day 8: I Heard You Like Registers - Part 2",
url = "https://adventofcode.com/2017/day/8#part2",
category = "2017")
public class Part2 {
private static final Pattern LINE_PATTERN =
Pattern.compile(
"(?<register>\\w+) (?<command>\\w+) (?<value>-?\\d+) if"
+ " (?<checkRegister>\\w+) (?<condition>[><=!]+) (?<checkValue>-?\\d+)");

public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
Map<String, Long> registers = new HashMap<>();
long max = 0;

while (in.hasNextLine()) {
final Matcher matcher = LINE_PATTERN.matcher(in.nextLine());

if (matcher.find()) {
String register = matcher.group("register");
String command = matcher.group("command");
long value = Long.parseLong(matcher.group("value"));

String checkRegister = matcher.group("checkRegister");
String condition = matcher.group("condition");
long checkValue = Long.parseLong(matcher.group("checkValue"));

if (conditionIsValid(registers, checkRegister, condition, checkValue)) {
switch (command) {
case "inc" -> registers.put(register, registers.getOrDefault(register, 0L) + value);
case "dec" -> registers.put(register, registers.getOrDefault(register, 0L) - value);
default -> throw new IllegalArgumentException();
}

max = Math.max(max, registers.get(register));
}
}
}

System.out.println(max);
}
}

private static boolean conditionIsValid(
final Map<String, Long> registers,
final String checkRegister,
final String condition,
final long checkValue) {
long actualValue = registers.getOrDefault(checkRegister, 0L);

return switch (condition) {
case "==" -> actualValue == checkValue;
case "!=" -> actualValue != checkValue;
case ">=" -> actualValue >= checkValue;
case "<=" -> actualValue <= checkValue;
case ">" -> actualValue > checkValue;
case "<" -> actualValue < checkValue;
default -> throw new IllegalArgumentException();
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.ck.adventofcode.year2017.day03;

import org.ck.codechallengelib.testhelper.BaseTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class Part1Test extends BaseTest {
@ParameterizedTest
@ValueSource(strings = {"01", "01a", "01b", "01c", "01d"})
public void test(String name) throws Exception {
runFileAsStdIn(Part1.class, name);
}
}
Loading

0 comments on commit 39f1775

Please sign in to comment.