diff --git a/2022/day_3/part 2 solution.py b/2022/day_3/part 2 solution.py index 66b2b10..623a95f 100644 --- a/2022/day_3/part 2 solution.py +++ b/2022/day_3/part 2 solution.py @@ -1,4 +1,4 @@ -with open(r'advent_of_code\2022\day_3\input.txt', 'r') as file: +with open(r'D:\repos\advent_of_code\2022\day_3\input.txt', 'r') as file: input = file.read() test_input = '''vJrwpWtwJgWrhcsFMMfFFhFp @@ -19,4 +19,19 @@ def task_1(file): ascii_start = 96 if common_item.islower() else 38 priority += ord(common_item) - ascii_start print("Task 1 result: " + str(priority)) -task_1(input) \ No newline at end of file +task_1(input) + +def task_2(file): + # group 3 lines together, find the common item between all 3 lines + # using ASCII + priority = 0 + #take the input and split it into groups of 3 lines + lines = file.split('\n') + grouped_lines = [lines[i:i + 3] for i in range(0, len(lines), 3)] + for group in grouped_lines: + common_items_set = set(group[0]).intersection(group[1], group[2]) + common_item = common_items_set.pop() + ascii_start = 96 if common_item.islower() else 38 + priority += ord(common_item) - ascii_start + print("Task 2 result: " + str(priority)) +task_2(input) \ No newline at end of file diff --git a/2022/day_4/input.txt b/2022/day_4/input.txt index b221142..72510c9 100644 --- a/2022/day_4/input.txt +++ b/2022/day_4/input.txt @@ -1 +1,1000 @@ -Paste any inputs into this file. \ No newline at end of file +71-71,42-72 +27-28,27-99 +15-79,14-78 +3-36,4-36 +5-67,32-57 +7-85,5-86 +2-97,97-97 +10-73,41-55 +34-99,34-55 +80-80,25-80 +50-76,50-79 +42-70,43-70 +95-96,81-96 +6-7,6-94 +37-39,17-38 +39-95,26-96 +52-77,53-76 +2-99,9-95 +23-94,79-91 +19-78,44-77 +47-47,1-48 +4-88,3-52 +51-60,50-52 +49-90,21-89 +76-95,97-99 +78-96,79-95 +90-90,77-90 +8-73,9-72 +54-54,54-74 +20-40,19-40 +7-96,7-7 +16-73,54-77 +8-30,33-59 +71-99,71-72 +22-74,21-74 +58-94,93-95 +92-96,34-94 +5-60,6-61 +2-78,41-79 +63-63,54-64 +21-55,78-88 +1-97,6-95 +14-52,14-73 +5-30,6-30 +4-82,3-91 +9-98,8-97 +1-99,9-95 +35-46,34-79 +2-3,2-45 +2-78,2-78 +11-57,56-57 +78-79,54-78 +17-81,18-43 +76-79,77-88 +4-90,11-91 +1-94,94-95 +57-93,56-92 +13-13,14-99 +42-50,10-50 +34-86,33-85 +2-25,1-95 +6-95,3-95 +5-31,8-30 +3-13,4-19 +10-97,9-60 +13-94,12-14 +9-93,8-93 +18-96,18-97 +53-94,53-74 +33-33,11-74 +58-76,76-77 +7-66,8-82 +38-76,37-77 +1-2,1-96 +24-98,23-99 +2-67,2-66 +30-84,1-30 +8-99,7-99 +89-94,78-89 +11-13,12-66 +75-83,12-75 +2-34,1-92 +36-38,36-93 +4-94,2-93 +6-95,6-99 +19-52,20-52 +49-95,48-94 +5-45,45-46 +3-96,4-84 +21-37,30-83 +33-67,67-67 +30-87,86-87 +73-73,7-73 +4-89,1-88 +5-60,60-61 +51-69,52-68 +3-92,2-3 +3-95,2-95 +13-73,70-71 +7-83,8-60 +62-94,94-94 +23-36,32-39 +17-52,45-53 +53-72,67-68 +48-95,49-49 +14-17,11-16 +70-97,70-96 +13-14,13-30 +9-9,8-97 +2-12,3-43 +79-79,37-79 +3-97,1-96 +56-77,55-77 +34-34,33-91 +2-80,81-81 +17-94,49-95 +25-84,13-85 +23-88,24-24 +1-35,2-23 +30-92,29-92 +37-37,37-38 +63-80,78-80 +4-83,1-83 +9-97,8-79 +2-98,3-88 +17-87,17-89 +37-38,38-60 +79-84,77-89 +5-96,96-97 +3-57,4-56 +50-91,46-50 +7-21,7-76 +79-96,25-79 +1-65,55-65 +16-93,15-97 +12-13,15-99 +68-97,18-61 +7-91,7-91 +16-89,15-90 +8-8,8-99 +28-28,10-29 +17-82,83-83 +17-74,19-73 +17-88,87-89 +11-31,10-32 +85-89,47-76 +34-61,34-35 +82-99,92-99 +14-41,13-42 +46-97,97-99 +3-51,4-51 +40-88,41-68 +58-64,55-63 +87-90,14-87 +7-39,8-9 +15-67,14-68 +21-95,20-92 +27-99,28-60 +50-87,50-88 +39-95,38-39 +67-67,66-67 +30-75,30-75 +54-89,53-90 +10-46,2-45 +30-90,12-94 +29-58,30-57 +22-22,21-73 +31-57,11-54 +10-65,10-89 +17-45,29-35 +43-58,44-73 +8-94,8-93 +26-43,8-26 +22-37,37-37 +26-57,13-56 +47-86,86-87 +89-89,58-89 +5-76,4-5 +96-96,30-97 +8-44,43-44 +22-78,22-77 +35-69,35-70 +7-93,6-59 +3-3,3-99 +23-99,23-98 +75-89,90-90 +13-97,97-97 +93-97,22-94 +36-46,45-46 +16-28,17-28 +26-78,78-91 +6-99,5-6 +55-94,40-94 +2-80,3-71 +13-13,12-94 +77-79,6-78 +1-80,5-74 +54-95,67-96 +23-89,80-89 +6-36,2-50 +2-3,3-65 +39-98,27-98 +59-66,35-78 +9-83,1-8 +41-66,58-67 +4-67,4-68 +9-77,77-78 +65-85,32-99 +43-94,31-44 +1-33,1-33 +5-50,5-19 +39-71,14-70 +1-99,53-99 +6-71,5-70 +17-88,18-88 +41-63,13-63 +4-4,3-89 +3-95,2-2 +15-21,6-21 +22-99,27-96 +1-87,56-71 +73-73,74-89 +65-95,66-94 +5-69,25-69 +21-86,21-55 +14-88,15-87 +20-33,19-34 +14-50,63-85 +7-23,22-55 +56-90,55-89 +60-85,60-61 +82-85,79-84 +66-66,99-99 +1-1,2-63 +6-61,1-6 +26-97,26-26 +36-81,76-82 +49-78,49-79 +17-99,12-98 +1-92,91-91 +11-97,99-99 +33-96,42-44 +8-71,3-76 +67-71,67-68 +49-72,49-71 +28-28,13-29 +38-68,67-67 +15-48,14-88 +16-37,16-36 +76-84,76-76 +5-96,26-97 +35-36,4-35 +1-96,2-97 +71-71,3-70 +37-87,37-86 +29-52,52-53 +12-79,16-80 +46-88,8-87 +8-91,8-84 +21-96,21-95 +17-97,7-96 +51-77,36-77 +13-21,6-13 +64-77,63-64 +4-8,2-44 +63-80,64-66 +51-90,52-89 +34-34,34-73 +3-81,1-81 +19-19,19-53 +68-73,14-51 +4-58,59-59 +8-30,12-30 +4-82,5-81 +26-99,27-99 +27-89,26-88 +38-62,55-85 +6-95,7-94 +18-56,17-55 +44-81,44-74 +16-20,14-19 +37-68,14-78 +6-11,7-10 +24-86,25-99 +10-97,1-97 +52-80,53-81 +28-64,28-63 +67-69,68-69 +3-5,3-5 +12-64,64-64 +3-4,4-71 +34-34,5-34 +29-36,28-74 +3-60,2-97 +36-93,35-92 +51-81,52-80 +13-80,10-96 +17-97,96-98 +90-92,19-91 +76-77,77-98 +42-42,48-73 +88-97,16-85 +70-75,37-74 +55-89,55-65 +38-73,41-72 +74-75,27-74 +32-43,41-43 +39-98,21-97 +43-71,42-43 +93-93,72-94 +19-45,19-44 +45-46,46-70 +2-70,2-69 +16-97,15-97 +26-63,25-62 +19-21,19-20 +7-98,10-98 +21-22,22-88 +51-93,49-92 +14-87,86-86 +37-50,36-72 +14-71,14-91 +27-29,28-73 +98-98,44-97 +20-41,19-24 +42-81,81-81 +54-55,10-55 +56-66,67-67 +46-77,46-78 +6-7,7-72 +74-95,69-76 +10-88,2-87 +45-84,44-84 +43-89,18-93 +19-77,19-76 +12-93,11-93 +60-61,18-60 +1-29,4-29 +39-52,25-51 +5-86,5-88 +3-95,5-90 +78-80,9-79 +29-56,29-55 +24-96,23-96 +19-93,92-94 +91-91,8-92 +53-58,22-67 +46-48,25-49 +22-28,29-29 +7-87,7-90 +14-94,13-14 +2-98,3-99 +14-80,13-81 +6-99,7-35 +3-86,2-87 +41-98,42-88 +15-15,11-15 +32-75,31-32 +11-45,16-26 +4-79,80-98 +90-92,91-93 +2-2,1-98 +24-65,23-23 +7-69,7-70 +76-84,77-83 +38-63,63-79 +11-11,10-46 +10-89,10-99 +16-73,16-72 +22-77,17-78 +99-99,25-99 +6-96,95-96 +52-90,52-62 +76-76,76-77 +14-88,14-88 +26-81,25-96 +53-81,81-81 +38-97,37-73 +16-81,15-15 +30-58,30-58 +61-89,5-89 +14-60,13-15 +22-93,23-94 +19-43,18-31 +6-87,1-87 +1-99,2-98 +3-84,75-84 +70-76,71-72 +15-15,15-39 +6-92,5-93 +3-32,2-31 +30-83,56-83 +78-78,79-98 +7-98,6-42 +18-57,18-57 +8-76,3-91 +12-97,13-98 +36-51,37-83 +3-95,1-97 +25-67,25-25 +14-79,14-33 +91-92,91-91 +3-80,2-81 +38-92,91-92 +93-98,93-97 +21-22,22-36 +31-89,27-30 +14-95,13-13 +25-36,24-36 +43-71,30-79 +37-37,36-83 +2-94,3-98 +2-89,60-88 +7-98,6-8 +66-70,65-70 +11-71,97-99 +9-95,8-94 +4-77,5-76 +38-85,37-86 +18-51,17-19 +13-13,6-13 +71-81,70-80 +46-81,47-47 +69-69,68-76 +37-56,37-56 +96-97,6-96 +53-59,54-58 +45-50,2-49 +5-33,6-6 +16-61,16-61 +13-51,3-51 +5-82,4-53 +5-81,54-81 +2-18,18-19 +29-29,5-29 +12-81,13-81 +2-71,16-71 +48-92,32-42 +8-12,8-75 +47-86,86-86 +5-97,4-96 +75-75,16-74 +13-95,8-97 +10-90,9-91 +48-62,47-63 +3-18,2-2 +1-18,17-81 +45-83,25-84 +36-91,37-92 +85-99,15-85 +39-85,2-96 +24-96,11-94 +12-89,11-58 +47-72,59-73 +31-91,30-81 +33-43,34-41 +16-83,15-83 +22-55,21-54 +39-82,26-97 +10-95,11-96 +2-98,4-79 +17-37,16-41 +76-76,25-77 +65-73,74-77 +9-82,82-83 +30-57,31-76 +4-74,5-34 +31-87,86-87 +65-74,15-73 +36-36,35-36 +1-99,1-2 +31-79,78-79 +6-88,7-96 +73-86,74-93 +31-49,49-85 +2-98,1-3 +14-57,15-58 +83-96,96-97 +6-92,6-93 +6-95,3-94 +16-96,26-92 +6-85,2-85 +38-39,38-92 +96-97,33-93 +12-13,13-51 +5-5,4-4 +1-95,1-95 +69-71,69-73 +27-98,26-99 +5-25,4-26 +43-68,68-69 +8-56,15-56 +3-99,4-98 +5-5,5-96 +17-71,18-32 +53-53,53-99 +12-18,14-19 +20-94,20-93 +15-62,14-63 +59-59,59-73 +99-99,11-96 +47-98,46-47 +4-86,86-86 +1-1,3-64 +19-45,18-84 +1-86,9-87 +45-45,32-46 +41-63,63-64 +3-5,5-38 +34-70,33-70 +34-91,3-92 +12-45,11-45 +29-30,34-86 +24-55,55-56 +6-41,7-99 +87-88,5-87 +5-63,6-6 +63-63,63-68 +2-95,96-98 +17-25,33-98 +40-99,41-82 +21-41,42-46 +50-86,12-91 +11-64,10-63 +21-21,21-39 +10-96,10-95 +18-95,18-94 +73-79,80-93 +14-69,15-70 +25-83,26-26 +83-94,61-82 +16-90,64-96 +2-3,3-21 +7-45,7-57 +6-85,5-84 +22-94,21-21 +5-64,5-63 +96-99,70-96 +18-87,17-18 +6-99,6-11 +49-49,2-48 +7-64,65-69 +41-46,14-47 +92-92,8-92 +6-96,5-24 +57-85,57-84 +39-99,40-99 +80-86,86-86 +52-91,94-98 +67-88,15-66 +15-39,40-40 +22-92,23-23 +31-84,74-84 +43-43,25-43 +16-90,15-85 +5-36,7-36 +53-58,52-59 +14-96,99-99 +3-94,2-95 +97-99,49-97 +7-67,54-67 +12-72,72-73 +69-76,75-75 +59-79,60-64 +72-99,78-92 +80-80,80-86 +25-26,25-88 +27-43,27-44 +27-76,18-75 +22-23,5-23 +52-69,53-70 +16-64,16-98 +3-51,5-96 +3-72,2-20 +26-26,27-37 +58-91,90-91 +14-20,11-44 +5-99,5-6 +31-32,31-99 +9-93,93-93 +21-81,20-80 +47-99,49-50 +28-89,27-28 +30-77,30-30 +91-93,59-92 +4-78,40-89 +38-95,39-78 +27-58,26-58 +32-92,24-32 +69-91,35-69 +31-95,13-94 +8-8,7-99 +14-93,8-93 +36-78,16-79 +7-53,10-52 +98-99,5-98 +9-15,10-14 +1-98,2-98 +2-60,3-60 +33-70,32-32 +83-97,82-89 +92-92,32-91 +1-97,1-96 +85-95,48-77 +3-26,4-4 +14-14,13-88 +13-90,14-91 +28-67,27-66 +69-75,76-76 +31-97,16-96 +21-47,46-47 +50-51,40-51 +17-97,8-97 +3-98,4-97 +58-97,59-92 +8-15,7-15 +94-95,4-94 +45-56,44-50 +21-44,22-43 +16-72,15-71 +65-84,65-84 +18-46,63-72 +61-71,41-54 +10-36,9-66 +3-68,3-69 +19-19,18-20 +69-70,4-69 +72-72,71-73 +30-95,30-31 +76-77,76-86 +21-71,21-66 +36-36,37-47 +17-98,98-98 +38-74,39-39 +1-95,3-94 +9-17,11-18 +1-93,1-94 +49-75,67-76 +83-91,90-91 +8-71,37-62 +31-51,22-50 +45-99,46-98 +8-32,6-32 +50-50,50-78 +75-80,75-81 +1-68,14-83 +13-90,6-95 +3-47,3-85 +25-47,25-25 +15-77,5-57 +7-69,48-69 +21-74,22-73 +48-84,83-84 +14-14,13-65 +41-96,20-98 +73-93,24-92 +14-45,15-15 +98-98,6-99 +34-63,33-34 +10-93,92-94 +2-97,31-97 +30-99,30-86 +6-89,89-90 +65-94,42-64 +38-40,39-74 +1-1,1-40 +9-26,25-25 +2-95,95-96 +14-93,10-92 +84-86,4-84 +21-21,20-92 +56-98,31-97 +4-31,3-31 +2-20,21-21 +12-91,13-91 +1-90,2-79 +3-4,4-80 +12-89,13-88 +6-88,88-88 +22-90,22-22 +18-87,19-35 +28-71,29-49 +39-52,52-53 +73-82,72-74 +70-98,30-99 +31-40,37-40 +10-28,15-86 +48-92,65-91 +10-96,9-11 +22-98,21-23 +2-98,3-21 +34-34,33-72 +23-42,2-58 +26-98,49-97 +65-95,29-94 +48-48,47-49 +23-66,22-65 +97-98,83-98 +32-47,31-53 +24-26,15-25 +82-88,89-89 +85-96,85-98 +22-76,24-77 +84-85,84-86 +26-27,26-98 +10-88,14-71 +73-81,1-72 +79-88,15-88 +74-86,64-87 +27-56,28-55 +37-38,36-89 +1-47,17-48 +50-94,45-76 +35-90,34-90 +7-98,6-6 +2-95,5-96 +60-95,60-98 +94-96,5-93 +25-97,28-98 +9-9,9-98 +6-15,15-83 +11-32,12-75 +7-97,7-99 +17-17,17-58 +46-95,45-94 +10-86,11-11 +41-43,40-43 +7-74,34-75 +8-97,9-97 +72-82,82-98 +19-20,19-38 +7-94,6-95 +9-48,10-97 +47-73,48-48 +32-93,8-99 +12-65,11-13 +43-51,27-50 +35-67,34-66 +9-28,9-28 +25-86,18-85 +92-92,11-93 +66-66,36-66 +59-60,12-59 +5-21,4-4 +40-93,40-93 +56-86,57-87 +2-97,1-2 +61-94,60-93 +36-64,35-65 +62-94,62-95 +27-99,27-27 +36-52,35-36 +31-91,30-31 +35-93,92-94 +2-98,1-3 +74-74,6-74 +46-79,80-80 +19-95,17-94 +15-16,16-83 +1-94,3-93 +79-79,10-79 +10-75,9-74 +2-84,1-83 +19-83,19-82 +13-14,14-34 +33-57,33-58 +3-7,4-91 +25-25,24-48 +46-47,44-46 +85-86,19-85 +40-42,37-42 +72-88,64-84 +25-68,25-25 +88-88,89-89 +3-14,4-15 +12-98,12-92 +5-90,38-91 +41-58,42-59 +24-63,64-84 +47-48,29-47 +30-62,62-77 +43-80,44-97 +90-99,5-90 +12-58,58-58 +18-66,6-87 +30-89,65-83 +23-93,93-93 +7-19,6-18 +12-99,13-57 +5-27,27-30 +3-4,4-75 +14-26,14-23 +91-95,34-94 +52-83,53-53 +16-24,13-23 +56-70,70-71 +10-80,80-81 +58-97,91-99 +16-82,46-83 +15-16,15-83 +26-81,27-80 +65-65,46-65 +23-93,24-92 +42-92,6-91 +25-65,25-25 +9-72,8-77 +36-94,35-37 +44-86,43-45 +51-89,50-90 +69-71,68-77 +10-36,28-37 +22-75,74-74 +38-38,37-94 +42-70,41-70 +3-92,2-72 +18-89,89-95 +82-89,6-89 +75-76,11-75 +12-26,19-27 +21-83,21-84 +2-97,3-99 +13-97,13-97 +89-92,89-89 +29-73,2-5 +41-60,50-59 +12-68,11-67 +26-59,26-58 +22-64,22-72 +31-74,30-75 +41-49,41-49 +26-92,19-92 +53-65,52-52 +47-54,53-55 +81-99,80-81 +22-81,23-81 +15-40,14-40 +5-96,4-96 +21-98,2-97 +13-57,6-56 +13-47,12-31 +66-66,58-65 +2-39,10-39 +11-82,10-81 +94-96,11-66 +5-61,4-60 +35-88,89-98 +58-80,58-81 +15-98,14-16 +33-99,32-34 +4-97,77-98 +14-15,40-47 +31-51,30-52 +28-97,29-97 +79-84,79-85 +19-64,19-63 +3-3,3-75 +36-72,37-92 +6-26,4-75 +4-4,3-13 +44-44,44-69 +3-93,4-93 +47-96,47-96 +15-54,16-41 +78-98,77-97 +53-55,32-55 +88-89,76-88 +16-84,15-95 +82-82,3-83 +43-44,28-43 +66-66,65-81 +6-96,5-95 +83-90,30-84 +61-72,39-61 +2-51,1-52 +11-18,10-24 +65-66,63-65 +7-97,1-96 +6-33,6-6 +50-89,51-51 +22-53,14-52 +49-97,48-98 +5-98,6-44 +15-85,14-84 +29-95,30-96 +75-76,42-75 +37-53,37-52 +3-84,3-3 +33-97,33-69 +49-98,63-99 +77-84,75-83 +3-77,1-76 +60-94,59-85 +79-80,3-79 +32-35,32-36 +30-31,31-89 +15-16,16-86 +8-98,8-97 +42-87,43-88 +34-64,20-63 +7-97,46-98 +4-19,19-67 +39-90,41-90 +40-78,78-79 +50-73,14-92 +58-87,59-60 +4-82,3-4 +61-63,60-68 +1-6,6-61 +17-19,17-18 +5-62,10-36 +15-40,14-39 +59-70,60-69 +93-95,37-92 +14-97,7-99 +91-92,55-91 +38-88,26-42 +6-72,5-7 +77-96,77-97 +5-84,84-84 +29-47,22-32 +53-54,52-53 +11-95,95-96 +7-40,21-95 +99-99,47-99 +18-76,18-82 +1-93,26-93 +37-37,2-41 +71-71,4-71 +26-97,26-26 +68-69,3-68 +96-96,24-97 +41-80,40-65 +54-99,67-95 +80-90,81-93 +35-59,35-59 +16-44,16-44 +2-78,19-79 +16-16,16-97 +2-38,2-2 +8-16,2-16 +47-72,48-73 +15-28,14-29 +4-90,5-5 +46-99,19-95 +66-86,66-77 +61-97,61-96 +30-93,47-93 +59-81,60-80 +21-22,21-22 +36-78,35-77 +35-93,35-35 +34-34,33-49 +24-69,1-68 +9-84,15-84 +11-11,10-94 +3-59,3-4 +25-69,68-68 +93-94,3-93 +72-98,85-98 +36-95,36-94 +36-85,36-86 +9-39,9-40 +97-98,34-97 +24-84,77-85 +37-85,38-38 +92-93,34-91 +44-69,43-78 +2-83,82-84 +19-37,40-62 +56-79,54-78 +60-60,60-97 +46-66,46-67 +18-76,19-86 +40-67,20-67 +13-98,14-97 +63-65,62-64 +22-88,21-89 +45-77,46-78 +14-52,14-53 +38-59,38-60 +1-21,21-21 +62-70,61-62 +50-56,23-99 +4-96,53-97 +68-69,19-68 +35-76,34-76 +29-76,30-77 +12-91,13-90 +7-94,96-99 +5-85,5-81 +56-57,57-58 +26-26,27-98 +12-28,11-58 +11-32,10-32 +10-42,10-56 +46-63,38-62 +31-95,3-94 +76-82,41-75 +87-88,4-88 +21-33,20-32 \ No newline at end of file diff --git a/2022/day_4/part 1 solution.py b/2022/day_4/part 1 solution.py index a61f5ee..a67580b 100644 --- a/2022/day_4/part 1 solution.py +++ b/2022/day_4/part 1 solution.py @@ -1,6 +1,48 @@ import os -with open(r'advent_of_code\2022\day_4\input.txt', 'r') as file: +with open(r'd:\repos\advent_of_code\2022\day_4\input.txt', 'r') as file: input = file.read() -print(input) +# print(input) + + +test_input = '''2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8''' + +lines = input.split('\n') +contained_pairs = 0 +for line in lines: + print(line) + line = line.split(',') + left_pair = line[0] + right_pair = line[1] + left_pair = left_pair.split('-') + right_pair = right_pair.split('-') + #print(left_pair) + #print(right_pair) + left_range = int(left_pair[1]) - int(left_pair[0]) +1 + right_range = int(right_pair[1]) - int(right_pair[0]) +1 + #print(left_range) + #print(right_range) + if (left_range == right_range) and (left_pair[1] == right_pair[1]): + #print('valid') + contained_pairs += 1 + elif (left_range > right_range): + # check to see if the right range starts and finishes within the left range + if int(right_pair[0]) >= int(left_pair[0]) and int(right_pair[1]) <= int(left_pair[1]): + contained_pairs += 1 + else: + contained_pairs += 0 + elif (right_range > left_range): + # check to see if the left range starts and finishes within the right range + if int(left_pair[0]) >= int(right_pair[0]) and int(left_pair[1]) <= int(right_pair[1]): + contained_pairs += 1 + else: + contained_pairs += 0 + else: + contained_pairs += 0 +print(contained_pairs) \ No newline at end of file diff --git a/2022/day_4/part 2 solution.py b/2022/day_4/part 2 solution.py index a61f5ee..07aead4 100644 --- a/2022/day_4/part 2 solution.py +++ b/2022/day_4/part 2 solution.py @@ -1,6 +1,51 @@ import os -with open(r'advent_of_code\2022\day_4\input.txt', 'r') as file: +with open(r'd:\repos\advent_of_code\2022\day_4\input.txt', 'r') as file: input = file.read() -print(input) +# print(input) + + +test_input = '''29-73,2-5 +2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8''' + +lines = input.split('\n') +contained_pairs = 0 +for line in lines: + #print(line) + line = line.split(',') + left_pair = line[0] + right_pair = line[1] + left_pair = left_pair.split('-') + right_pair = right_pair.split('-') + #print(left_pair) + #print(right_pair) + # right pair starts inbetween left pair + if int(left_pair[0]) <= int(right_pair[0]) <= int(left_pair[1]): + print('1',line) + contained_pairs += 1 + continue + # right pair ends inbetween left pair + if int(left_pair[1]) >= int(right_pair[1]) >= int(left_pair[0]): + print('2',line) + contained_pairs += 1 + continue + # left pair starts inbetween right pair + if int(right_pair[0]) <= int(left_pair[0]) <= int(right_pair[1]): + print('3',line) + contained_pairs += 1 + continue + # left pair ends inbetween right pair + if int(right_pair[1]) >= int(left_pair[1]) >= int(right_pair[0]): + print('4',line) + contained_pairs += 1 + continue + else: + contained_pairs += 0 + +print(contained_pairs) diff --git a/2022/day_4/puzzle_text.md b/2022/day_4/puzzle_text.md index 736c61d..58086ef 100644 --- a/2022/day_4/puzzle_text.md +++ b/2022/day_4/puzzle_text.md @@ -1 +1,57 @@ -# Day 4 Puzzle Text. \ No newline at end of file +# Day 4 Puzzle Text. + +--- 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? + +--- 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? \ No newline at end of file