Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 1, 2023
1 parent f59a43f commit eb33ced
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 316 deletions.
71 changes: 71 additions & 0 deletions adventofcode/src/main/java/org/ck/adventofcode/year2015/Day17.java
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 adventofcode/src/main/java/org/ck/adventofcode/year2015/Day18.java
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, "#");
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit eb33ced

Please sign in to comment.