From 80e9066b1d22ebe98f1d2ae272ddee422a718034 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Sun, 4 Dec 2022 10:40:33 +0100 Subject: [PATCH] Rust Day 4 Now with tests, very neat --- rust/Cargo.lock | 2 +- rust/Cargo.toml | 8 +- rust/src/day04/input.txt | 1000 ++++++++++++++++++++++++++++++++++++++ rust/src/day04/main.rs | 34 ++ rust/src/day04/part_1.rs | 54 ++ rust/src/day04/part_2.rs | 49 ++ 6 files changed, 1144 insertions(+), 3 deletions(-) create mode 100644 rust/src/day04/input.txt create mode 100644 rust/src/day04/main.rs create mode 100644 rust/src/day04/part_1.rs create mode 100644 rust/src/day04/part_2.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9686bdd..084ad4f 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "advent-of-code" -version = "22.3.2" +version = "22.4.2" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index f9d300e..6c4761f 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-of-code" -version = "22.3.2" +version = "22.4.2" edition = "2021" resolver = "2" @@ -37,4 +37,8 @@ path = "src/day02/main.rs" [[bin]] name = "day03" -path = "src/day03/main.rs" \ No newline at end of file +path = "src/day03/main.rs" + +[[bin]] +name = "day04" +path = "src/day04/main.rs" diff --git a/rust/src/day04/input.txt b/rust/src/day04/input.txt new file mode 100644 index 0000000..624e5b7 --- /dev/null +++ b/rust/src/day04/input.txt @@ -0,0 +1,1000 @@ +22-65,22-66 +91-94,63-91 +6-88,5-31 +85-86,64-86 +40-65,40-65 +25-82,24-94 +68-69,68-92 +60-91,89-92 +3-72,3-73 +44-85,84-85 +15-72,14-72 +84-92,84-92 +10-84,3-10 +32-62,31-61 +14-98,98-99 +24-78,24-61 +2-5,5-89 +58-95,58-59 +39-93,92-93 +11-82,81-82 +12-37,13-58 +53-92,52-91 +1-19,3-18 +90-91,91-98 +20-72,19-71 +91-91,1-92 +71-90,71-93 +77-94,77-94 +48-76,48-76 +48-58,48-53 +3-42,2-14 +3-4,3-94 +4-9,7-8 +14-97,77-97 +66-66,17-65 +12-99,13-98 +29-74,30-74 +30-93,29-92 +43-44,42-46 +7-9,8-81 +21-77,1-89 +85-92,86-91 +56-72,73-73 +16-75,17-42 +2-85,1-85 +97-97,13-98 +3-94,3-33 +55-91,1-99 +14-21,14-15 +1-3,2-98 +43-57,43-56 +9-95,8-10 +18-80,81-92 +19-72,19-71 +17-94,62-86 +45-59,46-60 +73-88,46-87 +63-82,82-86 +63-88,87-98 +20-28,20-56 +76-76,70-75 +36-73,35-73 +85-91,85-90 +4-82,4-85 +2-2,1-98 +27-71,27-81 +13-79,13-14 +32-43,33-42 +53-97,98-99 +41-62,42-66 +23-78,23-77 +6-92,6-91 +1-86,1-85 +3-89,84-90 +5-79,78-80 +51-73,50-72 +48-81,49-82 +32-33,33-88 +4-97,4-97 +13-88,14-65 +33-74,32-75 +26-48,48-78 +1-46,16-33 +2-6,6-67 +5-93,5-93 +5-6,4-22 +68-83,38-84 +10-70,18-69 +10-77,70-76 +4-94,3-93 +22-95,21-99 +32-72,31-92 +30-75,29-74 +3-96,95-95 +96-96,61-81 +52-77,53-77 +1-2,1-94 +4-96,3-98 +29-58,30-56 +31-49,30-60 +33-79,27-80 +10-51,8-11 +8-36,8-36 +36-93,36-92 +9-60,10-15 +48-80,79-87 +39-41,40-49 +48-87,64-87 +30-34,2-57 +81-82,13-81 +3-15,15-99 +37-72,38-72 +10-85,5-84 +6-79,78-93 +75-80,76-84 +55-72,55-55 +8-66,9-9 +54-64,63-99 +6-77,7-48 +2-64,1-32 +31-91,90-90 +19-66,18-94 +58-75,26-76 +58-60,1-59 +90-90,87-91 +5-91,5-91 +67-67,3-66 +13-81,31-36 +31-61,23-60 +18-92,17-91 +85-86,15-86 +10-68,9-11 +52-85,12-84 +7-27,26-26 +43-50,21-50 +25-75,74-75 +7-7,6-24 +1-63,42-62 +11-12,11-66 +25-94,93-93 +13-48,31-47 +3-58,59-59 +1-54,1-1 +5-5,5-96 +59-70,7-71 +18-19,18-81 +9-96,10-84 +33-41,33-38 +1-99,1-98 +69-83,68-78 +19-82,8-81 +2-23,3-24 +57-90,57-98 +75-83,74-76 +68-68,16-69 +2-86,1-33 +5-92,6-14 +51-76,52-75 +25-72,24-46 +9-76,10-50 +5-51,23-52 +19-83,20-93 +33-69,44-70 +22-69,23-69 +55-61,40-62 +2-16,16-81 +66-98,66-96 +27-85,28-86 +43-93,88-94 +19-83,32-82 +2-92,26-52 +87-87,45-93 +40-81,97-99 +4-97,98-98 +19-95,6-20 +52-96,53-53 +2-34,2-97 +62-83,63-66 +9-74,8-81 +53-83,53-83 +35-73,11-72 +9-97,10-95 +9-51,10-50 +25-27,26-35 +45-60,45-46 +12-65,28-65 +40-92,40-91 +5-5,4-98 +72-91,90-91 +16-87,17-91 +97-97,18-96 +77-78,75-79 +22-48,32-47 +70-70,35-69 +45-98,99-99 +65-83,83-92 +2-95,4-88 +5-10,10-46 +4-84,50-85 +58-58,15-59 +11-80,3-22 +46-48,3-47 +29-80,30-30 +34-96,5-95 +65-97,32-96 +26-31,30-46 +12-12,12-77 +26-92,26-92 +29-92,29-90 +26-27,27-30 +4-30,3-29 +29-57,1-28 +99-99,42-97 +92-97,78-98 +3-90,46-91 +44-76,43-75 +24-28,10-25 +1-95,1-52 +3-3,3-61 +39-94,40-93 +41-86,75-94 +28-28,26-27 +21-94,21-97 +12-41,41-54 +4-49,3-5 +75-90,44-75 +4-83,78-82 +5-93,93-94 +33-90,89-90 +12-32,13-98 +41-99,98-99 +4-67,3-38 +95-95,11-95 +7-46,7-46 +11-18,18-24 +7-92,12-57 +60-91,90-91 +86-94,86-95 +11-58,57-57 +7-10,10-97 +1-46,3-46 +1-52,1-2 +16-94,74-93 +7-98,18-97 +8-76,8-74 +37-92,54-91 +63-86,88-96 +4-55,47-58 +70-92,91-95 +73-77,71-77 +3-99,5-99 +56-58,56-57 +53-73,13-74 +88-97,97-97 +52-63,51-60 +13-94,14-91 +2-93,3-99 +42-75,67-75 +38-39,38-88 +8-93,92-97 +13-32,17-31 +3-17,16-53 +1-1,3-95 +5-88,4-87 +76-89,47-77 +5-54,4-55 +45-95,90-96 +81-89,36-88 +31-36,32-60 +87-99,10-88 +17-89,47-88 +26-94,25-99 +43-90,42-43 +59-87,59-80 +34-43,37-41 +1-54,1-53 +2-91,2-3 +40-95,46-95 +8-14,14-55 +38-52,51-69 +16-62,15-63 +30-55,31-74 +46-59,10-74 +35-81,80-88 +10-86,10-98 +18-68,18-68 +5-50,5-92 +48-98,99-99 +6-71,4-72 +6-6,6-43 +27-39,26-36 +15-15,14-82 +91-98,29-92 +46-51,50-52 +5-99,5-98 +4-91,4-92 +82-83,81-85 +68-78,21-68 +32-70,69-93 +25-59,25-60 +40-52,40-40 +28-68,93-94 +13-99,50-98 +10-33,10-11 +11-46,8-11 +20-32,19-31 +9-63,3-62 +24-92,92-97 +57-68,68-69 +39-91,39-91 +57-97,49-83 +2-58,21-92 +14-99,15-98 +42-90,43-51 +51-51,20-51 +29-43,39-43 +8-91,7-99 +11-75,11-76 +33-98,34-97 +99-99,28-97 +35-82,11-35 +80-82,45-81 +52-56,53-55 +29-77,28-93 +6-95,1-94 +12-77,12-13 +28-90,27-29 +71-74,72-77 +4-97,4-97 +40-85,39-60 +36-38,25-38 +25-35,24-28 +20-57,51-54 +80-81,3-81 +18-98,97-98 +82-94,71-83 +5-95,6-87 +16-78,3-79 +3-74,4-75 +14-87,13-42 +93-93,8-94 +7-20,9-21 +6-76,6-7 +4-98,69-99 +67-95,94-94 +66-87,79-86 +45-94,45-46 +9-13,10-46 +71-84,8-83 +3-56,4-82 +35-89,36-61 +54-77,53-55 +46-95,46-94 +9-19,7-19 +18-19,19-42 +37-54,53-54 +14-91,14-90 +9-35,9-36 +20-20,20-45 +96-97,25-83 +11-55,10-10 +10-95,11-89 +30-34,30-86 +30-37,35-38 +70-71,53-71 +6-12,6-10 +4-34,34-95 +48-79,48-72 +5-99,6-6 +60-77,78-94 +64-64,13-63 +13-31,14-30 +7-23,8-24 +87-92,88-93 +34-81,35-74 +7-58,8-77 +3-98,3-99 +7-94,94-95 +3-12,2-88 +21-97,22-72 +13-77,95-97 +15-77,15-16 +67-82,68-83 +28-28,25-56 +51-52,33-52 +12-90,12-90 +24-72,25-99 +1-56,3-55 +40-84,39-84 +8-9,8-86 +31-71,31-72 +76-90,64-75 +19-90,91-91 +42-70,45-69 +51-93,80-92 +21-36,22-37 +96-99,97-97 +83-99,1-98 +40-43,34-61 +1-97,2-96 +1-92,1-91 +11-92,9-73 +57-57,56-66 +13-71,12-75 +70-89,70-71 +12-24,16-24 +10-35,56-63 +18-85,51-76 +20-46,1-45 +4-64,4-64 +48-52,48-50 +84-85,17-85 +48-64,63-64 +19-28,27-29 +4-16,3-17 +29-66,2-28 +8-88,55-70 +60-84,61-85 +8-69,8-69 +15-67,3-68 +42-95,96-99 +21-55,22-54 +64-70,19-71 +17-82,17-81 +4-80,3-81 +7-95,7-73 +12-34,4-33 +3-95,63-96 +1-96,93-94 +45-50,36-49 +63-89,45-64 +8-96,9-87 +93-93,1-92 +10-11,10-11 +2-6,6-99 +40-41,40-68 +4-91,3-90 +4-68,66-68 +37-84,36-85 +15-85,29-86 +77-79,53-78 +50-81,80-81 +51-66,50-72 +53-53,29-54 +42-78,5-77 +35-53,36-76 +17-17,18-18 +4-79,4-79 +23-38,23-38 +46-81,80-80 +26-93,25-37 +59-61,20-60 +82-96,82-94 +16-40,17-21 +5-95,5-98 +26-26,26-64 +32-40,32-75 +3-55,6-54 +28-86,28-86 +4-87,4-96 +4-88,24-99 +4-66,5-5 +9-61,8-89 +48-70,48-70 +36-37,14-37 +21-49,21-50 +57-62,14-35 +8-62,8-66 +14-96,11-97 +13-44,43-44 +12-87,11-86 +2-24,25-97 +3-83,2-98 +10-80,10-80 +4-94,2-4 +1-77,20-77 +28-79,17-79 +8-82,7-7 +6-87,77-82 +4-93,3-65 +94-94,38-95 +74-92,74-93 +13-68,12-69 +41-51,41-42 +22-93,21-23 +7-12,13-64 +58-87,51-88 +18-66,19-91 +1-70,71-71 +9-17,8-37 +3-32,4-97 +54-90,27-91 +1-94,3-93 +3-99,4-70 +16-92,16-91 +22-57,22-57 +4-78,31-53 +28-94,28-29 +19-48,16-18 +48-68,49-67 +23-39,22-24 +12-87,23-95 +36-47,37-46 +66-69,66-68 +35-94,36-93 +13-22,2-20 +41-53,52-54 +4-51,3-96 +52-52,5-53 +20-85,19-84 +4-78,3-79 +86-98,15-97 +68-85,68-87 +2-55,3-54 +27-63,11-64 +75-83,52-83 +18-75,19-74 +7-88,8-87 +1-86,1-92 +6-43,38-44 +71-71,70-71 +6-43,24-83 +15-53,15-63 +69-97,69-69 +9-99,12-13 +31-41,41-42 +11-53,10-57 +46-67,14-70 +24-35,25-36 +13-91,23-90 +62-67,68-70 +62-99,63-68 +10-84,51-85 +5-98,1-98 +16-97,81-98 +51-92,41-52 +15-88,14-93 +3-92,2-93 +44-74,44-74 +4-95,94-96 +51-62,25-63 +28-69,11-68 +3-82,3-78 +5-95,5-98 +25-96,25-99 +9-9,9-66 +17-18,1-18 +2-86,4-85 +4-9,9-92 +2-98,2-98 +61-96,60-98 +1-89,35-50 +89-89,25-90 +1-99,1-33 +53-73,52-74 +30-55,29-90 +97-98,70-91 +66-69,65-66 +31-83,82-83 +2-67,67-95 +77-78,77-96 +30-98,30-98 +94-94,28-95 +37-83,8-82 +14-46,28-30 +75-80,12-81 +19-68,18-19 +65-85,96-99 +62-63,3-63 +19-95,97-97 +54-79,55-78 +12-98,97-99 +83-98,41-84 +68-88,34-69 +25-25,20-25 +2-66,8-99 +84-85,21-85 +52-98,53-87 +32-55,31-54 +4-51,6-90 +46-86,47-47 +75-75,10-76 +30-38,29-49 +5-96,4-5 +49-49,49-93 +8-67,39-65 +6-51,5-70 +38-70,39-69 +2-98,2-3 +51-53,50-59 +56-92,57-91 +11-90,11-52 +19-99,20-98 +54-77,52-55 +6-93,1-6 +62-80,58-62 +27-82,26-97 +10-79,9-78 +18-99,18-99 +7-87,7-84 +44-44,45-86 +89-91,6-90 +15-98,15-95 +56-59,54-60 +61-89,2-82 +30-78,12-96 +10-84,1-10 +61-62,46-62 +5-74,5-6 +29-33,30-32 +81-95,95-99 +51-98,50-85 +6-79,49-78 +47-52,12-61 +4-58,3-89 +75-94,31-76 +43-49,42-44 +3-4,4-47 +44-98,98-98 +67-72,66-72 +2-38,3-38 +3-5,4-47 +5-95,5-95 +34-97,35-78 +66-68,67-98 +28-32,29-35 +44-68,31-44 +28-53,27-55 +16-73,17-67 +12-49,49-97 +34-76,75-75 +38-96,37-43 +15-20,20-79 +15-22,22-35 +3-25,4-55 +6-90,6-89 +17-74,18-75 +15-60,14-61 +6-29,29-81 +23-66,11-24 +37-90,90-90 +1-24,2-57 +42-78,41-79 +94-99,74-95 +95-97,2-30 +13-61,56-60 +23-82,24-83 +56-56,31-57 +58-91,57-99 +13-25,14-24 +46-54,47-77 +47-74,73-74 +23-48,22-82 +79-80,81-81 +16-33,6-76 +54-70,55-70 +44-67,45-66 +2-4,3-97 +24-94,19-94 +33-77,49-93 +13-35,35-73 +28-84,28-83 +6-89,7-90 +40-80,42-80 +39-84,65-83 +83-83,59-84 +20-46,30-46 +31-34,32-46 +12-13,13-67 +35-75,34-74 +20-20,20-89 +13-70,5-60 +25-25,24-24 +84-87,83-86 +26-58,26-58 +6-96,5-7 +4-78,5-79 +15-15,15-27 +7-79,3-80 +62-64,24-63 +68-69,32-71 +27-68,28-41 +43-43,11-44 +27-82,83-83 +54-70,26-69 +16-16,17-70 +27-34,33-35 +33-97,96-97 +4-5,4-39 +43-43,43-92 +27-33,1-28 +23-91,23-58 +9-51,8-10 +4-6,5-76 +5-34,33-95 +97-98,46-98 +20-53,52-65 +7-72,6-96 +32-79,93-95 +21-86,20-85 +47-82,46-84 +9-96,8-86 +10-93,11-75 +11-12,11-77 +7-56,6-55 +3-7,7-43 +54-84,55-55 +3-95,3-97 +18-55,37-56 +40-68,39-91 +56-87,56-85 +12-34,13-35 +7-62,8-63 +95-95,54-95 +7-94,6-94 +14-92,13-99 +18-42,42-53 +75-96,76-95 +5-78,4-6 +93-94,14-94 +25-91,25-92 +96-98,97-99 +82-84,82-85 +37-39,38-42 +11-48,7-49 +64-66,65-90 +10-46,9-45 +35-92,34-52 +6-94,9-93 +12-96,95-96 +16-86,63-67 +5-96,5-96 +54-59,19-60 +3-92,3-52 +26-54,27-53 +28-97,28-93 +98-98,83-97 +64-98,13-99 +79-84,80-86 +5-98,4-97 +2-23,22-35 +7-11,7-87 +31-94,32-59 +25-41,17-42 +28-94,76-95 +6-93,5-94 +73-94,74-93 +6-44,17-44 +2-99,22-46 +1-73,1-72 +25-71,25-72 +35-36,6-36 +6-66,5-66 +92-95,92-95 +53-75,52-75 +5-55,3-97 +12-76,43-75 +5-49,13-48 +32-32,8-37 +18-96,19-19 +17-59,17-60 +1-17,2-95 +37-82,83-83 +39-79,39-39 +25-81,25-86 +11-52,51-51 +91-91,30-91 +43-58,44-57 +1-98,2-98 +31-62,31-62 +6-13,6-13 +65-92,13-91 +3-6,5-95 +21-88,14-21 +48-75,47-74 +70-93,30-92 +37-58,58-89 +14-24,14-23 +31-60,58-83 +4-95,1-56 +47-79,98-98 +36-65,37-38 +6-18,19-75 +14-50,98-99 +44-94,45-93 +37-39,38-56 +39-57,38-40 +7-36,6-90 +81-83,28-82 +15-86,15-77 +70-86,2-44 +8-23,7-23 +28-31,29-78 +5-67,4-66 +31-31,14-32 +45-95,13-96 +11-39,40-40 +6-95,21-94 +35-35,35-66 +4-97,4-98 +2-97,96-97 +14-14,13-50 +3-62,2-54 +3-98,54-98 +24-87,23-86 +29-84,28-78 +42-45,41-55 +8-32,10-33 +35-39,35-77 +10-89,23-59 +10-82,6-98 +52-84,83-84 +65-73,60-72 +7-75,74-91 +50-77,76-93 +1-94,2-95 +98-98,21-97 +19-79,18-24 +3-87,86-88 +47-47,41-48 +28-95,29-96 +71-85,70-72 +60-98,61-75 +3-7,7-96 +22-23,22-50 +90-92,38-91 +83-89,57-88 +6-86,7-87 +3-98,28-99 +52-62,15-62 +23-98,24-24 +38-86,87-94 +10-43,16-42 +17-25,18-76 +60-62,16-63 +66-89,66-88 +42-80,21-79 +6-91,6-93 +8-41,41-41 +80-87,79-99 +88-88,14-89 +35-76,23-75 +8-51,7-88 +2-99,41-71 +6-8,7-99 +18-83,18-19 +69-91,50-68 +28-43,28-29 +57-76,9-58 +56-58,52-57 +13-64,14-81 +6-84,5-83 +95-97,61-77 +9-11,10-12 +46-65,46-66 +18-83,17-83 +86-88,57-87 +15-74,79-99 +5-99,98-99 +13-16,14-69 +23-92,22-98 +70-96,69-98 +9-12,9-13 +10-12,11-52 +21-70,20-87 +44-88,44-84 +84-86,10-85 +30-92,30-91 +10-26,25-79 +42-59,41-42 +39-72,33-99 +19-66,39-66 +21-23,2-80 +23-23,22-23 +73-99,72-97 +4-72,3-5 +22-75,21-76 +24-53,52-52 +40-56,55-64 +17-85,18-84 +15-18,14-85 +8-71,3-8 +1-99,3-95 +41-41,40-96 +60-67,37-68 +9-26,10-91 +30-92,73-92 +44-85,45-86 +13-75,42-75 +11-11,64-83 +7-98,7-99 +53-57,37-54 +71-72,8-71 +86-86,7-86 +14-32,15-54 +13-20,12-12 +68-93,72-94 +3-68,3-4 +4-98,17-97 +15-94,41-83 +23-72,23-72 +26-86,2-27 +36-81,37-55 +10-94,9-93 +32-84,31-83 +44-95,14-45 +3-62,3-62 +46-78,45-66 +96-98,3-97 +91-92,31-92 +50-57,20-58 +1-64,3-64 +48-80,35-48 +69-90,69-91 +62-68,40-67 +74-76,74-76 +42-81,43-82 +16-58,58-59 +88-89,1-89 +68-69,69-90 +57-73,57-68 +58-83,58-94 +2-60,88-94 +14-18,13-20 +68-82,81-83 +77-79,26-78 +20-36,19-99 +12-87,13-89 +54-88,64-87 +12-85,12-86 +1-88,87-89 +43-64,64-81 +38-97,37-48 +12-73,57-74 +25-52,26-53 +22-22,21-37 +75-97,74-76 +13-54,14-32 +16-47,16-48 +9-93,10-92 +50-50,49-88 +10-10,10-76 +94-94,2-95 +33-93,63-92 +46-46,16-47 +90-97,1-91 +24-63,26-63 +38-84,19-62 +28-96,95-98 +56-56,49-58 +6-83,19-82 +31-87,77-88 +4-97,3-99 +13-86,14-85 +90-90,23-90 +8-25,26-26 +17-60,18-89 +10-61,11-62 +13-59,50-59 +52-73,34-73 +94-97,33-95 +20-93,19-97 +6-6,6-94 +7-81,6-89 +4-95,4-95 +9-65,65-66 +37-72,71-75 +57-96,56-72 +23-77,76-76 +7-89,6-88 +1-86,85-86 +42-44,43-93 +18-59,27-58 +19-88,18-95 +1-74,3-73 +22-67,54-66 +23-28,23-27 +6-73,6-73 +4-85,3-5 +46-95,46-96 +5-92,4-86 +22-61,21-61 +70-84,56-95 +8-9,8-85 +18-42,41-41 +12-97,96-97 +67-73,40-74 +13-74,13-81 +3-36,4-98 +8-96,5-6 +9-89,9-84 +67-85,68-84 +68-69,45-69 +34-66,66-71 +50-83,13-82 +31-74,32-83 +1-3,3-83 +4-88,4-4 +12-93,9-94 +70-98,30-98 \ No newline at end of file diff --git a/rust/src/day04/main.rs b/rust/src/day04/main.rs new file mode 100644 index 0000000..9620f8d --- /dev/null +++ b/rust/src/day04/main.rs @@ -0,0 +1,34 @@ +const INPUT: &str = include_str!("input.txt"); + +mod part_1; +use part_1::part_1; +mod part_2; +use part_2::part_2; + +fn parse_input() -> Vec<[[u32; 2]; 2]> { + INPUT + .lines() + .map(|line| { + let pairs = line + .split(',') + .take(2) + .map(|v| { + let boundary = v + .split('-') + .map(|v| v.parse::().expect("Non-numerical value in input")) + .take(2) + .collect::>(); + assert_eq!(boundary.len(), 2); + [boundary[0], boundary[1]] + }) + .collect::>(); + assert_eq!(pairs.len(), 2); + [pairs[0], pairs[1]] + }) + .collect::>() +} + +pub fn main() { + part_1(&parse_input()); + part_2(&parse_input()); +} diff --git a/rust/src/day04/part_1.rs b/rust/src/day04/part_1.rs new file mode 100644 index 0000000..b8a266e --- /dev/null +++ b/rust/src/day04/part_1.rs @@ -0,0 +1,54 @@ +#[inline(always)] +fn sections_contain_each_other(sections: [[u32; 2]; 2]) -> bool { + let start_1 = sections[0][0]; + let end_1 = sections[0][1]; + let start_2 = sections[1][0]; + let end_2 = sections[1][1]; + + (start_1 <= start_2 && end_1 >= end_2) || (start_1 >= start_2 && end_1 <= end_2) +} + +pub(crate) fn part_1(input: &[[[u32; 2]; 2]]) -> usize { + let times_contained = input + .iter() + .filter(|sections| sections_contain_each_other(**sections)) + .collect::>() + .len(); + println!("Part 1: {times_contained}"); + times_contained +} + +#[test] +fn test_containment() { + for num in 0..=1024u32 { + assert!(sections_contain_each_other([[num, num], [num, num]])); + } + + for num_a in 0..=1024u32 { + for num_b in num_a..=1024u32 { + assert!(sections_contain_each_other([ + [num_a, num_b], + [num_b, num_b] + ])); + assert!(sections_contain_each_other([ + [num_a, num_b], + [num_a, num_b] + ])); + } + } + + for num_a in 0..=1024u32 { + for num_b in num_a + 1u32..=num_a + 1024u32 { + assert!(!sections_contain_each_other([ + [num_a, num_a], + [num_b, num_b] + ])); + } + } + assert!(!sections_contain_each_other([[1, 3], [3, 83]])); +} + +#[test] +fn test_with_solution() { + assert_eq!(part_1(&crate::parse_input()), 584); +} diff --git a/rust/src/day04/part_2.rs b/rust/src/day04/part_2.rs new file mode 100644 index 0000000..66adda8 --- /dev/null +++ b/rust/src/day04/part_2.rs @@ -0,0 +1,49 @@ +#[inline(always)] +fn sections_overlap(sections: [[u32; 2]; 2]) -> bool { + let start_1 = sections[0][0]; + let end_1 = sections[0][1]; + let start_2 = sections[1][0]; + let end_2 = sections[1][1]; + + (start_1 <= end_2 && end_1 >= start_2) || (start_1 >= end_2 && end_1 <= start_2) +} + +pub(crate) fn part_2(input: &[[[u32; 2]; 2]]) -> usize { + let times_overlapped = input + .iter() + .filter(|sections| sections_overlap(**sections)) + .collect::>() + .len(); + println!("Part 2: {times_overlapped}"); + times_overlapped +} + +#[test] +fn test_overlap() { + for num in 0..=1024u32 { + assert!(sections_overlap([[num, num], [num, num]])); + } + + for num_a in 0..=1024u32 { + for num_b in num_a..=1024u32 { + assert!( + sections_overlap([[num_a, num_b], [num_b, num_b]]), + "{:?}", + [[num_a, num_b], [num_b, num_b]] + ); + assert!(sections_overlap([[num_a, num_b], [num_a, num_b]])); + } + } + + for num_a in 0..=1024u32 { + for num_b in num_a + 1u32..=num_a + 1024u32 { + assert!(!sections_overlap([[num_a, num_a], [num_b, num_b]])); + } + } + assert!(sections_overlap([[1, 3], [3, 83]])); +} + +#[test] +fn test_with_solution() { + assert_eq!(part_2(&crate::parse_input()), 933); +}