@@ -7,13 +7,18 @@ use crate::util::parse::*;
77
88type Input = ( u32 , usize ) ;
99
10+ /// Each square inch of fabric is stored in a single bit.
11+ /// The fabric is 1000 inches wide requiring sixteen `u64`.
12+ const WIDTH : usize = 16 ;
13+ const HEIGHT : usize = 1000 ;
14+
1015pub fn parse ( input : & str ) -> Input {
1116 let claims: Vec < _ > = input
1217 . iter_unsigned :: < usize > ( )
1318 . chunk :: < 5 > ( )
1419 . map ( |[ _, x1, y1, width, height] | {
15- let start = 16 * y1 + ( x1 / 64 ) ;
16- let end = start + 16 * height;
20+ let start = WIDTH * y1 + ( x1 / 64 ) ;
21+ let end = start + WIDTH * height;
1722
1823 // Create bitmask for each claim, for example `#123 @ 3,2: 5x4` becomes `11111000`.
1924 // Use an intermediate u128 to handle claims up to 65 inches wide.
@@ -25,13 +30,11 @@ pub fn parse(input: &str) -> Input {
2530 } )
2631 . collect ( ) ;
2732
28- // Each square inch of fabric is stored in a single bit.
29- // The fabric is 1000 inches wide requiring sixteen `u64`.
30- let mut fabric = vec ! [ 0 ; 16 * 1000 ] ;
31- let mut overlap = vec ! [ 0 ; 16 * 1000 ] ;
33+ let mut fabric = vec ! [ 0 ; WIDTH * HEIGHT ] ;
34+ let mut overlap = vec ! [ 0 ; WIDTH * HEIGHT ] ;
3235
3336 for & ( start, end, lower, upper) in & claims {
34- for index in ( start..end) . step_by ( 16 ) {
37+ for index in ( start..end) . step_by ( WIDTH ) {
3538 overlap[ index] |= fabric[ index] & lower;
3639 fabric[ index] |= lower;
3740
@@ -42,15 +45,20 @@ pub fn parse(input: &str) -> Input {
4245 }
4346 }
4447
48+ // Count the area of overlapping claims.
49+ let part_one = overlap. iter ( ) . map ( |n| n. count_ones ( ) ) . sum ( ) ;
50+
4551 // Find the first claim that doesn't overlap with any other claim.
46- let position = claims. iter ( ) . position ( |& ( start, end, lower, upper) | {
47- ( start..end) . step_by ( 16 ) . all ( |index| {
48- ( overlap[ index] & lower == 0 ) && ( upper == 0 || overlap[ index + 1 ] & upper == 0 )
52+ let part_two = claims
53+ . iter ( )
54+ . position ( |& ( start, end, lower, upper) | {
55+ ( start..end) . step_by ( WIDTH ) . all ( |index| {
56+ ( overlap[ index] & lower == 0 ) && ( upper == 0 || overlap[ index + 1 ] & upper == 0 )
57+ } )
4958 } )
50- } ) ;
59+ . map ( |id| id + 1 )
60+ . unwrap ( ) ;
5161
52- let part_one = overlap. iter ( ) . map ( |n| n. count_ones ( ) ) . sum ( ) ;
53- let part_two = position. unwrap ( ) + 1 ;
5462 ( part_one, part_two)
5563}
5664
0 commit comments