Skip to content

Commit

Permalink
AoC 2024 Day 5 - java
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 5, 2024
1 parent 10ecbc4 commit 76a0eed
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| python3 | [](src/main/python/AoC2024_01.py) | [](src/main/python/AoC2024_02.py) | [](src/main/python/AoC2024_03.py) | [](src/main/python/AoC2024_04.py) | [](src/main/python/AoC2024_05.py) | | | | | | | | | | | | | | | | | | | | |
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | [](src/main/java/AoC2024_04.java) | | | | | | | | | | | | | | | | | | | | | |
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | [](src/main/java/AoC2024_04.java) | [](src/main/java/AoC2024_05.java) | | | | | | | | | | | | | | | | | | | | |
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | | | | | | | | | | | | | | | | | | | | | |
<!-- @END:ImplementationsTable:2024@ -->

Expand Down
138 changes: 138 additions & 0 deletions src/main/java/AoC2024_05.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pareronia.aoc.StringOps;
import com.github.pareronia.aoc.StringOps.StringSplit;
import com.github.pareronia.aoc.solution.Sample;
import com.github.pareronia.aoc.solution.Samples;
import com.github.pareronia.aoc.solution.SolutionBase;

public final class AoC2024_05
extends SolutionBase<AoC2024_05.Input, Integer, Integer> {

private AoC2024_05(final boolean debug) {
super(debug);
}

public static AoC2024_05 create() {
return new AoC2024_05(false);
}

public static AoC2024_05 createDebug() {
return new AoC2024_05(true);
}

@Override
protected Input parseInput(final List<String> inputs) {
final List<List<String>> blocks = StringOps.toBlocks(inputs);
final Map<Integer, List<Integer>> order = new HashMap<>();
for (final String line : blocks.get(0)) {
final StringSplit split = StringOps.splitOnce(line, "\\|");
order.computeIfAbsent(
Integer.valueOf(split.left()), k -> new ArrayList<>())
.add(Integer.valueOf(split.right()));
}
final List<List<Integer>> updates = blocks.get(1).stream()
.map(line -> Arrays.stream(line.split(","))
.map(Integer::valueOf)
.toList())
.toList();
return new Input(order, updates);
}

private int solve(final Input input, final Mode mode) {
final Comparator<Integer> comparator
= (a, b) -> (
input.order.getOrDefault(a, new ArrayList<>()).contains(b)
? -1 : 1);
int ans = 0;
for (final List<Integer> update : input.updates) {
final List<Integer> correct = new ArrayList<>(update);
Collections.sort(correct, comparator);
switch (mode) {
case USE_CORRECT: {
if (update.equals(correct)) {
ans += correct.get(correct.size() / 2);
}
break;
}
case USE_INCORRECT: {
if (!update.equals(correct)) {
ans += correct.get(correct.size() / 2);
}
break;
}
}
}
return ans;
}

@Override
public Integer solvePart1(final Input input) {
return solve(input, Mode.USE_CORRECT);
}

@Override
public Integer solvePart2(final Input input) {
return solve(input, Mode.USE_INCORRECT);
}

@Override
@Samples({
@Sample(method = "part1", input = TEST, expected = "143"),
@Sample(method = "part2", input = TEST, expected = "123"),
})
public void samples() {
}

public static void main(final String[] args) throws Exception {
AoC2024_05.create().run();
}

private static final String TEST = """
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47
""";

record Input(
Map<Integer, List<Integer>> order,
List<List<Integer>> updates
) {
}

private enum Mode {
USE_CORRECT,
USE_INCORRECT;
}
}

0 comments on commit 76a0eed

Please sign in to comment.