@@ -7,13 +7,18 @@ use crate::util::parse::*;
7
7
8
8
type Input = ( u32 , usize ) ;
9
9
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
+
10
15
pub fn parse ( input : & str ) -> Input {
11
16
let claims: Vec < _ > = input
12
17
. iter_unsigned :: < usize > ( )
13
18
. chunk :: < 5 > ( )
14
19
. 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;
17
22
18
23
// Create bitmask for each claim, for example `#123 @ 3,2: 5x4` becomes `11111000`.
19
24
// Use an intermediate u128 to handle claims up to 65 inches wide.
@@ -25,13 +30,11 @@ pub fn parse(input: &str) -> Input {
25
30
} )
26
31
. collect ( ) ;
27
32
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 ] ;
32
35
33
36
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 ) {
35
38
overlap[ index] |= fabric[ index] & lower;
36
39
fabric[ index] |= lower;
37
40
@@ -42,15 +45,20 @@ pub fn parse(input: &str) -> Input {
42
45
}
43
46
}
44
47
48
+ // Count the area of overlapping claims.
49
+ let part_one = overlap. iter ( ) . map ( |n| n. count_ones ( ) ) . sum ( ) ;
50
+
45
51
// 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
+ } )
49
58
} )
50
- } ) ;
59
+ . map ( |id| id + 1 )
60
+ . unwrap ( ) ;
51
61
52
- let part_one = overlap. iter ( ) . map ( |n| n. count_ones ( ) ) . sum ( ) ;
53
- let part_two = position. unwrap ( ) + 1 ;
54
62
( part_one, part_two)
55
63
}
56
64
0 commit comments