Skip to content

Instantly share code, notes, and snippets.

@ming900518
Created July 29, 2023 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ming900518/1e39535ee2aa79e8e263cdfdbe73f893 to your computer and use it in GitHub Desktop.
Save ming900518/1e39535ee2aa79e8e263cdfdbe73f893 to your computer and use it in GitHub Desktop.
Advent of Code 2022 Day 4

Part 1 (Scala)

@main def main = {
    part1(input)
}

def part1(input: String) = {
  var count = 0;
  input.linesIterator.foreach((line) => {
    val (first, second) = line
      .split(",")
      .grouped(2)
      .map((elfs) =>
        (
          elfs
            .apply(0)
            .split("-")
            .grouped(2)
            .map((ranges) => {
              if ranges.apply(0) == ranges
                  .apply(1)
              then { List(ranges.apply(0).toInt) }
              else {
                Range(ranges.apply(0).toInt, ranges.apply(1).toInt + 1).toList
              }
            })
            .next(),
          elfs
            .apply(1)
            .split("-")
            .grouped(2)
            .map((ranges) => {
              if ranges.apply(0) == ranges
                  .apply(1)
              then { List(ranges.apply(0).toInt) }
              else {
                Range(ranges.apply(0).toInt, ranges.apply(1).toInt + 1).toList
              }
            })
            .next()
        )
      )
      .next();
    if first.containsSlice(second) | second.containsSlice(first) then {
      count += 1
    }
  })
  println("Day 4 Part 1: " + count)
}

Part 2 (Scala)

@main def main = {
    part2(input)
}

def part2(input: String) = {
  var count = 0;
  input.linesIterator.foreach((line) => {
    val (first, second) = line
      .split(",")
      .grouped(2)
      .map((elfs) =>
        (
          elfs
            .apply(0)
            .split("-")
            .grouped(2)
            .map((ranges) => {
              if ranges.apply(0) == ranges
                  .apply(1)
              then { List(ranges.apply(0).toInt) }
              else {
                Range(ranges.apply(0).toInt, ranges.apply(1).toInt + 1).toList
              }
            })
            .next(),
          elfs
            .apply(1)
            .split("-")
            .grouped(2)
            .map((ranges) => {
              if ranges.apply(0) == ranges
                  .apply(1)
              then { List(ranges.apply(0).toInt) }
              else {
                Range(ranges.apply(0).toInt, ranges.apply(1).toInt + 1).toList
              }
            })
            .next()
        )
      )
      .next();
    if (first intersect second).length > 0 then {
      count += 1
    }
  })
  println("Day 4 Part 2: " + count)
}
71-89,66-70
24-70,23-55
19-85,18-86
50-90,50-95
55-55,56-72
3-65,5-66
98-99,66-99
14-67,14-14
4-79,78-79
13-98,10-98
27-78,77-78
22-28,22-27
84-99,98-99
7-96,99-99
33-54,34-54
29-45,44-44
18-48,57-78
89-95,89-93
39-53,63-99
13-95,94-95
7-43,7-42
16-16,16-96
41-68,68-87
83-83,73-82
65-95,64-71
5-98,5-95
51-54,50-99
33-39,36-74
9-73,10-86
20-31,21-59
12-27,11-12
10-96,7-11
55-71,70-77
13-97,3-14
5-99,5-5
25-98,25-98
81-94,81-94
26-42,49-74
77-98,31-77
8-77,6-8
48-61,60-61
11-14,13-52
97-97,1-96
48-83,48-82
44-76,75-93
21-89,20-89
10-68,9-91
2-79,78-80
10-97,96-97
9-11,10-76
44-45,45-97
34-57,27-56
12-88,12-87
16-77,16-16
74-84,14-75
22-36,21-85
15-70,9-70
81-97,3-81
24-97,23-79
8-54,54-55
18-73,17-74
22-79,39-79
2-3,2-73
1-90,1-90
59-96,19-96
31-71,31-31
62-63,63-64
15-92,91-93
73-74,18-78
64-96,65-96
5-7,6-64
96-98,2-97
62-81,51-62
38-40,33-39
26-96,21-95
31-86,30-87
11-94,2-11
3-53,30-52
28-77,27-98
17-48,44-69
9-89,1-89
42-97,42-99
1-3,2-93
16-17,17-21
95-99,14-96
51-99,52-57
28-35,28-35
30-90,76-91
4-97,4-4
78-92,14-57
74-97,75-97
15-94,14-94
29-68,29-69
1-95,2-30
6-84,98-99
12-72,71-72
5-6,5-30
19-63,63-72
4-99,3-99
5-61,62-62
35-55,34-56
11-58,75-87
4-98,3-98
33-34,34-77
56-94,55-95
8-78,99-99
19-20,19-90
58-90,43-57
87-94,22-84
20-50,49-60
14-15,16-90
7-93,7-89
7-97,7-7
4-88,4-89
35-49,48-56
53-85,55-94
5-66,4-6
17-78,19-78
6-28,28-66
55-88,91-98
21-22,21-94
18-42,17-42
4-98,6-98
10-96,97-99
25-89,89-93
5-98,1-5
62-77,63-73
12-37,11-38
17-29,17-29
51-62,45-61
86-87,44-86
33-88,22-33
89-89,14-88
89-89,53-81
61-63,59-64
56-75,55-75
21-93,20-90
81-82,81-82
8-86,87-96
3-99,4-98
30-30,30-33
3-59,2-59
58-72,7-70
45-67,16-45
14-37,13-13
29-47,14-46
2-85,1-3
61-98,81-86
16-35,17-80
4-92,4-92
10-38,11-97
92-93,20-93
40-53,41-53
28-84,3-35
11-96,6-36
57-81,57-82
77-86,6-77
4-67,4-67
55-57,56-76
26-27,27-45
44-47,44-46
7-98,6-99
10-51,9-87
10-41,10-10
7-37,7-90
55-97,54-54
21-97,20-64
64-85,44-53
31-47,46-59
67-78,68-95
27-95,26-66
11-82,54-83
52-75,24-67
13-84,6-84
37-48,33-41
27-28,28-57
23-95,86-95
8-8,8-28
27-74,26-28
56-82,42-56
9-59,14-59
26-62,61-94
1-2,1-88
83-92,97-98
13-91,13-30
42-96,95-96
43-97,34-97
45-82,15-69
90-90,65-91
25-89,26-26
16-36,15-15
14-80,79-79
49-72,49-50
19-47,18-88
7-46,7-23
14-24,24-80
2-2,3-98
2-99,2-99
14-44,14-14
25-27,6-27
28-96,27-97
24-75,23-81
10-74,13-75
61-62,13-62
94-95,96-98
22-24,22-82
10-10,10-81
37-77,76-77
10-32,18-32
8-79,7-7
45-46,45-77
16-21,16-20
8-45,44-57
19-61,18-61
9-67,9-66
4-86,1-86
49-63,48-63
39-83,23-82
3-67,18-67
74-84,83-83
32-77,33-77
14-55,5-49
59-73,26-73
15-55,16-83
19-19,20-43
59-60,32-60
30-58,33-39
9-76,9-76
67-81,21-68
37-37,28-38
64-75,63-84
5-91,11-88
41-95,37-96
46-76,76-87
39-82,40-82
92-95,2-93
92-93,1-93
4-97,4-98
31-75,30-75
97-98,2-97
78-78,40-77
36-36,36-41
5-93,4-94
29-92,29-92
41-88,42-92
9-50,47-51
16-87,16-16
18-63,62-63
89-92,90-95
80-81,16-81
3-34,4-34
13-97,96-98
1-93,92-94
34-88,35-87
97-98,43-97
41-76,42-69
28-33,27-46
1-35,3-99
9-95,10-94
15-99,15-92
93-94,1-94
42-92,43-94
91-98,71-90
19-50,47-50
24-91,94-99
10-98,11-98
42-87,86-86
18-92,48-92
52-52,5-51
73-80,32-74
43-46,39-50
41-45,40-44
48-86,49-68
55-67,56-67
34-94,34-34
16-72,17-71
27-75,33-75
3-34,33-34
27-70,70-71
85-97,31-84
46-46,21-47
1-98,97-99
15-90,5-90
10-63,10-62
3-4,5-77
18-41,23-62
32-80,33-79
17-17,17-89
63-83,15-83
5-82,5-82
51-60,51-82
1-82,1-82
9-14,13-97
10-99,13-99
70-89,88-97
84-89,83-90
80-86,25-85
29-88,29-89
5-68,10-68
95-96,47-78
26-95,26-96
1-2,2-87
32-57,21-58
3-99,2-3
93-93,50-94
62-96,62-89
52-82,52-81
27-82,27-27
39-79,38-39
17-97,16-97
2-90,1-91
43-48,43-52
5-25,9-25
4-95,4-5
14-59,13-59
44-81,43-43
95-96,20-96
13-14,11-14
26-91,20-91
23-64,64-64
20-95,21-21
4-85,4-84
17-60,15-60
79-80,66-79
78-95,78-90
34-38,33-38
41-49,48-49
4-44,3-44
68-68,10-69
4-83,82-84
86-88,12-87
2-37,36-75
7-57,58-81
11-13,12-99
6-93,5-90
28-84,29-56
6-93,3-3
35-63,36-72
7-7,7-55
27-44,19-44
41-86,30-80
1-82,2-81
1-3,3-92
2-10,4-52
12-98,8-11
5-10,4-27
47-79,46-46
47-96,97-98
74-82,74-75
36-95,11-35
10-18,17-24
32-34,33-48
9-83,9-83
7-92,19-92
12-92,12-86
7-81,7-82
24-74,38-73
19-26,1-26
5-11,4-11
13-82,14-81
33-98,32-33
30-97,96-96
28-79,79-81
60-91,59-86
50-94,50-94
74-98,57-73
5-85,4-37
4-90,3-71
22-76,22-76
48-51,51-65
4-56,5-55
2-72,46-71
18-99,17-93
98-99,3-98
14-66,14-79
12-73,13-72
16-19,2-17
2-98,3-98
65-97,65-96
9-38,37-38
9-55,10-95
49-65,26-66
7-85,7-86
18-20,7-19
33-73,33-73
18-89,35-56
1-81,1-71
18-65,17-65
84-97,59-94
10-73,72-73
2-90,3-66
13-41,4-29
1-99,86-99
20-96,19-68
84-85,83-85
27-92,27-92
20-70,19-19
11-96,12-97
35-86,34-36
8-9,8-22
9-72,8-72
5-89,5-57
46-58,57-59
41-41,42-75
32-84,31-88
39-91,90-91
6-39,5-60
51-69,70-83
5-78,32-75
8-92,8-8
2-3,4-81
70-95,14-95
10-15,16-72
26-64,29-63
26-83,27-82
48-93,44-47
76-90,76-91
73-99,74-74
9-71,8-41
13-13,13-91
32-81,32-80
9-95,9-95
97-99,84-96
90-91,52-90
2-96,2-97
73-99,2-97
6-33,7-27
9-67,10-67
47-48,47-59
3-87,87-99
35-53,49-64
40-98,41-93
52-86,33-85
23-55,89-91
29-90,91-96
5-69,5-69
27-29,28-68
14-33,81-88
2-79,79-98
34-92,93-94
1-66,2-66
8-96,7-97
12-96,97-98
78-92,10-88
86-86,2-87
11-65,2-64
19-42,20-41
2-34,34-93
36-42,36-43
10-86,11-78
2-22,2-71
8-20,19-95
5-99,6-89
6-18,6-6
3-6,10-81
69-71,69-69
52-62,52-62
79-98,79-98
49-93,49-70
10-97,10-97
23-32,12-24
7-8,8-94
37-98,97-97
2-80,2-79
33-82,18-98
53-65,54-90
68-95,68-96
7-8,7-14
24-92,24-91
31-36,32-48
13-83,13-82
23-54,53-53
43-52,10-43
24-66,23-94
19-51,49-68
66-72,39-66
23-67,66-67
34-80,48-79
83-96,33-82
79-90,33-95
10-96,9-78
12-97,96-99
10-44,13-96
2-94,93-93
5-94,5-94
71-95,74-97
60-95,59-96
86-95,14-86
4-99,4-54
12-47,13-13
23-34,22-34
49-96,49-86
37-59,37-59
1-4,3-94
9-91,9-91
57-99,45-57
8-13,11-13
11-85,84-85
47-92,48-92
4-89,5-89
4-30,29-31
10-65,9-64
60-63,62-92
43-78,23-42
16-46,45-47
26-95,95-96
63-63,12-64
21-47,20-47
14-97,20-98
6-90,6-86
71-83,13-72
64-91,83-90
5-8,5-7
42-84,41-98
30-59,29-60
6-92,5-95
5-39,4-40
71-85,16-70
2-92,2-91
8-91,20-91
72-99,3-71
31-82,97-98
78-89,77-90
48-89,47-89
63-64,64-64
24-74,73-88
1-97,98-99
73-82,72-82
13-29,30-98
11-41,11-12
36-53,33-52
46-90,66-89
4-7,8-87
5-45,46-91
11-39,12-78
42-93,41-85
40-91,40-91
23-27,23-51
3-20,3-21
64-91,48-91
57-62,86-91
5-21,22-89
45-86,43-86
94-97,54-93
13-61,60-60
25-98,19-97
34-37,36-37
97-98,16-96
4-96,24-95
2-55,1-92
62-65,61-73
21-52,20-21
43-85,85-86
2-95,2-95
10-92,9-92
8-99,9-98
23-76,77-90
18-18,1-19
47-59,46-60
78-84,77-81
23-98,24-96
2-61,1-60
37-57,37-56
58-74,58-58
28-68,27-67
16-84,17-84
1-36,1-36
62-63,14-63
22-99,23-98
1-86,85-86
47-98,46-82
4-72,5-72
41-82,41-79
64-95,64-93
14-98,13-95
38-83,35-83
69-70,26-69
1-99,2-72
18-20,19-82
12-34,11-34
68-94,95-95
59-89,89-90
71-73,73-75
58-78,18-92
32-73,21-33
31-91,90-91
28-90,89-90
54-56,77-94
15-15,16-79
57-88,24-57
8-96,7-82
5-50,5-50
34-53,54-54
75-75,62-76
1-99,2-49
4-4,3-61
16-51,17-52
19-99,4-44
20-75,19-20
36-57,21-35
9-99,9-96
15-61,38-61
98-98,99-99
2-94,13-94
15-52,16-51
10-98,12-98
1-44,1-89
4-97,3-97
20-36,21-36
28-29,29-79
1-76,1-74
69-69,69-94
47-47,47-59
15-22,21-91
57-57,57-80
11-11,11-35
6-59,58-64
15-70,15-71
52-79,51-64
60-65,55-64
20-55,21-54
26-75,99-99
82-82,48-83
4-81,82-82
12-45,44-45
3-80,2-81
11-98,1-12
21-41,35-41
41-93,40-40
87-99,1-87
11-82,11-82
5-86,4-8
93-93,5-77
11-94,1-10
38-58,39-58
63-64,35-62
2-98,3-56
9-75,76-77
52-85,52-85
32-36,32-36
44-87,43-84
4-85,84-85
85-96,12-95
2-66,26-66
23-33,8-22
16-92,15-80
72-86,72-86
8-42,7-7
14-53,15-53
4-78,8-78
41-98,27-99
33-40,4-39
12-78,13-89
9-93,10-93
91-98,92-98
30-90,29-69
33-50,2-34
22-89,23-89
28-93,93-99
5-45,4-45
12-85,86-91
52-99,52-99
14-81,82-92
3-98,4-69
64-92,8-99
1-90,5-90
38-77,26-91
49-65,35-42
3-94,4-74
4-58,4-4
2-96,95-97
2-99,3-98
21-94,20-95
11-76,12-75
2-96,92-95
44-76,41-76
5-93,6-92
85-90,20-89
5-13,12-28
97-97,96-98
34-87,34-88
14-27,14-35
79-80,17-79
6-23,22-52
5-78,72-83
25-80,26-46
51-79,90-97
28-80,77-80
87-90,24-89
89-90,71-90
16-96,16-23
32-86,31-31
9-83,9-10
46-53,52-53
28-93,92-93
41-50,40-48
48-64,47-71
5-23,3-6
44-85,7-85
30-57,29-58
5-95,8-90
6-95,12-95
29-95,29-99
15-44,14-14
28-94,28-97
8-8,7-8
35-35,36-67
35-88,71-93
89-90,27-90
89-91,30-90
57-82,56-57
24-74,19-74
2-95,2-95
55-91,54-91
86-89,87-91
30-81,30-81
52-77,53-66
5-91,75-90
41-77,50-77
52-73,34-69
33-37,32-37
40-71,74-85
9-34,33-33
95-95,25-96
12-50,51-95
16-65,53-60
15-75,16-27
6-33,6-7
10-30,36-57
10-60,2-60
28-58,57-94
16-62,15-62
96-98,10-97
19-28,8-47
4-94,93-93
2-96,95-96
58-60,36-59
8-62,13-98
21-96,21-34
11-50,11-50
49-93,95-97
6-76,8-99
65-99,65-92
13-66,3-54
13-95,94-94
57-96,57-96
42-85,57-84
26-81,25-82
54-98,54-86
3-84,83-92
7-72,8-51
67-83,67-71
6-34,14-39
1-47,1-47
97-97,12-94
2-89,2-98
12-16,15-87
30-59,31-59
78-82,80-83
14-69,9-70
9-97,9-99
45-68,44-69
24-51,47-48
48-48,48-68
75-77,47-53
7-73,7-73
9-72,8-66
39-70,38-71
39-69,68-98
26-94,26-93
14-63,15-62
19-61,61-62
7-15,11-99
1-73,2-51
44-90,90-91
54-97,96-98
1-93,11-20
34-70,35-69
11-11,11-87
8-73,8-73
15-23,24-85
42-57,43-99
23-29,13-67
99-99,1-54
65-66,21-65
23-38,23-75
84-94,18-94
10-63,11-62
33-35,34-38
68-70,4-69
4-8,7-94
20-95,20-54
11-99,4-44
20-97,3-97
7-85,85-96
44-85,45-84
9-17,5-10
82-97,22-81
4-98,3-98
17-80,17-75
22-49,22-49
94-99,27-91
49-91,17-50
20-30,36-49
8-84,83-84
59-91,60-73
76-93,2-97
5-48,5-94
22-67,21-21
3-4,4-5
20-95,21-99
21-86,85-87
38-44,36-43
2-90,91-99
60-67,3-60
3-58,4-57
46-84,46-82
13-13,14-95
30-74,73-74
24-34,24-34
4-98,5-98
6-7,2-6
41-98,42-98
16-58,57-85
86-90,61-86
2-85,2-84
7-90,90-93
25-65,26-30
41-69,12-70
31-88,32-87
10-87,10-87
4-29,4-98
8-40,30-40
26-82,25-82
89-90,88-89
4-13,9-14
10-65,6-7
6-13,13-99
75-79,45-79
6-98,9-89
6-55,54-58
2-78,25-78
3-98,3-98
95-95,6-94
9-78,77-77
56-57,56-58
49-87,24-66
47-47,47-73
33-35,6-34
30-80,36-80
30-30,31-81
99-99,68-74
2-99,3-98
14-92,10-15
16-31,18-51
95-97,2-94
31-31,15-32
98-99,68-93
14-71,14-15
5-39,14-38
47-54,53-55
37-64,37-37
58-78,10-58
40-88,41-41
68-99,16-94
25-25,26-58
84-87,1-99
72-87,72-78
49-81,48-49
1-99,2-99
24-91,25-93
12-81,11-58
1-16,15-50
12-26,12-26
17-80,17-79
94-96,38-95
1-84,83-84
20-82,20-20
3-86,2-95
19-98,60-94
9-45,45-50
8-57,9-12
15-18,18-83
1-1,1-15
85-96,84-95
1-40,40-78
57-58,30-58
48-90,57-89
26-51,27-86
2-86,85-86
25-78,26-77
82-99,81-97
1-14,13-97
76-96,75-97
66-94,64-97
34-60,38-61
80-95,22-81
51-80,51-80
2-78,1-78
43-46,45-69
75-85,76-76
33-65,32-32
15-96,14-96
68-86,67-71
12-26,12-16
1-51,3-43
87-88,27-88
17-99,16-99
9-36,20-24
25-32,24-32
32-32,32-76
77-92,52-78
4-79,4-82
38-61,54-59
38-99,38-99
94-98,54-95
80-96,2-95
8-97,8-97
7-28,8-22
66-82,83-91
33-48,49-83
3-3,4-83
76-90,77-90
34-62,34-61
40-87,16-27
1-98,2-98
55-96,95-98
5-99,5-5
36-97,10-98
12-94,93-94
72-78,71-78
13-18,14-18
47-76,75-76
21-96,21-95
69-96,68-87
27-64,28-62
19-40,25-39
4-59,4-98
10-66,10-67
16-82,17-82
7-99,3-99
18-30,17-92
86-99,24-52
19-77,20-77
97-97,93-94
11-33,10-11
84-96,53-95
3-85,4-91
15-52,16-53
86-90,52-56
33-76,33-76
3-98,17-92
79-79,4-80
85-85,2-84
13-94,94-96
60-84,90-93
93-97,15-98
9-54,10-53
28-59,27-98
12-85,11-65
59-67,58-67
20-50,20-65
63-65,16-64
76-96,75-75
23-91,24-91
3-99,3-97
25-48,26-48
48-48,49-87
15-96,1-16
9-79,25-78
52-61,62-84
6-85,7-84
54-77,53-78
1-99,1-99
59-96,60-96
12-45,12-46
22-87,3-87
36-98,36-74
56-56,22-57
94-95,99-99
7-92,8-83
13-94,14-93
22-90,89-89
46-94,83-87
11-76,75-78
3-94,3-95
4-14,5-52
90-92,66-83
46-61,46-47
22-23,22-90
47-47,48-53
89-91,16-90
42-93,93-94
9-61,60-94
1-1,1-67
3-38,2-99
46-98,47-97
96-97,1-95
8-54,55-92
52-72,53-71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment