~macaptain/advent-of-code

513325c2b655fd3bcbeb7e61916aec0d6e70d338 — Michael Captain 2 months ago b3d22b2 master
Complete 2022 Day 4
A src/main/advent2022/Day04.kt => src/main/advent2022/Day04.kt +26 -0
@@ 0,0 1,26 @@
package advent2022

fun sectionRange(s: String): IntRange {
    val (start, end) = s.split("-").map { it.toInt() }
    return start..end
}

fun solve4a(lines: List<String>): Int {
    return lines.count { line ->
        val (s1, s2) = line.split(",").map { sectionRange(it) }.map { it.toSet() }
        s1.containsAll(s2) || s2.containsAll(s1)
    }
}

fun solve4b(lines: List<String>): Int {
    return lines.count { line ->
        val (s1, s2) = line.split(",").map { sectionRange(it) }.map { it.toSet() }
        s1.intersect(s2).isNotEmpty()
    }
}

fun main() {
    val input = input("day04")
    println(solve4a(input)) // 524
    println(solve4b(input)) // 798
}

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

A src/test/advent2022/Day04KtTest.kt => src/test/advent2022/Day04KtTest.kt +25 -0
@@ 0,0 1,25 @@
package advent2022

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

internal class Day04KtTest {
    private val exampleAssignmentPairs = listOf(
        "2-4,6-8",
        "2-3,4-5",
        "5-7,7-9",
        "2-8,3-7",
        "6-6,4-6",
        "2-6,4-8",
        )

    @Test
    fun solve4a() {
        assertEquals(2, solve4a(exampleAssignmentPairs))
    }

    @Test
    fun solve4b() {
        assertEquals(4, solve4b(exampleAssignmentPairs))
    }
}