-
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
12 changed files
with
211 additions
and
316 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
adventofcode/src/main/java/org/ck/adventofcode/year2015/Day17.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.year2015; | ||
|
||
import java.util.*; | ||
import java.util.function.ToIntFunction; | ||
import org.ck.adventofcode.util.AOCSolution; | ||
import org.ck.codechallengelib.annotation.Solution; | ||
|
||
@Solution( | ||
id = 20151701, | ||
name = "Day 17: No Such Thing as Too Much", | ||
url = "https://adventofcode.com/2015/day/17", | ||
category = "2015") | ||
@Solution( | ||
id = 20151702, | ||
name = "Day 17: No Such Thing as Too Much - Part 2", | ||
url = "https://adventofcode.com/2015/day/17#part2", | ||
category = "2015") | ||
public class Day17 extends AOCSolution { | ||
@Override | ||
protected void runPartOne(final Scanner in) { | ||
run(in, combinations -> combinations.values().stream().mapToInt(n -> n).sum()); | ||
} | ||
|
||
@Override | ||
protected void runPartTwo(final Scanner in) { | ||
run( | ||
in, | ||
combinations -> | ||
combinations.entrySet().stream() | ||
.min(Comparator.comparingInt(Map.Entry::getKey)) | ||
.orElseThrow() | ||
.getValue()); | ||
} | ||
|
||
private void run(final Scanner in, final ToIntFunction<Map<Integer, Integer>> getResult) { | ||
final List<Integer> buckets = new ArrayList<>(); | ||
|
||
final int amount = in.nextInt(); | ||
|
||
while (in.hasNextInt()) { | ||
buckets.add(in.nextInt()); | ||
} | ||
|
||
final Map<Integer, Integer> combinationsPerAmount = new HashMap<>(); | ||
count(amount, buckets, 0, 0, combinationsPerAmount); | ||
|
||
print(getResult.applyAsInt(combinationsPerAmount)); | ||
} | ||
|
||
private static void count( | ||
int amount, | ||
List<Integer> buckets, | ||
int position, | ||
int container, | ||
Map<Integer, Integer> cominationsPerAmount) { | ||
if (amount < 0) { | ||
return; | ||
} | ||
|
||
if (amount == 0) { | ||
cominationsPerAmount.putIfAbsent(container, 0); | ||
cominationsPerAmount.put(container, cominationsPerAmount.get(container) + 1); | ||
return; | ||
} | ||
|
||
for (int i = position; i < buckets.size(); ++i) { | ||
|
||
count(amount - buckets.get(i), buckets, i + 1, container + 1, cominationsPerAmount); | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
adventofcode/src/main/java/org/ck/adventofcode/year2015/Day18.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,102 @@ | ||
package org.ck.adventofcode.year2015; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.util.function.Consumer; | ||
import org.ck.adventofcode.util.AOCSolution; | ||
import org.ck.codechallengelib.annotation.Solution; | ||
|
||
@Solution( | ||
id = 20151801, | ||
name = "Day 18: Like a GIF For Your Yard", | ||
url = "https://adventofcode.com/2015/day/18", | ||
category = "2015") | ||
@Solution( | ||
id = 20151802, | ||
name = "Day 18: Like a GIF For Your Yard - Part 2", | ||
url = "https://adventofcode.com/2015/day/18#part2", | ||
category = "2015") | ||
public class Day18 extends AOCSolution { | ||
@Override | ||
protected void runPartOne(final Scanner in) { | ||
run(in, grid -> {}); | ||
} | ||
|
||
@Override | ||
protected void runPartTwo(final Scanner in) { | ||
run(in, Day18::fixGrid); | ||
} | ||
|
||
private void run(final Scanner in, final Consumer<List<List<String>>> fixGrid) { | ||
final int cycles = in.nextInt(); | ||
in.nextLine(); | ||
|
||
List<List<String>> grid = new ArrayList<>(); | ||
|
||
while (in.hasNextLine()) { | ||
grid.add(Arrays.asList(in.nextLine().split(""))); | ||
} | ||
|
||
fixGrid.accept(grid); | ||
|
||
for (int i = 0; i < cycles; ++i) { | ||
final List<List<String>> copy = new ArrayList<>(); | ||
|
||
for (int x = 0; x < grid.size(); ++x) { | ||
copy.add(new ArrayList<>()); | ||
|
||
for (int y = 0; y < grid.get(x).size(); ++y) { | ||
int alive = 0; | ||
for (int dx = Math.max(x - 1, 0); dx <= Math.min(x + 1, grid.size() - 1); ++dx) { | ||
for (int dy = Math.max(y - 1, 0); dy <= Math.min(y + 1, grid.size() - 1); ++dy) { | ||
if (dx == x && dy == y) { | ||
continue; | ||
} | ||
|
||
if ("#".equals(grid.get(dx).get(dy))) { | ||
++alive; | ||
} | ||
} | ||
} | ||
|
||
if ("#".equals(grid.get(x).get(y))) { | ||
if (alive >= 2 && alive <= 3) { | ||
copy.get(x).add("#"); | ||
} else { | ||
copy.get(x).add("."); | ||
} | ||
} else { | ||
if (alive == 3) { | ||
copy.get(x).add("#"); | ||
} else { | ||
copy.get(x).add("."); | ||
} | ||
} | ||
} | ||
} | ||
|
||
grid = copy; | ||
fixGrid.accept(grid); | ||
} | ||
|
||
int alive = 0; | ||
for (final List<String> strings : grid) { | ||
for (final String string : strings) { | ||
if ("#".equals(string)) { | ||
++alive; | ||
} | ||
} | ||
} | ||
|
||
print(alive); | ||
} | ||
|
||
private static void fixGrid(final List<List<String>> grid) { | ||
grid.get(0).set(0, "#"); | ||
grid.get(0).set(grid.get(0).size() - 1, "#"); | ||
grid.get(grid.size() - 1).set(0, "#"); | ||
grid.get(grid.size() - 1).set(grid.get(0).size() - 1, "#"); | ||
} | ||
} |
46 changes: 0 additions & 46 deletions
46
adventofcode/src/main/java/org/ck/adventofcode/year2015/day17/Part1.java
This file was deleted.
Oops, something went wrong.
52 changes: 0 additions & 52 deletions
52
adventofcode/src/main/java/org/ck/adventofcode/year2015/day17/Part2.java
This file was deleted.
Oops, something went wrong.
78 changes: 0 additions & 78 deletions
78
adventofcode/src/main/java/org/ck/adventofcode/year2015/day18/Part1.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.