From fb54b00fbabcbd70431334837ec22c9dffe36456 Mon Sep 17 00:00:00 2001 From: Andreas Date: Thu, 5 Dec 2024 09:53:05 +0100 Subject: [PATCH] Added solution for day 5 --- day5/example.txt | 28 + day5/input.txt | 1369 +++++++++++++++++++++++++++++++++++++++++++++ day5/solution.py | 49 ++ day5/solution2.py | 83 +++ 4 files changed, 1529 insertions(+) create mode 100644 day5/example.txt create mode 100644 day5/input.txt create mode 100644 day5/solution.py create mode 100644 day5/solution2.py diff --git a/day5/example.txt b/day5/example.txt new file mode 100644 index 0000000..d4d4441 --- /dev/null +++ b/day5/example.txt @@ -0,0 +1,28 @@ +47|53 +97|13 +97|61 +97|47 +75|29 +61|13 +75|53 +29|13 +97|29 +53|29 +61|53 +97|53 +61|29 +47|13 +75|47 +97|75 +47|61 +75|61 +47|29 +75|13 +53|13 + +75,47,61,53,29 +97,61,53,29,13 +75,29,13 +75,97,47,61,53 +61,13,29 +97,13,75,29,47 \ No newline at end of file diff --git a/day5/input.txt b/day5/input.txt new file mode 100644 index 0000000..88ca56c --- /dev/null +++ b/day5/input.txt @@ -0,0 +1,1369 @@ +79|26 +75|21 +75|86 +74|69 +74|86 +74|75 +35|62 +35|74 +35|47 +35|57 +86|28 +86|95 +86|53 +86|99 +86|46 +28|12 +28|25 +28|65 +28|83 +28|79 +28|43 +15|52 +15|18 +15|33 +15|67 +15|88 +15|57 +15|26 +95|72 +95|97 +95|12 +95|15 +95|74 +95|85 +95|82 +95|73 +26|38 +26|77 +26|47 +26|53 +26|97 +26|43 +26|21 +26|46 +26|95 +73|12 +73|52 +73|74 +73|86 +73|15 +73|57 +73|56 +73|68 +73|89 +73|88 +83|98 +83|48 +83|67 +83|57 +83|33 +83|44 +83|41 +83|34 +83|58 +83|15 +83|53 +37|41 +37|39 +37|18 +37|88 +37|58 +37|99 +37|69 +37|56 +37|36 +37|21 +37|53 +37|89 +54|56 +54|88 +54|98 +54|64 +54|12 +54|34 +54|86 +54|65 +54|67 +54|33 +54|57 +54|68 +54|52 +43|68 +43|75 +43|98 +43|83 +43|33 +43|88 +43|62 +43|73 +43|37 +43|65 +43|56 +43|94 +43|67 +43|57 +44|98 +44|48 +44|56 +44|15 +44|52 +44|12 +44|34 +44|41 +44|67 +44|68 +44|37 +44|88 +44|18 +44|89 +44|57 +98|69 +98|53 +98|33 +98|41 +98|36 +98|25 +98|95 +98|37 +98|99 +98|77 +98|56 +98|86 +98|21 +98|57 +98|89 +98|26 +58|46 +58|36 +58|72 +58|25 +58|77 +58|89 +58|21 +58|97 +58|47 +58|85 +58|62 +58|95 +58|38 +58|69 +58|82 +58|48 +58|54 +12|15 +12|74 +12|37 +12|52 +12|53 +12|56 +12|33 +12|77 +12|48 +12|89 +12|68 +12|57 +12|18 +12|98 +12|75 +12|34 +12|86 +12|69 +65|15 +65|67 +65|73 +65|79 +65|64 +65|33 +65|52 +65|34 +65|74 +65|98 +65|12 +65|86 +65|75 +65|88 +65|94 +65|68 +65|58 +65|41 +65|57 +62|41 +62|44 +62|98 +62|79 +62|64 +62|75 +62|12 +62|37 +62|65 +62|56 +62|33 +62|15 +62|34 +62|74 +62|88 +62|68 +62|18 +62|57 +62|54 +62|73 +46|12 +46|73 +46|44 +46|47 +46|57 +46|68 +46|82 +46|62 +46|79 +46|38 +46|35 +46|34 +46|67 +46|83 +46|64 +46|54 +46|98 +46|74 +46|94 +46|65 +46|43 +56|85 +56|25 +56|46 +56|72 +56|36 +56|99 +56|75 +56|28 +56|86 +56|82 +56|88 +56|48 +56|77 +56|97 +56|21 +56|26 +56|95 +56|41 +56|89 +56|39 +56|52 +56|69 +89|82 +89|28 +89|26 +89|21 +89|38 +89|69 +89|43 +89|36 +89|25 +89|62 +89|53 +89|95 +89|54 +89|48 +89|47 +89|46 +89|65 +89|35 +89|77 +89|99 +89|97 +89|39 +89|72 +33|41 +33|28 +33|58 +33|21 +33|53 +33|48 +33|97 +33|89 +33|88 +33|77 +33|57 +33|26 +33|86 +33|69 +33|36 +33|56 +33|25 +33|72 +33|75 +33|95 +33|52 +33|37 +33|99 +33|18 +41|86 +41|47 +41|46 +41|25 +41|36 +41|69 +41|72 +41|97 +41|21 +41|26 +41|53 +41|43 +41|28 +41|85 +41|95 +41|48 +41|77 +41|58 +41|38 +41|39 +41|82 +41|35 +41|99 +41|89 +97|74 +97|94 +97|79 +97|64 +97|65 +97|46 +97|34 +97|15 +97|68 +97|54 +97|12 +97|43 +97|82 +97|98 +97|47 +97|39 +97|62 +97|85 +97|38 +97|35 +97|44 +97|67 +97|83 +97|73 +47|98 +47|43 +47|68 +47|54 +47|65 +47|67 +47|64 +47|44 +47|62 +47|57 +47|73 +47|34 +47|18 +47|37 +47|75 +47|56 +47|33 +47|83 +47|94 +47|88 +47|12 +47|79 +47|15 +47|74 +68|72 +68|75 +68|77 +68|88 +68|57 +68|37 +68|18 +68|58 +68|56 +68|41 +68|53 +68|26 +68|86 +68|21 +68|69 +68|89 +68|25 +68|33 +68|36 +68|28 +68|99 +68|52 +68|48 +68|95 +34|95 +34|26 +34|99 +34|69 +34|52 +34|21 +34|68 +34|41 +34|75 +34|57 +34|77 +34|28 +34|25 +34|37 +34|56 +34|48 +34|58 +34|18 +34|89 +34|98 +34|88 +34|33 +34|86 +34|53 +99|64 +99|62 +99|12 +99|73 +99|94 +99|46 +99|54 +99|47 +99|79 +99|35 +99|95 +99|38 +99|43 +99|44 +99|65 +99|97 +99|72 +99|36 +99|83 +99|85 +99|39 +99|25 +99|82 +99|15 +67|52 +67|28 +67|68 +67|33 +67|34 +67|53 +67|37 +67|57 +67|74 +67|21 +67|26 +67|99 +67|69 +67|58 +67|89 +67|86 +67|98 +67|48 +67|18 +67|56 +67|41 +67|75 +67|88 +67|77 +18|88 +18|75 +18|89 +18|25 +18|86 +18|46 +18|77 +18|52 +18|26 +18|97 +18|56 +18|99 +18|28 +18|21 +18|41 +18|39 +18|69 +18|85 +18|48 +18|36 +18|72 +18|53 +18|58 +18|95 +69|85 +69|36 +69|77 +69|94 +69|38 +69|53 +69|48 +69|54 +69|97 +69|64 +69|25 +69|82 +69|62 +69|35 +69|47 +69|43 +69|65 +69|39 +69|46 +69|95 +69|28 +69|99 +69|21 +69|72 +64|83 +64|74 +64|75 +64|94 +64|34 +64|52 +64|58 +64|89 +64|12 +64|79 +64|57 +64|37 +64|18 +64|67 +64|41 +64|86 +64|44 +64|68 +64|73 +64|15 +64|98 +64|56 +64|88 +64|33 +53|35 +53|95 +53|94 +53|79 +53|54 +53|36 +53|46 +53|97 +53|73 +53|85 +53|21 +53|38 +53|82 +53|25 +53|43 +53|64 +53|99 +53|77 +53|72 +53|47 +53|62 +53|28 +53|39 +53|65 +25|44 +25|72 +25|79 +25|64 +25|47 +25|38 +25|97 +25|36 +25|73 +25|12 +25|95 +25|62 +25|15 +25|85 +25|67 +25|83 +25|46 +25|39 +25|43 +25|35 +25|65 +25|54 +25|82 +25|94 +36|65 +36|39 +36|97 +36|83 +36|38 +36|64 +36|46 +36|79 +36|44 +36|73 +36|74 +36|15 +36|67 +36|35 +36|85 +36|54 +36|62 +36|34 +36|47 +36|72 +36|12 +36|43 +36|82 +36|94 +72|54 +72|79 +72|62 +72|85 +72|39 +72|44 +72|94 +72|46 +72|82 +72|98 +72|43 +72|73 +72|97 +72|74 +72|15 +72|83 +72|35 +72|64 +72|65 +72|34 +72|47 +72|12 +72|67 +72|38 +21|39 +21|38 +21|47 +21|25 +21|46 +21|65 +21|99 +21|79 +21|28 +21|35 +21|97 +21|64 +21|36 +21|94 +21|62 +21|83 +21|43 +21|73 +21|95 +21|82 +21|85 +21|54 +21|77 +21|72 +77|79 +77|44 +77|82 +77|97 +77|72 +77|46 +77|43 +77|73 +77|35 +77|85 +77|39 +77|64 +77|94 +77|62 +77|54 +77|65 +77|83 +77|47 +77|38 +77|28 +77|25 +77|99 +77|36 +77|95 +57|69 +57|88 +57|48 +57|99 +57|77 +57|37 +57|36 +57|97 +57|52 +57|72 +57|75 +57|39 +57|21 +57|56 +57|18 +57|58 +57|95 +57|89 +57|86 +57|41 +57|53 +57|25 +57|26 +57|28 +52|35 +52|82 +52|72 +52|39 +52|28 +52|53 +52|26 +52|89 +52|97 +52|25 +52|38 +52|58 +52|95 +52|36 +52|85 +52|46 +52|21 +52|48 +52|47 +52|77 +52|86 +52|41 +52|69 +52|99 +39|12 +39|94 +39|65 +39|73 +39|46 +39|83 +39|98 +39|74 +39|62 +39|67 +39|44 +39|64 +39|38 +39|85 +39|33 +39|15 +39|43 +39|47 +39|35 +39|68 +39|79 +39|82 +39|34 +39|54 +94|98 +94|33 +94|73 +94|86 +94|34 +94|41 +94|37 +94|18 +94|26 +94|79 +94|56 +94|68 +94|83 +94|75 +94|15 +94|12 +94|74 +94|88 +94|67 +94|57 +94|89 +94|52 +94|58 +94|44 +38|75 +38|37 +38|79 +38|74 +38|57 +38|65 +38|33 +38|73 +38|34 +38|18 +38|64 +38|98 +38|54 +38|56 +38|12 +38|62 +38|68 +38|67 +38|44 +38|83 +38|15 +38|43 +38|47 +38|94 +88|52 +88|72 +88|77 +88|58 +88|97 +88|95 +88|85 +88|53 +88|38 +88|46 +88|35 +88|86 +88|48 +88|99 +88|69 +88|36 +88|39 +88|28 +88|89 +88|41 +88|82 +88|25 +88|26 +88|21 +48|21 +48|47 +48|97 +48|73 +48|85 +48|46 +48|25 +48|36 +48|54 +48|38 +48|62 +48|94 +48|28 +48|82 +48|39 +48|65 +48|72 +48|53 +48|43 +48|64 +48|77 +48|35 +48|99 +48|95 +82|83 +82|98 +82|38 +82|68 +82|74 +82|67 +82|12 +82|57 +82|94 +82|35 +82|33 +82|54 +82|43 +82|65 +82|37 +82|44 +82|15 +82|62 +82|73 +82|18 +82|47 +82|64 +82|79 +82|34 +85|79 +85|47 +85|73 +85|82 +85|35 +85|44 +85|98 +85|67 +85|12 +85|83 +85|65 +85|43 +85|57 +85|33 +85|38 +85|54 +85|74 +85|34 +85|94 +85|64 +85|15 +85|62 +85|68 +85|37 +79|15 +79|52 +79|41 +79|57 +79|67 +79|18 +79|88 +79|33 +79|12 +79|37 +79|69 +79|56 +79|83 +79|44 +79|68 +79|89 +79|75 +79|74 +79|98 +79|48 +79|86 +79|34 +79|58 +75|85 +75|89 +75|77 +75|52 +75|69 +75|48 +75|99 +75|26 +75|35 +75|82 +75|28 +75|46 +75|97 +75|36 +75|95 +75|39 +75|88 +75|58 +75|41 +75|53 +75|25 +75|72 +74|21 +74|18 +74|88 +74|99 +74|33 +74|89 +74|48 +74|53 +74|68 +74|34 +74|98 +74|57 +74|26 +74|25 +74|77 +74|41 +74|52 +74|56 +74|37 +74|28 +74|58 +35|68 +35|12 +35|44 +35|38 +35|18 +35|94 +35|56 +35|79 +35|98 +35|67 +35|83 +35|15 +35|43 +35|33 +35|37 +35|54 +35|65 +35|73 +35|64 +35|34 +86|39 +86|43 +86|85 +86|69 +86|36 +86|77 +86|48 +86|97 +86|26 +86|82 +86|89 +86|38 +86|35 +86|58 +86|25 +86|47 +86|72 +86|62 +86|21 +28|94 +28|35 +28|99 +28|36 +28|62 +28|39 +28|64 +28|95 +28|44 +28|82 +28|97 +28|46 +28|85 +28|47 +28|73 +28|38 +28|54 +28|72 +15|37 +15|74 +15|69 +15|86 +15|89 +15|21 +15|68 +15|53 +15|58 +15|75 +15|41 +15|77 +15|28 +15|98 +15|48 +15|34 +15|56 +95|35 +95|94 +95|65 +95|39 +95|43 +95|64 +95|36 +95|79 +95|83 +95|47 +95|38 +95|54 +95|44 +95|46 +95|67 +95|62 +26|69 +26|36 +26|28 +26|35 +26|25 +26|72 +26|65 +26|39 +26|85 +26|54 +26|99 +26|64 +26|48 +26|62 +26|82 +73|83 +73|58 +73|79 +73|37 +73|18 +73|98 +73|75 +73|26 +73|69 +73|33 +73|44 +73|67 +73|41 +73|34 +83|74 +83|37 +83|68 +83|89 +83|69 +83|86 +83|88 +83|26 +83|56 +83|75 +83|12 +83|52 +83|18 +37|28 +37|72 +37|77 +37|52 +37|25 +37|75 +37|95 +37|86 +37|48 +37|97 +37|26 +37|46 +54|79 +54|44 +54|15 +54|73 +54|37 +54|41 +54|74 +54|83 +54|75 +54|94 +54|18 +43|52 +43|44 +43|54 +43|34 +43|79 +43|18 +43|15 +43|64 +43|12 +43|74 +44|21 +44|75 +44|86 +44|53 +44|26 +44|74 +44|69 +44|33 +44|58 +98|28 +98|75 +98|88 +98|48 +98|58 +98|18 +98|52 +98|68 +58|99 +58|43 +58|35 +58|28 +58|53 +58|39 +58|26 +12|67 +12|26 +12|88 +12|41 +12|21 +12|58 +65|83 +65|44 +65|56 +65|37 +65|18 +62|83 +62|52 +62|94 +62|67 +46|15 +46|85 +46|33 +56|58 +56|53 +89|85 + +79,72,94,83,85 +83,69,15,79,37,44,52 +41,86,69,72,39,35,47 +94,62,85,35,43,54,57,79,38 +18,44,94,89,86,52,98,57,73,37,34,58,12,33,56 +72,46,88,58,26,89,85,39,86,41,48,36,69,82,75,28,95,97,52,25,53,99,77 +85,74,62,83,79,54,44,43,94,72,67,46,65,12,15,39,35,38,47 +75,52,28,37,86,34,77,48,53,18,33,21,41,69,57,25,98 +64,73,79,83,44,15,67,74,34,98,68,33,57,37,18,56,75,88,41,86,58 +65,64,73,79,83,44,12,15,67,74,98,68,37,56,75,88,52,41,86 +89,26,69,48,21,28,95,39,38,43,54 +89,26,48,77,99,25,72 +75,88,52,86,58,89,26,69,48,53,77,28,99,25,95,36,72,97,46,85,82 +73,54,44,62,57,83,52,18,15,65,56,12,75,68,34,74,33,94,67 +88,83,89,94,73,18,74 +67,34,98,68,33,57,37,75,58,89,26,69,21,77,28 +77,21,97,54,99,65,28,62,47,48,43,35,85 +74,57,75,15,88,44,89,41,33,58,94,37,83,73,68 +38,65,35,82,67,98,54,64,34,47,39,97,79,15,44,74,62,73,43,94,12,85,83 +83,44,12,15,67,74,34,68,33,57,37,18,56,75,88,52,41,86,58,89,26,69,48 +47,62,43,33,37,75,57 +57,18,75,88,52,41,86,89,26,69,53,28,95,36,97 +65,64,94,79,83,44,12,67,74,34,98,57,37,18,88,52,86 +64,75,83,12,37,88,57,67,62,56,98,94,18,54,79,74,68 +26,53,77,99,95,85,38 +67,34,53,86,37,26,98,28,88 +12,67,68,37,75,58,89,26,48,53,21 +56,79,15,67,41,73,33,98,34,74,37,57,26,86,75,89,44 +98,68,33,57,37,18,56,88,41,86,58,89,26,69,48,53,77,28,99,25,95 +12,73,38,79,33,56,18,65,74,67,62,43,34,94,44,15,54 +64,83,35,33,37,34,47,73,38,54,98,18,15,43,79,62,68,12,74 +18,56,75,88,41,86,58,89,26,48,53,77,99,25,95,36,97,39,46 +86,58,48,21,77,28,99,25,95,36,72,39,85,82,35,47,43 +98,68,33,57,37,18,56,88,52,86,58,89,26,69,48,53,77,28,99,25,95 +97,62,12,35,38,47,43,98,94,34,85,44,67,54,83 +94,73,79,83,44,12,67,74,34,98,68,33,57,37,18,56,88,52,41,86,58 +41,99,77,85,56,39,36,89,21 +73,12,33,37,56,52,26 +15,68,88,41,86 +52,58,89,69,48,53,28,25,95,36,72,46,85,82,38 +74,94,97,65,62,39,12,83,35,15,36,38,47,73,64,82,44 +86,18,52,58,12,68,34,44,64 +65,73,95,62,82,72,94,83,36,77,85,97,35,79,47 +82,69,86,95,48,58,75,39,77,52,25,88,99 +39,18,97,21,72,37,58 +46,82,35,47,62,65,73,44,33 +28,94,65,77,54,62,48 +62,35,43,74,44,54,15,79,39,73,64,65,38,12,85,47,34,98,82,67,46 +18,26,69,56,97,89,99,46,95,88,48 +37,86,12,56,48,57,41,75,69,18,89,52,26,21,58 +56,54,73,79,98,65,12,83,75,88,62,57,68,18,64,67,37,74,43,44,34,15,33 +35,46,43,97,39,73,62,85,28,99,54,79,21,72,38,25,77,64,47 +56,75,88,52,41,26,69,28,95 +86,58,26,69,53,21,77,28,99,25,36,72,39,46,35,47,43 +72,89,88,53,28,86,26,25,48 +35,65,39,64,34,46,97,44,79,67,38,54,94,15,47,43,85,12,82 +85,46,65,36,97,72,25,94,54,38,82,95,43,53,35,77,99,21,47,62,73 +34,98,33,18,56,75,88,52,86,26,69,99,25 +25,72,46,36,39,26,38,47,58,95,85,48,53,86,99,82,35,89,43,97,28,69,21 +25,95,36,72,97,46,85,82,47,43,62,64,94,73,79,83,44,12,15 +41,86,58,77,99,97,39 +37,41,58,48,21,77,36,97,39 +56,86,89,69,77 +67,79,15,82,34,65,83,94,98,47,38,54,73,33,74,35,57,85,12,68,64,43,44 +52,75,18,56,98,68,74,86,67,88,57,33,94,15,79,73,44,41,89 +99,25,95,36,72,97,39,46,85,82,38,47,43,62,54,65,64,94,73,79,83,44,12 +39,72,25,85,82,36,52 +35,26,77,52,46,72,88,69,89,85,99,58,21,39,95 +28,99,25,95,36,72,97,39,46,35,38,47,54,65,64,94,73,83,44 +56,86,89,33,28,88,53,37,58,69,26,21,48,72,18,52,57,41,77,25,95 +43,46,12,44,97,15,85,72,47,82,35,64,54,38,95,67,39,73,65,62,83 +43,54,65,64,15,33,37,56,88 +21,28,94,99,97,79,39,54,35,25,46 +99,72,82,58,36,38,52,46,85 +37,65,43,79,73,67,74,98,64,44,38,68,12,33,15,47,83 +74,86,26,12,52,57,73,68,34 +37,18,94,44,83,75,86,65,34,64,52 +43,15,83,57,74,62,44,56,33,12,38,79,94,54,67,64,18,34,68,47,65 +74,68,33,57,56,75,88,52,86,58,89,26,69,48,53,21,77,28,99 +65,34,79,88,74,44,12,41,18,94,83,98,67,86,68,52,64 +68,95,53,18,69,88,86,56,28,25,98,33,57,89,77 +97,47,43,53,54,82,73,64,99,39,46 +77,28,25,95,36,97,46,85,82,38,47,43,62,65,64,73,83 +72,85,88,53,39,52,95,99,86,69,56 +58,53,77,86,89,74,99,18,21 +28,25,69,85,97,64,46 +82,83,15,74,98,33,37 +21,77,28,99,25,95,36,72,97,39,46,85,82,35,38,43,62,54,65,64,94,73,79 +53,46,28,97,35,77,38,62,65,21,48,43,39,82,99,47,25,85,54,26,36 +39,35,38,47,43,62,54,73,79,74,68 +18,99,98,74,69,88,53,33,68,26,34,86,57 +82,35,43,62,54,65,73,79,44,67,34,68,33,57,37 +54,47,94,36,82,99,62,79,95,83,39,25,46,72,65,73,97,38,35,28,85 +33,57,37,18,56,75,88,52,41,86,58,89,26,69,48,53,21,77,99,25,95,36,72 +86,88,41,98,18,15,75,33,89,74,48,56,34,37,44,57,52,68,58,67,53 +88,41,58,89,26,69,53,21,77,28,25,36,97,39,46,82,35 +64,67,62,43,54,56,15,12,98,73,44,47,83,65,37,94,74 +56,88,52,89,53,77,85 +44,74,37,88,48,89,58,68,52,12,98,67,33,53,69,18,75,26,41,56,15 +98,83,12,94,18,65,37,35,15,47,33,68,67,74,54,34,73,43,57,44,38 +41,58,26,48,53,77,28,99,25,95,36,97,39,85,82,38,47 +47,43,62,54,64,94,73,83,44,12,15,67,34,98,68,33,57,37,18,56,75 +57,36,97,26,56,69,99 +75,28,53,41,21,18,89,48,25,52,46,88,36 +52,75,68,83,12,65,73,98,57,44,34,15,74,64,37,94,54,41,88 +74,75,67,52,21,15,58 +26,69,48,53,21,77,28,99,25,95,36,72,39,46,85,82,35,38,47,43,62,54,65 +67,74,34,68,33,57,37,18,75,88,86,26,69,48,21,77,28 +68,88,58,89,26,53,21,28,36 +83,12,15,74,75,88,48 +99,25,95,36,72,97,39,46,62,64,73,44,12 +97,21,35,94,64,65,43,47,72 +77,95,25,62,46,39,99,38,21,72,82,36,65 +57,73,67,79,75,37,68,33,86 +95,36,97,39,46,85,82,35,38,47,43,62,65,64,94,73,79,83,12,15,67 +57,48,89,26,41,18,72,52,33,75,88,77,99,56,69 +79,56,86,58,75,12,41,34,73,26,18,57,74 +89,43,69,26,39,47,48,28,85,35,54 +95,36,72,97,39,46,85,82,35,38,47,43,62,54,65,64,94,73,79,83,44,15,67 +94,82,64,83,38,35,12,62,72,34,65 +73,68,26,74,89,12,52,34,15 +52,86,58,26,21,28,99,25,95,36,97,46,85,35,38 +43,54,64,83,67,34,98,68,33,57,37,56,88 +69,56,86,33,44,89,79,68,88 +74,34,98,68,57,56,75,88,41,26,69,48,77,28,99 +74,98,33,37,18,56,88,41,86,26,69,48,53,21,77,28,99 +77,62,39,43,21,28,25,35,97,47,38,85,54 +41,86,53,21,77,28,25,95,36,72,97,46,85,82,35,38,47 +65,74,88,79,64,34,12,15,57,37,73,44,75,98,18,52,83,56,94 +62,64,94,79,83,44,15,67,74,34,98,57,52 +82,38,43,62,54,65,64,94,79,83,44,12,67,74,34,98,33,57,37 +68,33,18,34,88,26,74,52,48,57,12,15,83,58,56,37,69,41,86,89,98 +99,52,21,36,18 +75,88,53,28,77,18,52,67,26 +64,68,37,18,56,41,58 +52,72,37,77,18,39,69,86,58,36,75,25,48 +37,18,56,75,88,41,26,69,48,53,77,25,95,36,39 +82,58,53,48,72,86,69,35,38,46,47,77,43 +15,33,56,57,79,75,88,37,64,83,65,62,34 +15,98,33,57,37,18,58,69,77 +89,69,21,85,38,43,54 +58,77,99,25,36,97,39,46,82,38,43 +64,83,46,15,85,94,38,36,74,67,79,43,73,65,35 +39,46,82,38,47,62,65,94,73,79,83,44,12,15,67,74,34,98,68 +89,26,69,53,21,77,28,99,25,95,36,72,97,39,46,85,82,35,43,62,54 +26,46,89,39,97,95,77,41,21,72,48,35,25,88,52,58,28,82,99 +99,65,46,25,72,47,95,77,82,21,28,62,43,39,48 +33,67,64,68,74,56,98,57,94,44,62,79,54,65,47,73,37,38,12 +12,64,37,67,65,41,57,98,75,54,94,83,73,88,52,44,34,33,68 +47,73,38,62,65,82,57,15,43,94,44,68,85,33,54 +72,64,83,15,85,97,44,94,46,43,12,65,25 +48,53,47,95,26,97,86,39,43,25,72,85,99,89,69,58,28,46,21,77,82,38,36 +65,64,94,73,79,83,44,12,15,67,74,34,98,68,33,57,37,56,75,88,52,41,86 +41,58,33,56,15,52,86,44,79,67,88,83,74,75,34,37,26,89,12,98,57,73,68 +99,41,75,37,88,89,21,25,86,56,48,26,18,53,57,69,68 +68,35,33,62,12,64,43,15,79,44,54,65,34,46,73 +75,83,52,65,94,98,56,34,15,88,37,62,73,54,74,67,18,57,64 +28,89,77,36,38,82,48,99,53,35,62,39,69,46,95 +54,57,15,74,82,33,47,34,35,37,98 +15,67,74,34,98,68,33,57,37,18,56,75,88,52,41,86,58,26,69,48,53,21,77 +99,69,41,89,39,26,52,28,86,35,36,72,82,48,58,38,85,21,77,53,95,97,25 +41,69,89,57,68,53,25,88,48,28,34,98,56,86,75,99,58,77,18,52,26 +57,37,18,56,52,41,86,58,89,26,69,48,53,21,77,28,99,72,97 +57,37,18,56,88,52,86,26,69,53,21,72,97 +38,72,28,77,99,43,58,97,26 +62,56,54,65,18,94,88,37,33,15,74,68,44,34,64,98,73,52,12 +41,86,58,26,69,53,21,77,28,99,25,95,36,72,97,39,46,85,82,38,47 +62,44,12,15,34,18,52 +25,77,88,58,28,75,41,56,97,86,69,39,95,89,18,99,72,53,48,37,36,26,21 +88,52,41,86,58,89,26,69,48,21,77,28,36,72,97,82,35 +48,86,53,12,34,89,57,67,98,74,37,26,69,88,58,44,33 +18,88,52,86,89,26,69,21,95,36,97,39,46 +88,77,25,69,26,52,85,72,39,89,97 +12,67,56,86,89,26,69,48,21 +41,57,21,89,58,28,98,34,77,88,75,86,68,52,25,48,53,99,69,56,37,26,18 +94,12,83,97,65,79,73,95,54,67,36,15,44,47,72,82,62 +86,99,28,21,98,37,57,89,25,88,68,69,52,48,34 +53,28,36,97,39,46,38,47,43,62,65,64,73 +99,25,65,36,64 +44,69,26,88,86,15,79 +44,43,54,75,37,65,57,88,79,33,94,12,83,34,18,68,74 +52,58,89,26,77,28,25,36,97,85,82,35,38 +54,64,28,46,36 +54,44,62,37,74,18,47,64,94,67,33,98,15,73,12,56,65,43,34,38,79,57,83 +34,68,33,88,52,86,58,89,53 +43,38,77,95,64,21,62,82,28,35,48,53,69,65,47,99,85,36,97,39,54 +94,86,12,98,41,57,58,64,88,75,44,73,37,33,68,56,74 +64,44,74,41,75,15,68,83,94,79,33 +83,44,12,15,67,74,98,68,57,37,18,56,75,52,41,86,58,89,26,69,48 +25,35,39,73,94,36,65,47,53 +75,53,37,26,33,58,69,56,25,28,41,89,99,98,52,95,21 +79,85,36,39,95,72,64,44,73,46,54,83,65,15,38,97,94,12,47,67,35 \ No newline at end of file diff --git a/day5/solution.py b/day5/solution.py new file mode 100644 index 0000000..011f1ad --- /dev/null +++ b/day5/solution.py @@ -0,0 +1,49 @@ +import sys + +def main(): + lines = sys.stdin.read().splitlines() + + order_rules = set() + updates = [] + i = 0 + n = len(lines) + while i < n and lines[i].strip(): + line = lines[i].strip() + if '|' in line: + x, y = line.split('|') + x = int(x) + y = int(y) + order_rules.add((x, y)) + i += 1 + + while i < n and not lines[i].strip(): + i += 1 + + while i < n: + line = lines[i].strip() + if line: + update = [int(x) for x in line.split(',')] + updates.append(update) + i += 1 + + middle_pages = [] + + for update in updates: + position = {page: idx for idx, page in enumerate(update)} + valid = True + for x, y in order_rules: + if x in position and y in position: + if position[x] >= position[y]: + valid = False + break + + if valid: + mid_index = len(update) // 2 + middle_page = update[mid_index] + middle_pages.append(middle_page) + + total = sum(middle_pages) + print(total) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/day5/solution2.py b/day5/solution2.py new file mode 100644 index 0000000..abf4908 --- /dev/null +++ b/day5/solution2.py @@ -0,0 +1,83 @@ +import sys +import collections + +def main(): + + lines = sys.stdin.read().splitlines() + + order_rules = set() + updates = [] + i = 0 + n = len(lines) + while i < n and lines[i].strip(): + line = lines[i].strip() + if '|' in line: + x, y = line.split('|') + x = int(x) + y = int(y) + order_rules.add((x, y)) + i += 1 + + while i < n and not lines[i].strip(): + i += 1 + + while i < n: + line = lines[i].strip() + if line: + update = [int(x) for x in line.split(',')] + updates.append(update) + i += 1 + + middle_pages_correct = [] + middle_pages_incorrect = [] + + for update in updates: + position = {page: idx for idx, page in enumerate(update)} + + valid = True + for x, y in order_rules: + if x in position and y in position: + if position[x] >= position[y]: + valid = False + break + + if valid: + mid_index = len(update) // 2 + middle_page = update[mid_index] + middle_pages_correct.append(middle_page) + else: + pages_in_update = set(update) + graph = collections.defaultdict(list) + indegree = collections.defaultdict(int) + for x in pages_in_update: + indegree[x] = 0 + + for x, y in order_rules: + if x in pages_in_update and y in pages_in_update: + graph[x].append(y) + indegree[y] += 1 + + queue = collections.deque([node for node in pages_in_update if indegree[node] == 0]) + sorted_pages = [] + + while queue: + node = queue.popleft() + sorted_pages.append(node) + for neighbor in graph[node]: + indegree[neighbor] -= 1 + if indegree[neighbor] == 0: + queue.append(neighbor) + + if len(sorted_pages) != len(pages_in_update): + print("Cycle detected in ordering rules. Cannot perform topological sort.") + sys.exit(1) + + mid_index = len(sorted_pages) // 2 + middle_page = sorted_pages[mid_index] + middle_pages_incorrect.append(middle_page) + + total_incorrect = sum(middle_pages_incorrect) + print(total_incorrect) + +if __name__ == "__main__": + main()