File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11use crate :: grid:: parse_each_char;
2+ use std:: collections:: HashMap ;
23use std:: collections:: HashSet ;
34
45#[ derive( Debug ) ]
@@ -44,8 +45,31 @@ pub fn part1(input: &Input) -> usize {
4445 count
4546}
4647
47- pub fn part2 ( input : & Input ) -> & str {
48- "unimplemented"
48+ pub fn part2 ( input : & Input ) -> u64 {
49+ let mut beams: HashMap < usize , u64 > = HashMap :: new ( ) ;
50+ beams. insert ( input. start , 1 ) ;
51+ for splits in input. splitters . iter ( ) {
52+ let mut new_beams: HashMap < usize , u64 > = HashMap :: with_capacity ( beams. len ( ) * 2 ) ;
53+ for ( beam, paths) in beams. into_iter ( ) {
54+ if splits. contains ( & beam) {
55+ new_beams
56+ . entry ( beam - 1 )
57+ . and_modify ( |c| * c += paths)
58+ . or_insert ( paths) ;
59+ new_beams
60+ . entry ( beam + 1 )
61+ . and_modify ( |c| * c += paths)
62+ . or_insert ( paths) ;
63+ } else {
64+ new_beams
65+ . entry ( beam)
66+ . and_modify ( |c| * c += paths)
67+ . or_insert ( paths) ;
68+ }
69+ }
70+ beams = new_beams;
71+ }
72+ beams. into_values ( ) . sum ( )
4973}
5074
5175#[ test]
@@ -70,4 +94,5 @@ fn test() {
7094" ;
7195 let input = parse_input ( test_input) ;
7296 assert_eq ! ( 21 , part1( & input) ) ;
97+ assert_eq ! ( 40 , part2( & input) ) ;
7398}
You can’t perform that action at this time.
0 commit comments