From ae62ed76158cfeb57f9f73982c34db367a3abe3e Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 4 Dec 2022 19:05:21 +0100 Subject: [PATCH] Solve day4 --- 2022/day04_camp_cleanup.py | 54 ++ 2022/inputs/day04 | 1000 ++++++++++++++++++++++++++++++++++++ 2022/inputs/day04-test1 | 6 + 3 files changed, 1060 insertions(+) create mode 100644 2022/day04_camp_cleanup.py create mode 100644 2022/inputs/day04 create mode 100644 2022/inputs/day04-test1 diff --git a/2022/day04_camp_cleanup.py b/2022/day04_camp_cleanup.py new file mode 100644 index 0000000..ce401fd --- /dev/null +++ b/2022/day04_camp_cleanup.py @@ -0,0 +1,54 @@ +def main(filename: str, expected_part_1: int = None, expected_part_2: int = None): + print(f"\n+ Running on {filename}") + with open(filename) as f: + data = f.read().strip().split("\n") + + data = parse_data(data) + solution_part_1 = solve_part_1(data) + + print(f"1. Found {solution_part_1}") + if expected_part_1: + assert expected_part_1 == solution_part_1 + + solution_part_2 = solve_part_2(data) + print(f"2. Found {solution_part_2}") + if expected_part_2: + assert expected_part_2 == solution_part_2 + + +DataType = tuple[set[int], set[int]] + + +def parse_data(data: list[str]) -> DataType: + lines = [] + for line in data: + first, second = line.split(",") + first_1, first_2 = first.split("-") + first = set(range(int(first_1), int(first_2)+1)) + second_1, second_2 = second.split("-") + second = set(range(int(second_1), int(second_2)+1)) + lines.append((first, second)) + return lines + + +def solve_part_1(data: DataType) -> int: + total = 0 + for line in data: + first, second = line + if first.issuperset(second) or second.issuperset(first): + total += 1 + return total + + +def solve_part_2(data: DataType) -> int: + total = 0 + for line in data: + first, second = line + if first.intersection(second) != set(): + total += 1 + return total + + +if __name__ == "__main__": + main("inputs/day04-test1", expected_part_1=2, expected_part_2=4) + main("inputs/day04", expected_part_1=413) diff --git a/2022/inputs/day04 b/2022/inputs/day04 new file mode 100644 index 0000000..939866e --- /dev/null +++ b/2022/inputs/day04 @@ -0,0 +1,1000 @@ +8-17,16-49 +17-38,18-36 +17-43,43-43 +86-94,7-87 +23-97,22-85 +8-50,7-50 +82-84,1-83 +43-95,51-94 +7-89,8-90 +85-90,21-70 +46-69,7-46 +93-98,18-99 +42-85,53-92 +25-65,64-65 +13-53,12-53 +59-90,60-95 +1-79,58-78 +99-99,16-96 +56-86,86-86 +49-50,50-51 +79-83,80-83 +31-86,8-20 +8-85,9-86 +34-81,35-35 +7-87,8-95 +14-50,49-51 +2-65,1-1 +37-37,35-44 +17-72,3-39 +6-25,24-26 +56-94,37-93 +49-76,36-75 +26-37,25-53 +98-98,10-97 +48-87,49-97 +31-82,30-90 +5-98,4-93 +77-97,76-94 +96-98,1-97 +67-67,68-95 +26-51,50-50 +43-67,66-67 +31-68,30-90 +36-64,65-92 +2-2,3-11 +57-60,22-58 +43-55,44-56 +8-81,80-84 +2-52,2-2 +28-83,76-77 +20-83,13-82 +23-87,22-72 +11-92,91-91 +52-84,5-51 +74-78,67-97 +48-66,67-67 +86-86,39-87 +5-6,7-54 +14-15,14-98 +48-89,59-74 +51-75,52-67 +6-35,16-74 +65-98,44-96 +10-85,84-84 +26-96,95-95 +21-96,22-22 +13-69,12-68 +67-67,47-66 +64-70,71-71 +59-96,16-59 +69-69,56-70 +12-90,20-97 +6-45,5-98 +89-89,39-88 +2-69,1-3 +19-33,27-33 +69-97,94-94 +2-98,97-99 +22-88,22-38 +2-80,1-1 +64-64,63-63 +21-22,21-79 +18-88,19-89 +44-57,43-56 +55-56,3-55 +40-76,39-84 +71-94,70-94 +22-23,22-83 +2-12,15-26 +37-72,12-45 +4-79,3-61 +2-33,33-34 +33-82,18-32 +1-99,2-95 +7-79,7-79 +62-99,2-99 +88-94,18-96 +35-38,38-38 +26-27,26-56 +8-64,2-8 +11-11,13-88 +69-90,17-44 +21-22,22-42 +38-38,37-37 +16-52,10-15 +24-82,24-82 +9-85,2-26 +28-99,1-97 +86-86,25-85 +9-66,9-27 +73-73,74-90 +18-99,19-98 +85-93,92-92 +23-55,23-97 +6-79,80-96 +31-62,37-42 +42-44,43-49 +20-85,73-86 +56-96,13-94 +2-17,8-16 +1-59,5-84 +46-55,4-77 +65-87,16-87 +41-87,41-42 +1-94,2-92 +24-91,24-24 +8-67,6-25 +89-99,84-96 +23-67,2-24 +6-38,1-2 +67-92,72-98 +3-22,7-80 +11-53,5-11 +62-62,33-62 +30-96,31-96 +32-99,31-32 +15-15,14-70 +2-3,14-75 +37-56,38-44 +46-82,82-82 +58-59,32-58 +7-43,6-42 +25-26,26-84 +6-91,92-94 +3-69,2-70 +11-80,3-9 +56-57,57-82 +26-68,25-67 +43-47,7-46 +39-49,2-38 +50-51,32-50 +85-85,86-98 +8-56,5-57 +16-83,17-83 +43-93,44-92 +65-67,18-66 +19-76,19-75 +33-55,39-68 +43-44,38-43 +22-26,21-27 +1-4,3-68 +50-65,26-64 +33-94,98-99 +52-99,50-50 +26-43,20-42 +19-37,37-50 +2-99,99-99 +4-94,3-93 +88-95,3-87 +71-98,88-98 +11-58,10-11 +7-93,5-92 +98-98,62-82 +22-43,43-44 +21-28,25-28 +56-56,58-63 +9-10,9-85 +66-87,66-67 +8-79,78-78 +25-79,78-80 +40-40,40-62 +5-98,6-99 +3-34,28-99 +22-93,21-97 +26-42,25-97 +39-48,38-42 +32-50,32-32 +14-91,93-93 +8-62,7-89 +27-93,26-93 +64-66,59-65 +7-77,76-78 +11-72,63-71 +54-81,22-80 +3-89,89-89 +28-80,81-81 +9-91,19-43 +17-90,90-90 +24-75,76-81 +31-52,15-24 +38-75,39-76 +40-55,42-91 +95-95,67-94 +12-95,11-95 +8-54,54-55 +18-29,28-53 +41-41,42-76 +98-98,13-98 +23-99,14-27 +45-45,10-46 +2-70,81-98 +14-88,13-90 +21-58,28-66 +23-24,23-90 +12-70,11-85 +15-15,16-75 +46-48,26-45 +78-86,41-98 +84-84,83-84 +73-81,54-84 +19-65,66-88 +74-97,74-98 +7-86,6-7 +18-27,27-29 +91-91,10-90 +4-8,7-32 +21-42,21-42 +19-69,20-99 +67-68,17-67 +30-79,29-89 +2-86,1-2 +38-54,40-65 +28-48,20-29 +1-42,43-43 +20-41,16-41 +8-14,1-13 +19-90,18-20 +90-91,4-91 +1-89,1-89 +81-93,83-94 +29-66,29-29 +28-50,10-50 +2-28,2-84 +25-67,24-26 +35-53,36-37 +9-99,6-9 +20-78,23-91 +36-61,62-62 +15-39,14-31 +67-73,15-66 +33-99,62-90 +42-69,70-70 +79-80,80-81 +88-88,41-89 +2-84,84-84 +31-74,32-75 +9-87,9-94 +61-99,61-89 +2-99,3-16 +42-57,43-58 +23-83,23-94 +4-97,96-98 +2-99,30-36 +32-80,32-81 +7-31,8-14 +4-65,4-65 +7-8,7-41 +44-64,64-72 +86-95,21-87 +96-97,63-96 +10-94,11-96 +14-82,38-85 +9-77,10-78 +26-87,2-5 +96-96,80-95 +45-88,44-89 +44-44,45-74 +37-91,38-89 +68-86,85-85 +54-67,66-98 +24-73,23-72 +89-90,88-89 +51-71,1-51 +10-25,24-26 +85-86,36-88 +72-72,55-71 +2-4,4-50 +68-92,67-91 +83-86,82-88 +71-71,67-70 +95-96,6-96 +7-99,2-98 +77-82,65-78 +57-97,19-47 +81-91,82-82 +11-17,16-55 +95-98,50-54 +66-98,65-67 +1-94,93-96 +9-47,10-48 +91-95,13-92 +4-64,8-63 +26-27,26-96 +11-60,6-11 +28-28,27-82 +49-87,50-76 +2-96,1-97 +29-73,20-29 +72-72,73-82 +12-68,67-67 +98-99,1-97 +12-13,13-78 +33-65,66-73 +34-62,36-67 +63-93,64-92 +2-45,8-42 +11-99,12-97 +11-88,6-89 +23-61,58-58 +16-96,24-95 +2-70,6-69 +31-90,90-91 +9-25,7-8 +23-69,23-24 +54-64,53-54 +15-22,22-56 +3-80,2-40 +35-39,34-39 +20-45,8-64 +19-96,18-99 +11-72,1-16 +24-73,42-73 +23-48,47-89 +13-82,14-83 +42-64,42-63 +6-20,5-17 +2-6,3-7 +35-96,99-99 +33-83,34-83 +6-6,57-93 +67-81,82-82 +13-97,12-84 +5-64,5-97 +31-91,30-97 +76-95,75-77 +30-92,91-92 +19-86,19-20 +11-45,3-46 +2-37,19-98 +11-20,12-32 +28-98,97-98 +2-94,2-94 +1-99,99-99 +28-44,40-49 +8-46,7-9 +36-95,35-94 +14-91,15-91 +23-74,24-75 +24-45,27-46 +39-62,26-38 +13-15,14-95 +22-84,21-21 +2-89,88-99 +12-69,68-83 +24-92,10-60 +4-97,9-94 +29-74,34-51 +9-9,10-91 +97-98,33-96 +21-66,67-67 +2-95,1-89 +34-35,36-57 +84-84,13-85 +5-49,6-50 +47-97,48-75 +23-96,24-76 +4-59,2-3 +4-99,5-92 +2-95,1-99 +14-72,9-71 +40-95,41-75 +15-96,97-97 +9-50,49-51 +55-77,76-78 +97-97,7-96 +92-93,3-92 +40-74,39-84 +82-87,4-83 +2-81,3-77 +18-82,17-80 +13-80,81-81 +20-81,80-80 +9-96,1-6 +84-89,88-88 +6-77,5-76 +37-74,69-76 +95-98,3-96 +26-87,87-88 +2-43,44-44 +5-89,58-94 +34-75,75-75 +88-91,19-83 +23-78,28-77 +64-75,76-76 +32-46,31-46 +2-13,12-76 +51-96,50-99 +91-91,32-90 +3-76,66-81 +10-94,8-96 +88-89,14-89 +2-40,91-98 +29-84,30-85 +13-14,13-79 +2-97,98-99 +1-52,2-51 +75-98,40-93 +90-98,19-91 +47-49,8-48 +20-96,20-21 +20-87,21-43 +50-87,97-99 +1-38,77-84 +86-92,85-91 +15-15,14-14 +19-87,71-88 +22-66,65-67 +9-96,1-97 +12-94,13-76 +43-68,69-92 +18-60,59-97 +3-91,1-1 +8-26,7-27 +7-99,7-98 +22-65,65-87 +98-99,8-99 +78-93,35-79 +3-18,17-76 +82-86,81-85 +2-5,6-95 +38-63,64-86 +13-54,6-93 +37-90,37-89 +21-35,36-94 +34-95,33-91 +4-37,3-8 +1-3,3-82 +99-99,40-99 +7-8,7-94 +49-49,3-50 +16-72,71-73 +3-6,6-13 +11-92,10-10 +12-12,11-88 +42-75,43-91 +99-99,88-97 +46-48,7-47 +20-91,17-20 +75-76,27-75 +93-95,34-94 +26-63,62-62 +45-50,46-59 +7-70,8-68 +41-42,42-60 +50-51,51-86 +34-59,58-88 +53-94,63-99 +4-79,5-91 +20-23,14-78 +98-99,15-99 +15-15,14-53 +33-40,63-86 +8-91,14-91 +94-95,3-94 +55-83,74-84 +17-18,17-91 +4-81,3-3 +26-26,27-84 +85-87,48-86 +32-41,42-42 +98-99,42-97 +80-87,81-84 +93-96,1-94 +31-87,33-87 +26-27,18-26 +40-40,41-89 +86-88,25-87 +6-14,14-15 +32-67,31-68 +45-45,44-90 +96-96,5-95 +61-76,62-75 +46-78,45-96 +4-92,3-93 +48-56,61-70 +83-85,82-98 +89-89,73-90 +2-56,20-28 +45-55,45-54 +12-87,87-87 +2-91,1-94 +91-92,32-92 +2-85,1-84 +98-98,51-97 +90-91,81-91 +15-90,17-37 +3-97,1-3 +7-74,8-77 +46-75,45-45 +82-83,24-83 +17-25,24-80 +11-96,11-12 +32-81,31-69 +21-96,21-22 +76-98,76-77 +42-88,48-82 +60-61,18-61 +7-73,74-79 +24-99,25-80 +85-85,2-84 +92-92,93-93 +59-59,60-99 +95-96,9-94 +23-92,19-91 +11-94,93-97 +14-91,90-95 +46-77,77-77 +46-47,47-53 +3-47,2-3 +12-47,48-65 +4-44,4-5 +26-33,32-77 +31-81,81-81 +3-48,48-49 +4-7,7-93 +34-80,80-80 +50-77,51-78 +28-43,75-82 +36-89,51-60 +10-87,86-87 +1-99,1-99 +2-89,49-99 +23-36,37-37 +8-13,13-62 +22-24,23-68 +18-18,18-93 +32-49,33-51 +73-91,74-92 +10-59,44-58 +2-59,2-59 +3-89,89-90 +15-94,5-95 +42-88,88-88 +7-95,7-7 +1-75,1-75 +12-12,12-86 +93-94,2-93 +80-89,80-89 +14-91,95-95 +38-98,38-97 +96-99,11-97 +69-70,50-70 +13-81,80-82 +25-89,26-81 +47-48,32-47 +4-19,3-27 +34-64,33-33 +10-90,2-11 +5-98,4-99 +6-90,89-91 +41-52,41-53 +48-87,49-77 +2-53,5-74 +82-99,66-84 +59-60,11-59 +22-57,21-56 +38-81,81-81 +51-81,3-51 +25-73,26-36 +98-98,3-99 +96-96,1-95 +50-72,49-49 +91-91,2-90 +61-96,60-61 +10-45,11-15 +65-66,49-66 +82-87,45-83 +59-59,3-59 +5-72,4-73 +56-95,55-56 +13-83,14-96 +52-59,52-59 +44-46,42-45 +11-93,11-35 +74-92,16-81 +98-99,17-99 +20-99,1-93 +37-86,46-51 +15-16,16-98 +1-97,96-99 +73-75,23-74 +94-99,24-95 +94-99,26-95 +39-39,40-80 +58-89,57-59 +84-93,19-93 +7-43,43-98 +23-82,21-23 +2-59,1-98 +93-93,12-73 +9-99,9-39 +42-61,53-81 +12-79,11-69 +33-62,34-80 +11-99,8-10 +44-89,43-82 +7-71,70-71 +11-99,98-99 +87-91,92-97 +2-93,3-94 +58-79,64-80 +24-62,24-25 +3-86,3-3 +4-90,2-2 +3-90,4-91 +9-87,9-87 +23-57,22-57 +4-13,14-87 +11-23,24-24 +35-68,7-46 +3-3,4-80 +6-47,36-71 +21-93,92-97 +6-65,5-86 +21-22,8-21 +22-55,56-78 +51-85,84-84 +17-95,94-98 +32-61,32-60 +63-63,63-63 +48-65,56-66 +3-33,4-32 +7-76,71-79 +34-66,37-82 +21-65,25-66 +3-79,4-80 +17-88,95-99 +26-26,27-92 +81-95,3-80 +56-97,3-55 +58-86,23-86 +84-84,21-84 +82-84,29-82 +26-80,79-79 +40-41,39-52 +27-85,26-86 +39-39,6-39 +29-89,28-28 +11-42,11-12 +1-66,67-67 +15-83,82-83 +50-93,51-92 +78-80,83-96 +6-96,7-94 +10-11,10-45 +18-40,12-19 +31-55,7-32 +18-19,29-83 +29-29,30-80 +35-37,36-99 +94-94,28-94 +48-98,49-99 +63-66,60-62 +4-62,62-62 +77-84,85-85 +34-34,35-47 +68-90,20-69 +6-68,39-88 +8-99,7-99 +49-50,17-49 +74-85,73-85 +88-94,27-87 +4-89,88-88 +8-84,7-83 +60-64,60-64 +94-94,95-99 +43-91,41-99 +92-92,91-91 +67-67,19-66 +19-99,18-96 +47-79,78-79 +8-48,22-38 +35-59,32-35 +5-45,6-46 +20-90,19-89 +14-55,12-13 +34-34,34-43 +3-96,4-99 +39-40,40-65 +83-83,25-82 +15-92,14-91 +12-12,13-82 +43-57,43-57 +3-98,99-99 +30-66,65-67 +92-95,86-92 +48-52,13-42 +1-7,10-64 +60-98,44-59 +64-86,65-85 +29-97,96-96 +49-85,2-50 +35-38,33-37 +6-8,7-63 +1-90,90-91 +7-56,8-74 +2-61,2-3 +79-93,41-96 +6-47,5-6 +10-87,11-42 +93-99,35-94 +13-62,21-62 +11-49,10-10 +2-55,54-54 +59-68,3-27 +1-96,1-96 +38-75,39-76 +29-51,51-92 +6-61,7-60 +24-25,25-90 +27-32,28-31 +16-47,1-46 +20-88,7-18 +24-74,25-25 +40-86,41-90 +8-97,3-97 +95-95,1-94 +11-36,12-79 +8-98,3-99 +28-28,29-30 +20-86,21-85 +42-58,61-88 +43-58,52-69 +67-92,32-75 +81-97,44-89 +19-90,89-90 +12-92,11-13 +27-89,28-89 +16-97,17-98 +91-93,27-90 +15-95,14-95 +84-84,10-83 +35-35,34-87 +14-65,15-78 +25-90,91-95 +19-21,20-93 +12-92,11-95 +48-60,47-61 +11-92,10-93 +6-68,67-69 +19-57,20-20 +33-45,23-46 +6-6,11-99 +2-96,2-3 +11-21,7-12 +9-48,47-48 +19-57,2-19 +15-96,14-44 +78-82,12-79 +21-76,5-7 +8-31,24-59 +52-99,52-53 +9-90,89-89 +1-3,3-89 +18-92,15-93 +15-35,16-99 +1-7,6-83 +7-86,61-85 +19-89,19-89 +57-98,56-97 +22-22,12-23 +1-1,1-20 +21-23,22-94 +44-46,45-47 +10-90,4-11 +30-84,29-83 +55-73,55-73 +62-73,14-63 +59-81,47-58 +27-64,28-28 +33-79,55-84 +14-50,13-50 +77-98,76-95 +23-69,24-70 +80-82,1-81 +27-92,17-28 +4-49,50-71 +42-85,41-98 +97-99,15-98 +21-59,20-58 +64-98,99-99 +90-90,62-91 +58-69,70-94 +90-96,10-90 +95-96,65-96 +4-55,3-62 +19-71,70-83 +1-2,2-2 +13-99,12-14 +98-99,43-98 +3-35,36-36 +26-88,27-54 +1-99,2-98 +11-11,9-10 +94-99,19-93 +16-85,66-86 +53-65,53-65 +42-63,63-71 +87-87,57-87 +35-60,34-60 +31-84,38-55 +7-76,8-77 +1-78,2-2 +78-93,52-79 +83-96,83-96 +82-84,83-85 +1-25,1-19 +97-97,19-98 +78-78,24-77 +7-81,6-6 +18-84,81-88 +46-89,47-90 +20-93,61-93 +11-51,2-52 +94-94,56-94 +8-99,98-99 +1-2,3-51 +6-96,95-97 +9-10,9-66 +98-98,69-97 +64-64,65-78 +38-98,97-97 +46-52,40-50 +74-78,1-75 +2-56,5-55 +48-54,47-53 +35-67,68-98 +8-68,68-83 +95-97,35-94 +13-69,14-91 +42-71,71-72 +24-40,23-39 +65-98,64-89 +4-95,5-86 +53-84,84-84 +29-98,30-99 +4-98,3-5 +96-96,1-95 +50-84,85-85 +41-78,25-78 +4-84,84-85 +15-47,1-45 +86-89,50-93 +41-82,15-94 +18-32,33-35 +27-93,26-27 +2-90,89-92 +52-55,51-77 +34-43,33-42 +3-3,3-99 +93-97,33-94 +74-99,5-75 +27-99,24-99 +27-28,6-27 +1-2,1-57 +19-82,19-83 +37-73,36-72 +29-53,5-53 +21-94,94-94 +8-57,57-91 +58-59,57-58 +3-3,2-3 +43-80,12-27 +81-99,82-91 +48-49,14-49 +13-98,12-95 +14-98,13-79 +67-98,97-99 +31-84,32-32 +1-91,91-94 +2-2,3-96 +1-99,1-1 +22-93,22-90 +1-92,1-2 +28-88,1-1 +16-94,15-16 +36-70,69-70 +14-85,84-86 +21-54,15-98 +39-61,39-60 +54-56,9-55 +9-45,45-46 +14-19,14-20 +2-53,53-53 +58-69,68-68 +76-98,77-99 +21-73,22-49 +9-96,8-95 +1-50,1-49 +28-67,29-98 +41-78,40-79 +95-99,46-96 +9-88,8-10 +19-73,7-74 +29-65,29-66 +98-99,23-97 +44-44,43-47 +9-60,61-61 +39-64,38-65 +56-81,55-56 +74-75,74-90 +72-95,10-66 +3-86,2-2 +16-26,25-26 +13-61,6-51 +4-17,39-46 +39-97,40-76 +36-52,37-51 +41-96,40-40 +23-70,69-71 +10-90,10-87 +29-81,30-76 +89-89,89-89 +11-96,12-97 +11-80,55-80 +4-94,93-97 +11-94,10-93 +78-78,22-77 +53-98,53-98 +6-53,33-63 +13-14,13-30 +94-96,22-93 +22-96,95-95 +40-40,41-78 +66-66,4-66 +40-91,50-76 +61-91,87-88 +2-50,3-49 +98-98,1-99 +10-28,14-31 +44-95,51-54 +31-59,30-31 +10-94,9-92 +40-47,39-40 +3-88,4-89 +64-86,7-86 +33-37,32-35 +10-11,10-60 +95-95,3-94 +16-84,16-99 +96-97,20-96 +25-91,91-92 +3-3,4-95 +15-91,20-91 +59-90,16-60 +5-53,52-54 +3-99,4-4 +23-43,24-44 +26-91,53-91 +4-73,4-5 +64-64,2-64 +74-74,51-75 +35-51,34-34 +72-87,86-88 +3-92,2-2 +55-87,56-56 +69-81,70-82 +42-78,17-42 +51-58,54-58 +76-93,94-96 +4-88,3-88 +53-60,54-64 +2-75,3-15 +13-56,55-72 +3-26,9-9 +17-55,18-54 +39-75,40-55 +12-31,13-52 +91-97,46-91 +35-56,35-72 +46-46,47-47 +10-10,11-47 +19-54,18-82 +1-97,2-97 +13-46,13-46 +6-95,94-95 +16-33,16-33 +26-26,26-99 +42-83,78-79 +5-17,4-67 \ No newline at end of file diff --git a/2022/inputs/day04-test1 b/2022/inputs/day04-test1 new file mode 100644 index 0000000..99a66c5 --- /dev/null +++ b/2022/inputs/day04-test1 @@ -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