Skip to content

Commit 139fee4

Browse files
committed
[Year 2022 Day 4 Part 1] ok
1 parent 0bafb76 commit 139fee4

File tree

6 files changed

+1176
-0
lines changed

6 files changed

+1176
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
grammar Solution;
2+
3+
options {
4+
language = Java;
5+
}
6+
7+
@header {
8+
9+
}
10+
11+
root returns [ASD.Root out]
12+
@init {
13+
List<ASD.Pair> pairs = new ArrayList<>();
14+
}
15+
: pair[pairs]+ EOF {
16+
$out = new ASD.Root(pairs);
17+
}
18+
;
19+
20+
pair [List<ASD.Pair> pairs]
21+
: left=section ',' right=section { $pairs.add(new ASD.Pair($left.out, $right.out)); }
22+
;
23+
24+
section returns [ASD.Section out]
25+
: min=value '-' max=value { $out = new ASD.Section($min.out, $max.out); }
26+
;
27+
28+
value returns [long out]
29+
: INT { $out = Long.parseLong($INT.getText()); }
30+
;
31+
32+
INT
33+
// integer part forbis leading 0s (e.g. `01`)
34+
: '0' | [1-9][0-9]*
35+
;
36+
37+
WS
38+
: [ \t\n\r]+ -> skip
39+
;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package dev.vinyard.adventofcode.soluce.year2022.day4;
2+
3+
import org.apache.commons.lang3.Range;
4+
5+
import java.util.List;
6+
7+
public class ASD {
8+
9+
public static class Root {
10+
11+
private final List<Pair> pairs;
12+
13+
public Root(List<Pair> pairs) {
14+
this.pairs = pairs;
15+
}
16+
17+
public long result() {
18+
return pairs.stream().filter(Pair::contains).count();
19+
}
20+
21+
}
22+
23+
public record Pair(Section left, Section right) {
24+
public boolean contains() {
25+
return left.contains(right) || right.contains(left);
26+
}
27+
}
28+
29+
public static class Section {
30+
private final Range<Long> range;
31+
32+
public Section(long start, long end) {
33+
this.range = Range.between(start, end);
34+
}
35+
36+
public boolean contains(Section section) {
37+
return this.range.containsRange(section.range);
38+
}
39+
}
40+
41+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 = 1, description = "Camp Cleanup", link = "https://adventofcode.com/2022/day/4", tags = "unsolved")
10+
public class Day4Part1Solution implements Solution<Long> {
11+
12+
/**
13+
* <h2>--- Day 4: Camp Cleanup ---</h2>
14+
* <p>Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique <b>ID number</b>, and each Elf is assigned a range of section IDs.</p>
15+
* <p>However, as some of the Elves compare their section assignments with each other, they've noticed that many of the assignments <b>overlap</b>. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a <b>big list of the section assignments for each pair</b> (your puzzle input).</p>
16+
* <p>For example, consider the following list of section assignment pairs:</p>
17+
* <pre>
18+
* 2-4,6-8
19+
* 2-3,4-5
20+
* 5-7,7-9
21+
* 2-8,3-7
22+
* 6-6,4-6
23+
* 2-6,4-8
24+
* </pre>
25+
* <p>For the first few pairs, this list means:</p>
26+
* <ul>
27+
* <li>Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf was assigned sections 6-8 (sections 6, 7, 8).</li>
28+
* <li>The Elves in the second pair were each assigned two sections.</li>
29+
* <li>The Elves in the third pair were each assigned three sections: one got sections 5, 6, and 7, while the other also got 7, plus 8 and 9.</li>
30+
* </ul>
31+
* <p>This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this:</p>
32+
* <pre>
33+
* .234..... 2-4
34+
* .....678. 6-8
35+
*
36+
* .23...... 2-3
37+
* ...45.... 4-5
38+
*
39+
* ....567.. 5-7
40+
* ......789 7-9
41+
*
42+
* .2345678. 2-8
43+
* ..34567.. 3-7
44+
*
45+
* .....6... 6-6
46+
* ...456... 4-6
47+
*
48+
* .23456... 2-6
49+
* ...45678. 4-8
50+
* </pre>
51+
* <p>Some of the pairs have noticed that one of their assignments <b>fully contains</b> the other. For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are <b>2</b> such pairs.</p>
52+
* <p><b>In how many assignment pairs does one range fully contain the other?</b></p>
53+
*/
54+
@Override
55+
public Long solve(String input) {
56+
CharStream charStream = CharStreams.fromString(input);
57+
58+
SolutionLexer lexer = new SolutionLexer(charStream);
59+
CommonTokenStream tokens = new CommonTokenStream(lexer);
60+
SolutionParser parser = new SolutionParser(tokens);
61+
62+
ASD.Root root = parser.root().out;
63+
64+
return root.result();
65+
}
66+
}

0 commit comments

Comments
 (0)