From 5c4ac1622ac5287301e1b1f8a19481df2e6c04f0 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sun, 4 Dec 2022 16:17:48 +1030 Subject: [PATCH] 2022 Day 4 Python --- 2022/day4.py | 30 ++ 2022/input/4 | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1030 insertions(+) create mode 100644 2022/day4.py create mode 100644 2022/input/4 diff --git a/2022/day4.py b/2022/day4.py new file mode 100644 index 0000000..1eec81d --- /dev/null +++ b/2022/day4.py @@ -0,0 +1,30 @@ +import re +pattern = re.compile(r'(\d+)-(\d+),(\d+)-(\d+)') + +with open('input/4', 'r') as file: + lines = file.readlines() + +count = 0 +for group in lines: + x1, y1, x2, y2 = (int(x) for x in pattern.findall(group)[0]) + if (x2 >= x1) and (y2 <= y1): # Elf2 inside Elf1 + count += 1 + continue + if (x2 <= x1) and (y2 >= y1): # Elf1 inside Elf2 + count += 1 + continue +print(count) # Part 1 + +count = 0 +for group in lines: + x1, y1, x2, y2 = (int(x) for x in pattern.findall(group)[0]) + if (x2 >= x1) and (y2 <= y1): # Elf2 inside Elf1 + count += 1 + continue + if (x2 <= x1) and (y2 >= y1): # Elf1 inside Elf2 + count += 1 + continue + if (x1 <= x2 <= y1) or (x1 <= y2 <= y1): # Partial overlap + count += 1 + continue +print(count) # Part 2 diff --git a/2022/input/4 b/2022/input/4 new file mode 100644 index 0000000..1a03bfa --- /dev/null +++ b/2022/input/4 @@ -0,0 +1,1000 @@ +49-51,31-50 +96-99,2-95 +2-62,62-98 +34-76,10-59 +28-83,27-84 +40-41,41-86 +15-46,16-47 +53-93,54-93 +19-98,97-97 +29-52,44-71 +21-67,14-83 +11-93,11-12 +15-88,18-89 +5-87,6-6 +1-96,96-97 +64-88,64-91 +3-98,2-3 +33-87,34-86 +21-23,16-22 +63-95,63-99 +1-99,1-2 +11-26,26-39 +43-45,14-82 +11-94,65-98 +46-67,6-50 +26-64,17-63 +54-54,55-97 +60-93,60-99 +40-78,58-74 +27-64,2-33 +25-48,32-80 +65-83,64-66 +21-69,65-88 +37-74,36-75 +4-66,66-66 +6-14,6-47 +17-73,41-74 +79-95,20-92 +63-87,54-54 +72-93,24-72 +2-94,3-96 +9-94,8-48 +17-91,16-87 +3-84,9-84 +44-94,92-95 +55-56,56-96 +65-77,66-90 +7-40,7-30 +20-89,89-90 +14-15,17-45 +1-70,50-50 +28-92,91-92 +5-7,6-57 +32-50,36-67 +81-82,80-82 +58-89,13-59 +64-90,1-65 +29-96,28-29 +83-83,16-83 +14-84,85-85 +14-67,14-68 +41-58,40-42 +18-49,19-48 +66-85,26-72 +5-89,88-90 +5-97,5-6 +14-87,14-14 +26-76,47-92 +17-97,73-99 +28-88,27-28 +2-4,3-54 +3-98,16-16 +61-62,52-62 +22-53,21-22 +8-81,15-30 +5-75,4-74 +43-96,95-96 +39-41,41-90 +33-49,33-50 +10-12,11-81 +63-64,63-70 +10-55,10-91 +9-32,8-8 +60-61,60-77 +10-11,10-69 +2-77,65-76 +1-41,3-22 +28-70,29-71 +44-45,11-44 +1-99,2-98 +45-77,44-78 +15-96,95-99 +7-98,6-6 +28-98,49-99 +61-79,75-75 +13-84,46-62 +15-36,15-36 +54-80,80-93 +2-91,22-91 +32-85,32-33 +29-55,55-56 +45-66,45-46 +6-63,6-62 +2-91,5-91 +96-97,1-97 +39-60,31-60 +5-94,6-94 +8-94,7-8 +6-85,5-85 +12-88,11-26 +4-79,4-46 +1-8,10-96 +1-76,75-76 +6-81,77-82 +35-96,52-98 +29-82,29-83 +22-86,86-87 +17-57,17-63 +57-58,17-57 +48-99,48-97 +25-26,25-26 +16-17,17-93 +31-74,30-30 +18-25,18-94 +56-77,30-30 +8-61,50-61 +58-70,21-59 +2-3,2-70 +8-12,9-14 +5-98,97-98 +35-55,35-87 +20-60,20-36 +39-39,40-68 +39-76,40-88 +20-29,29-88 +61-62,61-84 +19-86,38-95 +56-68,1-71 +13-58,12-19 +12-54,12-68 +3-71,3-3 +71-72,35-71 +9-10,8-16 +20-71,21-70 +43-66,65-73 +3-88,4-87 +73-74,4-74 +62-97,15-95 +59-98,98-99 +10-90,89-99 +11-85,10-86 +4-6,5-6 +7-92,2-92 +54-78,78-78 +78-81,69-84 +18-44,17-19 +7-17,4-16 +71-90,70-88 +82-83,82-88 +47-72,39-72 +22-95,28-98 +98-98,68-98 +5-96,4-97 +76-92,1-77 +17-62,16-63 +20-41,20-83 +59-74,58-60 +8-83,8-9 +30-94,64-97 +18-67,12-67 +32-54,18-33 +31-81,31-81 +14-15,14-14 +89-92,18-90 +16-23,1-34 +1-98,1-51 +3-98,2-3 +16-97,97-98 +32-82,32-33 +1-11,2-12 +88-90,37-92 +2-15,3-15 +19-57,5-60 +15-17,14-17 +83-83,50-84 +2-32,27-54 +16-30,2-29 +37-38,38-93 +11-29,16-99 +78-83,37-77 +40-58,17-57 +9-35,8-10 +2-96,2-3 +20-43,20-70 +95-96,15-96 +10-37,4-16 +53-54,54-90 +90-99,49-91 +4-73,4-74 +55-56,56-76 +83-88,83-86 +82-83,69-82 +2-30,4-77 +6-8,8-49 +44-45,18-44 +6-66,66-67 +74-76,10-76 +25-53,24-25 +1-2,5-22 +6-96,96-96 +68-79,80-98 +43-98,42-44 +79-91,27-91 +17-17,17-17 +48-49,1-48 +4-58,20-58 +18-20,19-87 +86-87,7-86 +34-35,21-35 +18-67,17-68 +19-95,18-24 +44-77,38-76 +70-72,50-71 +19-99,20-99 +98-99,19-96 +22-57,5-20 +95-97,5-96 +4-98,3-97 +10-58,10-59 +5-8,7-95 +10-94,8-17 +1-11,12-72 +37-56,55-56 +32-78,19-78 +29-92,10-90 +11-73,60-73 +16-95,17-72 +90-93,43-91 +27-73,72-73 +25-57,28-57 +32-87,31-85 +96-99,2-97 +24-51,23-52 +25-54,32-55 +2-91,2-66 +15-82,14-15 +24-40,24-80 +14-34,13-34 +30-95,21-94 +53-74,53-73 +77-81,79-98 +9-98,6-6 +13-94,14-95 +46-81,23-80 +6-89,14-90 +42-56,43-51 +96-97,4-96 +11-11,12-26 +50-71,50-72 +81-85,27-82 +10-36,11-11 +30-31,30-75 +34-39,34-88 +30-65,30-64 +1-98,1-36 +67-73,63-67 +8-94,7-99 +64-65,64-81 +64-94,41-94 +8-53,7-8 +17-91,18-92 +30-80,31-81 +4-72,5-71 +5-81,80-82 +1-7,6-82 +72-73,29-73 +7-73,4-4 +4-6,5-95 +8-92,8-9 +12-82,81-83 +76-76,76-77 +42-67,42-84 +1-75,5-66 +3-99,3-91 +34-35,11-34 +7-98,97-98 +15-66,14-26 +83-83,63-83 +2-4,3-67 +4-96,4-4 +5-5,5-89 +5-94,5-6 +6-52,7-51 +1-64,1-63 +37-94,36-38 +50-87,50-88 +24-77,28-77 +12-91,77-91 +29-92,28-30 +41-42,41-57 +62-84,83-85 +46-95,19-42 +31-87,30-30 +2-21,1-2 +72-75,1-73 +59-98,59-94 +65-78,51-71 +11-58,12-58 +52-67,34-37 +55-62,53-60 +26-27,27-76 +12-97,33-98 +95-95,56-94 +17-90,17-91 +7-39,39-63 +45-98,37-46 +41-62,42-69 +5-98,2-5 +43-44,43-44 +34-35,34-35 +8-89,7-90 +33-87,32-86 +38-47,38-46 +2-93,93-93 +25-77,19-41 +15-81,16-81 +7-68,67-78 +52-77,18-69 +8-9,8-59 +4-82,3-5 +23-49,24-71 +10-93,4-92 +21-53,22-54 +5-50,5-99 +14-90,13-22 +14-75,15-94 +6-81,72-84 +22-99,22-99 +11-80,5-12 +79-96,22-78 +27-83,21-27 +88-94,72-89 +10-32,9-80 +3-5,5-99 +3-97,3-61 +15-83,29-68 +82-84,45-82 +50-95,10-50 +41-98,78-93 +19-89,10-92 +78-80,31-79 +22-71,21-71 +82-84,38-83 +13-19,13-14 +13-44,43-84 +43-82,43-97 +1-52,2-89 +21-30,31-88 +49-71,49-88 +36-81,80-92 +65-66,65-91 +75-87,22-76 +57-91,86-91 +16-23,16-16 +95-97,2-91 +64-68,65-95 +15-42,32-60 +55-98,70-96 +4-5,4-69 +10-51,10-52 +17-95,16-17 +30-69,29-40 +72-73,24-73 +13-87,12-88 +12-47,29-85 +19-20,20-51 +67-90,56-78 +27-85,1-86 +1-97,1-2 +54-60,34-58 +7-97,6-98 +41-84,42-84 +74-80,29-79 +22-52,14-22 +36-54,36-62 +32-88,22-89 +10-54,5-5 +6-36,7-17 +7-93,7-8 +38-81,37-37 +43-85,14-88 +20-90,89-90 +50-78,46-68 +18-59,5-59 +24-84,83-85 +10-63,10-11 +95-95,15-95 +58-84,59-59 +76-82,75-79 +48-51,48-55 +43-68,8-56 +67-96,68-96 +9-10,10-53 +8-68,6-6 +75-75,34-76 +52-53,53-54 +12-24,24-66 +41-42,19-42 +70-89,36-71 +52-87,49-86 +36-91,90-92 +16-52,17-52 +8-95,7-91 +92-95,9-93 +3-96,15-96 +58-79,57-73 +85-87,9-95 +60-61,2-60 +11-23,3-11 +14-90,3-15 +8-60,13-26 +82-91,85-91 +56-99,51-57 +5-95,9-96 +6-44,45-62 +63-99,62-98 +24-35,34-36 +51-52,51-98 +37-52,31-53 +51-51,50-72 +2-55,51-55 +6-83,82-95 +11-89,11-93 +30-62,8-41 +32-82,26-82 +8-87,7-86 +33-34,12-33 +35-93,36-87 +46-47,9-46 +3-87,3-3 +83-94,93-94 +5-98,4-99 +48-50,45-50 +35-50,35-43 +14-43,5-43 +48-97,10-96 +9-78,9-78 +59-61,58-60 +7-73,72-73 +4-97,3-5 +11-49,49-50 +42-99,99-99 +8-73,7-63 +4-82,34-87 +14-76,13-40 +26-95,25-98 +5-62,61-62 +28-45,22-45 +29-41,36-42 +85-98,61-86 +12-97,11-97 +6-78,5-60 +61-67,39-62 +58-77,58-73 +92-98,5-96 +41-98,39-41 +2-77,25-78 +62-71,1-49 +11-84,10-11 +25-42,26-78 +32-97,3-12 +36-73,7-73 +26-71,25-27 +1-48,19-48 +11-37,11-99 +49-76,42-50 +2-34,1-97 +60-66,66-66 +4-4,3-5 +37-37,38-40 +94-94,35-94 +82-84,5-83 +55-56,5-56 +20-93,20-95 +57-61,11-60 +86-87,15-87 +11-73,11-42 +10-35,2-34 +35-47,48-70 +12-94,2-97 +3-81,80-82 +18-49,18-70 +27-96,95-96 +38-39,39-57 +75-94,6-94 +19-60,56-56 +77-87,81-87 +4-7,3-9 +89-98,18-88 +2-2,3-75 +10-62,7-7 +10-23,9-35 +3-95,1-94 +32-83,82-83 +37-66,37-70 +36-45,45-70 +9-79,78-80 +2-96,1-1 +39-44,43-44 +10-95,11-98 +32-34,33-90 +17-93,29-94 +50-97,97-98 +21-90,21-73 +3-3,3-71 +32-96,11-95 +18-98,19-98 +39-93,92-93 +39-77,38-40 +30-91,6-92 +39-73,39-74 +38-96,39-56 +5-91,6-14 +19-19,20-87 +7-95,6-7 +38-94,2-95 +4-99,3-73 +23-99,24-74 +1-24,4-25 +10-25,25-86 +9-90,8-91 +20-26,23-72 +80-82,3-81 +8-38,8-9 +57-86,35-70 +6-90,3-4 +21-28,21-23 +11-96,10-90 +41-51,50-50 +30-57,13-31 +7-77,6-77 +87-88,2-88 +40-86,36-85 +73-75,9-74 +75-91,21-74 +1-89,12-90 +28-85,23-23 +79-79,11-79 +10-93,59-96 +38-38,39-48 +3-93,56-94 +8-13,7-8 +4-43,5-42 +22-22,20-24 +5-56,5-30 +43-83,66-83 +28-94,27-93 +22-23,23-88 +8-76,1-75 +29-94,28-29 +6-89,5-7 +22-97,22-89 +2-92,3-92 +19-19,18-69 +30-49,3-12 +2-38,32-96 +32-71,66-90 +10-51,14-51 +17-97,17-96 +42-49,59-64 +96-98,29-96 +7-8,7-43 +93-94,1-94 +57-71,58-73 +4-98,4-4 +89-90,6-89 +79-81,80-81 +52-94,51-75 +5-73,4-6 +90-91,34-91 +45-87,69-96 +13-30,14-57 +78-79,12-78 +10-45,9-10 +12-78,12-68 +3-14,10-16 +73-88,14-76 +14-14,10-15 +42-83,43-79 +15-94,94-95 +13-94,3-22 +9-62,9-62 +76-87,76-76 +3-11,2-11 +27-57,4-27 +28-60,27-60 +49-63,49-62 +58-69,65-69 +2-99,2-98 +33-89,33-97 +32-47,32-36 +56-56,41-56 +16-94,26-74 +91-91,10-92 +8-28,7-29 +6-91,5-6 +21-93,20-21 +57-74,22-29 +33-33,5-32 +78-82,80-82 +1-72,1-37 +72-94,13-73 +44-51,45-60 +2-97,1-1 +37-94,25-56 +16-71,17-72 +24-25,21-25 +9-81,44-97 +18-85,85-86 +40-93,92-98 +89-90,48-90 +3-74,3-3 +9-99,4-92 +17-27,27-98 +25-72,72-73 +41-99,72-99 +23-23,23-92 +47-78,39-48 +58-96,5-94 +7-90,89-92 +60-61,61-87 +43-67,28-67 +23-97,22-24 +1-31,19-32 +5-88,2-88 +1-34,2-97 +28-86,7-86 +16-80,16-80 +6-59,20-60 +68-89,73-95 +46-94,38-81 +20-25,21-78 +78-80,77-78 +33-97,96-98 +48-98,47-49 +4-98,2-98 +26-69,26-88 +26-75,75-78 +72-92,72-93 +4-92,4-93 +66-76,65-67 +2-97,3-97 +41-57,41-42 +6-77,76-97 +16-43,17-56 +49-51,4-50 +95-97,56-96 +1-97,80-97 +35-38,36-37 +10-84,18-84 +20-84,6-59 +18-81,36-82 +79-81,80-88 +41-96,89-95 +66-94,3-88 +71-72,56-72 +74-88,73-74 +57-73,58-60 +31-73,1-27 +14-25,9-25 +8-9,8-82 +3-96,95-97 +75-77,4-76 +16-89,15-16 +3-62,61-71 +20-28,19-48 +31-31,32-69 +5-61,6-60 +67-76,76-76 +82-93,70-89 +49-96,29-77 +3-14,7-15 +9-24,24-44 +10-82,9-59 +91-92,82-92 +4-61,3-73 +42-43,43-67 +63-78,64-64 +8-8,9-94 +54-55,54-64 +12-35,18-96 +12-55,53-53 +3-85,23-85 +26-93,32-93 +9-23,3-9 +49-82,48-48 +61-84,61-83 +35-37,36-91 +6-78,4-4 +73-80,4-76 +8-77,8-94 +18-22,20-22 +75-84,76-76 +51-91,24-90 +24-94,3-88 +17-17,18-59 +1-93,55-97 +15-75,5-75 +7-11,11-47 +3-85,3-4 +14-86,13-15 +13-93,33-95 +4-6,3-3 +33-56,33-89 +42-42,43-84 +46-77,76-77 +57-60,36-60 +23-24,24-53 +87-94,88-97 +1-83,1-52 +22-41,20-27 +12-86,85-87 +53-87,50-56 +8-51,8-50 +27-71,10-71 +7-62,8-63 +17-28,27-97 +1-90,90-90 +3-94,2-4 +3-98,2-24 +11-12,12-83 +18-37,19-36 +9-98,9-90 +4-98,3-99 +82-82,53-82 +5-54,6-88 +75-91,91-91 +20-98,19-98 +2-48,10-62 +16-42,41-42 +61-71,59-71 +3-92,3-4 +12-47,12-69 +23-26,26-65 +20-57,67-97 +65-95,64-65 +1-92,3-88 +5-83,5-89 +44-81,80-82 +30-32,31-84 +10-51,50-50 +25-52,16-25 +37-45,38-44 +46-47,47-62 +59-85,84-85 +13-86,12-87 +29-76,4-30 +79-93,12-80 +61-86,61-68 +13-85,15-85 +33-35,34-94 +68-69,33-71 +55-90,55-82 +20-94,23-94 +82-98,6-83 +26-78,26-79 +14-16,14-15 +6-47,46-49 +4-98,1-97 +17-76,37-65 +13-71,13-59 +24-67,14-58 +13-47,48-92 +37-38,37-60 +30-90,30-71 +62-74,33-86 +39-40,26-40 +6-81,5-16 +10-70,10-10 +4-84,83-92 +87-89,68-87 +47-95,16-84 +23-86,29-86 +30-98,30-62 +28-85,29-83 +87-98,56-97 +91-92,12-92 +89-99,67-90 +52-99,29-97 +5-92,3-16 +3-74,2-84 +54-83,55-55 +1-39,39-94 +1-98,97-98 +20-98,7-17 +27-47,10-27 +3-4,3-64 +86-87,4-86 +12-68,21-47 +78-79,37-79 +23-48,8-36 +5-88,4-82 +10-96,9-96 +1-99,2-99 +57-77,56-56 +66-85,65-68 +73-87,87-90 +13-96,12-89 +15-90,60-66 +75-75,12-75 +15-86,85-91 +26-67,25-44 +15-86,16-87 +23-58,43-69 +2-86,16-87 +18-78,9-19 +92-94,13-93 +8-46,2-45 +39-60,39-50 +76-85,75-77 +52-63,36-63 +4-66,3-4 +51-70,70-89 +4-7,7-97 +38-93,38-84 +2-9,9-96 +14-19,18-20 +3-79,2-37 +25-84,83-96 +21-36,22-65 +41-82,42-96 +2-77,38-78 +84-88,59-97 +3-70,4-71 +53-93,17-93 +94-95,2-99 +71-88,9-70 +12-41,13-41 +12-54,11-53 +72-73,73-78 +4-81,18-33 +23-52,10-94 +23-91,5-57 +45-45,43-45 +48-83,47-47 +12-96,11-11 +68-81,72-93 +14-92,8-14 +84-99,28-97 +21-81,22-82 +12-37,11-32 +40-45,39-39 +23-34,25-27 +23-23,24-65 +77-85,85-98 +91-98,91-96 +88-90,11-89 +5-5,6-98 +23-28,4-13 +20-58,19-58 +3-96,4-94 +16-22,8-19 +78-79,78-84 +67-80,62-68 +80-94,79-97 +12-79,78-79 +29-50,29-51 +2-41,41-42 +68-69,17-68 +38-39,2-39 +13-14,14-78 +9-80,8-9 +42-92,64-93 +67-78,66-73 +14-65,14-15 +6-59,1-35 +16-17,17-48 +15-28,16-33 +2-9,48-91 +3-4,4-86 +28-45,50-56 +15-23,16-86 +50-79,50-78 +9-62,8-8 +4-91,4-12 +24-25,25-65 +38-39,38-61 +8-8,18-76 +56-86,1-56 +13-68,68-74 +15-42,26-42 +40-45,40-61 +92-99,4-93 +20-35,25-32 +15-28,15-69 +26-94,8-94 +16-94,3-96 +16-98,16-21 +17-17,18-23 +26-30,26-27 +33-49,31-34 +13-99,85-90 +12-74,6-74 +54-60,53-57 +10-94,11-95 +37-53,37-38 +2-79,2-48 +46-55,53-60 +7-95,7-8 +7-83,8-98 +6-7,7-32 +47-82,46-47 +63-85,64-87 +77-78,57-77 +3-5,3-4 +5-98,97-99 +59-92,2-92 +4-97,1-99 +37-86,2-86 +46-47,9-47 +38-86,37-38 +24-48,52-71 +14-85,85-86 +20-83,20-21 +49-83,50-70 +44-63,45-88 +43-70,3-43 +34-76,2-75 +1-72,73-84 +49-89,45-89 +45-85,13-45 +29-98,28-30 +7-8,7-58 +20-20,21-41 +52-63,18-52 +81-95,76-81 +3-95,2-95 +74-84,68-75 +51-63,51-64 +19-72,40-72 +53-57,4-80 +21-88,19-87 +29-93,30-99 +4-98,7-98 +20-62,49-63 +5-71,5-70 +35-83,35-66 +12-71,35-78 +7-59,7-95 +7-62,7-70 +2-9,9-97 +5-11,8-12 +6-17,18-20 +5-87,3-3 +83-92,5-92 +12-95,12-54 +37-83,23-83 +39-48,40-53 +14-95,51-95 +37-98,70-80 +16-90,19-91 +2-27,16-46 +3-87,6-17 +49-49,49-79 +7-30,35-71 +21-40,22-29 +77-87,76-78 +11-68,23-69 +18-19,18-80 +21-79,13-21 +9-89,8-90 +4-85,7-89 +36-44,18-36 +1-21,2-44 +1-98,2-99 +56-57,3-56 +23-24,23-81 +22-25,10-24 +6-94,93-99 +13-37,12-14 +13-29,30-86 +65-88,66-90 +90-92,6-91 +42-43,43-67 +27-33,28-31 +31-90,37-90 +5-97,2-3 +25-55,1-26 +90-91,10-90 +35-52,36-53 +41-42,41-41 +7-75,18-75 +8-69,2-82 +56-96,11-96 +13-79,12-78 +5-11,11-94 +18-18,17-72 +4-94,1-93 +32-38,33-71 +3-5,4-98