From afc49634018989d9a30373e4062bcf283c9be74b Mon Sep 17 00:00:00 2001 From: Adrian Rumpold Date: Fri, 16 Dec 2022 12:44:56 +0100 Subject: [PATCH] Python: Day 4 solution --- 04/README.md | 63 +++ 04/input | 1000 +++++++++++++++++++++++++++++++++++++++++++ 04/py/solution.py | 67 +++ 04/py/test_day04.py | 55 +++ 4 files changed, 1185 insertions(+) create mode 100644 04/README.md create mode 100644 04/input create mode 100644 04/py/solution.py create mode 100644 04/py/test_day04.py diff --git a/04/README.md b/04/README.md new file mode 100644 index 0000000..f41e748 --- /dev/null +++ b/04/README.md @@ -0,0 +1,63 @@ +# Day 4: Camp Cleanup + +## Part 1 + +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? + +## Part 2 + +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. diff --git a/04/input b/04/input new file mode 100644 index 0000000..220988f --- /dev/null +++ b/04/input @@ -0,0 +1,1000 @@ +82-82,8-83 +6-98,6-93 +56-77,55-82 +51-68,51-61 +4-90,3-67 +29-30,29-97 +42-88,13-87 +17-95,33-96 +11-56,12-56 +16-90,89-94 +74-79,78-80 +20-82,19-87 +4-86,5-85 +37-54,37-55 +15-92,16-91 +48-93,3-92 +23-47,7-47 +75-77,2-76 +35-88,35-87 +56-64,65-89 +17-80,18-79 +28-87,27-88 +6-62,61-63 +25-85,24-25 +10-72,12-72 +54-71,44-70 +20-96,97-99 +19-46,14-47 +7-97,3-6 +14-78,5-7 +98-98,40-98 +1-92,1-83 +14-52,13-68 +40-43,40-82 +13-96,95-98 +16-18,17-82 +69-79,3-70 +24-58,23-25 +96-96,2-96 +5-99,4-97 +7-11,12-95 +26-28,1-27 +4-35,2-7 +22-94,21-95 +15-18,18-83 +66-68,65-66 +31-36,36-71 +26-37,26-31 +55-55,9-56 +21-22,21-29 +9-98,8-96 +7-39,2-38 +84-84,55-83 +18-41,18-41 +18-84,18-87 +3-73,4-74 +16-61,16-91 +44-83,43-45 +7-39,38-68 +29-46,13-30 +43-77,20-78 +50-85,61-84 +49-74,90-92 +59-81,81-81 +41-81,81-94 +17-80,7-79 +43-96,90-95 +96-99,1-97 +7-89,6-8 +73-75,73-76 +15-55,14-15 +56-88,88-98 +7-10,9-94 +55-57,57-61 +16-85,17-52 +41-54,41-53 +30-96,30-30 +70-87,84-88 +83-93,96-99 +13-31,13-70 +7-26,26-95 +34-51,33-75 +51-94,13-99 +11-95,1-95 +6-95,5-7 +71-78,21-79 +9-80,9-97 +32-49,31-50 +79-79,1-80 +76-87,75-92 +2-97,3-96 +1-16,16-69 +1-97,99-99 +29-36,28-36 +6-9,4-9 +55-61,20-56 +16-78,15-81 +52-53,52-82 +13-71,9-72 +65-96,96-97 +56-68,55-56 +94-99,3-93 +96-98,13-49 +12-89,88-89 +14-93,93-93 +4-99,1-4 +48-67,44-48 +13-50,47-51 +3-4,3-90 +74-80,76-81 +49-70,18-50 +6-36,6-39 +59-87,58-88 +31-78,32-79 +90-90,80-91 +94-95,56-94 +5-5,6-83 +37-50,46-47 +79-81,80-83 +4-93,7-94 +90-91,9-90 +75-87,44-87 +41-93,40-40 +3-13,12-51 +13-92,5-14 +36-80,36-52 +13-95,13-14 +83-88,22-83 +3-68,2-84 +47-63,47-64 +77-91,39-92 +17-99,17-17 +3-72,3-4 +2-90,3-90 +3-90,90-93 +50-86,50-87 +28-63,28-32 +44-94,43-45 +39-48,48-53 +7-44,7-60 +3-87,86-88 +39-99,40-98 +15-17,16-18 +27-94,26-26 +35-81,34-36 +40-67,39-77 +9-35,4-9 +33-83,34-47 +1-99,10-99 +79-85,49-79 +10-12,11-21 +19-83,19-26 +27-84,25-28 +4-84,5-85 +11-62,61-94 +1-94,93-95 +22-28,99-99 +50-81,81-81 +23-82,22-24 +18-73,17-72 +74-98,34-74 +8-10,9-21 +27-43,43-44 +10-78,9-82 +94-96,27-93 +73-73,39-73 +17-45,17-98 +19-34,18-34 +2-82,16-24 +63-63,5-63 +34-94,34-96 +99-99,12-99 +8-24,9-97 +4-61,3-62 +1-99,99-99 +2-77,1-78 +52-81,31-51 +7-71,33-72 +2-63,15-95 +17-86,15-17 +54-71,72-72 +4-8,9-73 +45-76,45-65 +32-50,15-33 +14-96,15-99 +66-88,42-89 +13-26,25-49 +20-96,95-97 +39-63,44-64 +47-70,70-70 +89-89,14-89 +5-94,4-95 +9-31,8-80 +5-5,4-99 +77-79,8-76 +3-93,2-94 +97-98,14-83 +10-97,7-10 +28-53,14-53 +9-61,8-67 +7-25,24-43 +37-39,37-38 +73-74,73-95 +60-60,52-61 +33-46,32-45 +2-76,2-75 +88-88,5-58 +58-82,57-59 +43-58,57-59 +25-89,24-89 +1-99,2-98 +35-37,3-35 +14-96,32-96 +65-84,64-65 +86-86,86-92 +14-96,95-95 +99-99,68-98 +67-89,68-88 +94-95,47-94 +7-94,95-95 +11-84,17-85 +14-41,10-41 +12-14,13-90 +13-79,79-79 +8-94,94-95 +13-14,13-73 +6-85,7-64 +2-98,98-99 +6-99,7-98 +5-88,4-96 +4-98,36-99 +4-5,5-90 +33-95,53-95 +13-89,88-90 +80-82,3-81 +63-98,63-64 +1-98,32-97 +64-88,12-87 +1-32,6-33 +61-79,61-80 +2-16,1-95 +77-80,3-64 +6-36,7-35 +12-28,12-13 +54-88,53-54 +22-32,21-80 +97-99,10-98 +30-98,30-97 +17-21,18-20 +44-63,23-43 +19-90,18-19 +7-73,7-91 +25-49,48-48 +30-43,31-42 +13-52,14-51 +50-55,45-54 +27-36,27-35 +17-17,16-26 +11-94,10-63 +17-46,47-51 +49-83,49-52 +18-64,18-22 +4-96,4-93 +16-17,16-54 +52-52,22-38 +2-4,5-97 +8-19,18-87 +62-99,62-86 +92-96,41-73 +5-80,6-79 +81-82,12-81 +10-10,11-70 +71-72,7-70 +49-83,37-50 +14-48,48-76 +8-98,5-7 +21-85,22-86 +59-99,58-98 +8-98,4-98 +30-52,10-85 +12-87,13-88 +28-65,28-79 +25-70,29-97 +60-60,6-60 +11-13,10-12 +18-50,18-80 +65-65,52-66 +1-94,1-80 +33-99,99-99 +29-81,19-82 +37-97,11-37 +60-60,6-59 +3-62,3-63 +27-96,26-98 +2-82,1-83 +9-17,27-56 +8-65,8-9 +72-73,37-72 +1-8,2-98 +5-98,4-98 +21-98,22-98 +11-95,10-95 +52-98,3-97 +1-91,3-92 +7-34,7-42 +23-58,22-58 +3-84,2-83 +40-46,46-83 +42-54,43-71 +86-88,11-87 +26-82,26-27 +52-68,33-53 +24-79,24-64 +5-40,5-39 +10-89,1-4 +5-7,7-10 +41-79,41-80 +12-66,13-67 +82-86,75-83 +14-63,63-87 +20-64,15-21 +2-14,1-91 +15-48,8-49 +25-92,8-26 +12-99,99-99 +8-89,7-80 +1-3,4-26 +15-70,15-24 +85-85,35-63 +85-86,86-86 +3-93,4-92 +35-43,43-43 +44-72,66-67 +65-66,66-67 +1-96,80-97 +27-57,57-57 +26-87,86-97 +3-97,98-98 +98-99,18-77 +24-86,97-98 +8-39,39-73 +15-74,12-55 +17-19,18-94 +18-94,94-94 +4-34,7-35 +3-64,2-2 +19-73,18-74 +83-94,82-84 +3-94,2-95 +35-96,14-99 +24-25,23-27 +81-93,24-81 +3-89,39-89 +94-95,68-94 +98-98,82-99 +9-90,4-89 +8-77,7-9 +22-39,23-39 +75-85,11-76 +70-80,79-81 +72-74,71-73 +11-95,11-97 +4-95,94-95 +11-91,12-38 +9-31,32-57 +83-90,58-88 +11-78,12-78 +55-80,64-79 +41-48,39-41 +87-89,1-88 +14-34,35-71 +46-48,47-66 +12-37,12-12 +61-75,75-75 +8-47,9-93 +37-37,35-37 +4-4,1-4 +36-93,35-99 +7-90,8-89 +60-82,60-83 +69-74,30-73 +37-95,12-94 +8-65,74-95 +63-66,59-67 +16-38,19-35 +21-27,26-80 +19-71,11-70 +97-97,15-98 +45-49,44-48 +2-2,2-83 +54-80,23-81 +75-97,74-98 +72-83,1-10 +3-83,4-4 +11-93,92-97 +4-95,6-94 +62-72,61-63 +29-45,10-30 +35-35,36-83 +3-66,3-64 +58-73,36-72 +14-36,16-36 +26-86,25-27 +28-56,57-73 +4-94,3-93 +82-87,19-73 +29-60,14-61 +14-35,14-15 +9-93,10-94 +11-13,20-30 +2-79,1-92 +10-42,84-99 +47-47,47-62 +1-94,1-98 +35-42,43-89 +98-98,43-99 +40-98,40-97 +90-93,74-89 +28-55,55-56 +1-7,6-75 +2-94,94-94 +71-91,71-92 +21-98,99-99 +70-94,5-40 +13-94,30-95 +25-93,25-89 +79-84,34-46 +8-9,9-71 +43-70,22-70 +11-95,9-11 +31-43,13-30 +21-54,53-55 +36-88,54-89 +58-58,27-57 +78-96,97-99 +15-53,52-54 +9-72,4-9 +22-30,22-27 +5-96,5-95 +19-25,19-77 +66-90,66-97 +90-92,52-91 +14-14,15-26 +57-62,58-61 +20-62,62-62 +34-97,34-35 +12-92,13-13 +91-93,16-92 +23-83,82-84 +96-98,9-97 +75-76,75-82 +24-84,23-25 +2-29,2-30 +22-35,21-86 +43-92,66-92 +15-88,14-92 +32-37,38-67 +76-96,76-95 +86-88,2-87 +7-98,1-6 +35-93,35-94 +13-96,1-12 +24-69,23-70 +73-79,2-73 +14-89,14-73 +34-60,61-75 +14-21,15-22 +26-86,24-25 +30-96,31-93 +9-95,94-98 +83-85,3-84 +16-96,95-97 +12-13,13-35 +57-66,57-65 +18-59,19-60 +34-70,91-96 +74-90,58-84 +47-47,48-88 +47-48,15-48 +97-97,12-98 +57-89,57-71 +79-90,79-84 +14-36,6-35 +13-97,46-97 +49-94,93-93 +73-96,54-74 +29-31,21-38 +24-98,26-46 +71-80,78-81 +10-90,89-91 +65-99,66-98 +28-62,29-62 +4-97,96-96 +32-98,31-31 +51-63,52-62 +12-44,13-29 +8-28,1-26 +9-99,6-10 +36-64,35-35 +51-65,52-91 +26-26,27-32 +30-91,30-90 +34-35,34-98 +6-60,5-60 +11-12,10-12 +1-99,99-99 +80-82,79-82 +23-89,23-89 +39-90,89-93 +55-64,64-64 +19-19,20-82 +3-89,1-90 +2-73,74-92 +7-48,6-47 +3-58,7-99 +32-94,10-93 +54-56,53-56 +90-90,7-89 +73-73,34-74 +70-91,71-91 +11-82,81-84 +99-99,3-99 +18-98,19-90 +23-56,24-57 +59-59,58-59 +9-85,98-99 +41-58,41-73 +38-82,37-81 +42-97,41-42 +1-14,10-52 +10-93,11-93 +81-92,38-91 +15-97,14-94 +2-94,37-82 +27-98,28-97 +92-93,3-93 +79-95,18-79 +9-14,9-61 +3-99,18-97 +66-98,65-99 +27-42,26-28 +7-8,8-34 +38-97,16-38 +22-89,21-85 +18-69,5-59 +4-89,3-5 +17-24,17-98 +2-14,2-21 +33-77,53-78 +2-87,3-29 +29-35,32-36 +77-78,44-77 +3-93,92-97 +5-97,5-99 +3-21,21-21 +41-86,54-82 +7-96,95-99 +43-43,44-66 +16-18,17-67 +12-13,12-86 +7-74,2-73 +3-77,11-78 +1-27,28-44 +2-73,1-74 +3-96,3-97 +75-90,15-75 +14-39,14-66 +3-96,2-97 +27-80,28-79 +43-43,41-43 +17-69,16-70 +2-43,2-33 +5-98,5-91 +3-15,16-78 +59-85,59-72 +12-91,8-91 +25-25,25-73 +51-70,23-71 +14-55,15-56 +26-94,26-95 +17-92,7-17 +67-79,66-66 +54-67,67-67 +79-81,28-80 +59-63,64-64 +17-37,17-81 +9-93,9-77 +32-99,32-91 +20-20,11-21 +29-95,29-45 +7-20,19-78 +76-93,77-80 +9-9,9-43 +35-75,35-53 +23-81,22-81 +9-94,24-94 +16-84,3-85 +12-79,10-79 +2-4,5-92 +33-97,15-98 +7-40,11-40 +55-77,56-77 +47-63,46-62 +26-88,5-69 +4-98,3-98 +8-23,2-23 +2-67,1-68 +10-72,11-61 +52-55,54-56 +28-38,27-38 +1-2,1-93 +96-96,90-96 +7-89,6-78 +1-71,71-72 +1-34,33-33 +29-57,29-58 +17-99,2-98 +42-88,89-89 +22-27,26-69 +31-91,24-91 +6-29,5-6 +4-95,3-3 +8-94,5-8 +26-84,80-85 +37-92,37-91 +17-42,17-50 +44-73,23-74 +7-43,33-83 +12-30,29-57 +27-74,73-75 +23-63,63-97 +1-68,2-68 +32-92,32-94 +12-54,11-66 +64-76,65-95 +29-99,29-61 +11-34,10-34 +7-18,19-34 +35-86,34-86 +37-83,37-38 +36-37,36-92 +33-36,41-84 +5-71,3-70 +18-42,43-53 +85-87,20-86 +24-88,23-89 +57-96,11-58 +5-28,22-29 +8-88,7-94 +4-6,5-87 +5-23,22-90 +1-99,99-99 +18-32,33-91 +5-8,9-94 +2-6,6-44 +3-82,4-82 +24-62,44-63 +47-51,46-51 +98-98,61-76 +9-16,5-8 +41-76,75-77 +44-89,43-88 +2-88,1-91 +42-91,7-91 +23-87,87-87 +7-76,7-64 +82-91,80-90 +54-85,53-95 +57-89,76-89 +4-90,91-91 +43-43,44-88 +5-93,4-92 +65-85,15-41 +1-47,1-95 +10-91,10-92 +97-99,5-97 +1-99,1-2 +2-72,2-71 +5-6,6-11 +52-91,50-53 +70-96,71-97 +70-76,51-70 +2-13,1-44 +28-81,29-80 +35-79,36-79 +45-68,44-72 +85-94,3-76 +3-3,2-4 +33-54,54-63 +2-71,70-95 +32-35,37-44 +15-50,15-16 +7-62,6-97 +28-45,28-39 +53-97,52-52 +58-72,58-73 +10-86,9-86 +55-93,22-59 +32-61,32-98 +7-97,99-99 +13-49,37-50 +33-65,65-65 +22-37,21-69 +88-98,28-89 +41-92,42-75 +60-82,14-61 +3-91,4-28 +18-57,17-17 +34-96,35-99 +1-87,77-84 +2-8,7-95 +5-97,4-98 +46-46,47-87 +92-93,17-92 +36-74,37-48 +24-80,24-81 +6-10,10-99 +6-55,3-56 +8-74,19-63 +28-28,13-28 +2-19,19-64 +36-54,15-54 +2-80,6-80 +63-64,64-90 +20-97,20-72 +4-80,4-5 +15-17,16-71 +4-20,19-21 +23-49,13-62 +37-96,36-94 +12-13,12-63 +28-79,78-78 +3-79,3-4 +54-74,20-70 +29-91,8-30 +5-6,5-48 +14-61,13-62 +18-90,17-91 +89-99,69-89 +12-87,11-88 +22-66,51-65 +7-93,1-7 +59-69,55-81 +35-54,53-55 +52-97,28-53 +69-71,32-70 +18-85,84-86 +3-49,4-48 +26-27,26-40 +94-96,16-95 +5-50,76-95 +90-96,9-95 +84-85,13-75 +4-31,5-30 +1-87,1-89 +77-79,32-78 +25-79,5-41 +71-73,24-72 +22-67,23-66 +5-74,19-74 +82-94,23-83 +48-58,42-57 +3-84,2-93 +6-99,3-99 +6-89,7-16 +24-69,24-25 +16-80,81-92 +4-48,9-48 +1-98,2-70 +67-78,72-78 +27-39,26-28 +3-98,2-2 +95-97,9-96 +73-88,72-89 +14-94,95-95 +1-62,63-75 +12-76,11-11 +95-95,86-96 +19-38,1-23 +42-87,42-86 +7-75,8-74 +5-7,6-40 +49-52,49-66 +80-85,83-85 +7-44,8-43 +71-94,62-67 +97-97,26-97 +3-98,2-99 +29-29,30-99 +3-65,1-66 +2-3,4-29 +46-95,99-99 +91-96,13-90 +2-85,3-84 +40-87,40-70 +36-49,38-48 +30-49,34-50 +25-69,51-68 +90-95,71-81 +65-99,66-87 +14-63,13-63 +13-94,95-97 +91-91,55-91 +30-65,65-81 +18-44,43-70 +14-44,13-15 +73-94,88-89 +12-71,47-58 +47-84,73-84 +20-57,17-72 +30-50,30-30 +22-23,22-66 +14-38,32-43 +11-83,12-83 +21-58,11-58 +41-86,40-87 +5-73,72-82 +38-64,15-39 +38-39,14-39 +12-94,2-13 +39-55,56-56 +88-89,88-89 +2-7,2-99 +91-94,38-91 +12-81,11-94 +9-87,9-83 +79-82,31-79 +23-32,24-31 +9-99,8-99 +54-66,54-63 +33-96,95-97 +21-88,21-87 +15-81,17-80 +38-91,39-92 +22-59,59-94 +26-99,26-37 +1-91,2-98 +11-59,11-95 +35-76,61-77 +84-94,19-80 +49-55,55-74 +37-99,36-98 +47-85,46-46 +66-72,35-66 +61-96,32-96 +14-57,75-76 +3-97,2-97 +90-92,66-95 +89-95,88-94 +36-94,94-98 +22-24,23-40 +99-99,19-97 +19-86,10-85 +23-61,51-61 +63-63,8-63 +5-80,80-80 +7-7,8-90 +9-75,9-10 +14-68,17-67 +72-99,71-72 +19-78,13-79 +95-99,8-96 +31-33,32-32 +12-87,11-88 +22-56,56-93 +98-99,21-97 +2-24,3-13 +3-99,3-97 +13-88,38-68 +64-84,42-85 +37-70,36-36 +26-50,25-44 +43-73,42-79 +5-5,6-31 +52-57,42-58 +16-22,22-98 +9-76,8-89 +29-69,29-68 +10-11,11-85 +2-45,1-46 +29-51,41-52 +98-99,2-96 +7-97,94-98 +12-92,11-13 +79-83,41-82 +29-92,93-94 +2-71,4-72 +7-99,6-6 +8-99,8-89 +51-81,51-80 +73-80,62-80 +5-99,5-97 +22-66,21-65 +46-97,28-47 +13-72,18-71 +30-85,31-91 +69-78,78-78 +36-80,37-45 +2-65,1-1 +32-32,30-32 +4-90,2-91 +32-52,52-71 +9-21,6-20 +13-13,14-20 +11-98,12-92 +11-86,87-91 +24-62,25-62 +18-76,17-76 +17-93,18-95 +44-44,45-80 +6-61,5-5 +50-58,25-59 +91-91,37-90 +13-70,12-14 +91-99,17-90 +4-98,4-99 +34-87,35-79 +33-69,28-68 +13-28,7-27 +41-47,40-42 +83-83,84-85 +42-63,44-56 +6-94,94-94 +18-98,98-98 +30-32,31-65 +20-37,20-38 +18-55,40-41 +43-53,52-86 +27-30,29-81 +17-91,17-76 +1-7,7-95 +20-38,39-93 +70-73,4-77 +34-36,34-88 +65-94,93-96 +7-70,6-6 +19-65,21-64 +34-97,96-96 +71-84,70-77 +15-80,80-80 +12-97,10-11 +45-95,94-99 +72-72,2-72 +41-41,42-47 +62-76,77-77 +3-88,4-96 +21-83,20-84 +4-98,3-98 +96-98,57-97 +51-92,50-96 +37-90,36-38 +23-90,22-22 +35-76,76-76 +15-54,15-53 +37-81,37-38 +19-51,50-52 +3-18,16-71 +55-55,53-55 +34-92,35-91 +4-59,4-58 +15-94,1-93 +34-34,35-82 +27-96,27-56 +54-58,54-84 +90-91,6-99 +1-77,1-94 +9-98,10-95 +40-97,20-64 +49-62,82-98 +9-97,9-98 +16-84,16-99 +13-95,61-96 +15-98,7-99 +94-95,30-86 +6-23,6-66 +4-65,3-4 +41-92,45-93 +3-58,3-59 +42-49,48-70 +95-97,84-96 +10-42,9-11 +22-92,21-90 +46-46,21-46 +48-65,47-64 +4-14,14-67 +25-65,42-65 +38-41,5-40 +1-2,1-93 +40-78,13-77 +58-96,58-99 +69-97,69-95 +6-94,7-95 +32-43,31-53 +17-80,16-18 +22-65,64-64 +62-63,10-62 +3-99,2-98 +23-92,23-24 +92-92,19-93 +3-94,3-96 diff --git a/04/py/solution.py b/04/py/solution.py new file mode 100644 index 0000000..d092745 --- /dev/null +++ b/04/py/solution.py @@ -0,0 +1,67 @@ +import operator +from collections.abc import Callable +from dataclasses import dataclass +from pathlib import Path + + +@dataclass(frozen=True, slots=True) +class Range: + lower: int + upper: int + + @classmethod + def from_pair(cls, s: str) -> "Range": + left, right = [int(num) for num in s.split("-")] + return cls(left, right) + + @staticmethod + def _compare(a: "Range", b: "Range", _op: Callable[[bool, bool], bool]): + if _op( + b.lower <= a.lower <= b.upper, + b.lower <= a.upper <= b.upper, + ): + return True + + if _op( + a.lower <= b.lower <= a.upper, + a.lower <= b.upper <= a.upper, + ): + return True + + return False + + def fully_contains(self, other: "Range") -> bool: + return Range._compare(self, other, operator.and_) + + def overlaps(self, other: "Range") -> bool: + return Range._compare(self, other, operator.or_) + + +def part1(data: str) -> int: + count = 0 + for line in data.splitlines(): + left, right = [Range.from_pair(s) for s in line.split(",")] + if left.fully_contains(right): + count += 1 + return count + + +def part2(data: str) -> int: + count = 0 + for line in data.splitlines(): + left, right = [Range.from_pair(s) for s in line.split(",")] + if left.overlaps(right): + count += 1 + return count + + +# Setup +data = (Path(__file__).parents[1] / "input").read_text() + +# Problem solving - part 1 +result = part1(data) +print(f"Part 1 solution: {result}") + +# Problem solving - part 2 +result = part2(data) +print(f"Part 2 solution: {result}") diff --git a/04/py/test_day04.py b/04/py/test_day04.py new file mode 100644 index 0000000..72e85e2 --- /dev/null +++ b/04/py/test_day04.py @@ -0,0 +1,55 @@ +import pytest + +from solution import Range, part1, part2 + + +@pytest.fixture +def example() -> str: + return """2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8""" + + +@pytest.mark.parametrize( + "a,b,expected", + [ + (Range(2, 4), Range(3, 4), True), + (Range(2, 4), Range(1, 4), True), + (Range(2, 4), Range(2, 4), True), + (Range(4, 4), Range(3, 4), True), + (Range(2, 4), Range(3, 5), False), + (Range(4, 4), Range(3, 3), False), + ], +) +def test_range_fully_contains(a: Range, b: Range, expected: bool) -> None: + assert a.fully_contains(b) == expected + assert b.fully_contains(a) == expected + + +@pytest.mark.parametrize( + "a,b,expected", + [ + (Range(5, 7), Range(7, 9), True), + (Range(2, 8), Range(3, 7), True), + (Range(6, 6), Range(4, 6), True), + (Range(2, 6), Range(4, 8), True), + (Range(5, 7), Range(8, 9), False), + (Range(2, 3), Range(5, 7), False), + (Range(6, 6), Range(4, 5), False), + (Range(2, 2), Range(4, 4), False), + ], +) +def test_range_overlaps(a: Range, b: Range, expected: bool) -> None: + assert a.overlaps(b) == expected + assert b.overlaps(a) == expected + + +def test_part1(example: str) -> None: + assert part1(example) == 2 + + +def test_part2(example: str) -> None: + assert part2(example) == 4