diff --git a/src/day05/example.txt b/src/day05/example.txt new file mode 100644 index 0000000..9d146d6 --- /dev/null +++ b/src/day05/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 diff --git a/src/day05/input.txt b/src/day05/input.txt new file mode 100644 index 0000000..c9a7937 --- /dev/null +++ b/src/day05/input.txt @@ -0,0 +1,1351 @@ +96|89 +15|17 +15|93 +55|49 +55|82 +55|72 +37|87 +37|26 +37|33 +37|12 +52|57 +52|89 +52|67 +52|19 +52|38 +58|97 +58|22 +58|91 +58|93 +58|32 +58|12 +61|49 +61|87 +61|58 +61|39 +61|63 +61|52 +61|84 +33|51 +33|49 +33|21 +33|71 +33|44 +33|97 +33|57 +33|67 +54|41 +54|56 +54|26 +54|96 +54|39 +54|44 +54|95 +54|52 +54|45 +88|57 +88|84 +88|55 +88|89 +88|91 +88|32 +88|25 +88|63 +88|33 +88|69 +24|26 +24|37 +24|45 +24|33 +24|25 +24|69 +24|15 +24|56 +24|87 +24|84 +24|63 +32|24 +32|21 +32|93 +32|62 +32|51 +32|72 +32|41 +32|71 +32|19 +32|95 +32|17 +32|53 +13|55 +13|65 +13|87 +13|96 +13|69 +13|25 +13|24 +13|26 +13|84 +13|33 +13|52 +13|39 +13|79 +31|25 +31|96 +31|55 +31|45 +31|61 +31|49 +31|39 +31|87 +31|58 +31|12 +31|37 +31|84 +31|89 +31|24 +93|38 +93|51 +93|62 +93|17 +93|44 +93|41 +93|79 +93|53 +93|65 +93|88 +93|56 +93|19 +93|71 +93|97 +93|24 +87|21 +87|19 +87|17 +87|89 +87|97 +87|93 +87|62 +87|55 +87|57 +87|15 +87|82 +87|22 +87|12 +87|63 +87|33 +87|49 +53|56 +53|37 +53|71 +53|54 +53|97 +53|17 +53|31 +53|88 +53|51 +53|79 +53|13 +53|61 +53|38 +53|95 +53|19 +53|47 +53|72 +71|51 +71|61 +71|37 +71|69 +71|96 +71|24 +71|87 +71|39 +71|65 +71|95 +71|47 +71|31 +71|79 +71|44 +71|88 +71|54 +71|84 +71|26 +84|58 +84|25 +84|77 +84|89 +84|82 +84|63 +84|57 +84|52 +84|33 +84|55 +84|17 +84|12 +84|26 +84|49 +84|67 +84|93 +84|53 +84|32 +84|91 +21|65 +21|39 +21|41 +21|56 +21|52 +21|37 +21|88 +21|62 +21|31 +21|47 +21|79 +21|61 +21|51 +21|69 +21|13 +21|72 +21|24 +21|54 +21|84 +21|95 +69|45 +69|57 +69|89 +69|33 +69|84 +69|58 +69|91 +69|63 +69|82 +69|96 +69|25 +69|15 +69|67 +69|49 +69|39 +69|93 +69|32 +69|87 +69|52 +69|12 +69|55 +49|17 +49|79 +49|67 +49|51 +49|54 +49|22 +49|57 +49|93 +49|72 +49|77 +49|38 +49|13 +49|82 +49|62 +49|44 +49|21 +49|95 +49|71 +49|32 +49|15 +49|53 +49|19 +47|58 +47|57 +47|39 +47|63 +47|89 +47|96 +47|33 +47|93 +47|91 +47|26 +47|45 +47|55 +47|25 +47|15 +47|82 +47|69 +47|67 +47|77 +47|32 +47|87 +47|49 +47|52 +47|12 +25|57 +25|32 +25|62 +25|63 +25|33 +25|77 +25|71 +25|19 +25|91 +25|21 +25|38 +25|72 +25|97 +25|93 +25|95 +25|12 +25|15 +25|82 +25|54 +25|53 +25|22 +25|67 +25|49 +25|17 +65|26 +65|52 +65|63 +65|47 +65|77 +65|91 +65|12 +65|39 +65|82 +65|96 +65|61 +65|56 +65|15 +65|49 +65|55 +65|88 +65|58 +65|25 +65|84 +65|87 +65|45 +65|89 +65|69 +65|33 +62|71 +62|45 +62|52 +62|95 +62|44 +62|56 +62|61 +62|39 +62|72 +62|65 +62|41 +62|31 +62|37 +62|97 +62|96 +62|47 +62|24 +62|69 +62|79 +62|84 +62|13 +62|54 +62|51 +62|88 +63|57 +63|97 +63|19 +63|15 +63|21 +63|77 +63|72 +63|54 +63|13 +63|67 +63|51 +63|71 +63|53 +63|38 +63|49 +63|17 +63|93 +63|95 +63|41 +63|32 +63|22 +63|82 +63|44 +63|62 +57|67 +57|93 +57|44 +57|54 +57|19 +57|79 +57|56 +57|38 +57|62 +57|21 +57|53 +57|13 +57|37 +57|22 +57|24 +57|31 +57|72 +57|17 +57|95 +57|65 +57|71 +57|51 +57|97 +57|41 +77|19 +77|79 +77|31 +77|22 +77|21 +77|67 +77|97 +77|62 +77|54 +77|82 +77|32 +77|51 +77|71 +77|53 +77|44 +77|95 +77|38 +77|57 +77|41 +77|24 +77|13 +77|93 +77|72 +77|17 +19|62 +19|97 +19|39 +19|21 +19|13 +19|54 +19|69 +19|88 +19|79 +19|45 +19|47 +19|56 +19|51 +19|84 +19|95 +19|72 +19|31 +19|61 +19|24 +19|44 +19|37 +19|41 +19|65 +19|71 +39|84 +39|67 +39|91 +39|12 +39|32 +39|53 +39|87 +39|25 +39|63 +39|89 +39|96 +39|58 +39|15 +39|49 +39|38 +39|52 +39|33 +39|22 +39|55 +39|57 +39|77 +39|93 +39|26 +39|82 +79|87 +79|88 +79|26 +79|61 +79|24 +79|52 +79|47 +79|37 +79|33 +79|91 +79|84 +79|65 +79|45 +79|63 +79|25 +79|39 +79|31 +79|56 +79|12 +79|58 +79|96 +79|69 +79|89 +79|55 +41|87 +41|79 +41|65 +41|39 +41|44 +41|55 +41|61 +41|37 +41|84 +41|58 +41|52 +41|45 +41|69 +41|56 +41|96 +41|47 +41|13 +41|51 +41|24 +41|31 +41|88 +41|25 +41|26 +41|89 +12|41 +12|77 +12|71 +12|67 +12|97 +12|93 +12|91 +12|82 +12|63 +12|62 +12|19 +12|32 +12|57 +12|21 +12|54 +12|38 +12|95 +12|53 +12|15 +12|49 +12|72 +12|17 +12|33 +12|22 +91|71 +91|63 +91|77 +91|49 +91|57 +91|51 +91|53 +91|33 +91|38 +91|82 +91|93 +91|62 +91|32 +91|41 +91|15 +91|22 +91|97 +91|21 +91|54 +91|17 +91|72 +91|95 +91|67 +91|19 +51|61 +51|26 +51|87 +51|65 +51|44 +51|84 +51|24 +51|39 +51|45 +51|52 +51|56 +51|96 +51|79 +51|47 +51|13 +51|89 +51|12 +51|69 +51|55 +51|31 +51|25 +51|58 +51|37 +51|88 +95|56 +95|58 +95|41 +95|39 +95|96 +95|79 +95|26 +95|47 +95|55 +95|45 +95|31 +95|61 +95|88 +95|84 +95|89 +95|65 +95|69 +95|44 +95|52 +95|87 +95|37 +95|24 +95|13 +95|51 +22|47 +22|21 +22|31 +22|62 +22|88 +22|45 +22|79 +22|97 +22|41 +22|37 +22|19 +22|61 +22|13 +22|24 +22|54 +22|69 +22|65 +22|44 +22|51 +22|95 +22|17 +22|71 +22|56 +22|72 +97|51 +97|31 +97|41 +97|54 +97|61 +97|71 +97|84 +97|45 +97|47 +97|95 +97|24 +97|26 +97|79 +97|96 +97|37 +97|72 +97|52 +97|69 +97|13 +97|44 +97|56 +97|65 +97|88 +97|39 +38|61 +38|31 +38|47 +38|65 +38|44 +38|88 +38|41 +38|37 +38|95 +38|62 +38|56 +38|79 +38|22 +38|17 +38|19 +38|97 +38|21 +38|71 +38|24 +38|51 +38|54 +38|72 +38|69 +38|13 +82|32 +82|53 +82|79 +82|17 +82|37 +82|44 +82|57 +82|31 +82|38 +82|54 +82|95 +82|13 +82|41 +82|19 +82|72 +82|24 +82|62 +82|71 +82|21 +82|93 +82|51 +82|67 +82|22 +82|97 +44|45 +44|87 +44|37 +44|47 +44|25 +44|56 +44|84 +44|58 +44|24 +44|89 +44|69 +44|13 +44|79 +44|26 +44|88 +44|52 +44|31 +44|91 +44|39 +44|12 +44|55 +44|61 +44|65 +44|96 +72|52 +72|65 +72|54 +72|84 +72|88 +72|44 +72|26 +72|96 +72|47 +72|37 +72|79 +72|39 +72|69 +72|71 +72|87 +72|95 +72|56 +72|31 +72|24 +72|41 +72|45 +72|51 +72|61 +72|13 +26|17 +26|57 +26|91 +26|67 +26|12 +26|89 +26|15 +26|32 +26|22 +26|82 +26|77 +26|38 +26|33 +26|55 +26|63 +26|49 +26|21 +26|58 +26|93 +26|19 +26|25 +26|53 +26|87 +26|62 +17|88 +17|45 +17|71 +17|44 +17|41 +17|79 +17|19 +17|97 +17|62 +17|39 +17|69 +17|54 +17|72 +17|37 +17|13 +17|47 +17|61 +17|21 +17|51 +17|31 +17|56 +17|24 +17|95 +17|65 +45|89 +45|53 +45|39 +45|52 +45|87 +45|63 +45|12 +45|91 +45|67 +45|32 +45|33 +45|93 +45|82 +45|57 +45|15 +45|77 +45|25 +45|96 +45|38 +45|84 +45|55 +45|58 +45|49 +45|26 +67|71 +67|61 +67|19 +67|95 +67|97 +67|21 +67|56 +67|41 +67|79 +67|24 +67|65 +67|37 +67|38 +67|22 +67|54 +67|72 +67|17 +67|51 +67|31 +67|13 +67|62 +67|88 +67|44 +67|53 +89|53 +89|25 +89|97 +89|93 +89|91 +89|62 +89|22 +89|54 +89|32 +89|15 +89|82 +89|38 +89|12 +89|17 +89|33 +89|49 +89|57 +89|19 +89|63 +89|21 +89|72 +89|77 +89|67 +89|71 +56|52 +56|87 +56|96 +56|25 +56|45 +56|63 +56|89 +56|88 +56|82 +56|61 +56|26 +56|33 +56|58 +56|84 +56|12 +56|39 +56|55 +56|15 +56|91 +56|47 +56|77 +56|32 +56|49 +56|69 +96|58 +96|22 +96|49 +96|77 +96|82 +96|32 +96|57 +96|19 +96|21 +96|87 +96|55 +96|33 +96|38 +96|15 +96|63 +96|17 +96|53 +96|12 +96|67 +96|26 +96|25 +96|91 +96|93 +15|19 +15|54 +15|13 +15|77 +15|38 +15|31 +15|72 +15|32 +15|41 +15|51 +15|71 +15|44 +15|53 +15|97 +15|95 +15|82 +15|67 +15|57 +15|21 +15|79 +15|62 +15|22 +55|33 +55|15 +55|93 +55|12 +55|38 +55|22 +55|89 +55|58 +55|21 +55|62 +55|67 +55|19 +55|97 +55|91 +55|17 +55|32 +55|25 +55|57 +55|63 +55|77 +55|53 +37|77 +37|25 +37|91 +37|49 +37|52 +37|58 +37|15 +37|63 +37|89 +37|56 +37|39 +37|47 +37|96 +37|88 +37|84 +37|69 +37|65 +37|61 +37|55 +37|45 +52|12 +52|82 +52|96 +52|53 +52|77 +52|58 +52|25 +52|49 +52|93 +52|55 +52|22 +52|33 +52|26 +52|32 +52|87 +52|15 +52|63 +52|91 +52|17 +58|19 +58|25 +58|21 +58|71 +58|62 +58|67 +58|82 +58|15 +58|33 +58|89 +58|49 +58|77 +58|63 +58|38 +58|17 +58|57 +58|53 +58|72 +61|32 +61|26 +61|57 +61|77 +61|47 +61|33 +61|96 +61|45 +61|55 +61|25 +61|12 +61|82 +61|15 +61|93 +61|91 +61|89 +61|69 +33|53 +33|63 +33|77 +33|62 +33|19 +33|54 +33|38 +33|32 +33|93 +33|72 +33|82 +33|22 +33|95 +33|17 +33|15 +33|41 +54|55 +54|51 +54|13 +54|87 +54|58 +54|65 +54|88 +54|47 +54|79 +54|84 +54|31 +54|24 +54|69 +54|37 +54|61 +88|61 +88|87 +88|82 +88|12 +88|39 +88|15 +88|49 +88|52 +88|77 +88|47 +88|26 +88|58 +88|45 +88|96 +24|39 +24|12 +24|91 +24|61 +24|52 +24|88 +24|96 +24|49 +24|47 +24|65 +24|58 +24|55 +24|89 +32|57 +32|65 +32|37 +32|67 +32|38 +32|44 +32|97 +32|13 +32|31 +32|79 +32|54 +32|22 +13|37 +13|88 +13|91 +13|31 +13|56 +13|58 +13|61 +13|47 +13|45 +13|12 +13|89 +31|63 +31|91 +31|65 +31|26 +31|88 +31|33 +31|56 +31|47 +31|52 +31|69 +93|72 +93|54 +93|37 +93|22 +93|31 +93|21 +93|67 +93|13 +93|95 +87|25 +87|38 +87|53 +87|32 +87|58 +87|67 +87|77 +87|91 +53|44 +53|62 +53|65 +53|21 +53|41 +53|24 +53|22 +71|55 +71|13 +71|45 +71|52 +71|56 +71|41 +84|96 +84|38 +84|87 +84|22 +84|15 +21|44 +21|45 +21|71 +21|97 +69|26 +69|53 +69|77 +49|41 +49|97 +47|84 + +79,65,51,72,41,95,37,24,97,61,13,44,21,38,71,56,17,31,47,62,22,88,19 +62,97,72,71,54,95,41,51,44,13,79,31,24,37,65,56,88,61,45,39,52 +13,79,21,69,97,54,22,17,56,51,44,65,72,19,71,61,88,62,24,41,95 +54,95,13,79,31,24,56,88,61,47,39,84,52,96,55 +56,69,39,52,26,55,89,25,91,15,82 +37,21,24,51,19,72,54,44,56,95,41,71,22,38,17,62,67,88,65,31,97 +79,51,97,84,61,41,21,95,71,72,62 +65,56,88,47,69,45,39,84,52,96,26,87,55,58,89,12,33,63,49,15,77 +72,41,54,19,95,13,65,24,44,38,62,22,71,97,93,31,57,37,53,67,21 +21,32,79,51,67,53,44,19,15 +57,13,19,93,82,67,79,53,22,62,41,71,54,21,32,17,72,44,51,15,38 +97,72,71,95,41,51,44,13,31,24,37,65,88,61,47,69,84,52,96 +17,21,72,95,41,51,44,79,65,56,88,47,45 +79,24,37,65,56,88,47,69,45,39,52,87,55,58,89,25,12,91,33 +62,97,95,41,51,44,13,56,52 +56,69,89,25,33,49,15,77,82 +62,71,54,95,45,39,52 +95,49,77,17,44,54,38,57,97,67,62,22,13,82,32,41,19 +41,51,44,13,79,31,24,37,65,56,88,61,47,69,45,84,52,96,26,87,55,58,89 +39,84,52,96,26,87,55,89,12,91,33,63,49,15,77,82,32,93,67,53,38 +12,15,77,32,93,53,22,19,62 +31,24,37,61,84,89,63 +47,39,84,96,26,55,58,91,33,15,77,32,93 +61,47,69,39,58,25,91,33,49,15,77,32,57 +12,91,25,96,45,13,58 +33,77,57,38,19,62,72 +51,67,71,19,53,54,32,93,17 +37,65,56,88,47,69,39,84,52,96,26,87,58,89,25,12,91,33,63,49,15 +15,77,57,93,17,21,62,97,54,95,13 +87,88,65,26,13,58,39,37,47,55,25,89,51 +63,19,72,89,49,25,15,67,62 +41,51,79,37,45,52,89 +31,65,56,88,61,47,69,45,39,52,26,87,55,58,89,25,12,91,63 +91,32,55,77,25 +96,91,52,37,84,31,12,55,39,87,89,69,24,25,65,88,47,45,33,58,26,79,56 +63,25,15,56,61,45,96,52,87,88,39,49,58,26,89,91,84,69,47,77,65 +26,87,55,89,33,49,15,82,32,57,93,53,38,22,17,19,21 +12,32,82,53,57,96,15,52,91,63,33 +62,45,61,44,54,88,52,97,69 +67,22,19,21,62,97,54,95,51,44,79,24,37,56,88 +41,51,44,13,79,31,65,56,88,61,69,45,39,84,52,96,26,87,55,58,89 +56,63,52,33,47,82,58 +38,93,15,84,57,22,89,91,26,53,67,96,82,77,25,32,63,58,33 +53,82,67,32,58,87,22,38,93,26,25,57,91,96,33,63,49,89,12,19,17,55,15 +96,26,55,58,89,25,91,33,63,15,77,57,93,67,53,38,17 +33,49,15,77,93,53,38,22,97,71,54,41,51 +26,87,58,89,12,91,33,77,32,57,93,67,53,38,21 +61,47,69,39,87,55,58,89,33,63,15,82,57 +44,38,15,51,53,72,62,22,95,93,41,17,71,82,97,19,13,32,21,79,57,54,67 +41,95,54,52,39,56,26,72,71,37,65,24,51,13,88 +24,65,88,45,39,84,52,26,55,58,89,25,12,91,33,63,49 +24,52,55,87,56,89,65,33,84,12,58,39,45,88,31,91,37,25,79,47,96,61,26 +88,39,24,63,25,61,33,37,56,96,31 +21,62,97,72,54,41,51,44,31,24,37,65,56,61,47,69,45,39,84 +88,61,69,45,39,84,52,96,26,87,55,58,89,25,12,91,33,63,49,77,32 +67,96,15,93,87,52,39,26,69,77,58,32,91,82,63,84,12,55,57 +38,93,51,71,31,97,57,21,72,13,41,44,65 +84,96,87,89,12,33,63,49,15,77,32,57,53 +61,47,39,55,25,12,33,82,57 +89,25,12,49,32,67,38,19,62 +82,93,97,71,54,79,24 +89,91,55,25,12,32,97,63,93 +62,97,72,54,95,41,51,44,13,79,31,24,65,88,61,45,39,84,52 +97,93,71,21,67,95,54,33,91,22,72,32,82,63,53,12,57,77,49 +13,24,47,69,84,96,87,58,25,12,91 +65,25,89,37,24,58,55,49,69,63,61,87,56,33,39 +13,49,54,32,22,62,19,44,97,57,38,71,15,51,82,93,21,53,67 +87,55,89,25,12,91,33,63,49,15,77,82,32,57,93,67,53,38,19,21,62 +91,63,49,38,17,97,41 +93,67,53,38,22,17,19,21,62,97,72,71,54,41,44,13,31,24,37,65,56 +84,52,26,87,58,89,25,12,91,33,63,15,82,32,93,67,53,38,22 +22,17,21,62,97,71,95,41,51,44,79,24,65,56,61,47,69 +51,89,45,69,26,56,24,88,61,47,39,31,44,65,96,25,55,37,58 +88,31,56,69,26,13,39,37,96,55,45,61,58,24,65 +21,24,41,72,93,53,17,65,13,54,62,95,38,57,37,31,97,19,71,22,67,51,79 +15,77,22,91,67,17,49,82,72,12,32 +32,93,67,53,22,17,19,21,62,71,54,41,51,13,24 +31,24,56,88,61,45,39,96,26,87,55,89,25,12,91,33,63 +72,95,41,51,44,13,31,24,37,65,56,88,47,69,39 +88,58,52,24,26,13,89,25,39,84,45 +21,62,51,13,37,65,84 +33,77,82,32,67,38,17,97,71,95,51 +12,33,91,53,21,82,32,15,57,93,17,38,77,67,89,87,62,25,58 +79,26,55,24,39,88,89,51,31,47,52,58,37,25,56,61,84,44,13 +56,31,45,39,91,52,79,12,96,69,13,37,25,88,87,89,24,65,58,84,61 +53,38,17,97,71,54,41,13,24 +89,93,53,33,87,25,77,57,63,19,91,15,22,38,21,12,58,32,67,82,17 +71,97,79,17,13,54,44,41,22,21,93,57,32,38,31 +97,72,71,54,95,51,44,79,65,56,88,69,45,52,96 +77,87,49,25,67,89,15,33,55,93,17,91,38,12,82,53,63,26,52,96,22 +49,77,82,32,53,62,41,51,13 +39,57,55,89,82,25,93,49,96,45,58,33,53,67,12,77,91 +17,31,21,95,69,41,61,79,13,72,47,88,97,44,62,65,51,45,24 +25,12,91,33,63,77,82,32,57,93,67,53,38,22,19,21,62,97,72,71,54 +62,71,54,41,51,24,37,65,88 +41,44,13,79,65,56,61,69,45,52,96,26,87,58,89 +62,72,51,44,88,61,84 +37,65,56,61,47,69,45,52,96,26,87,55,33,49,15 +47,69,45,96,26,55,58,25,12,63,49,15,77,82,93 +37,26,52,65,55,15,63 +19,57,17,77,67,58,22,72,12,62,53 +56,41,72,96,47,97,88,24,52,84,45,37,51 +32,53,21,62,95,41,44,79,37 +38,22,62,72,79,41,37,53,56,24,44,19,51,31,97,54,71 +96,39,58,25,61,55,13,47,65,84,52,12,89,87,56,91,45,69,37 +45,67,32,93,63,53,96 +79,95,69,96,54,87,71,26,52,65,41,44,31,24,51,84,61,37,56 +32,93,17,19,97,71,54,95,44,79,24 +45,63,25,69,58,93,87,33,12,49,84,55,47,89,26 +61,56,58,37,44,95,26,31,96,87,39,65,52,51,88,84,13,45,24,41,47,79,69 +31,61,58,33,47,24,79,52,87,89,91 +63,84,52,32,77,93,91,87,96,53,55,57,82,12,26,22,67,89,25 +71,54,95,51,24,88,47,69,84,52,26 +69,45,39,84,52,96,26,87,55,58,89,12,63,49,15,77,82,32,57,93,67 +25,12,82,57,93,53,54 +62,97,54,95,44,13,24,65,56,69,45,84,52 +26,87,89,25,12,49,53,38,22,19,21 +79,56,88,47,69,96,89,12,33 +57,93,53,38,19,62,71,54,41,44,79,31,65 +15,77,32,22,19,62,97,72,71,54,95,41,51,13,79 +49,77,32,93,67,53,17,62,97,72,71,51,13 +26,25,89,49,58,24,37 +25,12,33,63,49,15,82,32,93,67,38,22,21,62,97,71,54 +13,31,24,37,65,56,88,61,47,45,39,84,26,87,58,89,25,12,91 +51,44,24,65,56,88,47,96,87 +71,54,51,44,13,79,31,65,88,61,47,69,39,52,26 +55,89,25,12,91,33,63,49,77,82,32,57,93,53,38,22,17,19,21,62,97 +19,62,71,95,51,13,79,31,37,56,61,45,39 +33,63,15,77,67,17,97,71,51 +93,17,62,54,51,44,31,24,56 +51,93,19,53,13,21,95,44,67,41,17,24,31,22,38,62,79,72,56,65,37 +49,12,63,97,72,91,21,15,57,93,19,32,71,82,77,22,62,53,67,38,25,17,54 +93,62,41,54,53,67,72,17,71,91,97 +63,15,97,54,95,41,44 +26,61,84,54,45,41,51,52,87,37,13,47,88,39,56,65,55 +89,25,12,91,33,63,49,77,82,32,57,93,67,53,38,22,17,19,21,62,97,72,71 +25,49,72,53,71,57,91,15,17,82,22,67,54,38,62,63,77 +88,47,96,26,58,91,33,77,82 +71,53,67,54,72,95,21,17,41 +97,72,71,54,95,41,51,44,13,79,24,37,65,56,88,61,47,69,45,39,84,52,96 +71,54,51,13,79,37,56,47,69,26,87 +89,96,24,56,47,33,49,65,87 +69,45,39,84,52,26,87,55,91,33,57,93,67 +24,38,57,13,22,19,53,37,44,72,31,65,97,21,67,79,51,62,41,17,71,93,54 +13,12,25,84,79,96,55,65,89,44,24,31,61,88,39 +91,87,57,49,19,15,62,53,89 +26,87,58,89,25,12,91,33,77,82,57,93,67,53,38,22,17,19,21 +84,51,26,69,44,72,61,31,95,88,47,41,39 +15,67,33,38,52,53,32,12,17 +12,53,82,19,95,49,54,72,33,21,63,32,67 +32,93,53,22,21,72,54,95,51,44,79,31,37 +49,77,57,38,97,72,54,51,13 +31,37,56,88,61,47,69,84,52,96,87,55,58,91,63 +13,71,24,88,44,79,19,72,95,65,56,41,67,21,31,62,54 +63,49,15,77,82,32,57,93,67,53,38,22,17,19,21,62,97,72,71,95,41,51,44 +65,13,26,69,56,96,87,37,31,84,24,71,95 +61,96,89,91,33 +26,89,93,57,52,12,91,63,53,33,15,49,82,67,39,84,55,87,96,25,45,58,77 +52,91,82,25,88,77,45,55,32 +71,19,62,95,77,57,79,13,97,72,41 +53,38,22,17,19,62,97,72,71,54,95,41,51,44,13,79,31,24,37,65,56,88,61 +53,19,21,62,97,72,71,54,95,41,51,44,13,79,31,37,65,88,61 +44,71,67,54,51,77,82,72,31,38,32 +93,82,63,67,25,72,49,62,12,33,53,58,57,21,38,22,19 +19,21,62,71,41,24,37,65,56,88,61,47,69,45,39 +95,17,54,57,21,72,63,82,32,51,44,38,77,62,67,71,41 +15,63,88,12,39,37,91 +52,96,87,55,49,15,32,93,53,38,17 +63,55,91,89,45,31,56,65,26,96,37 +32,57,93,67,38,22,17,19,21,62,97,72,71,54,41,44,13,24,37 +69,12,32,87,88,77,47,58,96,89,49,84,39 +12,91,63,49,32,17,62,97,71,54,95 +31,19,44,47,71,51,62,17,97,88,37,79,61,24,22,13,21,65,56 +45,12,25,26,84,13,24,65,37,58,79,87,44,55,39 diff --git a/src/day05/mod.rs b/src/day05/mod.rs new file mode 100644 index 0000000..eb591e1 --- /dev/null +++ b/src/day05/mod.rs @@ -0,0 +1,94 @@ +use std::{cmp::Ordering, collections::HashSet}; + +use itertools::Itertools; + +fn parse(input: &str) -> (HashSet<(usize, usize)>, &str) { + let (rules, updates) = input.split("\n\n").collect_tuple().unwrap(); + ( + rules + .lines() + .map(|line| { + line.split('|') + .map(|x| x.parse().unwrap()) + .collect_tuple() + .unwrap() + }) + .collect(), + updates, + ) +} + +fn parse_update(rules: &HashSet<(usize, usize)>, update: &str) -> (Vec, Vec) { + let pages: Vec = update.split(',').map(|x| x.parse().unwrap()).collect(); + let mut sorted = pages.to_vec(); + sorted.sort_by( + |&x, &y| match (rules.contains(&(x, y)), rules.contains(&(y, x))) { + (true, false) => Ordering::Less, + (false, true) => Ordering::Greater, + _ => panic!(), + }, + ); + (pages, sorted) +} + +fn middle(pages: &[usize]) -> usize { + pages[pages.len() / 2] +} + +pub fn puzzle1(input: &str) -> usize { + let (rules, updates) = parse(input); + updates + .lines() + .filter_map(|line| { + let (pages, sorted) = parse_update(&rules, line); + if pages == sorted { + Some(middle(&pages)) + } else { + None + } + }) + .sum() +} + +pub fn puzzle2(input: &str) -> usize { + let (rules, updates) = parse(input); + updates + .lines() + .filter_map(|line| { + let (pages, sorted) = parse_update(&rules, line); + if pages == sorted { + None + } else { + Some(middle(&sorted)) + } + }) + .sum() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE: &str = include_str!("example.txt"); + const INPUT: &str = include_str!("input.txt"); + + #[test] + fn test_puzzle1_example() { + assert_eq!(puzzle1(EXAMPLE), 143); + } + + #[test] + fn test_puzzle1_input() { + assert_eq!(puzzle1(INPUT), 5166); + } + + #[test] + fn test_puzzle2_example() { + assert_eq!(puzzle2(EXAMPLE), 123); + } + + #[test] + fn test_puzzle2_input() { + assert_eq!(puzzle2(INPUT), 4679); + } +} diff --git a/src/main.rs b/src/main.rs index b1ee4da..765be62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod day01; mod day02; mod day03; mod day04; +mod day05; use std::{env, fs}; @@ -23,6 +24,9 @@ fn main() { (4, 1) => day04::puzzle1(&input).to_string(), (4, 2) => day04::puzzle2(&input).to_string(), + (5, 1) => day05::puzzle1(&input).to_string(), + (5, 2) => day05::puzzle2(&input).to_string(), + _ => panic!("no puzzle {} for day {}", puzzle, day), }; println!("{}", answer.trim_end());