Skip to content

Commit 86f8739

Browse files
committed
[Year 2022 Day 4 Part 2] ok
1 parent 139fee4 commit 86f8739

File tree

6 files changed

+75
-3
lines changed

6 files changed

+75
-3
lines changed

solutions/src/main/java/dev/vinyard/adventofcode/soluce/year2022/day4/ASD.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,24 @@ public Root(List<Pair> pairs) {
1414
this.pairs = pairs;
1515
}
1616

17-
public long result() {
17+
public long part1() {
1818
return pairs.stream().filter(Pair::contains).count();
1919
}
2020

21+
public long part2() {
22+
return pairs.stream().filter(Pair::overlaps).count();
23+
}
24+
2125
}
2226

2327
public record Pair(Section left, Section right) {
2428
public boolean contains() {
2529
return left.contains(right) || right.contains(left);
2630
}
31+
32+
public boolean overlaps() {
33+
return left.overlaps(right);
34+
}
2735
}
2836

2937
public static class Section {
@@ -36,6 +44,10 @@ public Section(long start, long end) {
3644
public boolean contains(Section section) {
3745
return this.range.containsRange(section.range);
3846
}
47+
48+
public boolean overlaps(Section section) {
49+
return this.range.isOverlappedBy(section.range);
50+
}
3951
}
4052

4153
}

solutions/src/main/java/dev/vinyard/adventofcode/soluce/year2022/day4/Day4Part1Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public Long solve(String input) {
6161

6262
ASD.Root root = parser.root().out;
6363

64-
return root.result();
64+
return root.part1();
6565
}
6666
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dev.vinyard.adventofcode.soluce.year2022.day4;
2+
3+
import dev.vinyard.aoc.plugins.solution.api.Solution;
4+
import dev.vinyard.aoc.plugins.solution.api.annotation.AdventOfCodeSolution;
5+
import org.antlr.v4.runtime.CharStream;
6+
import org.antlr.v4.runtime.CharStreams;
7+
import org.antlr.v4.runtime.CommonTokenStream;
8+
9+
@AdventOfCodeSolution(year = 2022, day = 4, part = 2, description = "Camp Cleanup", link = "https://adventofcode.com/2022/day/4", tags = "unsolved")
10+
public class Day4Part2Solution implements Solution<Long> {
11+
12+
/**
13+
* <h2>--- Part Two ---</h2>
14+
* <p>It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that <b>overlap at all</b>.</p>
15+
* <p>In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:</p>
16+
* <ul>
17+
* <li>5-7,7-9 overlaps in a single section, 7.</li>
18+
* <li>2-8,3-7 overlaps all of the sections 3 through 7.</li>
19+
* <li>6-6,4-6 overlaps in a single section, 6.</li>
20+
* <li>2-6,4-8 overlaps in sections 4, 5, and 6.</li>
21+
* </ul>
22+
* <p>So, in this example, the number of overlapping assignment pairs is <b>4</b>.</p>
23+
* <p><b>In how many assignment pairs do the ranges overlap?</b></p>
24+
*/
25+
@Override
26+
public Long solve(String input) {
27+
CharStream charStream = CharStreams.fromString(input);
28+
29+
SolutionLexer lexer = new SolutionLexer(charStream);
30+
CommonTokenStream tokens = new CommonTokenStream(lexer);
31+
SolutionParser parser = new SolutionParser(tokens);
32+
33+
ASD.Root root = parser.root().out;
34+
35+
return root.part2();
36+
}
37+
}

solutions/src/test/java/dev/vinyard/adventofcode/soluce/year2022/day4/Day4Part1SolutionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public Solution<Long> getSolution() {
1616
@Override
1717
public Stream<Arguments> testSolution() {
1818
return Stream.of(
19-
Arguments.of("soluce/year2022/day4/part1/test.txt", 2L, null, null),
19+
Arguments.of("soluce/year2022/day4/test.txt", 2L, null, null),
2020
Arguments.of("soluce/year2022/day4/input.txt", 503L, null, null)
2121
);
2222
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dev.vinyard.adventofcode.soluce.year2022.day4;
2+
3+
import dev.vinyard.adventofcode.common.BaseTest;
4+
import dev.vinyard.aoc.plugins.solution.api.Solution;
5+
import org.junit.jupiter.params.provider.Arguments;
6+
7+
import java.util.stream.Stream;
8+
9+
public class Day4Part2SolutionTest extends BaseTest<Long> {
10+
11+
@Override
12+
public Solution<Long> getSolution() {
13+
return new Day4Part2Solution();
14+
}
15+
16+
@Override
17+
public Stream<Arguments> testSolution() {
18+
return Stream.of(
19+
Arguments.of("soluce/year2022/day4/test.txt", 4L, null, null),
20+
Arguments.of("soluce/year2022/day4/input.txt", 827L, null, null)
21+
);
22+
}
23+
}

0 commit comments

Comments
 (0)