From c31dbd833d139414bb90fe2bbe457843a4f7f615 Mon Sep 17 00:00:00 2001 From: Anthony Oteri Date: Mon, 5 Dec 2022 19:46:14 -0500 Subject: [PATCH] Solution for Day 4 --- day4/Cargo.lock | 7 + day4/Cargo.toml | 8 + day4/data/input-test.txt | 6 + day4/data/input.txt | 1000 ++++++++++++++++++++++++++++++++++++++ day4/src/main.rs | 175 +++++++ 5 files changed, 1196 insertions(+) create mode 100644 day4/Cargo.lock create mode 100644 day4/Cargo.toml create mode 100644 day4/data/input-test.txt create mode 100644 day4/data/input.txt create mode 100644 day4/src/main.rs diff --git a/day4/Cargo.lock b/day4/Cargo.lock new file mode 100644 index 0000000..e92c02c --- /dev/null +++ b/day4/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day4" +version = "0.1.0" diff --git a/day4/Cargo.toml b/day4/Cargo.toml new file mode 100644 index 0000000..8842130 --- /dev/null +++ b/day4/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day4" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day4/data/input-test.txt b/day4/data/input-test.txt new file mode 100644 index 0000000..99a66c5 --- /dev/null +++ b/day4/data/input-test.txt @@ -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 \ No newline at end of file diff --git a/day4/data/input.txt b/day4/data/input.txt new file mode 100644 index 0000000..0f4a908 --- /dev/null +++ b/day4/data/input.txt @@ -0,0 +1,1000 @@ +2-88,13-89 +12-94,12-94 +34-69,34-61 +9-76,9-9 +35-82,34-52 +9-10,10-88 +57-71,71-75 +50-71,71-91 +44-67,43-43 +68-69,67-68 +6-47,6-48 +6-52,7-67 +43-51,40-44 +56-67,57-69 +94-94,68-94 +10-97,9-9 +40-95,40-98 +22-81,22-22 +7-58,7-59 +8-82,82-82 +22-27,23-44 +5-27,7-28 +11-75,11-75 +7-90,99-99 +65-89,65-66 +24-52,29-53 +41-57,27-58 +14-95,13-26 +27-34,26-75 +18-33,33-56 +21-95,21-95 +20-29,69-71 +81-81,23-90 +47-83,47-82 +28-50,86-98 +50-76,21-50 +3-69,64-79 +17-97,10-16 +54-92,10-93 +7-75,45-77 +72-72,42-73 +13-80,81-87 +25-26,25-64 +38-48,38-38 +2-99,2-99 +48-93,67-92 +12-85,2-94 +14-75,7-82 +29-46,23-29 +50-94,4-51 +3-37,37-91 +24-91,91-91 +25-48,44-47 +2-77,1-26 +1-58,1-21 +16-93,16-93 +3-99,8-98 +23-23,6-23 +17-67,68-68 +4-15,1-4 +35-41,35-40 +6-88,52-89 +11-48,11-61 +4-64,12-65 +7-90,7-8 +1-89,6-73 +64-69,38-70 +26-67,59-66 +41-54,16-42 +2-99,9-96 +2-8,7-88 +12-95,97-97 +17-92,12-93 +27-56,27-28 +19-66,65-77 +1-95,94-94 +24-48,23-95 +40-74,74-74 +20-99,20-97 +60-73,61-74 +23-85,23-86 +37-69,69-69 +37-64,27-37 +7-53,7-27 +84-97,93-94 +26-51,50-52 +12-93,13-93 +28-86,29-29 +11-25,18-46 +97-99,91-98 +27-73,53-58 +25-77,6-26 +26-29,29-68 +11-17,11-28 +99-99,13-99 +70-92,48-92 +50-61,50-62 +2-95,77-94 +17-17,17-90 +95-95,16-95 +12-96,13-97 +28-98,28-28 +18-91,3-80 +1-95,1-19 +86-89,22-89 +37-96,3-96 +2-18,18-56 +8-72,71-73 +46-48,32-47 +57-69,57-78 +10-22,9-9 +6-96,6-6 +44-58,5-59 +6-8,7-93 +76-95,77-99 +97-99,3-98 +69-69,64-69 +38-68,67-96 +68-74,67-95 +20-68,69-69 +6-88,9-85 +47-66,47-55 +9-94,93-95 +19-29,28-89 +4-83,90-90 +2-95,1-95 +36-37,36-36 +12-90,45-68 +1-80,89-94 +95-99,6-96 +1-1,2-95 +47-81,46-80 +1-57,38-47 +5-7,7-98 +18-75,18-94 +39-70,39-70 +62-93,62-62 +30-32,31-54 +22-82,82-84 +83-92,84-98 +54-93,6-94 +74-91,73-90 +11-90,11-90 +92-98,96-97 +13-73,55-69 +47-95,46-95 +11-97,11-99 +53-68,53-53 +45-89,45-46 +99-99,38-89 +14-88,96-96 +6-78,6-7 +31-67,67-67 +96-96,48-96 +40-69,25-39 +10-60,9-61 +38-97,78-93 +52-69,51-60 +21-72,21-33 +24-90,89-90 +36-83,84-84 +44-58,44-68 +95-95,82-94 +13-91,91-95 +27-95,94-96 +60-82,61-83 +11-77,12-12 +7-91,6-90 +15-94,14-48 +5-73,5-74 +10-40,10-40 +59-94,59-93 +84-84,84-84 +8-8,7-75 +20-81,20-81 +5-96,96-99 +5-63,5-6 +2-93,2-93 +72-74,31-73 +6-54,5-47 +8-36,9-86 +1-95,94-94 +11-66,10-73 +45-52,52-99 +1-92,1-95 +76-90,76-77 +6-53,54-55 +44-52,45-95 +5-70,27-70 +3-56,6-55 +28-37,36-37 +54-97,54-98 +4-63,1-4 +5-64,65-65 +19-74,17-18 +12-26,12-12 +34-78,35-99 +42-42,3-42 +69-70,69-77 +49-75,50-95 +30-52,1-28 +25-80,25-25 +97-99,10-98 +36-55,36-54 +6-55,8-55 +60-80,61-81 +76-81,37-77 +19-20,20-20 +11-33,33-85 +31-60,7-32 +3-9,1-2 +40-87,22-40 +9-35,8-35 +9-75,35-75 +19-86,18-85 +33-52,45-53 +15-49,49-66 +40-83,1-41 +10-54,23-88 +83-94,43-83 +7-74,3-75 +18-79,17-18 +6-63,1-6 +19-39,39-77 +69-78,69-79 +14-26,1-23 +3-49,1-58 +39-40,38-39 +16-58,54-57 +34-83,17-34 +79-88,10-80 +73-86,73-86 +4-15,13-14 +6-94,6-6 +84-93,35-84 +56-81,55-81 +3-30,3-4 +5-94,4-96 +5-87,42-87 +98-99,63-79 +20-95,10-21 +40-87,39-41 +39-90,40-97 +20-54,24-88 +1-40,2-65 +57-97,63-96 +1-42,1-1 +5-11,11-66 +39-96,38-95 +5-72,6-73 +7-7,10-96 +97-97,13-97 +71-97,2-4 +79-79,76-79 +20-56,56-74 +57-76,62-68 +20-63,20-57 +19-68,69-69 +41-70,41-70 +37-42,27-44 +77-98,57-99 +18-92,91-96 +10-85,10-96 +43-61,6-44 +25-65,66-95 +12-29,12-80 +22-70,69-71 +77-78,6-77 +20-89,22-88 +79-88,20-88 +98-98,3-99 +58-80,58-66 +27-32,25-47 +80-91,80-95 +24-70,24-86 +52-60,52-62 +80-80,16-80 +3-43,10-88 +2-80,1-99 +34-87,34-88 +28-30,28-32 +13-55,7-55 +16-38,7-16 +25-33,32-77 +58-90,57-57 +7-94,6-7 +5-97,32-96 +71-71,51-71 +10-21,10-11 +3-9,8-98 +26-34,1-81 +35-49,35-50 +11-70,10-71 +25-72,24-99 +27-84,71-72 +50-72,50-71 +11-62,11-34 +93-97,92-96 +71-71,28-71 +72-80,65-81 +27-32,23-28 +31-79,32-79 +12-80,12-81 +38-63,6-39 +27-98,60-97 +45-83,35-45 +52-52,30-52 +11-97,12-14 +3-94,1-99 +4-23,16-22 +1-12,13-91 +14-80,14-78 +3-89,1-3 +28-99,27-87 +14-50,11-66 +40-88,39-39 +38-98,98-99 +5-91,4-91 +4-22,23-23 +20-90,24-91 +2-63,1-62 +14-81,13-80 +52-52,1-51 +8-11,11-93 +9-66,1-9 +44-86,43-68 +13-13,13-94 +40-67,40-68 +70-73,34-68 +5-83,2-11 +37-49,36-49 +11-97,99-99 +44-99,43-98 +50-98,50-97 +38-39,38-80 +83-83,2-84 +14-40,3-23 +41-94,41-96 +61-83,59-62 +22-95,22-96 +32-77,32-77 +22-97,23-96 +39-85,70-84 +4-95,3-95 +14-39,14-38 +9-68,68-96 +62-63,3-48 +21-25,22-90 +4-4,3-98 +85-89,85-99 +7-42,7-43 +31-82,82-84 +45-53,41-54 +38-66,37-66 +5-80,4-72 +1-1,2-97 +3-3,2-3 +47-61,42-61 +70-94,94-98 +1-51,1-51 +53-70,93-99 +43-72,72-82 +3-7,6-77 +71-95,82-92 +82-89,6-82 +3-43,4-76 +45-91,46-91 +12-87,13-87 +11-92,11-46 +3-73,3-72 +91-91,47-92 +12-93,14-94 +43-45,9-44 +10-87,11-87 +20-34,33-88 +48-71,15-97 +20-64,19-64 +3-57,57-57 +6-7,7-99 +4-19,19-96 +68-72,69-72 +14-45,14-14 +22-93,22-23 +8-34,8-15 +83-93,92-96 +11-94,12-94 +54-84,38-85 +93-98,3-55 +32-77,39-40 +3-15,15-93 +16-28,11-29 +1-95,94-96 +6-25,25-26 +32-96,99-99 +1-35,2-5 +73-91,65-74 +62-77,41-63 +72-72,72-93 +5-81,5-81 +81-83,32-81 +68-89,68-89 +30-35,30-61 +48-97,8-49 +85-94,12-94 +15-95,15-96 +4-98,3-4 +33-84,33-92 +28-58,29-59 +44-80,44-80 +11-84,83-83 +95-98,1-96 +3-44,21-43 +91-91,46-69 +91-98,1-90 +3-20,19-72 +3-92,4-55 +23-68,98-98 +26-97,80-83 +98-98,2-97 +30-92,74-92 +6-44,7-60 +18-23,36-47 +9-94,98-99 +11-20,11-75 +50-95,5-51 +13-83,20-84 +27-27,27-47 +95-96,56-95 +27-28,27-29 +31-95,31-31 +19-50,21-23 +19-48,30-55 +7-36,22-80 +3-5,6-99 +21-22,23-52 +3-70,3-70 +4-8,7-90 +28-60,60-98 +81-93,58-82 +2-6,3-16 +15-75,16-76 +8-86,6-6 +1-78,78-95 +37-91,37-99 +94-94,17-94 +53-80,80-81 +1-90,89-89 +21-32,21-73 +65-74,65-74 +24-54,89-92 +8-47,9-47 +8-36,12-41 +6-79,1-23 +79-83,24-84 +91-91,16-92 +14-84,14-15 +51-67,50-50 +6-93,96-96 +29-55,29-43 +36-37,36-77 +36-36,19-35 +84-84,60-89 +66-88,14-89 +14-69,28-93 +79-94,39-62 +85-89,89-89 +1-78,2-78 +44-52,42-53 +55-97,55-97 +32-81,74-80 +90-90,16-89 +3-96,2-37 +15-58,16-71 +5-93,6-93 +1-4,4-13 +61-96,61-99 +17-87,17-86 +34-48,35-49 +17-58,10-17 +1-11,12-68 +56-86,27-64 +73-80,73-77 +96-96,76-97 +23-32,23-31 +3-94,5-95 +67-98,66-72 +87-93,56-92 +4-15,5-15 +24-76,25-75 +43-59,33-60 +66-74,66-68 +18-90,17-57 +68-72,72-93 +6-19,19-29 +94-99,6-95 +20-85,10-21 +45-66,45-67 +73-74,12-73 +35-75,34-59 +67-88,80-88 +99-99,2-97 +15-90,14-14 +9-59,9-86 +18-52,52-79 +65-73,27-74 +89-96,88-96 +97-97,5-78 +92-94,48-93 +27-40,27-57 +7-95,1-8 +7-17,7-49 +66-66,65-89 +7-46,12-78 +15-29,15-38 +7-7,8-64 +13-22,13-21 +11-11,10-45 +84-84,30-84 +42-91,91-91 +65-87,29-87 +87-87,18-86 +17-66,17-65 +87-88,78-86 +57-57,58-87 +22-53,22-54 +8-62,7-62 +37-90,37-91 +50-86,49-86 +6-58,6-67 +32-60,32-63 +91-96,91-91 +13-94,13-14 +4-95,4-95 +1-86,4-87 +30-96,59-95 +10-28,8-59 +2-49,66-71 +96-99,4-97 +3-74,2-2 +29-76,75-75 +4-92,4-91 +16-94,14-16 +35-35,36-96 +39-67,66-66 +17-97,16-67 +1-99,46-98 +41-74,40-74 +45-68,45-69 +68-95,94-99 +14-51,13-31 +91-91,4-91 +46-66,2-67 +61-66,6-71 +66-69,55-89 +11-11,6-10 +72-91,73-94 +1-92,1-16 +25-98,35-74 +56-87,73-87 +57-59,1-58 +4-5,4-91 +27-31,30-68 +98-98,22-98 +28-95,84-94 +9-24,8-24 +21-30,22-38 +79-81,6-83 +34-96,34-83 +31-33,26-32 +36-59,50-52 +25-95,19-25 +97-97,75-90 +97-99,27-98 +7-48,32-52 +12-72,11-33 +3-41,3-41 +2-95,3-95 +71-71,52-72 +92-98,20-92 +14-90,15-99 +86-97,85-87 +44-71,44-71 +10-68,1-10 +47-77,11-46 +8-25,25-47 +22-34,25-34 +17-90,18-48 +31-94,30-94 +10-97,10-96 +21-44,22-97 +42-97,2-98 +84-91,50-85 +60-96,78-82 +81-85,73-80 +41-73,40-73 +48-65,47-56 +5-42,6-79 +33-33,33-83 +46-79,47-79 +61-91,23-92 +93-95,3-94 +32-43,31-51 +83-97,70-83 +24-93,94-94 +2-94,8-91 +45-61,46-65 +46-50,48-75 +23-95,57-84 +3-99,92-95 +85-85,2-61 +42-87,87-93 +95-99,2-96 +3-4,3-98 +1-87,7-87 +34-72,72-73 +11-84,8-12 +80-99,63-98 +96-99,7-97 +73-75,30-73 +60-87,7-61 +21-41,19-40 +14-77,76-86 +53-97,36-98 +34-54,33-54 +4-36,11-66 +6-10,10-94 +1-57,3-87 +15-39,16-39 +34-72,14-33 +1-3,3-93 +23-53,22-53 +24-44,23-34 +9-28,3-9 +12-99,99-99 +87-98,87-98 +11-11,11-73 +96-96,4-91 +91-91,8-92 +18-47,18-94 +57-59,27-59 +12-96,13-75 +1-27,26-61 +21-60,60-61 +30-67,31-67 +22-78,22-78 +6-77,7-52 +28-45,44-78 +34-87,90-94 +65-76,77-77 +11-76,57-58 +83-84,14-83 +70-97,11-70 +97-98,11-97 +29-29,16-29 +87-94,18-87 +53-71,70-70 +3-96,2-90 +11-80,13-99 +68-68,69-83 +64-97,1-98 +39-42,40-58 +35-49,35-49 +58-88,58-87 +67-67,13-66 +20-20,6-19 +16-67,16-17 +3-46,2-4 +41-96,41-88 +20-89,18-21 +4-84,4-84 +81-82,20-81 +94-94,28-93 +64-74,8-65 +3-73,5-36 +15-67,6-86 +33-33,32-91 +23-97,57-97 +8-58,8-46 +9-10,9-99 +67-93,69-92 +93-99,5-93 +95-96,95-98 +15-22,35-53 +55-97,38-98 +95-99,3-96 +12-12,12-62 +89-89,41-88 +9-10,9-92 +41-85,67-84 +10-97,2-11 +19-73,61-72 +38-79,78-80 +49-61,50-72 +70-71,71-72 +25-32,26-31 +2-5,3-62 +12-94,11-93 +26-95,27-98 +40-81,40-80 +34-71,34-95 +99-99,16-89 +14-93,93-96 +19-70,4-20 +7-28,1-29 +13-59,12-59 +44-97,11-45 +8-36,36-37 +94-95,7-94 +43-43,9-42 +16-98,3-13 +89-96,59-82 +9-50,9-51 +70-98,69-97 +84-87,84-87 +25-83,13-70 +63-71,62-71 +3-31,3-96 +53-67,13-76 +98-98,59-99 +4-39,5-40 +19-91,17-90 +28-50,36-45 +34-86,33-38 +58-80,57-80 +90-90,9-90 +98-98,37-72 +2-39,3-53 +85-97,28-84 +96-97,86-98 +38-78,30-39 +1-42,2-41 +7-28,8-27 +6-74,7-75 +85-87,49-86 +98-98,16-46 +42-90,41-70 +5-70,6-70 +13-75,75-75 +25-69,5-51 +13-43,2-14 +77-91,78-78 +17-97,16-18 +3-94,1-3 +33-86,32-92 +13-57,57-68 +18-88,19-95 +11-97,11-96 +17-67,17-67 +44-95,44-94 +21-85,20-85 +30-64,41-64 +34-68,35-68 +37-77,77-79 +96-97,9-96 +78-85,9-79 +80-80,35-79 +16-50,16-36 +65-85,7-85 +52-92,2-52 +77-77,30-78 +4-61,2-5 +41-91,30-90 +93-96,20-90 +9-11,10-73 +39-42,38-42 +46-72,45-76 +57-63,56-66 +9-68,5-46 +5-92,98-98 +39-46,39-46 +10-48,10-65 +3-22,7-91 +43-82,2-94 +21-69,20-69 +70-70,2-71 +9-94,8-91 +23-99,23-80 +6-98,6-97 +45-45,22-44 +31-79,32-32 +47-67,47-48 +5-80,80-81 +34-54,29-55 +16-97,16-96 +6-71,71-74 +17-47,17-46 +74-85,85-86 +7-10,9-82 +30-30,30-90 +51-54,55-55 +20-28,21-98 +67-67,64-66 +50-51,35-50 +4-68,5-5 +1-4,4-40 +20-20,20-84 +29-31,26-73 +12-12,11-12 +45-94,44-93 +36-45,18-37 +14-41,13-98 +38-42,42-84 +32-63,31-62 +32-79,1-79 +32-32,32-88 +83-95,83-96 +50-84,49-83 +30-56,30-74 +7-67,67-82 +5-39,40-94 +3-7,7-82 +42-45,42-67 +1-97,96-99 +30-87,30-64 +3-97,3-98 +52-81,52-81 +65-80,65-80 +46-55,54-91 +17-88,87-92 +7-97,97-97 +57-81,57-81 +54-97,54-54 +13-13,13-86 +30-35,36-36 +77-77,68-76 +11-82,12-91 +7-72,2-48 +85-94,28-86 +17-88,88-99 +9-56,7-9 +56-96,33-96 +85-89,85-89 +13-13,13-99 +76-93,62-76 +86-86,31-86 +31-95,31-96 +68-70,44-69 +9-54,9-10 +66-97,25-98 +44-93,93-93 +63-63,17-62 +3-98,2-10 +18-96,19-97 +19-80,2-81 +44-45,4-44 +1-37,7-36 +59-61,39-62 +33-98,33-57 +38-77,37-76 +13-96,11-14 +28-98,29-99 +4-44,2-45 +37-95,37-95 +53-68,54-68 +3-72,4-88 +14-21,21-82 +43-65,42-47 +21-52,51-93 +65-79,56-65 +22-65,36-77 +42-54,53-99 +10-95,9-95 +1-78,2-76 +81-81,16-81 +26-49,29-70 +45-87,44-87 +14-15,14-67 +14-15,14-84 +2-42,41-79 +1-93,93-94 +61-98,60-97 +3-9,12-99 +48-62,49-56 +64-65,41-64 +27-52,27-51 +88-91,30-89 +17-46,46-46 +10-80,11-81 +3-7,1-6 +80-81,80-96 +67-96,67-96 +96-98,2-96 +89-90,60-89 +16-83,16-82 +67-77,68-70 +33-68,33-37 +8-29,1-7 +28-71,28-70 +64-89,79-89 +18-35,18-36 +20-86,20-88 +68-69,40-68 +24-74,23-82 +79-88,79-88 +5-56,4-55 +1-98,2-2 +10-99,4-24 +12-15,13-15 +30-93,99-99 +6-14,6-97 +3-86,4-52 +15-52,13-52 +28-78,78-79 +27-28,27-95 +40-88,29-96 +1-3,2-52 +27-43,27-33 +23-65,54-99 +4-49,48-50 +46-67,47-68 +94-94,2-94 +24-31,30-66 +14-87,6-58 +2-5,4-98 +40-92,39-91 +2-87,87-88 +7-74,19-73 +58-91,58-92 +11-82,5-97 +62-65,4-62 +95-96,8-95 +6-68,6-68 +5-98,4-10 +46-72,89-96 +24-58,58-92 +33-36,37-94 +47-48,48-76 +18-20,3-32 +11-65,10-58 +32-97,32-95 +27-53,19-75 +10-60,45-59 +32-62,63-65 +1-88,2-88 +48-93,63-92 +46-79,78-96 +18-47,4-17 +6-63,6-7 +39-80,36-69 +20-78,20-78 +26-81,26-27 +23-96,20-79 +45-59,45-60 +37-47,36-64 +33-45,5-42 +3-98,4-98 +18-75,4-19 +10-93,10-10 +24-70,7-24 +20-53,40-49 +56-81,72-80 +1-70,4-71 +15-18,19-92 +84-98,83-95 +35-48,47-49 +27-63,62-78 +9-79,14-95 +5-77,4-77 +20-39,20-40 +26-67,72-92 +69-80,55-80 +68-83,68-92 +87-87,6-86 +1-92,1-94 +14-81,14-99 +49-51,49-52 +71-71,49-70 +69-70,69-72 +18-54,3-50 +97-99,1-97 +4-34,33-33 +67-84,11-84 +31-52,47-51 +81-97,97-98 +27-62,54-61 +1-83,1-99 +30-78,30-78 +12-38,12-37 +37-76,24-74 +1-96,1-95 +96-96,12-96 +41-61,40-60 +81-87,81-99 +1-19,9-19 +2-8,7-42 +35-36,36-71 +67-79,67-78 +69-69,65-68 +8-8,9-64 +4-12,6-78 +14-47,47-64 +9-93,9-93 +95-98,15-29 +30-87,88-88 +12-77,78-78 +8-15,18-50 +10-98,11-97 +1-2,1-97 +2-92,2-86 +50-50,50-87 \ No newline at end of file diff --git a/day4/src/main.rs b/day4/src/main.rs new file mode 100644 index 0000000..32888b2 --- /dev/null +++ b/day4/src/main.rs @@ -0,0 +1,175 @@ +/* +--- 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? + +Your puzzle answer was 515. + +--- 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? + +Your puzzle answer was 883. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +*/ +use std::collections::HashSet; +use std::error::Error; + +fn expand_range(range_str: &str) -> Result, Box> { + let parts = range_str.split_once('-').unwrap(); + + let begin: u32 = parts.0.parse()?; + let end: u32 = parts.1.parse()?; + + Ok((begin..=end).collect()) +} + +fn parse_line(line: &str) -> Result<(HashSet, HashSet), Box> { + let splits = line.split_once(',').unwrap(); + Ok((expand_range(splits.0)?, expand_range(splits.1)?)) +} + +fn find_intersection(s1: &HashSet, s2: &HashSet) -> HashSet { + s1.intersection(s2).copied().collect() +} + +fn fully_contains(s1: &HashSet, s2: &HashSet) -> bool { + let isect = find_intersection(s1, s2); + isect == *s1 || isect == *s2 +} + +fn part1() -> usize { + let input = include_str!("../data/input.txt"); + input + .lines() + .map(|l| parse_line(l).unwrap()) + .filter(|(l, r)| fully_contains(l, r)) + .count() +} + +fn part2() -> usize { + let input = include_str!("../data/input.txt"); + input + .lines() + .map(|l| parse_line(l).unwrap()) + .filter(|(l, r)| !find_intersection(l, r).is_empty()) + .count() +} + +fn main() { + println!("Part1 answer {}", part1()); + println!("Part2 answer {}", part2()); +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn test_parse_line() -> Result<(), Box> { + let input = "2-4,6-8"; + assert_eq!( + parse_line(input)?, + (HashSet::from([2, 3, 4]), HashSet::from([6, 7, 8])) + ); + + Ok(()) + } + + #[test] + fn test_find_intersection() { + let lhs = HashSet::from([1, 2, 3]); + let rhs = HashSet::from([2, 3, 4]); + let expected = HashSet::from([2, 3]); + + assert_eq!(find_intersection(&lhs, &rhs), expected); + } + + #[test] + fn test_fully_contains() { + let lhs = HashSet::from([2, 3, 4, 5]); + let rhs = HashSet::from([3, 4]); + + assert!(fully_contains(&lhs, &rhs)); + assert!(fully_contains(&rhs, &lhs)); + + let rhs = HashSet::from([4, 5, 6]); + assert!(!fully_contains(&lhs, &rhs)); + assert!(!fully_contains(&rhs, &lhs)); + } + + #[test] + fn test_part_1() { + let input = include_str!("../data/input-test.txt"); + let result = input + .lines() + .map(|l| parse_line(l).unwrap()) + .filter(|(l, r)| fully_contains(l, r)) + .count(); + assert_eq!(result, 2); + } + + #[test] + fn test_part_2() { + let input = include_str!("../data/input-test.txt"); + let result = input + .lines() + .map(|l| parse_line(l).unwrap()) + .filter(|(l, r)| !find_intersection(l, r).is_empty()) + .count(); + assert_eq!(result, 4); + } +}