From 34608bbdf1e30feabd2f4afc250b4dc2717ab7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kleszczy=C5=84ski?= Date: Mon, 2 Dec 2024 16:24:32 +0100 Subject: [PATCH] 2024 Day 02 --- 2024/d01/main.ts | 14 +- 2024/d02/input.txt | 1000 ++++++++++++++++++++++++++++++++++++++ 2024/d02/main.ts | 48 ++ 2024/d02/test-runs/0.txt | 6 + 2024/tsconfig.json | 1 - 2024/util/aoc.ts | 70 +-- 2024/util/array.ts | 17 + README.md | 2 +- package.json | 5 +- 9 files changed, 1118 insertions(+), 45 deletions(-) create mode 100644 2024/d02/input.txt create mode 100644 2024/d02/main.ts create mode 100644 2024/d02/test-runs/0.txt create mode 100644 2024/util/array.ts diff --git a/2024/d01/main.ts b/2024/d01/main.ts index 3e97021..a76d243 100644 --- a/2024/d01/main.ts +++ b/2024/d01/main.ts @@ -12,9 +12,12 @@ const part1 = (data: ReadonlyArray<[number, number]>): number => { } const part2 = (data: ReadonlyArray<[number, number]>): number => { - const rightCounts = counting(data.map(item => item[1]), item => item) + const rightCounts = counting( + data.map(item => item[1]), + item => item, + ) const similiarityScores = data.map(item => (rightCounts[item[0]] ?? 0) * item[0]) - + return sum(similiarityScores) } @@ -24,12 +27,9 @@ const reader = (file: string): ReadonlyArray<[number, number]> => { .map(line => { const match = line.match(/(\d+)\s+(\d+)/) - return [ - parseInt(match[1], 10), - parseInt(match[2], 10) - ] as [number, number] + return [parseInt(match[1], 10), parseInt(match[2], 10)] as [number, number] }) } testRuns('01', reader, part1, part2) -solutionRuns('01', reader, part1, part2) \ No newline at end of file +solutionRuns('01', reader, part1, part2) diff --git a/2024/d02/input.txt b/2024/d02/input.txt new file mode 100644 index 0000000..05f7e9f --- /dev/null +++ b/2024/d02/input.txt @@ -0,0 +1,1000 @@ +6 8 9 10 12 13 12 +76 77 80 82 84 86 88 88 +84 86 88 90 93 97 +21 24 26 29 30 33 36 42 +23 24 25 26 24 25 27 +79 82 83 82 81 +64 66 63 66 67 68 69 69 +65 66 67 70 71 68 70 74 +42 45 48 46 47 48 49 54 +10 13 13 15 16 +57 59 62 62 61 +13 15 18 18 21 23 23 +83 84 85 88 88 92 +79 81 83 85 85 92 +76 78 79 81 83 87 88 91 +38 40 43 47 50 47 +84 87 89 93 95 96 96 +56 57 59 62 66 70 +75 76 80 83 85 87 92 +12 15 16 22 25 +42 45 46 53 50 +17 18 20 23 29 32 32 +72 73 79 82 84 88 +44 47 49 51 56 59 61 66 +88 86 88 89 90 93 95 +77 76 77 79 81 83 84 82 +33 30 31 34 36 36 +74 71 74 76 77 80 81 85 +33 30 32 34 35 42 +58 55 54 55 58 +17 14 17 14 15 18 17 +3 1 3 2 4 6 6 +61 60 61 60 61 65 +32 29 32 29 32 34 36 43 +53 52 54 57 60 60 62 +59 58 61 62 63 64 64 61 +42 40 40 42 44 45 47 47 +35 33 36 38 38 42 +62 59 59 61 62 64 65 70 +57 55 57 61 62 +27 26 27 31 32 31 +80 79 83 85 85 +69 68 70 73 74 76 80 84 +34 31 34 38 43 +33 31 37 40 43 +73 72 74 77 83 84 81 +91 90 96 98 99 99 +78 77 79 85 87 90 94 +14 13 19 21 23 30 +35 35 37 39 42 43 45 +9 9 12 15 18 20 19 +91 91 93 94 95 95 +35 35 37 39 41 44 45 49 +71 71 73 74 79 +63 63 66 65 67 70 +44 44 46 49 51 49 52 51 +72 72 74 76 74 74 +80 80 77 80 81 85 +67 67 69 68 71 74 75 81 +88 88 89 89 91 93 +15 15 15 17 14 +5 5 6 6 9 9 +33 33 35 35 36 38 42 +70 70 73 73 80 +71 71 72 76 79 81 84 +61 61 64 68 69 70 67 +67 67 69 71 74 77 81 81 +26 26 30 33 37 +74 74 78 79 82 83 89 +50 50 57 59 61 +82 82 85 87 93 95 98 97 +63 63 69 70 71 72 72 +40 40 41 44 49 51 55 +7 7 8 11 14 16 21 28 +5 9 11 12 13 16 18 21 +36 40 41 44 47 49 51 49 +87 91 92 95 96 96 +79 83 86 87 90 91 93 97 +3 7 8 11 14 20 +69 73 74 75 78 76 79 80 +31 35 38 40 37 36 +70 74 75 78 81 79 82 82 +58 62 59 61 64 66 67 71 +81 85 88 87 90 96 +46 50 53 54 55 55 56 59 +32 36 39 39 36 +86 90 92 94 94 95 95 +25 29 31 32 35 35 36 40 +14 18 20 23 23 29 +46 50 53 56 57 61 62 63 +11 15 17 18 20 24 21 +47 51 55 58 58 +57 61 65 66 68 72 +18 22 25 29 32 37 +56 60 62 63 64 71 73 74 +19 23 30 31 34 33 +33 37 39 42 47 48 50 50 +70 74 75 78 83 86 90 +41 45 51 53 55 57 58 65 +9 14 17 18 19 21 +67 74 76 78 79 80 77 +58 63 65 67 68 70 71 71 +38 45 48 51 55 +4 11 12 14 20 +64 71 72 73 71 74 +93 98 99 97 94 +63 69 72 75 74 74 +15 21 22 21 24 27 31 +10 16 19 17 22 +3 8 8 11 12 +46 52 54 55 55 56 58 55 +35 42 42 45 47 48 50 50 +43 49 49 52 56 +51 57 59 60 60 65 +5 11 14 16 20 23 25 27 +19 26 28 32 35 37 40 37 +64 71 75 77 78 79 79 +27 33 37 40 43 44 48 +26 31 35 37 39 46 +77 82 85 90 93 +59 66 73 74 71 +81 87 88 95 96 97 97 +52 57 62 65 69 +22 28 30 31 36 37 38 45 +65 64 63 61 64 +72 70 69 66 63 63 +98 95 94 93 89 +37 36 34 31 28 25 22 17 +61 60 57 55 58 56 55 54 +25 24 23 22 21 24 26 +77 76 77 74 72 71 71 +86 85 84 81 82 78 +69 68 65 64 63 65 64 57 +33 31 28 28 27 24 +65 63 62 62 65 +73 70 70 69 68 65 65 +57 55 55 54 52 48 +78 77 74 73 73 68 +75 73 70 67 66 62 61 +90 88 84 83 80 78 79 +19 16 12 10 9 9 +50 47 44 43 39 38 34 +35 32 29 27 23 22 16 +47 45 43 40 33 31 +86 85 84 79 81 +28 26 23 17 17 +32 30 23 21 18 14 +46 43 41 40 38 35 29 23 +47 50 47 46 44 41 38 37 +86 88 87 86 84 87 +88 90 89 86 83 83 +68 71 69 67 64 62 59 55 +81 82 81 78 77 74 71 64 +8 11 13 11 9 +49 51 52 49 48 47 44 47 +66 68 71 70 69 66 63 63 +23 24 26 23 19 +47 48 46 47 40 +47 50 47 47 46 +37 38 36 36 38 +38 39 39 37 34 31 31 +33 36 36 33 30 26 +87 88 88 85 84 82 81 75 +79 80 78 74 72 71 69 +76 79 76 73 69 67 68 +31 33 31 29 27 23 23 +92 94 90 87 85 82 78 +92 94 90 88 85 78 +80 82 77 75 74 72 +81 82 80 73 75 +10 11 8 3 3 +42 44 43 41 39 34 30 +48 51 50 43 42 40 33 +72 72 70 68 67 65 +47 47 45 43 42 44 +64 64 61 59 59 +60 60 57 55 53 49 +40 40 39 36 33 30 25 +86 86 84 83 86 83 +36 36 38 37 35 33 36 +55 55 54 51 50 52 52 +76 76 74 75 72 68 +91 91 89 92 90 89 83 +81 81 81 78 75 73 +69 69 66 66 65 64 61 64 +73 73 71 69 69 69 +18 18 17 15 15 14 10 +80 80 80 78 76 70 +32 32 31 27 25 24 22 +94 94 91 90 89 86 82 85 +10 10 7 5 1 1 +72 72 71 67 63 +20 20 16 13 8 +38 38 35 32 31 30 24 22 +97 97 95 92 86 84 82 85 +23 23 18 16 14 12 12 +86 86 85 79 77 73 +42 42 35 33 31 28 22 +45 41 38 37 36 34 32 +85 81 78 77 76 75 76 +53 49 46 44 44 +92 88 85 82 78 +42 38 35 34 31 30 29 24 +41 37 36 34 35 33 30 27 +64 60 59 56 54 51 54 55 +10 6 5 4 3 6 6 +10 6 8 7 3 +98 94 93 90 93 92 87 +66 62 62 61 59 56 53 52 +93 89 87 85 85 83 81 83 +38 34 32 29 27 27 25 25 +80 76 74 71 69 66 66 62 +40 36 35 32 29 27 27 21 +52 48 44 42 41 +35 31 29 28 24 27 +65 61 58 54 54 +76 72 70 68 65 63 59 55 +77 73 72 70 66 59 +55 51 48 42 41 +80 76 74 72 66 67 +29 25 19 18 17 15 15 +60 56 53 47 45 41 +86 82 80 74 71 65 +67 60 57 55 52 +76 71 68 67 64 63 60 63 +99 93 91 90 89 87 84 84 +63 57 54 53 50 47 44 40 +49 42 39 36 35 30 +42 37 38 37 36 35 +31 24 21 23 20 17 20 +25 20 18 15 18 15 15 +92 87 86 85 88 87 83 +53 46 44 46 43 42 40 34 +74 68 66 66 64 +73 67 67 64 65 +69 64 61 61 61 +43 38 36 33 33 29 +20 14 12 12 7 +18 12 8 5 2 +99 94 91 90 86 88 +74 69 67 63 61 61 +65 59 56 52 48 +81 75 72 69 66 63 59 52 +18 13 11 9 7 2 1 +73 66 59 57 58 +35 28 27 21 20 20 +60 55 50 49 48 47 43 +84 79 78 77 76 71 65 +8 10 13 14 17 16 +18 21 23 24 25 26 28 28 +24 27 28 29 33 +29 32 35 38 43 +55 58 59 58 61 +70 71 74 72 69 +60 61 63 61 63 64 66 66 +34 37 40 37 41 +27 30 27 28 33 +1 3 6 6 7 8 10 +71 74 74 76 79 76 +61 62 64 64 66 68 68 +29 31 31 34 37 38 40 44 +50 53 55 57 57 59 60 66 +36 37 38 40 43 46 50 53 +5 8 12 13 14 12 +58 59 63 64 66 69 71 71 +11 13 16 19 23 27 +59 61 64 66 69 71 75 80 +70 73 78 79 82 84 +28 29 34 35 32 +85 88 93 96 98 98 +17 20 21 23 30 31 33 37 +26 29 30 32 35 37 43 48 +17 16 18 19 22 23 +46 45 46 48 49 52 51 +22 21 24 26 28 28 +20 18 20 22 25 27 31 +47 44 47 48 55 +26 23 26 28 30 33 32 34 +90 89 91 94 96 94 95 93 +99 97 98 99 97 97 +60 57 59 60 59 63 +71 69 67 70 71 76 +72 71 74 77 77 80 81 +44 42 43 43 45 48 51 48 +28 26 29 31 34 35 35 35 +53 52 52 54 55 59 +52 51 53 55 55 62 +93 92 96 98 99 +64 62 65 69 67 +4 1 4 8 9 9 +81 78 81 85 88 92 +10 8 11 15 21 +84 83 88 90 92 +23 21 22 27 24 +46 43 45 46 51 54 54 +59 56 59 65 69 +19 16 19 21 26 32 +85 85 86 88 89 91 +18 18 21 23 20 +52 52 55 58 60 62 62 +55 55 58 60 63 67 +52 52 54 56 57 59 66 +65 65 67 70 67 70 73 76 +31 31 32 35 38 37 40 38 +62 62 65 63 63 +18 18 16 17 18 19 23 +14 14 15 13 14 17 19 24 +42 42 43 45 45 47 48 49 +65 65 65 67 64 +49 49 51 51 53 53 +82 82 85 87 87 88 92 +30 30 33 33 38 +66 66 68 72 74 76 77 +40 40 43 45 47 51 49 +86 86 88 89 93 93 +55 55 58 62 66 +76 76 80 81 84 87 89 94 +10 10 17 19 22 23 +73 73 74 81 83 80 +73 73 75 80 81 81 +16 16 19 21 22 28 32 +71 71 77 80 85 +25 29 32 34 36 37 39 41 +27 31 32 33 35 36 39 38 +58 62 64 65 66 66 +59 63 66 67 70 72 76 +60 64 66 69 76 +70 74 76 79 76 77 +48 52 51 54 57 54 +64 68 71 72 71 71 +53 57 60 58 60 63 64 68 +59 63 62 65 67 68 74 +41 45 46 47 48 48 51 +69 73 75 77 77 79 77 +50 54 57 58 58 61 61 +29 33 33 36 38 39 42 46 +9 13 14 14 15 16 17 22 +62 66 68 72 75 76 78 80 +3 7 11 12 15 18 21 19 +27 31 33 34 38 41 43 43 +38 42 45 49 53 +26 30 33 37 40 42 48 +23 27 28 35 38 +37 41 42 44 50 49 +71 75 77 83 85 85 +56 60 61 67 70 72 75 79 +11 15 16 21 22 29 +2 8 10 11 13 16 17 20 +51 57 59 60 58 +55 60 63 65 67 69 70 70 +47 54 55 56 57 61 +29 36 37 40 42 44 51 +88 93 94 92 93 +46 52 53 52 55 56 55 +76 81 79 80 82 82 +31 37 38 36 39 40 44 +66 72 74 77 76 77 78 83 +59 64 65 66 66 68 70 72 +28 34 34 36 39 37 +61 68 71 73 73 73 +37 44 46 46 48 52 +49 55 56 58 59 59 61 66 +7 13 16 18 20 22 26 27 +28 33 37 38 41 44 41 +29 35 38 42 43 45 45 +79 85 86 90 91 95 +72 77 78 82 83 90 +42 47 50 56 57 58 60 61 +60 65 67 72 75 73 +15 22 24 27 33 35 36 36 +48 53 55 57 62 66 +36 41 47 48 50 56 +88 87 84 82 85 +28 25 22 19 16 16 +92 91 89 87 83 +59 56 54 53 48 +85 84 83 84 83 82 +71 69 66 68 70 +70 67 64 62 63 63 +97 96 99 96 92 +38 35 33 36 35 32 29 24 +19 18 15 13 12 12 9 +76 73 71 71 69 68 71 +57 55 54 52 49 47 47 47 +88 87 85 85 81 +49 48 47 47 42 +35 34 32 28 25 +20 19 15 13 11 13 +95 92 88 87 87 +86 85 84 83 79 77 76 72 +34 32 29 25 24 19 +82 80 78 71 68 65 +53 50 44 41 40 38 39 +43 41 40 39 33 33 +48 45 43 42 35 31 +45 43 37 35 32 26 +32 33 31 28 27 24 22 19 +29 32 30 28 25 24 21 22 +13 16 15 14 14 +89 90 89 86 85 83 82 78 +32 34 31 30 23 +50 53 50 52 50 48 +4 7 9 7 9 +4 7 4 1 3 1 1 +22 25 26 25 23 19 +13 14 17 15 8 +66 67 64 63 63 62 61 60 +98 99 96 96 93 91 89 91 +8 10 7 7 6 3 3 +45 48 47 47 43 +54 57 57 56 49 +40 41 39 35 32 31 +80 83 82 78 77 75 74 76 +48 51 48 47 44 40 40 +68 70 67 63 60 57 55 51 +85 86 85 82 78 76 70 +77 78 75 74 73 68 66 +67 70 69 66 64 62 57 59 +69 70 67 65 62 55 55 +41 43 37 35 31 +87 90 83 80 79 72 +57 57 56 53 51 +75 75 72 71 68 65 66 +49 49 46 43 42 40 38 38 +69 69 67 65 61 +96 96 95 94 91 86 +9 9 6 8 7 +79 79 80 77 76 79 +62 62 59 61 61 +54 54 56 54 53 51 48 44 +56 56 59 56 50 +84 84 82 81 81 78 76 75 +57 57 54 52 52 49 46 48 +74 74 71 68 66 66 66 +73 73 71 71 67 +92 92 92 91 86 +97 97 93 91 88 +60 60 58 54 56 +14 14 12 11 8 4 3 3 +47 47 44 43 41 39 35 31 +38 38 37 35 33 29 27 22 +68 68 61 60 57 55 +30 30 29 22 19 21 +43 43 42 37 34 33 32 32 +68 68 67 60 56 +41 41 38 36 34 32 26 21 +46 42 41 38 36 35 33 +44 40 37 34 31 28 27 29 +35 31 30 29 26 26 +38 34 33 31 29 28 24 +95 91 90 87 86 81 +91 87 84 87 85 83 +40 36 35 37 39 +39 35 33 34 31 28 26 26 +37 33 34 33 32 28 +29 25 24 22 19 22 20 14 +80 76 73 73 70 68 66 64 +84 80 79 78 75 75 77 +28 24 24 23 23 +19 15 15 13 10 9 5 +81 77 76 76 74 73 72 66 +95 91 90 86 85 +90 86 84 81 77 75 76 +44 40 38 34 34 +38 34 31 29 28 24 20 +97 93 89 87 84 82 76 +55 51 44 43 40 38 +82 78 76 69 72 +53 49 46 41 40 40 +51 47 44 43 38 34 +76 72 69 66 60 58 52 +75 69 66 63 61 58 57 +60 54 53 52 51 49 51 +17 12 11 8 5 2 2 +90 83 81 79 78 77 76 72 +62 57 54 51 45 +52 45 42 43 42 40 +69 62 64 61 64 +70 65 62 61 64 63 63 +46 41 44 42 41 40 39 35 +42 35 34 35 34 29 +81 74 72 69 69 68 65 62 +15 10 7 5 2 2 1 2 +69 63 63 60 59 59 +80 73 72 72 68 +79 73 73 71 68 65 60 +62 56 55 53 52 48 45 +77 71 67 65 66 +60 54 51 48 47 44 40 40 +26 20 19 18 17 13 10 6 +93 87 84 81 77 74 73 67 +38 31 30 23 20 +94 88 86 79 81 +82 77 75 72 67 65 62 62 +47 40 33 31 27 +55 49 47 40 38 31 +75 70 67 66 66 63 61 +41 37 35 34 33 33 33 +22 21 19 16 14 +2 3 4 7 10 12 14 +76 79 80 83 85 87 88 89 +92 90 89 87 84 82 +40 43 44 46 47 50 53 +18 16 14 11 8 6 +63 66 69 70 73 +55 53 50 48 45 44 42 40 +28 25 22 20 19 17 15 +79 78 75 73 72 69 68 66 +70 73 74 75 76 79 +34 31 29 28 25 +40 43 45 47 50 53 +73 70 67 64 61 60 59 +72 71 70 68 66 63 61 58 +53 51 49 46 44 42 +51 53 54 57 59 60 62 +78 80 83 86 88 89 90 93 +74 75 77 80 83 86 89 92 +21 18 15 14 11 9 7 +35 34 31 29 28 27 24 23 +61 63 66 67 69 70 71 72 +12 14 17 19 20 23 26 +35 34 33 30 29 +4 6 7 10 13 15 +33 30 29 27 26 23 +29 32 34 37 39 +83 84 85 88 89 91 92 94 +9 8 6 5 4 +11 10 8 5 2 +34 32 31 30 28 26 +78 79 80 82 85 +27 25 24 21 20 18 +62 63 64 66 69 71 72 75 +46 43 42 41 38 36 +11 13 14 15 16 18 +30 28 27 26 24 22 19 18 +80 78 77 74 71 68 67 66 +26 25 22 21 20 18 +77 76 74 73 70 67 65 +26 29 30 32 33 34 36 37 +25 24 23 22 21 18 17 +14 12 10 8 6 3 1 +74 77 78 79 80 82 +42 41 40 38 37 36 33 +50 47 45 44 43 41 40 38 +86 83 81 80 77 75 +9 6 5 3 2 +50 47 45 43 42 40 38 35 +15 18 20 23 24 25 28 31 +67 66 63 62 60 59 56 +49 47 46 43 42 39 38 +77 75 73 71 69 67 65 63 +3 4 5 8 10 13 16 19 +2 5 7 10 12 13 +74 77 79 80 83 85 +70 68 65 64 61 60 +61 60 59 58 57 55 +89 90 92 94 95 97 99 +51 49 47 45 44 42 41 +74 72 69 68 65 63 61 +3 4 7 9 12 15 +42 45 47 50 52 +90 87 84 81 80 79 +84 82 80 77 75 73 71 +41 42 44 45 46 49 50 52 +93 91 88 85 83 80 77 +47 50 53 55 57 59 +7 9 12 15 18 20 21 22 +72 74 77 80 83 85 87 90 +61 62 65 67 70 71 +50 53 54 55 58 59 61 +17 16 14 11 8 6 3 +62 64 65 67 68 70 72 75 +28 31 32 34 36 37 38 39 +70 73 74 75 76 79 81 83 +50 51 52 53 55 56 58 +21 19 16 13 10 8 5 3 +76 78 80 82 84 +71 69 66 65 62 61 60 +55 52 49 46 44 +43 44 47 49 50 53 56 +63 62 59 57 56 55 +99 97 96 93 92 89 87 84 +22 24 27 28 30 32 33 +33 34 36 38 39 41 +49 52 54 55 58 61 +87 88 89 91 92 94 +36 38 39 42 43 45 46 47 +43 46 47 49 50 53 54 55 +43 42 39 37 35 +87 85 83 82 80 78 77 +84 85 86 88 91 94 +27 24 22 19 17 +74 75 77 78 81 83 +83 80 78 75 72 70 69 +78 80 81 83 86 89 +75 78 81 84 87 +26 28 29 32 34 35 38 39 +42 39 38 37 34 +86 83 80 77 74 71 +90 91 92 93 94 +96 94 92 91 89 +81 80 79 76 75 72 +68 71 72 74 77 79 80 82 +67 70 71 72 73 75 76 +69 66 64 62 60 59 57 54 +48 50 52 54 56 58 60 62 +58 55 52 51 48 46 43 42 +86 89 92 95 98 +79 78 75 72 70 69 66 64 +32 35 38 40 43 46 +75 76 79 80 81 83 85 +65 66 69 71 73 74 77 +81 78 77 76 74 71 69 +41 38 35 32 31 29 27 +32 30 27 24 23 20 18 15 +52 50 49 46 43 42 +44 43 40 37 35 33 31 29 +36 34 31 30 28 27 +60 57 56 55 54 51 49 46 +66 67 68 71 73 +22 24 27 30 33 34 +18 20 23 26 27 +99 98 95 94 92 91 88 85 +49 50 53 56 59 62 63 +83 84 87 90 93 95 96 97 +12 15 16 18 19 20 23 25 +3 4 6 9 11 +28 25 24 21 18 17 14 11 +81 79 77 74 72 +42 39 36 34 31 30 28 26 +59 56 53 52 50 48 46 45 +62 64 67 69 72 74 +35 36 39 41 43 44 47 +40 42 43 45 48 50 52 55 +25 22 21 18 15 14 13 +99 96 95 93 90 87 +92 91 88 86 84 +20 18 16 13 12 9 7 +60 62 63 64 67 +78 79 82 83 85 86 88 +38 36 33 31 30 27 +79 82 83 86 89 92 95 +84 81 80 78 76 74 71 70 +44 43 40 38 35 33 31 +68 71 74 77 80 +17 15 13 10 7 +18 17 15 14 12 9 6 +12 11 10 9 6 3 1 +92 90 89 86 85 82 +68 67 64 62 59 57 +74 72 71 69 67 +84 81 80 79 76 74 72 +68 67 65 62 61 60 58 +25 27 28 31 32 34 36 +45 48 49 52 53 56 58 61 +36 34 31 29 26 23 +43 44 47 49 52 55 57 59 +66 67 68 71 74 77 +21 20 18 16 14 +56 59 61 62 65 67 +25 26 27 30 31 34 37 +17 18 20 21 22 24 +70 69 68 65 64 63 61 +60 61 63 64 67 70 71 +77 76 75 72 70 69 +65 62 60 58 57 +58 56 55 53 50 +65 66 69 72 75 76 +46 47 49 51 52 54 55 57 +60 62 64 65 67 +7 8 9 12 15 +58 55 53 52 51 48 45 44 +25 22 19 17 16 +92 94 95 98 99 +53 56 59 61 62 64 +34 37 40 43 46 48 49 50 +31 29 28 27 25 23 21 20 +41 42 43 46 47 +77 78 79 80 82 84 +54 56 58 60 62 +15 16 18 19 22 +16 19 21 23 25 +14 12 11 10 7 6 5 +26 28 29 31 34 35 +36 33 32 30 29 26 25 24 +12 14 17 20 22 25 28 +37 36 35 33 30 29 +70 67 66 65 64 62 61 58 +71 72 75 76 79 82 83 84 +66 64 61 58 57 +64 66 68 69 71 72 +33 36 37 38 40 42 44 45 +19 18 16 13 10 +52 53 56 58 59 62 63 64 +19 16 13 11 9 6 +46 49 52 53 54 57 58 +66 67 70 71 72 74 76 79 +72 75 77 78 80 83 +62 61 59 57 55 +63 64 65 68 71 +25 28 29 32 34 36 +59 60 61 64 66 69 +52 50 47 45 43 42 +34 36 39 41 43 45 47 49 +30 28 26 24 21 +92 90 87 84 82 81 +74 71 68 67 64 63 62 61 +15 14 13 11 9 7 +89 87 85 82 80 77 +85 86 87 90 92 93 94 95 +52 55 58 61 64 65 68 +84 81 79 77 74 72 +25 23 21 18 16 14 11 8 +47 46 43 41 40 38 +85 84 81 78 77 75 +1 2 4 6 7 9 10 11 +21 20 18 17 15 12 11 +69 71 73 75 76 77 78 80 +70 73 76 78 79 82 84 87 +96 94 91 88 87 85 84 +79 80 83 85 87 89 +72 70 68 65 62 60 59 57 +95 94 92 90 87 85 83 80 +80 79 76 75 73 71 69 66 +74 71 68 65 62 59 58 +32 29 26 23 20 17 16 15 +87 89 91 92 95 98 99 +45 46 49 51 54 56 57 +24 21 18 15 12 11 8 5 +93 91 90 89 87 84 81 79 +58 59 62 64 66 68 +22 23 26 27 28 29 30 +9 12 13 15 18 +94 91 89 87 85 84 83 +40 42 45 46 49 52 53 56 +40 41 43 44 45 47 50 52 +42 40 37 34 31 29 27 26 +28 26 23 20 17 14 +54 51 50 47 44 42 40 +12 10 8 7 5 +19 16 13 12 11 8 6 3 +82 80 77 75 73 72 +79 82 84 86 89 90 93 +98 97 96 93 92 90 +25 24 22 21 18 +67 65 62 60 58 57 55 54 +28 25 24 21 18 +83 82 79 77 76 +91 89 87 84 81 79 78 +36 35 32 29 27 24 +60 63 64 66 69 71 73 75 +45 44 42 40 39 +20 21 24 25 26 27 30 +43 44 46 48 51 54 +92 89 86 84 82 79 +15 12 11 10 8 7 +39 40 43 45 48 50 53 +13 11 9 7 4 2 +47 46 44 41 40 +27 24 22 21 19 17 +49 48 47 46 45 44 41 +50 47 46 45 44 +53 54 56 59 61 63 65 +85 82 79 76 75 73 70 69 +62 63 66 69 70 72 74 77 +47 44 43 42 39 38 36 +17 20 21 23 25 +76 79 80 83 86 +42 45 47 48 51 53 54 +46 45 42 40 39 38 37 +24 23 22 20 18 16 13 11 +31 29 27 26 24 21 19 +12 14 15 16 18 21 +57 56 55 54 51 49 47 46 +70 69 68 67 65 64 63 61 +51 50 47 44 42 40 39 38 +77 75 74 73 72 70 +65 64 62 60 58 57 56 +48 49 51 54 56 59 +70 71 72 75 76 77 +60 59 56 54 53 51 +57 56 54 53 51 50 +47 49 52 53 55 +49 47 46 44 42 39 37 34 +90 88 86 85 83 82 79 +63 64 67 69 72 75 76 78 +98 96 95 92 90 88 85 +62 65 68 70 71 +81 78 76 73 72 70 69 67 +69 67 64 62 61 58 56 +74 72 69 67 65 +42 43 44 45 48 49 52 54 +52 49 48 46 44 42 39 36 +32 31 30 28 27 25 23 +37 36 33 32 30 +82 79 77 75 73 71 70 +33 35 38 41 44 +85 83 80 77 75 74 73 +20 17 14 13 12 10 8 7 +2 3 6 8 11 12 +17 15 13 10 9 +44 47 48 51 53 55 +15 16 18 20 21 24 27 +31 32 35 37 39 +58 60 62 63 64 66 67 +80 79 78 77 76 74 +46 49 51 52 53 55 57 59 +57 60 61 62 64 66 67 70 +70 67 64 61 60 +48 45 44 43 40 +51 49 47 44 42 40 38 +37 36 34 31 28 +78 80 83 85 87 88 90 93 +28 27 25 22 20 19 16 +21 23 25 28 29 +14 17 18 21 22 25 +51 53 55 56 58 +64 67 69 72 74 77 +4 7 8 11 12 15 +84 83 82 79 76 73 72 70 +87 86 85 84 82 +74 72 69 68 65 64 61 58 +51 48 47 44 43 42 41 38 +54 51 50 48 47 45 44 42 +11 12 15 16 18 20 +79 81 84 85 86 +29 30 32 35 37 +67 66 65 64 62 59 57 55 +18 19 20 22 23 26 29 32 +80 78 76 75 74 72 71 68 +6 7 8 11 13 16 +53 56 57 59 61 +6 9 11 13 15 18 +90 87 85 83 82 80 +77 75 73 71 70 +43 45 48 49 50 52 53 +74 75 76 79 81 83 +56 58 60 62 65 66 68 +65 67 70 73 75 77 78 +40 38 35 34 33 30 29 26 +67 70 73 75 78 +42 45 48 49 50 +55 52 51 50 49 46 45 +20 18 15 13 10 9 8 5 +37 40 43 46 48 +18 16 15 13 12 9 7 +73 70 69 68 67 65 64 +4 5 7 8 9 12 13 +28 30 31 32 33 +47 48 50 52 55 +10 12 14 16 18 +10 9 6 5 4 1 +54 52 49 46 43 41 38 +20 18 17 15 13 12 +25 26 27 30 31 34 +70 69 68 67 65 62 61 +39 42 44 46 47 48 51 54 +30 31 32 34 37 +74 71 70 68 67 65 62 61 +71 74 75 78 81 82 +42 45 46 48 50 52 +45 48 50 52 55 57 60 +76 73 72 70 67 65 +17 15 12 11 10 +8 10 12 13 16 17 +33 36 39 42 43 +35 37 39 40 42 45 46 49 +66 63 61 59 56 +21 20 19 17 14 12 +77 74 71 69 68 67 +67 69 70 71 73 +15 18 20 22 25 26 29 32 +40 43 46 49 52 54 56 57 +69 68 67 66 63 60 +80 82 85 87 89 +51 50 49 47 45 42 41 +95 93 92 89 88 +10 11 14 16 19 20 21 +89 86 83 80 77 74 72 +47 44 42 41 40 39 37 36 +47 49 52 55 58 +22 23 25 28 29 +78 80 83 84 87 89 92 +29 28 25 24 23 21 +13 14 17 18 20 23 +42 45 46 48 49 50 51 54 +62 59 57 56 54 51 50 48 +64 66 68 70 71 73 75 77 +29 26 25 22 21 +29 27 25 24 23 22 21 +4 5 7 9 10 13 +92 89 86 85 82 81 78 +66 63 62 60 59 57 55 52 +56 54 52 51 49 +84 87 90 91 93 +21 18 16 14 13 11 10 8 +64 62 61 60 59 57 55 53 +84 86 88 90 92 93 95 +69 68 67 64 61 59 56 53 +22 23 26 29 30 +80 77 76 74 71 +65 67 69 70 72 75 76 79 +55 57 60 61 62 65 68 69 +10 9 7 6 3 +2 4 6 7 9 12 13 15 +2 3 4 5 8 10 12 +86 85 84 83 82 81 +91 89 87 86 83 80 79 78 +19 22 24 26 28 31 32 +50 52 54 56 57 59 +94 93 92 90 88 86 +40 37 35 32 31 29 28 26 +44 47 48 50 53 56 57 59 +2 3 4 7 10 12 15 16 +73 70 67 64 61 60 58 +66 64 61 60 59 +30 33 36 39 40 42 44 45 +58 61 62 64 66 68 70 +42 45 48 50 52 +85 84 83 80 79 76 73 +49 46 45 44 41 38 +77 78 80 82 84 87 88 91 +16 14 11 9 8 5 +37 35 32 31 29 28 27 +53 54 56 59 61 63 +99 98 97 94 92 91 89 86 +94 92 91 88 87 86 +73 75 76 79 80 82 85 87 +53 51 50 49 47 44 41 39 +83 80 77 74 71 68 66 63 +30 33 36 37 40 +74 73 72 71 68 67 64 +71 69 68 67 66 64 +73 72 69 66 65 63 60 59 +48 47 44 41 38 35 +42 45 48 50 52 53 56 +13 11 9 7 5 3 1 +51 50 47 45 43 +11 13 16 18 19 22 +13 12 9 8 7 5 +57 56 55 52 50 48 47 44 +75 78 81 82 84 85 87 +49 51 54 56 57 60 62 64 +23 21 18 16 15 +72 70 68 65 62 +37 35 34 32 29 27 25 23 +78 80 83 85 87 88 +23 24 27 30 31 32 +13 16 18 20 22 24 +63 61 60 58 56 55 52 +54 51 49 48 47 44 41 40 +10 13 15 18 20 23 26 +74 72 69 66 64 62 +61 60 58 56 53 50 49 +35 34 31 29 26 25 +18 16 13 10 7 5 2 +65 68 71 74 76 79 +63 64 67 70 71 72 +58 60 61 63 65 66 69 71 +58 59 62 65 68 +83 84 87 90 92 93 94 +73 71 68 65 63 60 57 55 +82 85 87 89 92 95 96 +40 43 44 46 49 50 +75 78 79 82 84 86 87 +24 26 27 29 32 33 34 35 +80 79 78 75 72 69 67 66 +7 10 12 14 17 18 21 +72 69 67 65 63 61 60 +76 75 73 72 69 68 67 +62 59 56 54 53 50 47 45 +12 14 17 19 21 +51 50 49 47 45 +50 49 47 45 43 +66 67 70 71 74 75 +73 74 75 76 77 79 82 85 +33 34 36 37 38 40 +21 22 23 26 28 +36 34 32 30 27 24 22 +81 83 85 86 89 90 92 93 +50 51 54 57 60 61 64 66 +12 10 8 6 3 +79 76 75 73 72 +84 86 89 92 93 +66 68 70 73 74 77 78 81 +47 49 52 54 57 58 +52 55 57 60 62 +77 74 72 70 67 64 61 +67 68 69 71 73 75 77 +96 95 94 91 89 87 85 82 +33 30 29 27 24 23 20 18 +57 55 52 50 47 +41 44 45 47 50 51 53 +15 18 21 24 26 28 +23 22 20 17 16 13 10 7 +75 73 72 70 67 66 65 64 +47 48 49 50 53 55 +88 91 92 94 97 98 +43 46 47 50 52 54 56 58 +44 45 48 51 52 54 \ No newline at end of file diff --git a/2024/d02/main.ts b/2024/d02/main.ts new file mode 100644 index 0000000..cfe036f --- /dev/null +++ b/2024/d02/main.ts @@ -0,0 +1,48 @@ +import { readFileSync } from 'node:fs' + +import { testRuns, solutionRuns } from '../util/aoc' +import { nextItemDiffReducer } from '../util/array' + +const isLineSafe = (line: ReadonlyArray): boolean => { + const sign = Math.sign(line[0]) + + return line.every(item => [1, 2, 3].includes(Math.abs(item))) && line.every(item => Math.sign(item) === sign) +} + +const part1 = (data: ReadonlyArray>): number => { + return data.map(nextItemDiffReducer).filter(isLineSafe).length +} + +const part2 = (data: ReadonlyArray>): number => { + return data + .map(line => { + const lines: Array> = [line] + + for (let i = 0; i < line.length; i++) { + lines.push([...line.slice(0, i), ...line.slice(i + 1, line.length)]) + } + + return lines + }) + .map(lines => { + return lines.map(nextItemDiffReducer) + }) + .filter(lines => { + return lines.some(isLineSafe) + }).length +} + +const reader = (file: string): ReadonlyArray> => { + return readFileSync(file, 'utf-8') + .trim() + .split('\n') + .map(line => { + return line + .trim() + .split(' ') + .map(item => parseInt(item, 10)) + }) +} + +testRuns('02', reader, part1, part2) +solutionRuns('02', reader, part1, part2) diff --git a/2024/d02/test-runs/0.txt b/2024/d02/test-runs/0.txt new file mode 100644 index 0000000..82cd679 --- /dev/null +++ b/2024/d02/test-runs/0.txt @@ -0,0 +1,6 @@ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 \ No newline at end of file diff --git a/2024/tsconfig.json b/2024/tsconfig.json index 640ea06..3c43903 100644 --- a/2024/tsconfig.json +++ b/2024/tsconfig.json @@ -1,4 +1,3 @@ { "extends": "../tsconfig.json" } - diff --git a/2024/util/aoc.ts b/2024/util/aoc.ts index d20985f..69da02e 100644 --- a/2024/util/aoc.ts +++ b/2024/util/aoc.ts @@ -3,71 +3,71 @@ import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import chalk from 'chalk' -const __dirname = dirname(fileURLToPath(import.meta.url)); +const __dirname = dirname(fileURLToPath(import.meta.url)) const testRun1 = (result: TResult, testFileName: string, durationInMs: number): void => { - console.log(chalk.yellow(`[TST] (${testFileName})\t\tPart 1: ${result}\t\t${durationInMs}ms`)); + console.log(chalk.yellow(`[TST] (${testFileName})\t\tPart 1: ${result}\t\t${durationInMs}ms`)) } const testRun2 = (result: TResult, testFileName: string, durationInMs: number): void => { - console.log(chalk.red(`[TST] (${testFileName})\t\tPart 2: ${result}\t\t${durationInMs}ms`)); + console.log(chalk.red(`[TST] (${testFileName})\t\tPart 2: ${result}\t\t${durationInMs}ms`)) } const solutionPart1 = (result: TResult, durationInMs: number): void => { - console.log(chalk.green(`[SLN]\t\t\tPart 1: ${result}\t\t${durationInMs}ms`)); + console.log(chalk.green(`[SLN]\t\t\tPart 1: ${result}\t\t${durationInMs}ms`)) } const solutionPart2 = (result: TResult, durationInMs: number): void => { - console.log(chalk.blue(`[SLN]\t\t\tPart 2: ${result}\t\t${durationInMs}ms`)); + console.log(chalk.blue(`[SLN]\t\t\tPart 2: ${result}\t\t${durationInMs}ms`)) } const testRuns = ( - day: string, - reader: (file: string) => TInput, - part1: (data: TInput) => TResult, + day: string, + reader: (file: string) => TInput, + part1: (data: TInput) => TResult, part2: (data: TInput) => TResult, ): void => { - const dirPath = join(__dirname, `../d${day}/test-runs`); + const dirPath = join(__dirname, `../d${day}/test-runs`) if (existsSync(dirPath)) { - const testFiles = readdirSync(dirPath); + const testFiles = readdirSync(dirPath) - testFiles.forEach(file => { - const testData = reader(join(dirPath, file)); + testFiles.forEach(file => { + const testData = reader(join(dirPath, file)) - const start1 = Date.now(); - const valuePart1 = part1(testData); - const duration1 = Date.now() - start1; + const start1 = Date.now() + const valuePart1 = part1(testData) + const duration1 = Date.now() - start1 - const start2 = Date.now(); - const valuePart2 = part2(testData); - const duration2 = Date.now() - start2; + const start2 = Date.now() + const valuePart2 = part2(testData) + const duration2 = Date.now() - start2 - testRun1(valuePart1, file, duration1); - testRun2(valuePart2, file, duration2); - }); + testRun1(valuePart1, file, duration1) + testRun2(valuePart2, file, duration2) + }) } } const solutionRuns = ( - day: string, - reader: (file: string) => TInput, - part1: (data: TInput) => TResult, + day: string, + reader: (file: string) => TInput, + part1: (data: TInput) => TResult, part2: (data: TInput) => TResult, ): void => { - const filePath = join(__dirname, `../d${day}/input.txt`); - const data = reader(filePath); + const filePath = join(__dirname, `../d${day}/input.txt`) + const data = reader(filePath) - const start1 = Date.now(); - const valuePart1 = part1(data); - const duration1 = Date.now() - start1; + const start1 = Date.now() + const valuePart1 = part1(data) + const duration1 = Date.now() - start1 - const start2 = Date.now(); - const valuePart2 = part2(data); - const duration2 = Date.now() - start2; + const start2 = Date.now() + const valuePart2 = part2(data) + const duration2 = Date.now() - start2 - solutionPart1(valuePart1, duration1); - solutionPart2(valuePart2, duration2); + solutionPart1(valuePart1, duration1) + solutionPart2(valuePart2, duration2) } -export { testRuns, solutionRuns } \ No newline at end of file +export { testRuns, solutionRuns } diff --git a/2024/util/array.ts b/2024/util/array.ts new file mode 100644 index 0000000..1c6905e --- /dev/null +++ b/2024/util/array.ts @@ -0,0 +1,17 @@ +/** + * Use with reduce to get difference between current and next item + * + * e.g: [7, 6, 4, 2, 1].map(nextItemDiffReducer) + * returns [1, 2, 2, 1] + */ +const nextItemDiffReducer = (line: ReadonlyArray): ReadonlyArray => { + return line.reduce((acc, item, index, arr) => { + if (index >= arr.length - 1) { + return acc + } + + return [...acc, item - arr[index + 1]] + }, []) +} + +export { nextItemDiffReducer } diff --git a/README.md b/README.md index cff9bbe..16bc5d9 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Some editions were finished later, or even done in a different time (for example | [2021](2021) | [Elixir][lang-elixir] | 06/50 |Why have i chosen Elixir... | | [2022](2022) | [TypeScript][lang-ts] | 38/50 |With some help from [Radash][lib-radash] and [Ramda][lib-ramda] | | [2023](2023) | [TypeScript][lang-ts] | 20/50 |With some help from [Radash][lib-radash] and [Ramda][lib-ramda] | -| [2024](2024) | [TypeScript][lang-ts] | 02/50 |With some help from [Radash][lib-radash] and [Ramda][lib-ramda] | +| [2024](2024) | [TypeScript][lang-ts] | 04/50 |With some help from [Radash][lib-radash] and [Ramda][lib-ramda] | [lang-c#]: https://learn.microsoft.com/pl-pl/dotnet/csharp/ [lang-elixir]: https://elixir-lang.org/ diff --git a/package.json b/package.json index ab83bf1..dc80165 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,10 @@ "scripts": { "aoc": "tsx", "dotnet": "dotnet run --project", - "format": "prettier 2022 2023 --write", + "format": "prettier 2022 2023 2024 --write", + "format:2022": "prettier 2022 --write", + "format:2023": "prettier 2023 --write", + "format:2024": "prettier 2024 --write", "postinstall": "husky" }, "devDependencies": {