File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ use aoc:: {
2+ common, io,
3+ range:: { Range , RangeUnion } ,
4+ } ;
5+
6+ fn solve < const PART : usize > ( input : & str ) -> usize {
7+ let mut ans = 0 ;
8+ let mut ru: RangeUnion < usize > = RangeUnion :: new ( ) ;
9+ for line in input. lines ( ) {
10+ if line. is_empty ( ) {
11+ continue ;
12+ }
13+ if line. contains ( "-" ) {
14+ let tokens = io:: tokenize_nums :: < usize > ( line, "-" ) ;
15+ ru. add_range ( Range :: new ( tokens[ 0 ] , tokens[ 1 ] + 1 ) ) ;
16+ } else {
17+ let token = io:: parse_num :: < usize > ( line) ;
18+ if ru. contains ( token) {
19+ ans += 1 ;
20+ }
21+ }
22+ }
23+
24+ if PART == 1 {
25+ ans
26+ } else {
27+ ru. spread ( )
28+ }
29+ }
30+
31+ fn main ( ) {
32+ if let Some ( input) = common:: get_input ( ) {
33+ common:: timed ( & input, solve :: < 1 > , true ) ;
34+ common:: timed ( & input, solve :: < 2 > , false ) ;
35+ }
36+ }
37+
38+ #[ cfg( test) ]
39+ mod tests {
40+ use super :: * ;
41+
42+ #[ test]
43+ fn test_samples ( ) {
44+ let sample_input = "3-5\n 10-14\n 16-20\n 12-18\n \n 1\n 5\n 8\n 11\n 17\n 32" ;
45+ assert_eq ! ( solve:: <1 >( sample_input) , 3 ) ;
46+ assert_eq ! ( solve:: <2 >( sample_input) , 14 ) ;
47+ }
48+ }
You can’t perform that action at this time.
0 commit comments