-
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
11 changed files
with
2,110 additions
and
65 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
53 changes: 42 additions & 11 deletions
53
adventofcode/src/main/java/org/ck/adventofcode/year2024/Day01.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 |
---|---|---|
@@ -1,34 +1,65 @@ | ||
package org.ck.adventofcode.year2024; | ||
|
||
import java.util.Scanner; | ||
import java.util.*; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
import java.util.function.IntToLongFunction; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import org.ck.adventofcode.util.AOCSolution; | ||
import org.ck.codechallengelib.annotation.Solution; | ||
|
||
@Solution( | ||
id = 20240101, | ||
name = "Day 1: ?", | ||
name = "Day 1: Historian Hysteria", | ||
url = "https://adventofcode.com/2024/day/1", | ||
category = "2024", | ||
solved = false) | ||
category = "2024") | ||
@Solution( | ||
id = 20240102, | ||
name = "Day 1: ? - Part 2", | ||
name = "Day 1: Historian Hysteria - Part 2", | ||
url = "https://adventofcode.com/2024/day/1#part2", | ||
category = "2024", | ||
solved = false) | ||
category = "2024") | ||
public class Day01 extends AOCSolution { | ||
|
||
@Override | ||
protected void runPartOne(final Scanner in) { | ||
run(in); | ||
run( | ||
in, | ||
(one, two) -> { | ||
Collections.sort(one); | ||
Collections.sort(two); | ||
|
||
return i -> Math.abs(one.get(i) - two.get(i)); | ||
}); | ||
} | ||
|
||
@Override | ||
protected void runPartTwo(final Scanner in) { | ||
run(in); | ||
run( | ||
in, | ||
(one, two) -> { | ||
final Map<Integer, Long> collect = | ||
two.stream() | ||
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); | ||
|
||
return i -> collect.containsKey(one.get(i)) ? collect.get(one.get(i)) * one.get(i) : 0; | ||
}); | ||
} | ||
|
||
private void run(final Scanner in) { | ||
print("Whoopsie"); | ||
private void run( | ||
final Scanner in, | ||
final BiFunction<List<Integer>, List<Integer>, IntToLongFunction> getValueGenerator) { | ||
final List<Integer> one = new ArrayList<>(); | ||
final List<Integer> two = new ArrayList<>(); | ||
|
||
while (in.hasNextLine()) { | ||
final String[] split = in.nextLine().split(" +"); | ||
|
||
one.add(Integer.parseInt(split[0])); | ||
two.add(Integer.parseInt(split[1])); | ||
} | ||
|
||
final IntToLongFunction valueGenerator = getValueGenerator.apply(one, two); | ||
print(IntStream.range(0, one.size()).mapToLong(valueGenerator).sum()); | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
adventofcode/src/test/java/org/ck/adventofcode/year2024/Day01Test.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
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 @@ | ||
2057374 |
Oops, something went wrong.