From c43a24c3349b790e582822678b203db429f6e364 Mon Sep 17 00:00:00 2001 From: merrywhether Date: Wed, 4 Dec 2024 23:52:05 -0800 Subject: [PATCH] day 05 --- src/05/input.txt | 1370 +++++++++++++++++++++++++++++++++++++++++++++ src/05/main.ts | 69 +++ src/05/sample.txt | 28 + src/05/test.ts | 12 + 4 files changed, 1479 insertions(+) create mode 100644 src/05/input.txt create mode 100644 src/05/main.ts create mode 100644 src/05/sample.txt create mode 100644 src/05/test.ts diff --git a/src/05/input.txt b/src/05/input.txt new file mode 100644 index 0000000..2c06efa --- /dev/null +++ b/src/05/input.txt @@ -0,0 +1,1370 @@ +66|19 +99|87 +99|55 +49|28 +49|64 +49|81 +43|81 +43|55 +43|13 +43|39 +18|34 +18|51 +18|66 +18|53 +18|65 +83|87 +83|74 +83|56 +83|26 +83|17 +83|47 +84|64 +84|81 +84|43 +84|61 +84|57 +84|66 +84|12 +74|14 +74|88 +74|64 +74|66 +74|15 +74|99 +74|49 +74|52 +87|26 +87|38 +87|57 +87|18 +87|84 +87|43 +87|28 +87|65 +87|22 +64|45 +64|14 +64|77 +64|55 +64|94 +64|29 +64|56 +64|26 +64|25 +64|22 +88|37 +88|77 +88|55 +88|76 +88|64 +88|35 +88|57 +88|99 +88|43 +88|13 +88|19 +19|56 +19|76 +19|37 +19|14 +19|99 +19|64 +19|83 +19|43 +19|24 +19|15 +19|35 +19|77 +48|58 +48|39 +48|15 +48|81 +48|75 +48|29 +48|13 +48|35 +48|45 +48|12 +48|61 +48|14 +48|37 +53|14 +53|42 +53|15 +53|83 +53|25 +53|12 +53|77 +53|58 +53|76 +53|48 +53|39 +53|37 +53|56 +53|55 +29|62 +29|97 +29|88 +29|65 +29|17 +29|52 +29|42 +29|38 +29|51 +29|28 +29|22 +29|47 +29|87 +29|84 +29|45 +14|24 +14|62 +14|87 +14|56 +14|65 +14|47 +14|45 +14|18 +14|42 +14|75 +14|17 +14|92 +14|58 +14|84 +14|25 +14|83 +77|13 +77|17 +77|22 +77|38 +77|94 +77|58 +77|75 +77|24 +77|49 +77|92 +77|83 +77|84 +77|87 +77|56 +77|18 +77|47 +77|45 +45|74 +45|26 +45|97 +45|18 +45|75 +45|57 +45|38 +45|49 +45|19 +45|51 +45|94 +45|62 +45|28 +45|92 +45|34 +45|88 +45|87 +45|84 +24|26 +24|88 +24|42 +24|22 +24|65 +24|34 +24|17 +24|62 +24|97 +24|94 +24|47 +24|28 +24|52 +24|49 +24|38 +24|29 +24|84 +24|18 +24|75 +35|25 +35|38 +35|14 +35|18 +35|26 +35|87 +35|97 +35|24 +35|22 +35|58 +35|65 +35|94 +35|29 +35|47 +35|56 +35|76 +35|45 +35|37 +35|62 +35|77 +62|48 +62|88 +62|81 +62|49 +62|97 +62|15 +62|99 +62|51 +62|84 +62|53 +62|61 +62|65 +62|34 +62|66 +62|64 +62|28 +62|92 +62|74 +62|38 +62|57 +62|19 +39|25 +39|22 +39|13 +39|62 +39|35 +39|24 +39|58 +39|56 +39|14 +39|94 +39|87 +39|77 +39|47 +39|26 +39|18 +39|76 +39|83 +39|42 +39|75 +39|97 +39|29 +39|37 +92|43 +92|28 +92|55 +92|99 +92|49 +92|81 +92|34 +92|19 +92|88 +92|66 +92|15 +92|39 +92|51 +92|17 +92|48 +92|57 +92|74 +92|84 +92|61 +92|64 +92|52 +92|35 +92|12 +81|29 +81|55 +81|58 +81|25 +81|39 +81|35 +81|76 +81|77 +81|56 +81|12 +81|24 +81|83 +81|22 +81|64 +81|13 +81|75 +81|45 +81|15 +81|99 +81|47 +81|37 +81|87 +81|42 +81|14 +15|77 +15|22 +15|94 +15|56 +15|47 +15|76 +15|24 +15|83 +15|75 +15|18 +15|14 +15|29 +15|58 +15|12 +15|13 +15|64 +15|39 +15|45 +15|87 +15|37 +15|35 +15|25 +15|55 +15|42 +28|88 +28|35 +28|53 +28|57 +28|76 +28|12 +28|81 +28|77 +28|14 +28|19 +28|48 +28|39 +28|61 +28|99 +28|83 +28|66 +28|55 +28|34 +28|51 +28|43 +28|15 +28|37 +28|64 +28|25 +37|42 +37|26 +37|77 +37|47 +37|92 +37|62 +37|22 +37|45 +37|94 +37|25 +37|56 +37|24 +37|58 +37|14 +37|18 +37|87 +37|38 +37|17 +37|13 +37|75 +37|83 +37|97 +37|65 +37|29 +38|12 +38|57 +38|81 +38|43 +38|34 +38|61 +38|17 +38|28 +38|49 +38|55 +38|88 +38|51 +38|53 +38|39 +38|92 +38|15 +38|84 +38|19 +38|64 +38|52 +38|48 +38|99 +38|74 +38|66 +56|17 +56|38 +56|13 +56|22 +56|87 +56|65 +56|42 +56|74 +56|75 +56|49 +56|94 +56|84 +56|28 +56|26 +56|62 +56|52 +56|92 +56|18 +56|24 +56|97 +56|29 +56|45 +56|47 +56|58 +25|77 +25|29 +25|62 +25|58 +25|47 +25|56 +25|42 +25|87 +25|65 +25|75 +25|92 +25|97 +25|13 +25|83 +25|84 +25|94 +25|38 +25|24 +25|74 +25|22 +25|26 +25|45 +25|17 +25|18 +47|18 +47|62 +47|51 +47|28 +47|52 +47|97 +47|88 +47|22 +47|49 +47|43 +47|92 +47|26 +47|17 +47|19 +47|74 +47|66 +47|87 +47|53 +47|38 +47|84 +47|94 +47|57 +47|34 +47|65 +55|62 +55|42 +55|87 +55|18 +55|22 +55|24 +55|76 +55|75 +55|58 +55|35 +55|94 +55|39 +55|12 +55|14 +55|13 +55|47 +55|29 +55|37 +55|45 +55|56 +55|26 +55|77 +55|25 +55|83 +17|74 +17|43 +17|84 +17|28 +17|15 +17|51 +17|35 +17|57 +17|52 +17|12 +17|39 +17|99 +17|66 +17|88 +17|49 +17|48 +17|55 +17|64 +17|61 +17|81 +17|76 +17|34 +17|19 +17|53 +12|42 +12|37 +12|75 +12|47 +12|14 +12|18 +12|25 +12|35 +12|13 +12|22 +12|83 +12|45 +12|56 +12|76 +12|26 +12|87 +12|29 +12|65 +12|24 +12|94 +12|62 +12|58 +12|77 +12|97 +52|25 +52|35 +52|64 +52|77 +52|53 +52|28 +52|57 +52|61 +52|14 +52|66 +52|55 +52|81 +52|37 +52|99 +52|12 +52|34 +52|88 +52|76 +52|19 +52|43 +52|48 +52|51 +52|39 +52|15 +61|12 +61|87 +61|24 +61|81 +61|25 +61|77 +61|45 +61|15 +61|83 +61|47 +61|37 +61|56 +61|39 +61|13 +61|14 +61|75 +61|58 +61|29 +61|76 +61|55 +61|64 +61|99 +61|35 +61|42 +76|38 +76|45 +76|94 +76|97 +76|14 +76|83 +76|13 +76|24 +76|18 +76|56 +76|22 +76|26 +76|42 +76|65 +76|75 +76|37 +76|25 +76|58 +76|87 +76|47 +76|77 +76|92 +76|29 +76|62 +94|84 +94|28 +94|57 +94|66 +94|26 +94|81 +94|99 +94|88 +94|38 +94|74 +94|62 +94|97 +94|34 +94|19 +94|51 +94|49 +94|65 +94|53 +94|48 +94|17 +94|52 +94|43 +94|92 +94|61 +65|17 +65|61 +65|19 +65|99 +65|53 +65|81 +65|43 +65|88 +65|28 +65|49 +65|84 +65|66 +65|92 +65|38 +65|64 +65|39 +65|48 +65|57 +65|74 +65|15 +65|55 +65|52 +65|34 +65|51 +57|66 +57|24 +57|55 +57|19 +57|25 +57|15 +57|14 +57|35 +57|48 +57|77 +57|81 +57|61 +57|43 +57|64 +57|56 +57|76 +57|58 +57|99 +57|12 +57|39 +57|83 +57|53 +57|37 +57|13 +51|66 +51|55 +51|25 +51|53 +51|48 +51|81 +51|12 +51|99 +51|39 +51|43 +51|61 +51|76 +51|57 +51|14 +51|37 +51|77 +51|35 +51|15 +51|64 +51|19 +51|56 +51|34 +51|83 +51|88 +42|18 +42|51 +42|66 +42|28 +42|17 +42|84 +42|26 +42|45 +42|49 +42|38 +42|87 +42|74 +42|75 +42|88 +42|94 +42|52 +42|97 +42|22 +42|62 +42|65 +42|47 +42|92 +42|57 +42|34 +97|66 +97|15 +97|28 +97|49 +97|52 +97|17 +97|81 +97|84 +97|38 +97|61 +97|43 +97|65 +97|99 +97|48 +97|55 +97|34 +97|19 +97|57 +97|88 +97|74 +97|64 +97|51 +97|53 +97|92 +34|43 +34|76 +34|39 +34|35 +34|53 +34|55 +34|37 +34|57 +34|66 +34|77 +34|58 +34|61 +34|13 +34|48 +34|83 +34|14 +34|25 +34|15 +34|99 +34|56 +34|19 +34|12 +34|64 +34|81 +75|22 +75|88 +75|28 +75|66 +75|74 +75|65 +75|51 +75|62 +75|94 +75|97 +75|52 +75|57 +75|49 +75|18 +75|19 +75|92 +75|38 +75|17 +75|26 +75|87 +75|47 +75|34 +75|43 +75|84 +22|57 +22|28 +22|88 +22|18 +22|17 +22|92 +22|52 +22|19 +22|26 +22|62 +22|53 +22|61 +22|65 +22|38 +22|51 +22|97 +22|66 +22|43 +22|84 +22|74 +22|34 +22|94 +22|49 +22|48 +58|49 +58|74 +58|45 +58|26 +58|62 +58|87 +58|22 +58|88 +58|75 +58|47 +58|65 +58|24 +58|18 +58|17 +58|42 +58|97 +58|94 +58|29 +58|84 +58|92 +58|28 +58|38 +58|51 +58|52 +26|92 +26|99 +26|34 +26|74 +26|61 +26|81 +26|43 +26|19 +26|48 +26|62 +26|65 +26|38 +26|17 +26|28 +26|51 +26|15 +26|52 +26|84 +26|88 +26|66 +26|53 +26|57 +26|97 +26|49 +13|65 +13|17 +13|94 +13|58 +13|47 +13|22 +13|49 +13|84 +13|51 +13|29 +13|75 +13|92 +13|42 +13|26 +13|87 +13|24 +13|97 +13|28 +13|18 +13|74 +13|62 +13|38 +13|52 +13|45 +66|83 +66|55 +66|25 +66|13 +66|61 +66|39 +66|43 +66|58 +66|15 +66|35 +66|14 +66|53 +66|24 +66|56 +66|29 +66|64 +66|37 +66|77 +66|48 +66|76 +66|99 +66|81 +66|12 +99|75 +99|14 +99|39 +99|22 +99|77 +99|42 +99|45 +99|83 +99|12 +99|29 +99|15 +99|37 +99|58 +99|18 +99|35 +99|56 +99|47 +99|64 +99|25 +99|76 +99|13 +99|24 +49|57 +49|88 +49|19 +49|99 +49|35 +49|76 +49|25 +49|61 +49|15 +49|39 +49|66 +49|34 +49|52 +49|14 +49|48 +49|37 +49|55 +49|43 +49|51 +49|12 +49|53 +43|61 +43|14 +43|45 +43|25 +43|83 +43|64 +43|35 +43|12 +43|37 +43|53 +43|15 +43|76 +43|29 +43|58 +43|56 +43|48 +43|77 +43|24 +43|42 +43|99 +18|17 +18|97 +18|61 +18|52 +18|48 +18|19 +18|57 +18|81 +18|74 +18|43 +18|49 +18|62 +18|84 +18|92 +18|94 +18|38 +18|26 +18|88 +18|28 +83|18 +83|49 +83|24 +83|42 +83|45 +83|62 +83|65 +83|29 +83|22 +83|97 +83|75 +83|13 +83|58 +83|94 +83|38 +83|84 +83|92 +83|52 +84|49 +84|52 +84|48 +84|15 +84|35 +84|34 +84|37 +84|74 +84|99 +84|19 +84|39 +84|76 +84|53 +84|55 +84|88 +84|28 +84|51 +74|19 +74|55 +74|12 +74|51 +74|57 +74|34 +74|48 +74|76 +74|28 +74|53 +74|35 +74|81 +74|37 +74|61 +74|39 +74|43 +87|51 +87|17 +87|92 +87|49 +87|88 +87|34 +87|53 +87|74 +87|62 +87|48 +87|94 +87|52 +87|66 +87|19 +87|97 +64|18 +64|24 +64|58 +64|13 +64|35 +64|87 +64|47 +64|83 +64|76 +64|37 +64|75 +64|12 +64|42 +64|39 +88|39 +88|48 +88|25 +88|66 +88|83 +88|81 +88|15 +88|61 +88|14 +88|56 +88|12 +88|34 +88|53 +19|61 +19|48 +19|42 +19|58 +19|81 +19|39 +19|29 +19|55 +19|13 +19|25 +19|12 +19|53 +48|47 +48|25 +48|64 +48|76 +48|24 +48|99 +48|77 +48|56 +48|42 +48|83 +48|55 +53|13 +53|81 +53|99 +53|64 +53|75 +53|45 +53|61 +53|35 +53|24 +53|29 +29|75 +29|26 +29|49 +29|74 +29|94 +29|92 +29|34 +29|57 +29|18 +14|77 +14|13 +14|97 +14|38 +14|29 +14|26 +14|94 +14|22 +77|62 +77|42 +77|65 +77|97 +77|74 +77|26 +77|29 +45|47 +45|17 +45|52 +45|66 +45|65 +45|22 +24|87 +24|51 +24|92 +24|45 +24|74 +35|75 +35|83 +35|13 +35|42 +62|52 +62|43 +62|17 +39|12 +39|45 +92|53 + +62,65,92,74,49,52,28,88,57,61,81,99,15 +61,99,55,39,35,76,25,77,83,13,29,45,47 +84,97,38,34,94,65,57,81,19,66,74 +49,52,51,88,66,19,43,53,48,61,81,99,15,55,39,12,35,76,37 +13,58,25,62,12,37,97,29,42,87,77 +94,75,58,38,62,29,56,26,18,97,47,49,24,42,45,22,17,13,74,84,83,65,87 +48,64,37,13,24,42,75 +75,22,18,94,26,62,97,65,38,92,17,84,74,49,52,51,88,34,66 +34,66,43,53,81,64,13 +52,28,88,61,81,15,12 +19,53,48,64,55,99,56,34,43,77,88,76,66,25,35 +12,35,76,37,14,25,83,56,13,58,24,29,42,45,75,47,87,22,18,94,26,62,97 +48,81,84,28,92,34,43,66,51,57,62,97,17,65,49,19,15,88,38,52,61,99,53 +48,58,29,53,37,25,64,76,77,55,15,83,45 +22,24,35,45,75,87,26,77,56,14,76,12,62,37,97,18,83,58,13,94,47 +49,52,28,51,88,57,66,19,43,53,61,81,99,15,64,55,39,12,35,76,37 +26,97,65,38,49,52,88,48,99 +34,57,66,19,43,53,48,61,81,99,15,64,55,39,12,35,76,37,14,25,77,83,13 +45,75,22,84,28 +34,76,15,49,55,84,66,19,35,64,12 +35,76,37,14,25,77,83,56,58,24,42,45,47,87,22,18,62,97,65 +42,76,48,29,81,45,58,99,53,61,14,83,24,35,64 +55,61,77,53,43,83,57,48,99,15,66,56,14,25,12 +84,49,52,51,88,34,57,19,43,48,61,99,55,35,76 +52,28,34,57,66,53,48,61,99,55,12,35,76,37,14 +74,49,52,28,51,88,34,57,43,53,48,81,99,64,55,12,35,76,37 +76,65,87,83,58,38,26,77,42,25,47,13,97,24,45,18,22,29,37,56,75,62,94 +53,99,15,12,76,25,77,29,45 +43,53,48,61,81,99,15,64,55,39,12,35,76,37,14,25,83,56,13,58,24,29,42 +39,14,37,64,61,58,56 +99,65,55,88,28,48,19 +88,38,26,99,19,66,17,49,34,43,53,61,28,65,74,92,62 +35,39,45,12,83,56,47,42,25,75,37,81,24,76,99,87,64,14,29 +55,81,61,43,25,88,35,48,66,12,15,14,34,52,37,99,19,64,57 +75,47,87,22,18,94,26,62,97,65,38,92,17,84,74,49,52,28,51,88,34,57,19 +37,14,25,77,83,56,13,24,42,45,47,87,22,18,94,26,62,97,65,38,92 +58,45,47,18,94,26,92,17,52,28,51 +76,37,58,24,42,45,47 +56,13,24,29,42,47,87,22,18,94,26,62,97,65,38,92,17,84,74,49,52 +34,88,84,66,75,19,57,94,62,47,49 +45,75,47,87,22,18,94,26,62,97,65,38,17,84,74,49,52,28,51,88,34,57,66 +28,84,52,49,42,26,18,38,97,75,17,94,87 +35,99,58,61,48,37,15,19,55,64,53,81,13,56,43,39,24 +57,19,53,99,12,37,58 +75,25,83,14,64,12,77,39,56,45,35,81,61,99,76,13,55,47,24,58,29,42,37 +83,14,92,62,38,42,24,75,26,97,29,94,13,37,47,18,25,45,22 +45,75,47,87,22,18,94,26,62,65,38,92,17,84,74,49,52,28,51,88,34,57,66 +52,64,55,17,38,92,43,53,65,51,28,74,66,88,19,81,99,84,57,48,61,15,49 +83,56,58,24,42 +18,94,26,62,97,65,38,92,17,84,74,49,52,28,51,88,34,57,66,19,43,53,61 +17,74,49,28,51,88,66,53,61,99,55,12,35 +57,66,19,43,53,48,61,64,55,39,12,76,37,14,25,77,83,13,58 +43,58,61,39,57,14,37,77,66,81,55,48,99,12,13,64,15,53,19,56,76 +55,39,12,76,37,14,25,77,83,56,13,58,24,29,42,45,75,47,87,22,18,94,26 +65,92,17,84,52,28,51,88,34,66,19,43,53,48,61,81,99,64,55 +52,28,51,88,34,57,66,19,43,53,48,61,99,15,64,55,39,12,35,37,25 +56,42,18,65,14,45,94,22,26,58,75,47,25 +76,37,14,25,77,56,13,58,24,29,42,45,75,87,18,94,26,62,97,65,38 +81,97,62,17,43,28,15,51,38 +65,17,84,74,49,52,51,34,43,48,61,81,99,15,55 +92,74,52,66,53,81,12 +24,45,75,47,22,94,62,97,65,92,17,74,52,51,88 +49,52,88,34,57,66,19,53,61,81,99,15,64,39,12,35,37 +17,84,74,49,52,28,51,88,34,57,66,19,43,53,48,81,99,15,39,12,35 +37,58,87,45,47,55,94,22,35,42,18,26,77 +62,97,65,92,49,34,57,43,53,48,81,99,15 +28,88,34,57,66,19,43,53,48,61,81,99,15,64,55,12,76,37,14,25,77 +34,57,19,43,53,48,61,81,99,15,64,55,39,12,35,76,37,14,25,77,83,56,13 +29,92,94,77,24,62,58,42,65,97,84,47,75,22,25,87,83,26,18 +74,28,51,34,57,19,43,48,61,81,15,64,55,12,35,76,37 +35,12,76,25,42,13,37,26,77,87,39,29,94,56,45,47,14,58,75,22,24,83,62 +94,26,97,52,88,34,57,43,81 +75,47,87,18,94,26,62,97,65,38,92,17,84,49,52,51,88,34,57,66,19 +35,83,81,15,24 +29,39,55,64,13,47,12,18,42,87,24,94,14 +49,52,28,51,88,34,57,66,19,53,48,61,81,99,15,55,39,12,35,37,14 +48,61,81,15,55,39,35,76,37,14,77,56,13,58,24,29,42,45,75 +39,35,14,83,24,29,45,87,22,26,62 +43,53,61,81,15,55,39,76,25,77,58,29,42 +49,81,61,48,55,88,15,12,14,37,51,57,66,34,99,52,64,39,76 +97,47,37,87,75,83,77,94,45,56,76,42,25,13,29,22,18,58,14,38,65 +53,48,15,12,35,13,58,29,45 +42,22,12,47,75,35,18,24,58,45,97,29,13,76,25,26,37,87,94,56,83 +49,47,26,74,65,38,19,57,84,92,66,43,87,52,97,94,17,34,18,28,62 +55,39,12,35,76,14,77,56,13,29,45,75,47,87,22,18,26 +58,97,28,84,18,29,42,92,13,87,47 +24,64,76,13,83,15,77,61,56,42,75,12,81,39,45,25,29,37,14,48,58,99,55 +92,74,49,28,57,53,81 +58,24,29,42,45,75,47,87,22,18,94,26,62,97,65,38,92,17,84,49,52,28,51 +74,43,84,28,19,87,62,38,94,88,18,66,47,34,65 +34,66,61,15,55,35,25,77,83 +29,42,45,47,22,65,38,92,49,28,51,88,34 +13,24,29,42,45,75,47,87,94,65,74,49,28 +37,25,77,83,56,42,45,47,62,38,92 +77,56,24,42,45,75,26,62,97,17,74 +57,66,19,43,53,48,61,81,99,15,55,39,12,35,37,25,56 +48,35,76,64,77,37,43,39,61,15,99 +83,56,58,24,42,45,87,22,94,97,65,92,84,74,49 +77,64,14,22,37,29,39,13,24,76,83,99,35 +58,66,99,15,43,25,64,76,12,61,19,35,13,14,39 +37,14,58,42,47,87,22,65,92 +55,61,42,75,81,15,58,64,83,13,25,14,48,56,24,29,39,77,45,99,12 +92,43,65,52,66,61,74,81,84,62,15,53,97,28,19,57,88,34,17,51,49 +26,62,97,65,38,92,17,84,74,49,52,28,51,34,57,66,19,43,53,48,61,81,99 +47,87,22,18,94,62,65,92,74,49,52,28,88,57,43 +92,52,94,81,57,49,97 +25,77,83,56,13,58,29,42,45,75,87,22,94,26,62,97,65,17,84 +84,94,97,45,47,66,92,26,49,88,57 +47,42,94,24,12,87,37,64,35 +38,87,51,97,65,94,52,26,66,57,49,18,75,92,62,17,88,84,47,34,74,45,22 +26,97,84,88,66,61,99 +66,43,62,65,87,94,17,57,49,97,19,52,47 +28,84,65,66,74,38,55,99,64 +14,77,83,13,29,42,45,75,47,22,26,62,65 +62,65,38,92,52,88,15 +84,88,66,19,61,81,12 +14,55,76,48,88,43,49,28,57,37,15,53,66,19,39,52,64,81,51 +87,22,18,94,26,62,97,65,38,92,17,84,74,49,52,28,51,88,34,19,53 +28,42,38,87,22,45,97,34,92,51,17,26,47,29,94,18,49,52,74,75,65,84,62 +97,17,18,26,45,13,22,29,94,25,38,87,42,56,47,14,62,24,58 +17,84,74,52,28,88,34,57,66,19,43,99,64,55,35 +48,99,15,64,39,12,37,14,83,56,13,24,42,45,75 +14,25,77,83,56,13,58,24,29,42,45,75,47,22,18,94,26,62,97,65,38,92,17 +55,25,15,35,58,24,19,77,39,66,12,13,37,53,56,81,76,14,43,61,48,83,64 +14,19,83,48,76,24,13,61,29,15,25,37,12,99,81 +88,34,99,64,35,83,56 +22,94,26,62,97,65,92,17,84,74,28,88,34,57,66,19,48 +43,83,53,25,51,57,37 +99,77,64,28,37,12,15,43,25 +84,52,51,34,19,43,48,61,99,15,64,55,39,35,76 +56,24,38,75,62,29,74,26,47,17,94,97,45,87,52,49,42,84,13 +13,58,24,29,42,87,94,26,65,74,28 +97,65,38,92,17,84,74,49,52,28,51,88,34,57,66,19,53,48,61,81,99,15,64 +42,94,56,76,25,58,37,45,14,18,12,22,87,47,24,75,26,97,13 +26,49,28,57,22,45,87,66,18,92,65 +42,18,92,17,84,34,57 +52,28,51,88,34,57,66,19,43,53,48,61,81,99,15,55,39,12,35,76,37,14,25 +47,38,92,17,84,74,49,88,34,66,43 +76,53,15,19,51,37,66,83,35,48,61,88,55,77,81 +87,94,97,65,38,92,84,49,52,28,51,88,66,43,53 +19,52,99,49,66,34,53,81,61,64,57,74,12,55,43,39,15,92,17,88,28 +55,39,12,35,14,13,42,45,75 +29,84,24,75,18,38,47,22,87,28,42,49,74,51,17,65,88,26,62,45,97,92,52 +87,58,49,42,75,84,52 +53,48,61,81,99,15,64,55,39,12,35,76,37,25,77,56,13,58,24,42,45 +52,57,66,48,61,37,25 +55,12,35,37,14,25,77,13,58,29,42,45,75,47,87,22,18 +81,15,64,39,12,76,25,77,83,56,13,24,42,45,75,47,87 +18,38,17,84,49,52,34,19,43,48,61 +88,52,66,97,74,17,92,94,65,45,75,84,87,51,47 +19,53,48,99,55,12,76,14,25,77,83,56,13,58,29 +13,24,29,45,87,94,26,62,65,84,74,49,28 +77,55,75,26,42,37,29 +37,56,42,45,47,13,35,55,22,83,75,64,58,76,15,14,18 +66,97,26,65,57,92,88,62,18,51,74,94,38,48,53,19,49,52,34 +56,26,18,92,49,38,83,13,47,97,94 +49,52,88,19,43,81,99,15,12,35,76,37,14 +37,83,35,24,13,43,12,99,77,66,64,14,55,39,48,58,76 +17,26,51,22,38,42,57,74,84,92,34 +29,42,45,75,47,87,18,26,62,97,38,17,74,52,34 +19,43,53,48,61,81,64,55,12,35,76,14,25,77,83,58,24 +99,28,15,14,12,55,37,49,51 +28,12,53,81,43,74,76,57,55,64,35,52,37,34,99,49,51,88,66,48,19,39,61 +18,94,26,62,65,92,17,84,74,52,51,88,34,57,53,48,61 +39,12,35,76,37,14,25,77,83,56,13,24,29,42,45,75,47,87,22,18,94,26,62 +25,77,83,56,13,58,24,29,42,45,75,47,22,18,94,26,62,97,65,38,92,17,84 +57,66,19,43,48,61,81,99,15,64,55,39,35,76,37,25,83,56,58 +53,55,57,81,28,48,74,39,17,38,52,61,66,15,84,51,92 +22,77,12,42,39,14,45,37,29,94,18,83,55,75,47,24,35,76,56,13,58,26,25 +58,25,22,42,26,14,62,29,18,56,83 +34,76,37,25,83,56,13 +77,56,24,42,45,22,18,26,62,97,38,92,17,84,74 +29,87,94,26,65,17,84,74,49,52,28,88,34 +15,64,55,39,12,35,14,25,77,24,29,42,45,75,47,87,18 +47,65,43,94,74,38,62 +22,18,94,26,62,97,92,17,84,52,28,88,34,66,43,53,48 +28,51,57,66,43,53,99,64,55,12,76,37,25 +24,83,58,35,14,45,64,75,15,29,25,76,81,55,42,39,77,13,61,47,56 +26,62,97,65,38,92,17,84,74,49,52,28,88,34,57,66,19,43,53,48,61,81,99 +81,15,12,35,14,25,56,13,58,75,87 +37,14,25,77,83,56,13,58,29,42,45,75,87,22,94,26,62,97,65,38,92 +64,55,37,58,45,22,94 +38,92,17,84,74,49,52,28,51,88,34,57,19,43,53,48,61,81,99,15,64,55,39 +15,64,55,39,12,35,76,37,14,25,77,83,56,58,24,29,42,45,75,47,87,22,18 +14,25,77,83,56,58,24,29,42,45,75,47,87,22,18,94,26,97,65,92,17 +65,26,38,62,24,29,94,45,84,17,51,42,52,87,28 +56,13,29,42,45,87,22,18,26,62,97,65,92,84,74,49,52 +97,65,38,92,17,84,74,52,28,51,88,34,57,66,19,43,53,48,61,81,99,15,64 +22,62,97,17,74,49,28,51,43,53,48 +35,99,25,12,76,53,64,48,15,19,52,57,61,43,51,81,14,88,66,37,55,34,28 +22,18,94,26,62,97,65,38,17,84,74,49,52,28,51,88,34,57,66,19,43,53,48 +74,94,97,47,65,29,49,87,18,13,24,52,84,58,22,56,75,42,17,62,38 +65,92,17,74,49,52,28,34,57,66,43,48,61,81,99,64,55 diff --git a/src/05/main.ts b/src/05/main.ts new file mode 100644 index 0000000..af1e35a --- /dev/null +++ b/src/05/main.ts @@ -0,0 +1,69 @@ +// { legalUpdateMiddlePageSum: 5955, fixedUpdateMiddlePageSum: 4030 } + +export async function main(target = "input") { + const dirpath = new URL(".", import.meta.url).pathname; + const text = await Deno.readTextFile(`${dirpath}${target}.txt`); + + // precedence rules yield all the pages that must be updated before a given page + // (and thus can't come after) + const { precedenceRules, updates } = text.split("\n").reduce((agg, line) => { + if (line.match(/^\d+\|\d+$/)) { + const [preceder, follower] = line.split("|").map(Number); + agg.precedenceRules[follower] ??= []; + agg.precedenceRules[follower].push(preceder); + } else if (line.match(/^[\d,]+$/)) { + agg.updates.push(line.split(",").map(Number)); + } + + return agg; + }, { + precedenceRules: {} as Record, + updates: [] as number[][], + }); + + function testUpdate(update: number[]) { + return update.reduce((agg, page, idx) => { + if (agg.isLegal) { + if (agg.illegalFollowers.includes(page)) { + agg.illegalPosition = idx; + agg.isLegal = false; + } else if (precedenceRules[page]) { + agg.illegalFollowers.push(...precedenceRules[page]); + } + } + return agg; + }, { + illegalPosition: -Infinity, + isLegal: true, + illegalFollowers: [] as number[], + }); + } + + return updates.reduce((agg, update) => { + let isDefaultLegal = true; + let result = testUpdate(update); + while (!result.isLegal) { + isDefaultLegal = false; + + // move illegal item one position forward + update.splice( + result.illegalPosition - 1, + 2, + update[result.illegalPosition], + update[result.illegalPosition - 1], + ); + + result = testUpdate(update); + } + + agg[ + isDefaultLegal ? "legalUpdateMiddlePageSum" : "fixedUpdateMiddlePageSum" + ] += update[Math.floor(update.length / 2)]; + + return agg; + }, { legalUpdateMiddlePageSum: 0, fixedUpdateMiddlePageSum: 0 }); +} + +if (import.meta.main) { + console.log(await main()); +} diff --git a/src/05/sample.txt b/src/05/sample.txt new file mode 100644 index 0000000..9d146d6 --- /dev/null +++ b/src/05/sample.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/05/test.ts b/src/05/test.ts new file mode 100644 index 0000000..3a4d5c7 --- /dev/null +++ b/src/05/test.ts @@ -0,0 +1,12 @@ +import { assertEquals } from "@std/assert"; +import { main } from "./main.ts"; + +Deno.test("correct legal update middle page sum for sample", async () => { + const result = await main("sample"); + assertEquals(result.legalUpdateMiddlePageSum, 143); +}); + +Deno.test("correct fixed update middle page sum for sample", async () => { + const result = await main("sample"); + assertEquals(result.fixedUpdateMiddlePageSum, 123); +});