-
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.
- Loading branch information
1 parent
b3db5c8
commit 52d29e9
Showing
2 changed files
with
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package advent2019; | ||
|
||
import com.google.common.io.CharStreams; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Éamonn McManus | ||
*/ | ||
public class Puzzle1 { | ||
public static void main(String[] args) throws Exception { | ||
try (Reader r = new InputStreamReader(Puzzle1.class.getResourceAsStream("puzzle1.txt"))) { | ||
List<Long> numbers = CharStreams.readLines(r).stream().map(Long::valueOf).toList(); | ||
long part1 = numbers.stream().mapToLong(n -> n / 3 - 2).sum(); | ||
System.out.printf("Part 1 sum is %d\n", part1); | ||
long part2 = 0; | ||
for (long n : numbers) { | ||
for (long m = n; (m = m / 3 - 2) > 0; ) { | ||
part2 += m; | ||
} | ||
} | ||
System.out.printf("Part 2 sum is %d\n", part2); | ||
} | ||
} | ||
} |
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,58 @@ | ||
package advent2019; | ||
|
||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static java.lang.Math.addExact; | ||
import static java.lang.Math.multiplyExact; | ||
|
||
import com.google.common.base.Splitter; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.io.CharStreams; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Éamonn McManus | ||
*/ | ||
public class Puzzle2 { | ||
public static void main(String[] args) throws Exception { | ||
try (Reader r = new InputStreamReader(Puzzle2.class.getResourceAsStream("puzzle2.txt"))) { | ||
String line = CharStreams.toString(r).trim(); | ||
ImmutableList<Integer> originalMemory = | ||
Splitter.on(',').splitToStream(line).map(Integer::valueOf).collect(toImmutableList()); | ||
|
||
// Part 1 | ||
System.out.printf("Part 1 result is %d\n", run(originalMemory, 12, 2)); | ||
|
||
// Part 2 | ||
outer: | ||
for (int i = 0; i < 100; i++) { | ||
for (int j = 0; j < 100; j++) { | ||
if (run(originalMemory, i, j) == 19690720) { | ||
System.out.printf("Part 2 result is %d,%d => %d\n", i, j, i * 100 + j); | ||
break outer; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static int run(ImmutableList<Integer> originalMemory, int input1, int input2) { | ||
List<Integer> memory = new ArrayList<>(originalMemory); | ||
memory.set(1, input1); | ||
memory.set(2, input2); | ||
for (int pc = 0; memory.get(pc) != 99; pc += 4) { | ||
int lhs = memory.get(memory.get(pc + 1)); | ||
int rhs = memory.get(memory.get(pc + 2)); | ||
int result = | ||
switch (memory.get(pc)) { | ||
case 1 -> addExact(lhs, rhs); | ||
case 2 -> multiplyExact(lhs, rhs); | ||
default -> throw new AssertionError(memory.get(pc)); | ||
}; | ||
memory.set(memory.get(pc + 3), result); | ||
} | ||
return memory.get(0); | ||
} | ||
} |