-
Notifications
You must be signed in to change notification settings - Fork 2
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
29 changed files
with
2,397 additions
and
1 deletion.
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
74 changes: 74 additions & 0 deletions
74
adventofcode/src/main/java/org/ck/adventofcode/year2017/day03/Part1.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,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; | ||
}; | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
adventofcode/src/main/java/org/ck/adventofcode/year2017/day03/Part2.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,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; | ||
}; | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
adventofcode/src/main/java/org/ck/adventofcode/year2017/day08/Part1.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,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(); | ||
}; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
adventofcode/src/main/java/org/ck/adventofcode/year2017/day08/Part2.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,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(); | ||
}; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
adventofcode/src/test/java/org/ck/adventofcode/year2017/day03/Part1Test.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,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); | ||
} | ||
} |
Oops, something went wrong.