File tree Expand file tree Collapse file tree 7 files changed +2366
-1
lines changed Expand file tree Collapse file tree 7 files changed +2366
-1
lines changed Original file line number Diff line number Diff line change 8
8
- [ Day 3] ( https://github.com/ankjevel/adventofcode/tree/2020/day_03 ) 🌟 🌟
9
9
- [ Day 4] ( https://github.com/ankjevel/adventofcode/tree/2020/day_04 ) 🌟 🌟
10
10
- [ Day 5] ( https://github.com/ankjevel/adventofcode/tree/2020/day_05 ) 🌟 🌟
11
- - [ Day 6] ( # )
11
+ - [ Day 6] ( https://github.com/ankjevel/adventofcode/tree/2020/day_06 ) 🌟 🌟
12
12
- [ Day 7] ( # )
13
13
- [ Day 8] ( # )
14
14
- [ Day 9] ( # )
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ name = " day_06"
3
+ version = " 0.1.0"
4
+ authors = [" Dennis Pettersson <mail@dennispettersson.se>" ]
5
+ edition = " 2018"
6
+
7
+ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
+
9
+ [dependencies ]
Original file line number Diff line number Diff line change
1
+ pub mod part_01;
2
+ pub mod part_02;
3
+
4
+ pub type Input = Vec < Vec < String > > ;
5
+
6
+ pub fn parse_input ( input : & str ) -> Input {
7
+ input
8
+ . lines ( )
9
+ . map ( |string| string. trim ( ) )
10
+ . into_iter ( )
11
+ . fold ( vec ! [ vec![ ] ] , |mut list, string| {
12
+ if string. is_empty ( ) {
13
+ list. push ( vec ! [ ] ) ;
14
+ } else {
15
+ let last = list. last_mut ( ) . unwrap ( ) ;
16
+ last. push ( string. to_string ( ) ) ;
17
+ }
18
+ list
19
+ } )
20
+ . into_iter ( )
21
+ . filter ( |list| list. len ( ) > 0 )
22
+ . collect ( )
23
+ }
24
+
25
+ #[ cfg( test) ]
26
+ mod tests {
27
+ use super :: * ;
28
+
29
+ const EXAMPLE_DATA : & ' static str = "
30
+ abc
31
+
32
+ a
33
+ b
34
+
35
+
36
+ " ;
37
+
38
+ #[ test]
39
+ fn it_parses_example ( ) {
40
+ assert_eq ! (
41
+ parse_input( & EXAMPLE_DATA ) ,
42
+ vec![ vec![ "abc" ] , vec![ "a" , "b" ] ]
43
+ ) ;
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ use std:: io:: Result ;
2
+
3
+ use day_06:: { parse_input, part_01:: main as part_01, part_02:: main as part_02} ;
4
+
5
+ fn main ( ) -> Result < ( ) > {
6
+ let input = parse_input ( include_str ! ( "../../input/day_06" ) ) ;
7
+
8
+ println ! ( "part_01: {:?}" , part_01( & input) . unwrap( ) ) ;
9
+ println ! ( "part_02: {:?}" , part_02( & input) . unwrap( ) ) ;
10
+
11
+ Ok ( ( ) )
12
+ }
Original file line number Diff line number Diff line change
1
+ use std:: io:: Result ;
2
+
3
+ use crate :: Input ;
4
+
5
+ pub fn main ( input : & Input ) -> Result < usize > {
6
+ Ok ( input. iter ( ) . fold ( 0 , |sum, group| {
7
+ sum + ( 'a' ..='z' )
8
+ . map ( char:: from)
9
+ . filter ( |c| {
10
+ group
11
+ . iter ( )
12
+ . filter ( |p| p. contains ( & c. to_string ( ) ) )
13
+ . collect :: < Vec < _ > > ( )
14
+ . len ( )
15
+ > 0
16
+ } )
17
+ . collect :: < Vec < char > > ( )
18
+ . len ( )
19
+ } ) )
20
+ }
21
+
22
+ #[ cfg( test) ]
23
+ mod tests {
24
+ use crate :: parse_input;
25
+
26
+ use super :: * ;
27
+
28
+ const EXAMPLE_DATA : & ' static str = "
29
+ abc
30
+
31
+ a
32
+ b
33
+ c
34
+
35
+ ab
36
+ ac
37
+
38
+ a
39
+ a
40
+ a
41
+ a
42
+
43
+ b
44
+ " ;
45
+
46
+ #[ test]
47
+ fn it_gets_the_example_correct ( ) {
48
+ assert_eq ! ( main( & parse_input( & EXAMPLE_DATA ) ) . unwrap( ) , 11 ) ;
49
+ }
50
+ }
Original file line number Diff line number Diff line change
1
+ use std:: io:: Result ;
2
+
3
+ use crate :: Input ;
4
+
5
+ pub fn main ( input : & Input ) -> Result < usize > {
6
+ Ok ( input. iter ( ) . fold ( 0 , |sum, group| {
7
+ sum + ( 'a' ..='z' )
8
+ . map ( char:: from)
9
+ . filter ( |c| {
10
+ group
11
+ . iter ( )
12
+ . filter ( |p| p. contains ( & c. to_string ( ) ) )
13
+ . collect :: < Vec < _ > > ( )
14
+ . len ( )
15
+ == group. len ( )
16
+ } )
17
+ . collect :: < Vec < char > > ( )
18
+ . len ( )
19
+ } ) )
20
+ }
21
+
22
+ #[ cfg( test) ]
23
+ mod tests {
24
+ use crate :: parse_input;
25
+
26
+ use super :: * ;
27
+
28
+ const EXAMPLE_DATA : & ' static str = "
29
+ abc
30
+
31
+ a
32
+ b
33
+ c
34
+
35
+ ab
36
+ ac
37
+
38
+ a
39
+ a
40
+ a
41
+ a
42
+
43
+ b
44
+ " ;
45
+
46
+ #[ test]
47
+ fn it_gets_the_example_correct ( ) {
48
+ assert_eq ! ( main( & parse_input( & EXAMPLE_DATA ) ) . unwrap( ) , 6 ) ;
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments