@@ -24,26 +24,61 @@ pub fn part_one(input: &str) -> Option<u64> {
2424}
2525
2626pub fn part_two ( input : & str ) -> Option < u64 > {
27+ // zero alocations for ZOOM ZOOM
28+ let bytes = input. as_bytes ( ) ;
29+ let width = input. find ( '\n' ) . unwrap ( ) ;
30+ let mut map_counts = vec ! [ 0 ; width] ;
31+ let s_loc = width / 2 ; // starting location of 'S'
32+ map_counts[ s_loc] = 1 ;
33+ let mut skip = s_loc;
34+ for row in bytes. chunks ( width + 1 ) . step_by ( 2 ) . skip ( 1 ) {
35+ skip -= 1 ;
36+ for c in skip..width - skip - 1 {
37+ let curr = row[ c] ;
38+ match curr {
39+ b'^' => {
40+ map_counts[ c - 1 ] += map_counts[ c] ;
41+ map_counts[ c + 1 ] += map_counts[ c] ;
42+ map_counts[ c] = 0 ;
43+ }
44+ _ => ( ) ,
45+ }
46+ }
47+ }
48+ Some ( map_counts. iter ( ) . sum ( ) )
49+ }
50+
51+ pub fn part_two_original ( input : & str ) -> Option < u64 > {
2752 let map = input. c_map ( ) ;
28- let mut map_counts = vec ! [ vec![ 0 ; map[ 0 ] . len( ) ] ; map. len( ) ] ;
29- for r in 0 ..map. len ( ) {
30- for c in 0 ..map[ r] . len ( ) {
31- let prev = map_counts[ if r > 0 { r - 1 } else { 0 } ] [ c] ;
32- let curr = map[ r] [ c] ;
53+ let width = map[ 0 ] . len ( ) ;
54+ let mut map_counts = vec ! [ 0 ; width] ;
55+ let mut new_map_counts = vec ! [ 0 ; width] ;
56+ let mut skip = width / 2 ;
57+ for row in map. iter ( ) . step_by ( 2 ) {
58+ for c in skip..row. len ( ) - skip {
59+ let prev = map_counts[ c] ;
60+ let curr = row[ c] ;
3361 match ( prev, curr) {
34- ( _, 'S' ) => map_counts [ r ] [ c] = 1 ,
62+ ( _, 'S' ) => new_map_counts [ c] = 1 ,
3563 ( x, '.' ) if x > 0 => {
36- map_counts [ r ] [ c] += map_counts[ r - 1 ] [ c] ;
64+ new_map_counts [ c] += map_counts[ c] ;
3765 }
3866 ( x, '^' ) if x > 0 => {
39- map_counts [ r ] [ c - 1 ] += map_counts[ r - 1 ] [ c] ;
40- map_counts [ r ] [ c + 1 ] += map_counts[ r - 1 ] [ c] ;
67+ new_map_counts [ c - 1 ] += map_counts[ c] ;
68+ new_map_counts [ c + 1 ] += map_counts[ c] ;
4169 }
4270 _ => ( ) ,
4371 }
4472 }
73+ let temp = map_counts;
74+ map_counts = new_map_counts;
75+ new_map_counts = temp;
76+ if skip != 0 {
77+ skip -= 1 ;
78+ new_map_counts. iter_mut ( ) . for_each ( |x| * x = 0 ) ;
79+ }
4580 }
46- let timelines = map_counts[ map_counts . len ( ) - 1 ] . iter ( ) . sum ( ) ;
81+ let timelines = map_counts. iter ( ) . sum ( ) ;
4782 Some ( timelines)
4883}
4984
0 commit comments