From 244d3dfa233371db0adcff329283bb1ce9bba9f5 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Tue, 6 Dec 2022 14:47:43 +0300 Subject: 2022, day4: done --- 2022/day4/example | 6 + 2022/day4/input | 1000 +++++++++++++++++++++++++++++++++++++++++++ 2022/day4/part1/Cargo.toml | 9 + 2022/day4/part1/src/main.rs | 50 +++ 2022/day4/part2/Cargo.toml | 9 + 2022/day4/part2/src/main.rs | 49 +++ 6 files changed, 1123 insertions(+) create mode 100644 2022/day4/example create mode 100644 2022/day4/input create mode 100644 2022/day4/part1/Cargo.toml create mode 100644 2022/day4/part1/src/main.rs create mode 100644 2022/day4/part2/Cargo.toml create mode 100644 2022/day4/part2/src/main.rs (limited to '2022/day4') 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 @@ +2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-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 @@ +30-31,2-31 +6-92,4-5 +35-39,42-44 +35-95,35-95 +12-98,99-99 +10-15,16-94 +37-85,84-84 +2-23,31-56 +52-68,51-69 +47-62,25-52 +9-62,61-61 +78-99,77-99 +92-94,11-93 +77-83,62-76 +3-7,29-59 +12-96,96-97 +48-48,12-47 +72-86,71-86 +6-90,90-95 +5-92,2-4 +11-63,1-64 +72-72,72-82 +65-68,68-69 +3-99,5-99 +5-80,5-5 +40-93,41-93 +27-31,25-58 +6-93,93-93 +25-75,75-76 +31-78,79-79 +16-60,33-86 +38-65,39-97 +46-52,45-88 +13-64,12-80 +53-53,35-52 +32-38,12-30 +43-49,42-96 +4-23,24-77 +32-63,27-64 +46-72,23-73 +78-89,56-88 +42-78,79-79 +2-87,2-88 +51-57,57-58 +56-63,55-63 +2-12,21-62 +5-7,6-41 +57-86,87-92 +17-18,17-42 +11-84,7-10 +13-75,4-75 +15-76,14-14 +96-96,2-97 +68-94,43-95 +57-66,67-85 +5-52,20-88 +25-62,25-62 +3-82,81-83 +88-89,85-88 +31-49,30-68 +58-60,59-61 +16-33,15-16 +13-97,13-66 +4-87,10-86 +6-6,7-99 +33-35,34-98 +48-88,82-89 +31-73,30-74 +1-95,2-95 +11-87,10-86 +60-61,21-60 +51-91,51-52 +15-37,16-38 +5-10,10-32 +23-25,24-34 +21-40,21-40 +12-52,13-31 +10-10,10-94 +41-82,4-82 +5-48,6-34 +73-81,42-72 +62-77,76-82 +41-82,56-83 +19-52,18-39 +19-92,54-93 +2-39,2-40 +95-96,3-95 +7-70,6-92 +7-89,6-58 +67-88,68-80 +14-74,73-86 +41-41,33-40 +2-91,8-90 +69-81,11-80 +8-90,9-98 +62-87,86-86 +7-70,39-69 +4-9,3-12 +4-96,11-95 +1-21,3-22 +18-24,92-99 +34-87,7-90 +96-99,82-88 +6-50,6-34 +13-53,12-54 +2-98,3-95 +8-11,17-91 +26-92,3-56 +36-61,46-84 +86-86,22-85 +20-76,76-76 +21-80,21-30 +34-78,55-90 +12-96,11-95 +43-43,15-44 +54-88,53-89 +37-37,50-66 +23-95,7-11 +7-58,21-59 +3-5,4-68 +5-75,8-76 +42-68,42-67 +36-36,36-47 +9-69,9-92 +30-56,36-80 +12-85,13-84 +11-64,65-87 +41-60,60-61 +2-30,31-41 +23-60,9-35 +87-89,15-88 +4-97,96-98 +9-92,9-91 +40-66,66-66 +39-95,69-96 +48-87,23-47 +5-96,6-95 +18-66,19-65 +5-38,39-61 +12-92,93-93 +9-97,10-97 +16-51,50-67 +3-14,14-28 +7-7,8-88 +6-45,5-36 +58-58,40-58 +8-56,7-56 +5-15,6-14 +97-97,7-96 +26-70,27-94 +20-21,20-91 +3-16,3-13 +9-10,9-92 +16-81,67-94 +72-88,6-73 +4-88,3-68 +25-53,24-54 +63-75,63-74 +31-31,31-56 +1-84,1-83 +41-89,40-67 +4-53,5-54 +13-83,13-14 +11-87,11-86 +4-63,4-63 +9-96,8-97 +2-74,1-88 +40-83,39-83 +46-71,45-75 +4-74,74-75 +99-99,72-98 +33-51,52-82 +22-69,22-23 +35-36,36-91 +8-97,97-98 +18-20,19-94 +6-16,7-17 +35-36,36-97 +4-78,79-79 +14-69,4-14 +50-64,19-51 +10-31,15-17 +52-74,74-74 +42-91,90-91 +35-69,10-70 +1-3,4-4 +37-66,67-67 +6-92,73-74 +21-96,21-97 +6-37,6-68 +25-78,79-92 +78-88,65-75 +14-99,8-8 +39-66,54-67 +33-80,79-91 +15-83,84-84 +51-51,10-50 +72-80,72-81 +4-92,2-24 +33-93,92-94 +4-10,4-68 +8-94,7-96 +1-39,38-69 +6-36,36-37 +86-86,31-85 +77-94,76-96 +8-75,17-76 +7-93,7-45 +8-61,40-70 +17-29,32-94 +26-26,12-25 +14-94,10-12 +29-42,32-39 +2-91,2-3 +12-85,28-86 +57-70,71-90 +9-16,17-87 +17-55,18-55 +18-98,19-19 +88-88,22-87 +5-93,4-92 +75-76,76-84 +5-6,6-41 +42-94,36-41 +52-66,1-62 +5-82,4-4 +1-87,3-88 +47-58,56-61 +7-57,7-97 +39-93,33-93 +31-48,20-32 +18-81,19-82 +16-77,17-78 +2-2,4-43 +22-23,23-93 +10-11,15-68 +89-95,15-88 +19-69,18-18 +85-98,34-84 +47-73,29-52 +56-56,23-55 +2-97,2-94 +34-64,17-63 +46-46,47-75 +15-90,91-91 +63-63,35-62 +46-56,52-89 +82-90,82-90 +17-23,5-7 +7-79,8-79 +40-76,39-71 +81-82,11-99 +31-41,37-44 +97-98,1-98 +13-92,11-16 +63-63,3-62 +10-23,27-46 +18-99,19-80 +43-65,55-56 +14-14,14-20 +14-75,15-74 +24-75,12-45 +32-32,33-83 +4-92,2-3 +35-50,34-72 +11-90,91-91 +62-84,78-97 +64-64,24-65 +13-93,12-94 +70-89,58-88 +35-44,36-43 +57-90,91-91 +19-20,20-96 +33-96,26-87 +9-61,10-62 +2-81,1-3 +69-98,69-98 +60-66,64-97 +8-8,4-7 +33-76,32-77 +35-96,34-86 +87-92,66-86 +5-39,8-67 +27-78,79-80 +5-98,1-1 +6-79,5-95 +65-87,88-88 +50-98,49-49 +55-58,54-60 +13-97,12-96 +74-74,1-73 +12-61,64-74 +10-98,10-11 +68-70,17-69 +41-58,13-65 +7-77,6-77 +50-95,41-49 +4-68,2-4 +14-54,14-40 +59-93,60-82 +21-95,26-95 +41-81,41-62 +41-89,40-88 +21-39,22-40 +30-94,29-88 +1-93,1-93 +5-78,4-79 +15-86,85-87 +89-90,17-89 +47-85,48-85 +84-84,83-85 +6-98,6-6 +3-92,3-93 +9-9,8-60 +36-90,6-35 +57-61,56-58 +4-92,3-93 +7-69,8-69 +78-78,4-79 +52-93,53-93 +30-90,30-91 +58-78,77-79 +21-41,42-53 +16-37,6-38 +2-77,3-77 +6-55,17-69 +36-73,37-71 +93-94,26-93 +22-79,22-79 +11-92,91-93 +1-62,3-65 +9-65,8-64 +11-32,7-33 +5-94,2-5 +80-82,80-81 +46-77,45-76 +2-98,1-99 +2-4,6-68 +56-83,55-82 +12-14,13-98 +78-90,5-80 +3-69,2-2 +83-83,76-82 +3-99,1-1 +28-72,28-50 +57-67,32-54 +4-99,7-98 +18-18,19-48 +79-85,84-84 +83-89,81-87 +31-89,30-87 +49-72,49-71 +28-29,28-51 +48-97,1-47 +13-45,8-12 +24-75,25-75 +63-89,76-86 +16-23,15-16 +25-49,26-48 +17-94,16-88 +6-95,7-96 +87-90,86-89 +11-93,3-95 +6-66,5-65 +97-99,39-98 +23-66,24-65 +54-56,55-70 +92-97,26-90 +7-86,7-87 +60-78,77-79 +90-95,71-94 +32-87,33-87 +13-13,14-72 +12-86,6-87 +24-44,45-75 +5-96,5-97 +2-83,7-79 +95-97,1-90 +72-80,73-75 +11-85,12-86 +7-50,8-8 +35-99,55-79 +56-98,57-99 +6-87,7-87 +28-72,27-71 +20-77,78-83 +8-67,68-83 +10-68,9-9 +35-63,35-64 +90-90,90-90 +5-97,1-99 +10-93,9-19 +6-90,4-77 +46-75,80-97 +9-77,10-82 +73-88,74-81 +43-65,43-44 +66-82,66-81 +62-63,63-77 +35-76,11-77 +22-93,89-90 +3-96,2-3 +11-29,2-33 +75-82,62-81 +46-47,46-53 +9-99,8-98 +32-93,33-87 +47-51,52-60 +18-83,84-84 +31-70,31-69 +26-93,19-92 +93-93,4-92 +2-14,4-35 +1-98,36-99 +7-83,82-96 +95-95,32-95 +34-39,9-38 +2-63,62-93 +24-98,99-99 +26-50,89-93 +82-97,35-81 +22-33,16-33 +1-5,11-66 +9-63,9-63 +36-36,36-77 +5-98,6-99 +22-93,50-92 +89-92,89-93 +48-90,91-91 +6-88,7-89 +27-38,38-84 +9-87,11-40 +24-83,23-82 +1-69,1-68 +16-67,62-65 +13-15,1-85 +41-65,40-42 +19-77,20-77 +44-93,45-93 +27-94,94-97 +12-97,12-96 +9-38,8-48 +22-49,6-49 +12-18,13-52 +14-52,13-14 +12-51,13-52 +10-36,37-37 +14-72,13-15 +2-99,72-81 +21-34,22-22 +86-86,60-85 +20-36,20-20 +10-96,11-96 +14-24,15-23 +8-96,8-95 +16-22,22-23 +32-90,33-97 +7-31,32-67 +35-68,22-77 +3-69,5-69 +13-62,12-61 +26-32,27-32 +68-77,67-76 +5-99,6-98 +2-56,8-65 +96-99,11-67 +3-25,15-24 +25-91,84-90 +9-39,10-38 +24-94,23-24 +78-80,29-79 +4-47,5-85 +39-81,81-82 +33-33,13-32 +31-69,32-83 +19-95,17-21 +75-75,74-74 +14-89,15-98 +4-99,6-98 +60-90,59-66 +50-72,46-73 +26-84,25-49 +9-77,8-77 +48-99,49-94 +91-95,10-90 +7-63,24-64 +79-85,11-80 +98-99,20-82 +2-98,1-97 +56-68,54-67 +30-30,31-87 +89-96,58-88 +6-56,3-5 +12-49,15-62 +26-33,26-66 +18-18,19-98 +13-27,14-26 +12-15,3-19 +5-98,46-98 +4-89,3-90 +5-15,5-19 +85-88,45-87 +94-97,97-98 +24-34,34-77 +55-56,2-55 +6-86,7-87 +42-50,43-50 +16-57,17-58 +3-22,2-3 +56-57,56-96 +5-91,82-92 +30-53,53-54 +12-25,13-47 +72-90,90-91 +15-49,49-89 +5-81,6-37 +57-58,58-82 +11-65,2-10 +41-97,42-96 +55-71,54-70 +4-81,1-98 +2-65,1-49 +7-45,6-45 +98-98,8-98 +8-93,41-92 +1-90,2-98 +14-93,43-92 +66-97,98-98 +36-87,87-88 +2-62,4-61 +22-76,23-76 +49-54,50-55 +50-53,52-94 +45-95,75-96 +22-77,78-78 +71-73,72-86 +9-83,10-82 +1-56,53-54 +71-71,63-70 +7-73,7-8 +6-9,6-66 +10-98,97-98 +4-50,50-50 +7-95,7-91 +6-7,7-96 +73-73,1-73 +43-68,44-77 +26-30,25-31 +92-92,9-92 +88-89,1-88 +12-97,96-98 +91-91,59-90 +4-9,3-16 +16-97,96-96 +50-89,49-90 +94-96,84-95 +55-71,70-72 +16-90,5-16 +1-96,3-95 +5-35,7-34 +16-72,16-54 +3-99,1-2 +8-87,10-88 +10-98,10-98 +8-67,4-69 +23-79,72-72 +17-74,17-75 +57-77,77-78 +74-84,85-98 +22-64,30-65 +3-73,1-72 +1-78,31-91 +4-93,4-94 +51-88,51-79 +5-19,19-99 +68-68,31-67 +2-98,3-3 +48-63,47-64 +67-71,72-73 +9-42,9-54 +2-50,2-39 +53-53,3-54 +36-37,36-69 +63-74,64-73 +14-65,13-65 +41-94,98-98 +1-60,6-99 +95-98,80-94 +71-74,32-78 +31-31,31-31 +6-7,6-33 +5-95,96-98 +5-93,6-94 +52-88,51-89 +55-76,55-77 +4-98,3-96 +59-89,58-94 +10-88,9-86 +13-97,13-96 +18-97,10-16 +7-98,4-97 +84-85,14-84 +26-88,76-87 +52-72,71-81 +38-52,39-52 +22-88,1-89 +7-97,2-7 +4-25,3-25 +43-73,42-74 +26-26,25-47 +34-84,40-84 +8-69,7-68 +2-52,1-85 +52-60,52-61 +38-71,37-70 +28-86,27-85 +99-99,7-98 +45-67,8-81 +7-93,7-58 +37-56,28-56 +29-99,66-98 +78-98,7-97 +4-72,3-77 +1-3,2-73 +1-14,2-35 +12-13,12-95 +40-84,84-84 +12-56,28-47 +3-96,1-39 +58-74,74-74 +24-32,24-32 +11-91,5-10 +41-69,36-97 +9-92,8-59 +40-51,39-52 +97-99,16-96 +34-96,33-69 +68-86,30-87 +45-96,44-98 +12-89,6-12 +5-81,32-89 +5-8,6-9 +4-42,1-2 +32-42,79-96 +5-96,4-95 +54-62,12-62 +30-86,86-86 +50-58,58-58 +41-61,40-62 +1-99,2-99 +21-52,20-20 +17-69,1-17 +3-97,2-96 +35-37,36-57 +8-32,9-11 +3-4,4-99 +92-96,72-92 +2-3,2-90 +34-40,41-69 +81-98,65-80 +12-57,58-58 +39-40,14-39 +47-77,78-93 +5-5,6-91 +24-87,86-93 +31-80,30-80 +32-82,33-55 +61-75,62-74 +16-98,16-99 +19-21,22-86 +54-54,16-53 +40-74,21-28 +20-28,28-76 +94-96,47-71 +25-74,74-75 +39-81,3-67 +5-97,2-52 +21-63,62-62 +7-53,6-84 +60-88,59-60 +23-61,24-61 +76-93,77-94 +17-30,30-86 +10-23,9-77 +6-90,5-96 +38-69,37-39 +1-3,4-94 +73-74,3-74 +70-83,41-84 +72-79,78-83 +56-76,15-67 +92-92,6-93 +2-90,89-89 +19-19,20-90 +18-79,17-80 +11-11,2-16 +95-95,57-95 +51-93,52-86 +47-52,53-96 +1-5,13-43 +44-63,62-64 +43-97,42-42 +46-54,45-53 +7-61,8-21 +18-79,41-80 +54-56,28-55 +7-54,11-54 +12-61,62-62 +80-81,80-94 +38-39,38-76 +8-94,26-83 +3-66,2-4 +6-27,7-26 +2-96,71-86 +67-99,66-98 +28-73,27-90 +2-93,1-93 +80-95,72-94 +88-90,24-89 +93-93,22-93 +36-86,51-92 +25-63,24-63 +90-91,12-84 +9-46,10-46 +31-31,17-31 +51-52,49-51 +14-18,2-12 +72-73,64-73 +6-82,10-81 +58-84,15-15 +51-78,52-68 +54-56,60-68 +78-79,6-78 +96-96,95-96 +28-81,43-82 +5-95,5-5 +2-17,1-99 +10-94,9-61 +85-87,3-91 +2-73,14-72 +95-95,21-94 +17-57,37-81 +95-95,4-95 +28-84,12-93 +4-98,1-1 +96-97,7-97 +60-99,61-99 +97-97,7-98 +73-74,5-74 +59-94,60-60 +83-85,84-91 +2-5,5-40 +29-79,28-78 +37-67,26-54 +28-39,10-41 +84-84,85-85 +19-69,12-68 +29-31,30-32 +58-77,57-59 +17-25,24-26 +37-80,36-81 +16-84,16-97 +3-97,1-96 +7-63,7-64 +7-54,6-54 +14-47,13-46 +13-64,55-67 +19-36,35-35 +67-80,67-79 +8-66,5-35 +56-93,44-55 +6-74,15-90 +64-85,21-71 +22-22,23-27 +16-87,87-88 +80-82,81-84 +3-89,2-89 +43-95,16-96 +67-69,3-88 +8-82,4-95 +28-93,28-93 +23-62,22-24 +73-98,73-73 +84-92,91-91 +55-81,54-55 +52-60,40-59 +19-20,20-95 +30-90,29-90 +57-72,23-71 +71-74,74-75 +9-88,38-92 +44-89,43-90 +17-80,79-85 +60-67,59-67 +44-51,31-84 +79-80,15-79 +79-84,68-85 +97-98,30-96 +22-94,23-95 +95-95,1-95 +11-98,11-99 +49-92,82-91 +43-52,44-51 +76-86,51-81 +23-68,32-69 +22-40,22-23 +65-94,81-94 +36-89,88-90 +7-96,4-97 +6-6,9-19 +82-85,33-85 +65-67,66-93 +94-95,67-89 +85-85,77-84 +60-89,83-90 +6-21,2-75 +20-91,16-19 +95-97,51-94 +21-82,8-8 +16-19,20-83 +96-97,8-81 +34-90,5-94 +94-95,70-94 +27-80,27-81 +12-83,13-13 +91-94,8-92 +6-80,5-79 +12-21,12-20 +29-64,28-63 +32-72,23-71 +54-56,54-57 +68-69,11-68 +5-99,4-95 +34-55,35-56 +42-71,42-72 +21-94,93-96 +8-8,8-96 +26-99,25-97 +30-75,2-29 +27-58,28-52 +29-75,28-76 +3-77,3-78 +11-35,11-36 +26-43,38-41 +51-56,33-57 +19-21,20-98 +48-93,47-48 +2-81,31-75 +17-75,16-16 +57-82,48-48 +24-74,25-65 +20-20,21-21 +78-96,77-79 +4-97,4-98 +4-30,19-31 +11-97,10-89 +21-64,1-64 +31-99,16-98 +38-47,40-40 +27-95,12-17 +51-93,51-92 +90-91,6-91 +26-27,16-26 +3-78,1-1 +51-51,52-92 +38-42,32-36 +36-46,10-67 +7-70,6-71 +52-81,51-81 +74-74,75-84 +28-64,29-29 +15-47,15-16 +4-94,95-95 +15-65,15-97 +98-98,1-99 +41-97,42-98 +84-94,90-95 +15-82,83-83 +3-10,10-73 +90-97,5-96 +29-81,29-45 +64-93,5-99 +26-95,25-26 +37-49,38-50 +32-96,10-33 +28-30,28-31 +18-75,19-75 +49-51,17-50 +16-46,15-17 +3-4,3-95 +7-30,30-31 +9-96,9-95 +22-38,18-39 +29-32,50-89 +98-99,1-99 +81-97,9-83 +32-38,64-69 +35-52,51-53 +17-96,17-98 +14-97,98-98 +10-66,11-65 +4-91,4-91 +52-76,73-74 +17-17,16-17 +6-73,7-74 +9-90,9-89 +42-74,42-84 +68-77,69-77 +42-79,40-51 +15-89,15-90 +4-86,3-3 +43-46,40-46 +3-97,2-98 +13-26,12-97 +91-93,15-92 +4-89,4-99 +41-66,14-40 +30-71,31-71 +57-61,38-60 +36-99,35-98 +36-91,20-36 +80-81,16-80 +1-99,4-96 +24-80,25-80 +1-98,2-99 +17-43,17-44 +4-99,2-2 +11-46,11-45 +70-92,69-86 +15-15,3-14 +5-80,7-81 +33-91,52-60 +62-68,4-62 +23-33,22-33 +21-71,7-72 +41-63,40-62 +19-20,20-26 +9-87,8-86 +90-90,91-96 +31-32,50-68 +21-35,31-85 +14-98,15-15 +95-96,46-95 +9-64,10-21 +92-98,30-66 +66-86,79-95 +1-71,34-71 +31-92,91-93 +21-25,8-17 +17-49,1-69 +3-88,2-98 +54-86,53-55 +29-79,95-96 +4-25,3-25 +2-44,3-61 +34-85,33-35 +8-10,8-11 +95-97,45-98 +63-92,62-93 +61-79,60-80 +36-85,20-36 +45-71,10-53 +56-85,52-84 +40-86,41-86 +6-83,4-78 +18-53,19-54 +69-73,69-72 +3-76,75-75 +28-88,19-60 +3-85,10-85 +36-69,35-69 +58-82,57-82 +16-98,15-99 +71-84,84-85 +7-68,42-67 +3-69,70-70 +2-10,7-11 +59-91,27-91 +4-99,5-94 +71-84,30-70 +28-28,12-27 +52-88,14-26 +91-91,42-90 +6-56,5-42 +15-37,17-53 +10-86,86-87 +11-91,4-92 +46-46,19-45 +37-92,37-96 +58-60,35-59 +6-33,5-33 +34-34,13-34 +3-62,1-1 +33-64,49-61 +37-45,36-52 +13-96,8-82 +3-99,2-3 +41-66,27-67 +85-87,20-86 +9-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 @@ +[package] +name = "part1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "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 @@ +use itertools::Itertools; + +#[derive(Debug)] +struct Elf { + start: usize, + end: usize, +} + +#[derive(Debug)] +struct Pair(Elf, Elf); + +impl Elf { + pub fn new(range: &str) -> Self { + if let Some((start, end)) = range.to_string().split("-").collect_tuple() { + let start: usize = start.parse().unwrap(); + let end: usize = end.parse().unwrap(); + Self { start, end } + } else { + panic!("Expected two elements"); + } + } +} + +impl Pair { + pub fn new(line: &str) -> Self { + if let Some((first, second)) = line.to_string().split(",").collect_tuple() { + Pair(Elf::new(first), Elf::new(second)) + } else { + panic!("Expected two elements"); + } + } +} + +impl Pair { + fn contains(&self) -> bool { + return (self.0.start <= self.1.start && self.0.end >= self.1.end) + || (self.0.start >= self.1.start && self.0.end <= self.1.end); + } +} + +fn main() { + let sum: usize = include_str!("../../example") + .lines() + .map(|l| Pair::new(l)) + .map(|p| p.contains()) + .filter(|r| *r) + .count(); + + println!("{}", sum); +} 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 @@ +[package] +name = "part2" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "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 @@ +use itertools::Itertools; + +#[derive(Debug)] +struct Elf { + start: usize, + end: usize, +} + +#[derive(Debug)] +struct Pair(Elf, Elf); + +impl Elf { + pub fn new(range: &str) -> Self { + if let Some((start, end)) = range.to_string().split("-").collect_tuple() { + let start: usize = start.parse().unwrap(); + let end: usize = end.parse().unwrap(); + Self { start, end } + } else { + panic!("Expected two elements"); + } + } +} + +impl Pair { + pub fn new(line: &str) -> Self { + if let Some((first, second)) = line.to_string().split(",").collect_tuple() { + Pair(Elf::new(first), Elf::new(second)) + } else { + panic!("Expected two elements"); + } + } +} + +impl Pair { + fn overlaps(&self) -> bool { + !(self.0.end < self.1.start || self.1.end < self.0.start) + } +} + +fn main() { + let sum: usize = include_str!("../../input") + .lines() + .map(|l| Pair::new(l)) + .map(|p| p.overlaps()) + .filter(|r| *r) + .count(); + + println!("{}", sum); +} -- cgit v1.2.3-70-g09d2