summaryrefslogtreecommitdiffstats
path: root/2022
diff options
context:
space:
mode:
Diffstat (limited to '2022')
-rw-r--r--2022/day4/example6
-rw-r--r--2022/day4/input1000
-rw-r--r--2022/day4/part1/Cargo.toml9
-rw-r--r--2022/day4/part1/src/main.rs50
-rw-r--r--2022/day4/part2/Cargo.toml9
-rw-r--r--2022/day4/part2/src/main.rs49
6 files changed, 1123 insertions, 0 deletions
diff --git a/2022/day4/example b/2022/day4/example
new file mode 100644
index 0000000..9f9e9cf
--- /dev/null
+++ b/2022/day4/example
@@ -0,0 +1,6 @@
12-4,6-8
22-3,4-5
35-7,7-9
42-8,3-7
56-6,4-6
62-6,4-8
diff --git a/2022/day4/input b/2022/day4/input
new file mode 100644
index 0000000..20a83ca
--- /dev/null
+++ b/2022/day4/input
@@ -0,0 +1,1000 @@
130-31,2-31
26-92,4-5
335-39,42-44
435-95,35-95
512-98,99-99
610-15,16-94
737-85,84-84
82-23,31-56
952-68,51-69
1047-62,25-52
119-62,61-61
1278-99,77-99
1392-94,11-93
1477-83,62-76
153-7,29-59
1612-96,96-97
1748-48,12-47
1872-86,71-86
196-90,90-95
205-92,2-4
2111-63,1-64
2272-72,72-82
2365-68,68-69
243-99,5-99
255-80,5-5
2640-93,41-93
2727-31,25-58
286-93,93-93
2925-75,75-76
3031-78,79-79
3116-60,33-86
3238-65,39-97
3346-52,45-88
3413-64,12-80
3553-53,35-52
3632-38,12-30
3743-49,42-96
384-23,24-77
3932-63,27-64
4046-72,23-73
4178-89,56-88
4242-78,79-79
432-87,2-88
4451-57,57-58
4556-63,55-63
462-12,21-62
475-7,6-41
4857-86,87-92
4917-18,17-42
5011-84,7-10
5113-75,4-75
5215-76,14-14
5396-96,2-97
5468-94,43-95
5557-66,67-85
565-52,20-88
5725-62,25-62
583-82,81-83
5988-89,85-88
6031-49,30-68
6158-60,59-61
6216-33,15-16
6313-97,13-66
644-87,10-86
656-6,7-99
6633-35,34-98
6748-88,82-89
6831-73,30-74
691-95,2-95
7011-87,10-86
7160-61,21-60
7251-91,51-52
7315-37,16-38
745-10,10-32
7523-25,24-34
7621-40,21-40
7712-52,13-31
7810-10,10-94
7941-82,4-82
805-48,6-34
8173-81,42-72
8262-77,76-82
8341-82,56-83
8419-52,18-39
8519-92,54-93
862-39,2-40
8795-96,3-95
887-70,6-92
897-89,6-58
9067-88,68-80
9114-74,73-86
9241-41,33-40
932-91,8-90
9469-81,11-80
958-90,9-98
9662-87,86-86
977-70,39-69
984-9,3-12
994-96,11-95
1001-21,3-22
10118-24,92-99
10234-87,7-90
10396-99,82-88
1046-50,6-34
10513-53,12-54
1062-98,3-95
1078-11,17-91
10826-92,3-56
10936-61,46-84
11086-86,22-85
11120-76,76-76
11221-80,21-30
11334-78,55-90
11412-96,11-95
11543-43,15-44
11654-88,53-89
11737-37,50-66
11823-95,7-11
1197-58,21-59
1203-5,4-68
1215-75,8-76
12242-68,42-67
12336-36,36-47
1249-69,9-92
12530-56,36-80
12612-85,13-84
12711-64,65-87
12841-60,60-61
1292-30,31-41
13023-60,9-35
13187-89,15-88
1324-97,96-98
1339-92,9-91
13440-66,66-66
13539-95,69-96
13648-87,23-47
1375-96,6-95
13818-66,19-65
1395-38,39-61
14012-92,93-93
1419-97,10-97
14216-51,50-67
1433-14,14-28
1447-7,8-88
1456-45,5-36
14658-58,40-58
1478-56,7-56
1485-15,6-14
14997-97,7-96
15026-70,27-94
15120-21,20-91
1523-16,3-13
1539-10,9-92
15416-81,67-94
15572-88,6-73
1564-88,3-68
15725-53,24-54
15863-75,63-74
15931-31,31-56
1601-84,1-83
16141-89,40-67
1624-53,5-54
16313-83,13-14
16411-87,11-86
1654-63,4-63
1669-96,8-97
1672-74,1-88
16840-83,39-83
16946-71,45-75
1704-74,74-75
17199-99,72-98
17233-51,52-82
17322-69,22-23
17435-36,36-91
1758-97,97-98
17618-20,19-94
1776-16,7-17
17835-36,36-97
1794-78,79-79
18014-69,4-14
18150-64,19-51
18210-31,15-17
18352-74,74-74
18442-91,90-91
18535-69,10-70
1861-3,4-4
18737-66,67-67
1886-92,73-74
18921-96,21-97
1906-37,6-68
19125-78,79-92
19278-88,65-75
19314-99,8-8
19439-66,54-67
19533-80,79-91
19615-83,84-84
19751-51,10-50
19872-80,72-81
1994-92,2-24
20033-93,92-94
2014-10,4-68
2028-94,7-96
2031-39,38-69
2046-36,36-37
20586-86,31-85
20677-94,76-96
2078-75,17-76
2087-93,7-45
2098-61,40-70
21017-29,32-94
21126-26,12-25
21214-94,10-12
21329-42,32-39
2142-91,2-3
21512-85,28-86
21657-70,71-90
2179-16,17-87
21817-55,18-55
21918-98,19-19
22088-88,22-87
2215-93,4-92
22275-76,76-84
2235-6,6-41
22442-94,36-41
22552-66,1-62
2265-82,4-4
2271-87,3-88
22847-58,56-61
2297-57,7-97
23039-93,33-93
23131-48,20-32
23218-81,19-82
23316-77,17-78
2342-2,4-43
23522-23,23-93
23610-11,15-68
23789-95,15-88
23819-69,18-18
23985-98,34-84
24047-73,29-52
24156-56,23-55
2422-97,2-94
24334-64,17-63
24446-46,47-75
24515-90,91-91
24663-63,35-62
24746-56,52-89
24882-90,82-90
24917-23,5-7
2507-79,8-79
25140-76,39-71
25281-82,11-99
25331-41,37-44
25497-98,1-98
25513-92,11-16
25663-63,3-62
25710-23,27-46
25818-99,19-80
25943-65,55-56
26014-14,14-20
26114-75,15-74
26224-75,12-45
26332-32,33-83
2644-92,2-3
26535-50,34-72
26611-90,91-91
26762-84,78-97
26864-64,24-65
26913-93,12-94
27070-89,58-88
27135-44,36-43
27257-90,91-91
27319-20,20-96
27433-96,26-87
2759-61,10-62
2762-81,1-3
27769-98,69-98
27860-66,64-97
2798-8,4-7
28033-76,32-77
28135-96,34-86
28287-92,66-86
2835-39,8-67
28427-78,79-80
2855-98,1-1
2866-79,5-95
28765-87,88-88
28850-98,49-49
28955-58,54-60
29013-97,12-96
29174-74,1-73
29212-61,64-74
29310-98,10-11
29468-70,17-69
29541-58,13-65
2967-77,6-77
29750-95,41-49
2984-68,2-4
29914-54,14-40
30059-93,60-82
30121-95,26-95
30241-81,41-62
30341-89,40-88
30421-39,22-40
30530-94,29-88
3061-93,1-93
3075-78,4-79
30815-86,85-87
30989-90,17-89
31047-85,48-85
31184-84,83-85
3126-98,6-6
3133-92,3-93
3149-9,8-60
31536-90,6-35
31657-61,56-58
3174-92,3-93
3187-69,8-69
31978-78,4-79
32052-93,53-93
32130-90,30-91
32258-78,77-79
32321-41,42-53
32416-37,6-38
3252-77,3-77
3266-55,17-69
32736-73,37-71
32893-94,26-93
32922-79,22-79
33011-92,91-93
3311-62,3-65
3329-65,8-64
33311-32,7-33
3345-94,2-5
33580-82,80-81
33646-77,45-76
3372-98,1-99
3382-4,6-68
33956-83,55-82
34012-14,13-98
34178-90,5-80
3423-69,2-2
34383-83,76-82
3443-99,1-1
34528-72,28-50
34657-67,32-54
3474-99,7-98
34818-18,19-48
34979-85,84-84
35083-89,81-87
35131-89,30-87
35249-72,49-71
35328-29,28-51
35448-97,1-47
35513-45,8-12
35624-75,25-75
35763-89,76-86
35816-23,15-16
35925-49,26-48
36017-94,16-88
3616-95,7-96
36287-90,86-89
36311-93,3-95
3646-66,5-65
36597-99,39-98
36623-66,24-65
36754-56,55-70
36892-97,26-90
3697-86,7-87
37060-78,77-79
37190-95,71-94
37232-87,33-87
37313-13,14-72
37412-86,6-87
37524-44,45-75
3765-96,5-97
3772-83,7-79
37895-97,1-90
37972-80,73-75
38011-85,12-86
3817-50,8-8
38235-99,55-79
38356-98,57-99
3846-87,7-87
38528-72,27-71
38620-77,78-83
3878-67,68-83
38810-68,9-9
38935-63,35-64
39090-90,90-90
3915-97,1-99
39210-93,9-19
3936-90,4-77
39446-75,80-97
3959-77,10-82
39673-88,74-81
39743-65,43-44
39866-82,66-81
39962-63,63-77
40035-76,11-77
40122-93,89-90
4023-96,2-3
40311-29,2-33
40475-82,62-81
40546-47,46-53
4069-99,8-98
40732-93,33-87
40847-51,52-60
40918-83,84-84
41031-70,31-69
41126-93,19-92
41293-93,4-92
4132-14,4-35
4141-98,36-99
4157-83,82-96
41695-95,32-95
41734-39,9-38
4182-63,62-93
41924-98,99-99
42026-50,89-93
42182-97,35-81
42222-33,16-33
4231-5,11-66
4249-63,9-63
42536-36,36-77
4265-98,6-99
42722-93,50-92
42889-92,89-93
42948-90,91-91
4306-88,7-89
43127-38,38-84
4329-87,11-40
43324-83,23-82
4341-69,1-68
43516-67,62-65
43613-15,1-85
43741-65,40-42
43819-77,20-77
43944-93,45-93
44027-94,94-97
44112-97,12-96
4429-38,8-48
44322-49,6-49
44412-18,13-52
44514-52,13-14
44612-51,13-52
44710-36,37-37
44814-72,13-15
4492-99,72-81
45021-34,22-22
45186-86,60-85
45220-36,20-20
45310-96,11-96
45414-24,15-23
4558-96,8-95
45616-22,22-23
45732-90,33-97
4587-31,32-67
45935-68,22-77
4603-69,5-69
46113-62,12-61
46226-32,27-32
46368-77,67-76
4645-99,6-98
4652-56,8-65
46696-99,11-67
4673-25,15-24
46825-91,84-90
4699-39,10-38
47024-94,23-24
47178-80,29-79
4724-47,5-85
47339-81,81-82
47433-33,13-32
47531-69,32-83
47619-95,17-21
47775-75,74-74
47814-89,15-98
4794-99,6-98
48060-90,59-66
48150-72,46-73
48226-84,25-49
4839-77,8-77
48448-99,49-94
48591-95,10-90
4867-63,24-64
48779-85,11-80
48898-99,20-82
4892-98,1-97
49056-68,54-67
49130-30,31-87
49289-96,58-88
4936-56,3-5
49412-49,15-62
49526-33,26-66
49618-18,19-98
49713-27,14-26
49812-15,3-19
4995-98,46-98
5004-89,3-90
5015-15,5-19
50285-88,45-87
50394-97,97-98
50424-34,34-77
50555-56,2-55
5066-86,7-87
50742-50,43-50
50816-57,17-58
5093-22,2-3
51056-57,56-96
5115-91,82-92
51230-53,53-54
51312-25,13-47
51472-90,90-91
51515-49,49-89
5165-81,6-37
51757-58,58-82
51811-65,2-10
51941-97,42-96
52055-71,54-70
5214-81,1-98
5222-65,1-49
5237-45,6-45
52498-98,8-98
5258-93,41-92
5261-90,2-98
52714-93,43-92
52866-97,98-98
52936-87,87-88
5302-62,4-61
53122-76,23-76
53249-54,50-55
53350-53,52-94
53445-95,75-96
53522-77,78-78
53671-73,72-86
5379-83,10-82
5381-56,53-54
53971-71,63-70
5407-73,7-8
5416-9,6-66
54210-98,97-98
5434-50,50-50
5447-95,7-91
5456-7,7-96
54673-73,1-73
54743-68,44-77
54826-30,25-31
54992-92,9-92
55088-89,1-88
55112-97,96-98
55291-91,59-90
5534-9,3-16
55416-97,96-96
55550-89,49-90
55694-96,84-95
55755-71,70-72
55816-90,5-16
5591-96,3-95
5605-35,7-34
56116-72,16-54
5623-99,1-2
5638-87,10-88
56410-98,10-98
5658-67,4-69
56623-79,72-72
56717-74,17-75
56857-77,77-78
56974-84,85-98
57022-64,30-65
5713-73,1-72
5721-78,31-91
5734-93,4-94
57451-88,51-79
5755-19,19-99
57668-68,31-67
5772-98,3-3
57848-63,47-64
57967-71,72-73
5809-42,9-54
5812-50,2-39
58253-53,3-54
58336-37,36-69
58463-74,64-73
58514-65,13-65
58641-94,98-98
5871-60,6-99
58895-98,80-94
58971-74,32-78
59031-31,31-31
5916-7,6-33
5925-95,96-98
5935-93,6-94
59452-88,51-89
59555-76,55-77
5964-98,3-96
59759-89,58-94
59810-88,9-86
59913-97,13-96
60018-97,10-16
6017-98,4-97
60284-85,14-84
60326-88,76-87
60452-72,71-81
60538-52,39-52
60622-88,1-89
6077-97,2-7
6084-25,3-25
60943-73,42-74
61026-26,25-47
61134-84,40-84
6128-69,7-68
6132-52,1-85
61452-60,52-61
61538-71,37-70
61628-86,27-85
61799-99,7-98
61845-67,8-81
6197-93,7-58
62037-56,28-56
62129-99,66-98
62278-98,7-97
6234-72,3-77
6241-3,2-73
6251-14,2-35
62612-13,12-95
62740-84,84-84
62812-56,28-47
6293-96,1-39
63058-74,74-74
63124-32,24-32
63211-91,5-10
63341-69,36-97
6349-92,8-59
63540-51,39-52
63697-99,16-96
63734-96,33-69
63868-86,30-87
63945-96,44-98
64012-89,6-12
6415-81,32-89
6425-8,6-9
6434-42,1-2
64432-42,79-96
6455-96,4-95
64654-62,12-62
64730-86,86-86
64850-58,58-58
64941-61,40-62
6501-99,2-99
65121-52,20-20
65217-69,1-17
6533-97,2-96
65435-37,36-57
6558-32,9-11
6563-4,4-99
65792-96,72-92
6582-3,2-90
65934-40,41-69
66081-98,65-80
66112-57,58-58
66239-40,14-39
66347-77,78-93
6645-5,6-91
66524-87,86-93
66631-80,30-80
66732-82,33-55
66861-75,62-74
66916-98,16-99
67019-21,22-86
67154-54,16-53
67240-74,21-28
67320-28,28-76
67494-96,47-71
67525-74,74-75
67639-81,3-67
6775-97,2-52
67821-63,62-62
6797-53,6-84
68060-88,59-60
68123-61,24-61
68276-93,77-94
68317-30,30-86
68410-23,9-77
6856-90,5-96
68638-69,37-39
6871-3,4-94
68873-74,3-74
68970-83,41-84
69072-79,78-83
69156-76,15-67
69292-92,6-93
6932-90,89-89
69419-19,20-90
69518-79,17-80
69611-11,2-16
69795-95,57-95
69851-93,52-86
69947-52,53-96
7001-5,13-43
70144-63,62-64
70243-97,42-42
70346-54,45-53
7047-61,8-21
70518-79,41-80
70654-56,28-55
7077-54,11-54
70812-61,62-62
70980-81,80-94
71038-39,38-76
7118-94,26-83
7123-66,2-4
7136-27,7-26
7142-96,71-86
71567-99,66-98
71628-73,27-90
7172-93,1-93
71880-95,72-94
71988-90,24-89
72093-93,22-93
72136-86,51-92
72225-63,24-63
72390-91,12-84
7249-46,10-46
72531-31,17-31
72651-52,49-51
72714-18,2-12
72872-73,64-73
7296-82,10-81
73058-84,15-15
73151-78,52-68
73254-56,60-68
73378-79,6-78
73496-96,95-96
73528-81,43-82
7365-95,5-5
7372-17,1-99
73810-94,9-61
73985-87,3-91
7402-73,14-72
74195-95,21-94
74217-57,37-81
74395-95,4-95
74428-84,12-93
7454-98,1-1
74696-97,7-97
74760-99,61-99
74897-97,7-98
74973-74,5-74
75059-94,60-60
75183-85,84-91
7522-5,5-40
75329-79,28-78
75437-67,26-54
75528-39,10-41
75684-84,85-85
75719-69,12-68
75829-31,30-32
75958-77,57-59
76017-25,24-26
76137-80,36-81
76216-84,16-97
7633-97,1-96
7647-63,7-64
7657-54,6-54
76614-47,13-46
76713-64,55-67
76819-36,35-35
76967-80,67-79
7708-66,5-35
77156-93,44-55
7726-74,15-90
77364-85,21-71
77422-22,23-27
77516-87,87-88
77680-82,81-84
7773-89,2-89
77843-95,16-96
77967-69,3-88
7808-82,4-95
78128-93,28-93
78223-62,22-24
78373-98,73-73
78484-92,91-91
78555-81,54-55
78652-60,40-59
78719-20,20-95
78830-90,29-90
78957-72,23-71
79071-74,74-75
7919-88,38-92
79244-89,43-90
79317-80,79-85
79460-67,59-67
79544-51,31-84
79679-80,15-79
79779-84,68-85
79897-98,30-96
79922-94,23-95
80095-95,1-95
80111-98,11-99
80249-92,82-91
80343-52,44-51
80476-86,51-81
80523-68,32-69
80622-40,22-23
80765-94,81-94
80836-89,88-90
8097-96,4-97
8106-6,9-19
81182-85,33-85
81265-67,66-93
81394-95,67-89
81485-85,77-84
81560-89,83-90
8166-21,2-75
81720-91,16-19
81895-97,51-94
81921-82,8-8
82016-19,20-83
82196-97,8-81
82234-90,5-94
82394-95,70-94
82427-80,27-81
82512-83,13-13
82691-94,8-92
8276-80,5-79
82812-21,12-20
82929-64,28-63
83032-72,23-71
83154-56,54-57
83268-69,11-68
8335-99,4-95
83434-55,35-56
83542-71,42-72
83621-94,93-96
8378-8,8-96
83826-99,25-97
83930-75,2-29
84027-58,28-52
84129-75,28-76
8423-77,3-78
84311-35,11-36
84426-43,38-41
84551-56,33-57
84619-21,20-98
84748-93,47-48
8482-81,31-75
84917-75,16-16
85057-82,48-48
85124-74,25-65
85220-20,21-21
85378-96,77-79
8544-97,4-98
8554-30,19-31
85611-97,10-89
85721-64,1-64
85831-99,16-98
85938-47,40-40
86027-95,12-17
86151-93,51-92
86290-91,6-91
86326-27,16-26
8643-78,1-1
86551-51,52-92
86638-42,32-36
86736-46,10-67
8687-70,6-71
86952-81,51-81
87074-74,75-84
87128-64,29-29
87215-47,15-16
8734-94,95-95
87415-65,15-97
87598-98,1-99
87641-97,42-98
87784-94,90-95
87815-82,83-83
8793-10,10-73
88090-97,5-96
88129-81,29-45
88264-93,5-99
88326-95,25-26
88437-49,38-50
88532-96,10-33
88628-30,28-31
88718-75,19-75
88849-51,17-50
88916-46,15-17
8903-4,3-95
8917-30,30-31
8929-96,9-95
89322-38,18-39
89429-32,50-89
89598-99,1-99
89681-97,9-83
89732-38,64-69
89835-52,51-53
89917-96,17-98
90014-97,98-98
90110-66,11-65
9024-91,4-91
90352-76,73-74
90417-17,16-17
9056-73,7-74
9069-90,9-89
90742-74,42-84
90868-77,69-77
90942-79,40-51
91015-89,15-90
9114-86,3-3
91243-46,40-46
9133-97,2-98
91413-26,12-97
91591-93,15-92
9164-89,4-99
91741-66,14-40
91830-71,31-71
91957-61,38-60
92036-99,35-98
92136-91,20-36
92280-81,16-80
9231-99,4-96
92424-80,25-80
9251-98,2-99
92617-43,17-44
9274-99,2-2
92811-46,11-45
92970-92,69-86
93015-15,3-14
9315-80,7-81
93233-91,52-60
93362-68,4-62
93423-33,22-33
93521-71,7-72
93641-63,40-62
93719-20,20-26
9389-87,8-86
93990-90,91-96
94031-32,50-68
94121-35,31-85
94214-98,15-15
94395-96,46-95
9449-64,10-21
94592-98,30-66
94666-86,79-95
9471-71,34-71
94831-92,91-93
94921-25,8-17
95017-49,1-69
9513-88,2-98
95254-86,53-55
95329-79,95-96
9544-25,3-25
9552-44,3-61
95634-85,33-35
9578-10,8-11
95895-97,45-98
95963-92,62-93
96061-79,60-80
96136-85,20-36
96245-71,10-53
96356-85,52-84
96440-86,41-86
9656-83,4-78
96618-53,19-54
96769-73,69-72
9683-76,75-75
96928-88,19-60
9703-85,10-85
97136-69,35-69
97258-82,57-82
97316-98,15-99
97471-84,84-85
9757-68,42-67
9763-69,70-70
9772-10,7-11
97859-91,27-91
9794-99,5-94
98071-84,30-70
98128-28,12-27
98252-88,14-26
98391-91,42-90
9846-56,5-42
98515-37,17-53
98610-86,86-87
98711-91,4-92
98846-46,19-45
98937-92,37-96
99058-60,35-59
9916-33,5-33
99234-34,13-34
9933-62,1-1
99433-64,49-61
99537-45,36-52
99613-96,8-82
9973-99,2-3
99841-66,27-67
99985-87,20-86
10009-26,8-25
diff --git a/2022/day4/part1/Cargo.toml b/2022/day4/part1/Cargo.toml
new file mode 100644
index 0000000..f14fe90
--- /dev/null
+++ b/2022/day4/part1/Cargo.toml
@@ -0,0 +1,9 @@
1[package]
2name = "part1"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
9itertools = "0.10.5"
diff --git a/2022/day4/part1/src/main.rs b/2022/day4/part1/src/main.rs
new file mode 100644
index 0000000..a4b7e38
--- /dev/null
+++ b/2022/day4/part1/src/main.rs
@@ -0,0 +1,50 @@
1use itertools::Itertools;
2
3#[derive(Debug)]
4struct Elf {
5 start: usize,
6 end: usize,
7}
8
9#[derive(Debug)]
10struct Pair(Elf, Elf);
11
12impl Elf {
13 pub fn new(range: &str) -> Self {
14 if let Some((start, end)) = range.to_string().split("-").collect_tuple() {
15 let start: usize = start.parse().unwrap();
16 let end: usize = end.parse().unwrap();
17 Self { start, end }
18 } else {
19 panic!("Expected two elements");
20 }
21 }
22}
23
24impl Pair {
25 pub fn new(line: &str) -> Self {
26 if let Some((first, second)) = line.to_string().split(",").collect_tuple() {
27 Pair(Elf::new(first), Elf::new(second))
28 } else {
29 panic!("Expected two elements");
30 }
31 }
32}
33
34impl Pair {
35 fn contains(&self) -> bool {
36 return (self.0.start <= self.1.start && self.0.end >= self.1.end)
37 || (self.0.start >= self.1.start && self.0.end <= self.1.end);
38 }
39}
40
41fn main() {
42 let sum: usize = include_str!("../../example")
43 .lines()
44 .map(|l| Pair::new(l))
45 .map(|p| p.contains())
46 .filter(|r| *r)
47 .count();
48
49 println!("{}", sum);
50}
diff --git a/2022/day4/part2/Cargo.toml b/2022/day4/part2/Cargo.toml
new file mode 100644
index 0000000..69fa504
--- /dev/null
+++ b/2022/day4/part2/Cargo.toml
@@ -0,0 +1,9 @@
1[package]
2name = "part2"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
9itertools = "0.10.5"
diff --git a/2022/day4/part2/src/main.rs b/2022/day4/part2/src/main.rs
new file mode 100644
index 0000000..d7f378a
--- /dev/null
+++ b/2022/day4/part2/src/main.rs
@@ -0,0 +1,49 @@
1use itertools::Itertools;
2
3#[derive(Debug)]
4struct Elf {
5 start: usize,
6 end: usize,
7}
8
9#[derive(Debug)]
10struct Pair(Elf, Elf);
11
12impl Elf {
13 pub fn new(range: &str) -> Self {
14 if let Some((start, end)) = range.to_string().split("-").collect_tuple() {
15 let start: usize = start.parse().unwrap();
16 let end: usize = end.parse().unwrap();
17 Self { start, end }
18 } else {
19 panic!("Expected two elements");
20 }
21 }
22}
23
24impl Pair {
25 pub fn new(line: &str) -> Self {
26 if let Some((first, second)) = line.to_string().split(",").collect_tuple() {
27 Pair(Elf::new(first), Elf::new(second))
28 } else {
29 panic!("Expected two elements");
30 }
31 }
32}
33
34impl Pair {
35 fn overlaps(&self) -> bool {
36 !(self.0.end < self.1.start || self.1.end < self.0.start)
37 }
38}
39
40fn main() {
41 let sum: usize = include_str!("../../input")
42 .lines()
43 .map(|l| Pair::new(l))
44 .map(|p| p.overlaps())
45 .filter(|r| *r)
46 .count();
47
48 println!("{}", sum);
49}