Camp Cleanup


Fork me on GitHub
2022-12-04

Day 04: Camp Cleanup

Description:
--- Day 4: Camp Cleanup ---

Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a range of section IDs.

However, as some of the Elves compare their section assignments with each other, they've noticed that many of the assignments overlap. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a big list of the section assignments for each pair (your puzzle input).

For example, consider the following list of section assignment pairs:

2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

For the first few pairs, this list means:

Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf was assigned sections 6-8 (sections 6, 7, 8).
The Elves in the second pair were each assigned two sections.
The Elves in the third pair were each assigned three sections: one got sections 5, 6, and 7, while the other also got 7, plus 8 and 9.

This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this:

.234..... 2-4
.....678. 6-8

.23...... 2-3
...45.... 4-5

....567.. 5-7
......789 7-9

.2345678. 2-8
..34567.. 3-7

.....6... 6-6
...456... 4-6

.23456... 2-6
...45678. 4-8

Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are 2 such pairs.

In how many assignment pairs does one range fully contain the other?

--- Part Two ---

It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all.

In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:

5-7,7-9 overlaps in a single section, 7.
2-8,3-7 overlaps all of the sections 3 through 7.
6-6,4-6 overlaps in a single section, 6.
2-6,4-8 overlaps in sections 4, 5, and 6.

So, in this example, the number of overlapping assignment pairs is 4.

In how many assignment pairs do the ranges overlap?

Input:
17-99,18-24
14-91,22-91
63-76,66-83
17-57,17-28
77-88,14-78
15-92,14-15
23-23,24-40
14-42,41-42
23-74,21-24
6-94,94-94
16-18,17-52
50-96,40-96
4-67,3-66
26-81,26-99
18-93,24-93
13-18,17-61
7-46,47-92
46-91,46-98
1-73,72-73
51-86,36-52
16-16,13-16
5-35,1-36
14-99,5-15
2-32,2-92
45-86,44-45
10-96,10-97
2-89,89-95
9-95,8-97
40-78,41-50
48-66,4-49
21-24,22-49
43-92,98-98
91-92,42-92
3-95,3-98
8-11,10-99
65-75,40-64
55-83,82-82
34-50,24-35
20-62,19-62
43-74,42-46
28-32,28-32
38-52,51-51
5-75,66-75
32-46,41-55
97-97,2-98
13-34,12-13
33-93,85-99
7-80,12-79
98-98,54-99
37-38,38-79
20-79,28-82
1-1,2-66
7-68,1-30
35-35,36-52
3-46,2-46
21-62,51-63
30-30,29-42
7-64,8-64
4-94,5-93
35-60,36-60
75-76,69-76
6-80,5-95
23-50,24-50
25-92,89-92
34-56,35-35
65-84,65-69
47-76,47-96
1-3,2-98
10-55,16-55
5-89,99-99
8-74,7-74
13-82,13-82
1-99,3-98
11-17,10-29
81-98,63-98
61-71,60-97
6-92,60-91
7-96,6-96
18-81,13-82
46-61,47-91
22-91,19-91
23-23,23-95
20-20,21-96
4-69,8-70
5-22,21-59
74-80,75-81
95-95,24-93
15-85,9-86
6-7,7-93
11-28,10-12
15-27,16-62
12-24,11-33
21-22,22-65
31-73,7-30
2-80,1-81
1-92,1-90
4-38,5-19
1-40,4-94
49-70,48-48
3-91,91-91
32-99,29-33
11-52,10-70
16-95,15-17
18-18,18-26
83-99,83-84
11-94,11-12
27-80,68-80
17-96,95-96
2-83,1-91
58-66,59-65
45-45,14-54
2-3,3-92
47-47,48-85
39-39,39-77
30-31,30-63
10-11,11-43
6-86,10-85
45-57,57-58
1-5,4-97
1-27,2-53
7-90,7-90
3-28,2-3
77-79,8-78
45-48,28-47
13-73,64-74
14-97,14-83
50-51,51-94
21-86,22-22
11-49,12-49
30-68,67-68
48-50,49-81
24-96,23-24
27-44,26-28
9-80,10-74
44-74,2-44
66-73,64-78
8-82,8-82
3-85,55-93
22-65,13-64
10-19,10-26
79-83,29-79
48-56,52-60
25-93,3-93
1-99,5-97
60-81,60-81
15-26,10-24
50-78,10-51
32-75,33-75
11-21,10-93
1-71,1-98
29-65,30-35
52-90,51-52
56-95,75-94
8-9,9-98
2-53,4-52
7-65,3-13
73-83,72-83
9-18,8-76
14-38,4-37
22-57,22-78
2-99,12-98
2-43,20-44
13-88,87-95
5-61,5-63
14-68,14-52
9-89,8-99
43-99,42-43
10-39,39-39
45-60,42-45
39-40,39-89
44-97,96-96
10-96,15-99
50-90,90-94
40-90,39-89
39-39,40-66
15-96,15-96
48-59,49-58
19-58,18-19
21-92,20-91
1-89,1-2
13-91,12-14
70-80,69-94
28-92,27-28
5-95,95-95
21-22,22-74
2-58,3-7
35-51,35-36
88-89,1-89
84-86,73-85
22-63,9-58
44-93,44-45
54-81,53-54
5-91,90-92
12-29,2-25
4-97,97-97
1-90,8-89
17-37,17-37
10-89,15-90
18-19,19-19
24-44,25-75
21-77,17-77
1-99,2-97
12-91,11-91
50-64,50-64
51-97,52-97
7-45,6-6
61-62,61-82
5-8,5-13
13-35,36-94
91-91,44-93
2-78,1-1
51-52,52-80
52-74,52-80
58-63,15-62
9-78,9-9
19-93,20-93
7-18,4-17
27-73,72-98
17-89,16-88
20-83,21-61
55-55,55-96
43-88,48-87
52-62,51-93
5-27,10-26
3-62,2-93
3-95,4-87
30-96,62-97
25-26,46-97
44-82,43-88
9-30,11-97
31-32,31-77
25-26,25-77
18-55,17-19
14-91,15-92
7-76,8-76
62-63,63-66
26-99,7-47
5-79,4-4
2-97,3-97
85-86,35-85
6-90,21-90
44-90,90-90
32-89,33-84
28-88,31-87
27-45,28-43
26-52,47-63
14-53,24-82
26-76,26-70
7-90,9-91
69-69,11-69
10-82,10-47
39-68,40-40
10-94,10-10
28-78,27-82
7-95,96-96
68-83,68-83
28-51,50-50
98-99,2-99
7-65,6-6
52-54,53-80
13-78,13-70
96-96,28-96
59-60,20-60
44-78,36-44
13-15,14-68
38-71,71-72
6-92,6-98
42-77,43-58
31-61,25-60
3-97,12-98
3-36,2-3
29-69,32-96
17-69,17-89
4-96,4-96
79-99,72-97
1-99,98-99
22-95,21-22
2-97,2-3
5-96,4-97
23-24,24-53
6-96,96-98
75-76,4-76
4-67,7-66
53-91,52-98
37-37,38-99
36-94,37-95
21-79,20-22
42-82,41-42
28-54,41-59
57-97,57-57
47-93,48-93
28-96,29-92
15-73,72-73
89-90,90-99
47-49,34-48
80-82,81-84
60-80,59-80
11-12,12-77
36-96,11-37
16-95,4-95
2-98,1-1
51-86,24-52
89-94,2-90
8-9,8-90
2-98,2-3
4-4,4-5
5-98,4-5
67-81,41-81
1-84,84-84
7-95,12-95
5-5,5-86
1-84,30-99
58-88,44-71
50-98,50-98
18-61,31-74
47-48,47-69
2-41,1-41
65-98,65-98
28-62,59-62
3-38,1-4
34-43,41-44
26-91,25-25
17-57,21-57
20-21,21-86
20-82,42-81
1-61,4-70
49-91,26-73
5-78,78-78
27-98,28-98
55-59,54-61
3-94,1-3
6-40,39-99
16-75,17-54
1-96,2-97
7-7,8-96
11-98,11-98
96-98,95-97
5-85,2-85
1-98,1-1
6-11,7-35
5-54,3-68
19-96,20-96
49-85,56-85
33-58,57-57
66-79,67-80
9-99,8-10
10-86,46-86
5-14,13-86
42-96,19-69
2-83,11-83
9-29,9-93
9-80,8-38
28-28,29-82
5-73,5-48
23-54,23-54
17-96,6-74
49-76,48-77
36-59,37-48
12-96,11-97
81-98,82-99
41-41,42-50
9-9,10-15
87-91,11-91
54-63,55-64
45-46,45-70
72-82,68-91
6-94,93-94
18-64,19-64
46-64,34-48
7-62,61-63
2-95,3-95
45-45,46-55
57-65,56-94
33-95,94-98
22-88,22-28
68-89,69-90
10-24,11-15
32-98,26-55
5-83,83-83
13-79,11-14
44-46,45-81
2-94,1-1
54-64,52-64
12-21,21-96
38-39,7-39
73-96,60-76
69-94,2-70
1-5,3-3
36-79,28-46
1-8,2-56
30-31,5-30
8-89,15-90
7-93,30-96
85-87,63-86
10-10,11-77
2-5,2-97
16-96,17-79
52-68,52-55
15-91,14-38
1-62,2-62
63-98,62-64
8-76,30-76
1-3,2-93
22-92,7-91
11-44,12-57
3-4,3-46
9-9,10-86
43-58,43-50
13-67,64-68
2-3,3-77
12-97,35-99
1-99,4-99
29-56,15-56
52-98,53-84
24-80,9-59
10-10,1-11
12-49,19-79
10-15,11-84
17-71,14-18
42-89,41-45
3-97,97-98
42-42,43-83
9-74,32-74
20-21,21-89
55-55,54-54
67-78,41-68
21-22,22-93
11-66,11-87
11-11,11-93
94-94,18-95
1-99,13-99
57-89,6-57
18-19,18-25
6-15,6-39
17-96,3-95
10-77,9-11
34-94,33-34
26-60,45-60
25-25,12-98
77-78,63-78
29-29,29-70
94-99,36-95
91-91,2-92
3-25,4-6
78-88,79-85
60-80,59-80
64-93,60-89
63-78,47-75
36-85,84-98
90-90,47-90
2-39,5-39
6-90,24-91
12-96,21-97
57-98,56-58
27-76,76-76
24-80,24-25
50-90,89-98
90-99,90-97
17-44,44-45
4-77,1-77
46-90,47-86
10-86,10-86
24-80,23-80
24-24,24-96
20-22,20-24
2-77,5-77
47-61,46-46
65-67,66-75
8-18,7-18
18-19,18-95
50-85,50-85
63-82,62-82
2-62,47-77
22-38,23-92
64-90,64-74
62-77,63-65
14-15,15-84
30-51,31-75
25-38,30-39
1-84,2-75
38-81,39-80
30-83,5-83
11-91,21-90
30-90,31-90
25-93,92-98
87-93,69-88
14-51,12-60
32-32,31-31
33-89,50-90
2-96,1-96
96-99,12-97
43-44,3-42
2-94,3-94
7-10,9-90
59-65,38-65
6-17,5-13
37-83,38-47
57-72,9-72
3-4,4-42
1-93,2-83
34-63,11-62
1-92,7-92
39-39,39-63
13-98,14-91
96-96,2-96
3-94,2-2
97-97,17-98
3-94,3-99
18-95,17-19
44-54,41-52
24-29,25-69
4-8,7-59
16-93,8-93
23-53,53-54
74-98,73-73
96-99,50-96
33-99,33-86
19-34,9-35
12-30,15-47
3-85,34-86
11-23,13-35
28-45,21-46
2-99,2-50
7-83,81-81
39-65,40-95
3-69,5-68
1-94,3-93
2-89,1-2
1-58,3-82
3-88,1-56
4-21,20-39
59-64,44-64
49-81,81-81
11-97,11-12
84-84,19-85
45-46,35-46
4-20,3-54
45-95,44-95
40-40,14-41
7-61,8-61
41-86,85-86
46-46,42-47
75-75,76-76
6-99,7-94
57-94,58-95
3-83,1-84
35-35,36-58
22-63,21-64
85-85,86-94
3-50,2-20
12-78,78-91
2-95,6-96
18-62,26-61
13-90,13-92
1-17,17-67
16-18,17-57
32-85,32-97
87-89,85-89
41-62,40-63
3-68,67-87
26-93,21-92
24-78,14-78
3-51,4-51
5-27,14-26
26-74,25-88
47-89,48-88
27-97,88-96
6-6,6-60
24-37,23-60
41-84,83-85
78-80,22-79
92-93,37-92
18-79,17-79
22-72,71-91
28-39,33-35
45-90,7-90
4-98,90-99
27-99,26-35
1-99,2-2
62-66,63-97
65-94,93-93
68-79,68-93
70-70,50-71
74-90,73-95
7-96,73-97
1-34,33-34
16-93,15-15
55-59,56-62
2-24,4-24
43-44,44-74
18-97,96-97
4-45,3-3
13-99,98-98
7-32,31-38
41-44,42-49
28-96,27-59
5-53,5-37
68-86,67-96
34-63,26-64
97-98,3-97
30-74,31-85
9-93,10-98
66-93,65-76
30-78,13-78
29-48,33-48
8-8,8-99
10-78,16-78
30-51,30-30
19-48,19-19
10-53,9-54
3-91,4-91
42-47,45-56
47-87,46-65
8-17,16-78
34-75,50-74
98-99,27-98
16-40,29-41
22-25,24-84
9-83,15-84
75-76,32-75
11-11,12-99
14-98,14-15
66-66,27-66
8-21,7-20
4-88,34-89
38-88,37-77
80-81,42-81
4-31,8-95
8-10,9-89
55-95,55-73
29-87,87-88
2-2,2-99
28-92,96-98
9-17,9-42
48-95,49-94
4-27,3-91
43-68,42-72
76-94,75-87
8-9,9-96
15-15,16-79
15-68,14-36
11-64,3-62
8-78,44-77
49-60,50-64
15-87,16-79
86-87,62-87
24-60,59-60
1-99,1-99
26-82,26-82
2-95,1-97
6-92,6-93
16-59,15-58
7-87,87-87
87-97,22-88
45-46,46-46
10-90,61-90
16-68,4-68
17-18,7-18
36-90,89-89
38-52,38-78
13-92,13-92
34-34,34-38
50-51,49-82
7-98,5-20
5-72,61-82
48-49,48-97
23-54,24-80
19-95,20-67
13-64,64-64
49-92,91-92
74-84,73-83
18-35,34-59
61-91,10-91
70-89,89-89
1-47,1-2
21-68,19-22
38-76,38-96
29-79,78-79
29-88,30-73
35-88,34-58
21-21,22-68
35-63,2-64
13-56,11-11
12-96,11-96
6-14,9-59
3-61,47-84
14-33,14-15
62-69,62-92
33-59,32-59
3-90,24-89
37-98,42-99
5-66,7-67
28-46,12-29
31-73,72-73
15-75,15-96
12-17,14-16
92-93,8-93
7-79,5-45
21-32,22-29
23-26,22-95
2-80,4-80
11-95,77-95
69-98,60-96
67-84,67-73
81-91,91-92
38-86,37-85
11-11,9-30
1-28,2-52
20-72,20-72
4-57,3-98
37-40,36-40
24-81,23-23
20-90,3-89
43-45,1-44
2-77,6-77
11-99,98-98
63-90,62-85
3-95,2-98
18-21,22-43
20-77,21-77
53-81,45-84
79-87,82-87
1-87,62-90
34-73,35-84
56-86,17-86
13-88,14-84
17-83,17-82
24-98,24-59
8-99,9-27
72-88,88-88
9-93,9-82
59-69,63-91
20-26,20-25
1-89,90-92
59-97,42-81
41-79,40-42
28-76,28-77
33-84,31-83
42-74,42-75
22-98,23-96
17-30,16-62
28-33,29-83
3-97,98-98
7-38,5-38
61-69,35-68
77-77,4-78
40-44,36-46
35-86,34-35
57-93,56-72
37-86,5-86
24-76,23-76
1-13,13-14
69-97,6-97
6-7,7-37
1-24,2-6
36-99,7-97
10-25,24-97
32-90,90-91
2-99,2-94
14-92,6-91
24-45,23-57
32-55,42-47
46-85,46-47
4-76,3-19
80-80,80-95
69-70,68-70
7-96,1-81
68-99,49-96
22-98,23-27
64-96,65-97
24-51,23-50
32-33,33-84
20-98,19-98
17-19,18-92
2-98,2-98
26-73,22-85
36-89,23-87
65-86,86-97
17-76,16-91
25-48,25-37
42-92,38-92
3-90,3-56
91-98,85-92
38-54,39-54
7-97,7-99
31-95,31-32
27-94,26-28
57-85,23-84
52-54,53-87
45-62,61-62
16-17,16-75
14-97,15-98
1-83,82-82
1-93,26-93
12-97,10-96
87-87,1-88
29-92,30-93
21-23,22-90
7-78,11-79
7-97,12-95
8-96,7-98
11-79,24-80
20-35,7-31
39-46,39-39
38-89,39-89
33-60,7-63
3-59,2-2
21-34,21-88
1-78,2-77
61-71,53-71
21-53,22-53
47-97,46-97
47-48,47-69
79-82,72-81
94-96,1-95
28-29,28-62
51-98,51-59
9-58,58-84
11-74,32-74
80-80,13-79
12-98,12-94
47-96,48-97
50-97,96-99
65-66,44-88
38-71,70-99
22-65,22-23
12-56,49-55
10-10,10-58
30-43,39-62
61-94,3-62
60-87,12-60
12-96,12-98
1-29,13-32
7-8,7-73
27-74,27-74
66-74,65-75
53-73,72-73
2-3,3-98
9-18,39-46
1-59,3-98
9-39,8-97
54-54,39-55
23-73,41-91
48-89,48-91
17-40,7-41
77-89,51-78
22-80,21-80
9-57,19-69
28-61,29-60
98-98,17-99
11-83,11-83
32-60,39-61
11-62,11-12
31-94,25-85
2-13,1-7
8-95,7-7
62-70,91-94
38-98,38-98
33-94,33-78
6-70,29-71
14-95,2-61
25-25,25-44
44-70,36-52
19-19,20-27
20-75,5-76
2-94,93-93
37-40,38-40
2-97,2-2
57-78,56-84
40-88,10-59
91-96,30-91
1-1,2-63
21-83,20-20
49-91,48-92
45-58,5-51
21-21,22-90
95-97,7-96
1-2,1-45
31-70,32-70
35-36,1-35
12-95,12-86
63-97,63-97
11-34,10-27
21-81,20-21
80-83,74-79
5-99,98-98
27-38,26-87
18-43,19-42
3-91,2-7
12-77,12-96
67-76,51-75
78-82,7-78
47-95,46-95
12-84,13-45
35-65,35-75
71-71,13-72
2-99,98-98
6-82,5-7
49-50,50-52
3-98,1-98
61-61,22-61
27-42,29-41
5-21,20-80
51-82,50-50
2-11,4-10
10-83,10-98
33-33,2-33
1-2,1-97
17-96,3-95
45-77,46-59
18-58,17-18
41-94,93-94
2-97,95-99
67-82,64-82
1-94,1-94
5-23,2-24
16-20,6-20
3-6,5-93
21-35,20-45
4-94,59-98
53-86,40-54
6-8,7-95
1-97,1-1
26-46,36-47
82-82,52-82
21-38,22-34
88-94,20-94
96-97,1-96
36-48,35-48
65-65,66-90
2-97,1-2
80-97,80-92
74-87,68-81
69-82,70-79
87-96,4-95
91-91,33-90
17-73,73-74
22-22,21-98
44-63,45-64
37-55,51-61
51-56,56-56
29-83,28-84
69-70,69-97
22-24,23-30
7-7,8-90
8-32,3-31
79-79,79-91
4-98,5-80
1-99,2-70
11-52,12-12
35-88,36-88
31-92,30-93
8-34,33-82
3-97,99-99
5-86,54-70
5-98,3-99
4-93,1-4
15-92,14-91
9-94,13-94
2-98,2-30
9-95,29-95
59-94,60-95
36-95,81-98
17-95,18-96
3-26,25-84
32-99,13-75
9-44,8-97
3-96,96-99
21-21,20-59
14-56,28-55
19-35,35-84
82-83,56-83
89-89,78-89
24-64,23-57
71-71,39-72
84-91,90-94
18-98,19-98
12-70,13-71
75-95,94-95
72-79,72-77
11-90,10-94
2-98,2-98
4-86,3-87
6-97,2-97
68-97,45-97
91-92,12-92
32-59,32-98
11-91,12-85
28-84,74-85
8-99,2-99
2-69,3-9
72-97,58-91
4-33,3-33
12-63,11-64
56-56,57-70
56-62,51-59
17-70,18-62
1-94,1-99
1-78,1-1
3-97,2-2

Part 1:
import std/strutils
import std/sequtils

proc run04_1(): string =
    const input = staticRead"../input/day04.txt"

    var ctr = 0

    let lines = splitLines(input).filter(proc(p: string): bool = p != "")

    for line in lines:

        let pairs = line.split(",")
        let sec1s = parseInt(pairs[0].split("-")[0])
        let sec1e = parseInt(pairs[0].split("-")[1])
        let sec2s = parseInt(pairs[1].split("-")[0])
        let sec2e = parseInt(pairs[1].split("-")[1])

        if sec1s <= sec2s and sec2e <= sec1e:
            ctr+=1 # sec2 in sec1
            echo "[", sec2s, "..", sec2e, "]", " fully in ", "[", sec1s, "..", sec1e, "]"
        elif sec2s <= sec1s and sec1e <= sec2e:
            ctr+=1 # sec1 in sec2
            echo "[", sec1s, "..", sec1e, "]", " fully in ", "[", sec2s, "..", sec2e, "]"
        else:
            echo "[", sec1s, "..", sec1e, "]", " no overlap ", "[", sec2s, "..", sec2e, "]"

    return intToStr(ctr)


when not defined(js):
    echo run04_1()
else:
    proc js_run04_1(): cstring {.exportc.} =
        return cstring(run04_1())
Result: 550

Part 2:
import std/strutils
import std/sequtils

proc run04_2(): string =
    const input = staticRead"../input/day04.txt"

    var ctr = 0

    let lines = splitLines(input).filter(proc(p: string): bool = p != "")

    for line in lines:

        let pairs = line.split(",")
        let sec1s = parseInt(pairs[0].split("-")[0])
        let sec1e = parseInt(pairs[0].split("-")[1])
        let sec2s = parseInt(pairs[1].split("-")[0])
        let sec2e = parseInt(pairs[1].split("-")[1])

        if (sec1s <= sec2s and sec2s <= sec1e) or (sec1s <= sec2e and sec2e <= sec1e):
            ctr+=1
            echo "[", sec2s, "..", sec2e, "]", " overlaps ", "[", sec1s, "..", sec1e, "]"
        elif (sec2s <= sec1s and sec1s <= sec2e) or (sec2s <= sec1e and sec1e <= sec2e):
            ctr+=1
            echo "[", sec2s, "..", sec2e, "]", " overlaps ", "[", sec1s, "..", sec1e, "]"
        else:
            echo "[", sec1s, "..", sec1e, "]", " nolap ", "[", sec2s, "..", sec2e, "]"

    return intToStr(ctr)

when not defined(js):
    echo run04_2()
else:
    proc js_run04_2(): cstring {.exportc.} =
        return cstring(run04_2())
Result: 931


made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript