From b475fb3726fb78ede0a4f9d0563b3d06f522b33c Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Mon, 2 Dec 2024 21:53:44 +0900 Subject: [PATCH 1/5] chore: add puzzle input and description --- .../src/main/kotlin/com/eunicehong/Day02.kt | 35 +- .../src/test/resources/input/puzzle_02.txt | 1000 +++++++++++++++++ 2 files changed, 1034 insertions(+), 1 deletion(-) diff --git a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt index b95ab46..4174bb1 100644 --- a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt +++ b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt @@ -1,7 +1,7 @@ package com.eunicehong /** - * # 2024 Day 2: ?? + * # 2024 Day 2: Red-Nosed Reports * * Copyright (c) Eric Wastl * @@ -12,6 +12,39 @@ internal class Day02 { /** * ## Part 1 * + * Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office. + * + * While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron. + * + * They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data. + * + * The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example: + * + * ``` + * 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 + * ``` + * This example data contains six reports each containing five levels. + * + * The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true: + * + * The levels are either all increasing or all decreasing. + * Any two adjacent levels differ by at least one and at most three. + * In the example above, the reports can be found safe or unsafe by checking those rules: + * + * `7 6 4 2 1`: Safe because the levels are all decreasing by 1 or 2. + * `1 2 7 8 9`: Unsafe because 2 7 is an increase of 5. + * `9 7 6 2 1`: Unsafe because 6 2 is a decrease of 4. + * `1 3 2 4 5`: Unsafe because 1 3 is increasing but 3 2 is decreasing. + * `8 6 4 4 1`: Unsafe because 4 4 is neither an increase or a decrease. + * `1 3 6 7 9`: Safe because the levels are all increasing by 1, 2, or 3. + * So, in this example, 2 reports are safe. + * + * Analyze the unusual data from the engineers. How many reports are safe? */ fun solution1(puzzle: String): String { val lines = puzzle.lines().filter { it.isNotEmpty() } diff --git a/eunice-hong/src/test/resources/input/puzzle_02.txt b/eunice-hong/src/test/resources/input/puzzle_02.txt index e69de29..e62f8c1 100644 --- a/eunice-hong/src/test/resources/input/puzzle_02.txt +++ b/eunice-hong/src/test/resources/input/puzzle_02.txt @@ -0,0 +1,1000 @@ +69 72 75 78 80 79 +40 42 43 46 47 48 49 49 +57 58 59 60 64 +29 32 35 37 43 +16 17 18 19 20 17 19 20 +42 45 43 45 48 49 46 +58 59 62 65 63 66 66 +28 31 34 31 35 +37 39 41 38 40 43 45 50 +64 66 68 68 71 +50 51 53 53 55 53 +44 45 48 48 51 54 55 55 +20 22 22 23 25 29 +38 40 41 41 43 46 49 54 +24 27 28 30 33 37 38 40 +82 83 86 88 92 89 +68 69 71 74 76 80 80 +3 6 9 13 16 20 +62 65 67 71 74 81 +68 69 72 78 81 +74 77 79 85 82 +61 64 67 69 70 77 80 80 +46 48 53 56 58 61 65 +65 67 70 77 82 +61 60 62 64 65 66 69 +13 11 14 15 18 15 +57 56 59 62 65 66 69 69 +45 44 46 47 48 49 52 56 +38 36 39 42 45 52 +69 67 69 66 69 +89 86 84 87 84 +10 8 7 9 11 12 12 +97 94 91 92 94 98 +2 1 4 3 5 6 9 16 +63 62 65 68 68 71 73 +54 51 51 54 55 52 +21 20 21 21 23 26 26 +33 32 35 35 39 +70 69 69 72 73 74 76 82 +4 1 4 7 8 12 15 +8 5 8 9 13 14 16 15 +16 15 18 20 24 25 27 27 +54 53 56 60 62 66 +62 60 64 65 70 +26 23 28 30 31 +53 51 53 54 57 62 64 61 +9 6 13 16 18 19 20 20 +75 72 78 80 82 85 87 91 +8 7 10 11 16 18 25 +84 84 86 89 92 95 96 +36 36 39 41 42 41 +21 21 22 24 25 26 26 +72 72 74 77 81 +3 3 6 8 15 +9 9 12 11 14 +75 75 76 75 77 75 +7 7 8 10 12 10 10 +52 52 51 54 58 +76 76 77 80 79 84 +41 41 42 42 44 45 +85 85 87 87 90 89 +4 4 7 9 9 9 +23 23 24 27 30 31 31 35 +48 48 51 51 56 +1 1 3 7 10 13 16 19 +19 19 21 25 26 28 29 26 +73 73 75 76 80 80 +40 40 44 47 51 +74 74 78 81 84 87 93 +9 9 11 12 15 22 25 +1 1 3 10 11 14 16 15 +50 50 52 58 60 60 +59 59 60 65 69 +34 34 40 42 47 +30 34 36 39 42 +2 6 7 9 12 13 10 +15 19 21 23 24 24 +30 34 35 38 41 44 46 50 +47 51 52 55 60 +90 94 92 94 97 +68 72 70 72 75 77 80 79 +53 57 59 60 57 59 60 60 +88 92 95 96 93 97 +26 30 31 29 36 +82 86 87 88 88 90 92 94 +71 75 76 79 79 80 82 79 +42 46 46 49 50 51 54 54 +14 18 18 19 20 24 +75 79 80 80 81 87 +61 65 69 71 74 76 +69 73 75 78 80 84 85 83 +34 38 42 43 46 47 47 +35 39 41 43 47 51 +2 6 7 10 14 20 +17 21 24 30 31 34 36 38 +12 16 21 23 24 23 +87 91 93 99 99 +22 26 32 35 36 37 39 43 +46 50 57 59 62 65 67 73 +46 52 54 55 56 +27 33 35 38 40 43 46 45 +17 24 25 27 27 +19 24 25 28 30 32 36 +36 42 45 47 54 +35 42 43 44 42 45 47 +50 56 58 55 52 +32 37 38 35 38 38 +49 54 57 54 56 58 60 64 +53 58 57 58 61 64 71 +74 80 80 83 86 87 90 92 +36 42 43 45 45 48 49 48 +82 87 87 89 89 +31 38 41 41 43 45 46 50 +40 47 49 50 50 51 58 +59 64 68 69 71 73 74 76 +59 66 69 73 71 +75 81 84 88 90 92 92 +13 19 21 24 27 31 32 36 +23 30 32 35 39 45 +16 22 25 27 29 31 36 38 +3 10 12 13 19 21 23 22 +4 10 11 14 16 22 22 +4 11 14 16 23 25 29 +3 8 9 10 16 21 +64 63 60 57 55 57 +81 80 77 75 74 74 +34 33 31 30 28 24 +23 21 20 19 16 13 10 4 +56 54 51 52 49 +44 43 41 38 35 37 35 37 +82 81 84 81 79 79 +77 75 74 77 74 70 +12 11 13 10 7 2 +32 31 28 28 25 23 20 +6 3 2 2 1 3 +77 76 73 71 68 68 68 +25 22 19 17 15 15 13 9 +87 85 84 84 83 78 +42 39 37 33 32 30 +88 87 86 83 81 77 75 76 +15 12 8 6 4 4 +96 94 92 88 86 84 80 +84 81 78 76 72 69 67 62 +51 50 49 46 44 38 37 +54 53 52 49 43 42 41 43 +99 97 92 90 89 86 86 +97 95 88 85 81 +35 32 30 25 22 19 14 +73 76 75 74 73 71 68 65 +79 81 80 79 78 77 76 78 +5 8 6 4 1 1 +84 87 86 83 82 81 77 +21 23 20 18 16 15 8 +13 14 16 15 14 12 11 9 +29 30 29 31 34 +58 60 63 62 59 58 57 57 +30 32 33 32 28 +86 87 85 88 85 84 77 +36 37 37 36 35 32 31 30 +4 6 6 3 6 +27 29 29 26 26 +65 67 66 66 64 60 +90 91 89 89 83 +74 76 72 71 68 66 +74 76 74 73 69 68 70 +23 25 24 22 18 18 +59 61 57 55 53 51 47 +87 88 87 83 82 81 80 74 +55 58 57 54 51 50 44 42 +92 95 92 91 90 87 80 83 +69 70 64 63 61 58 58 +34 37 36 33 32 26 24 20 +36 37 35 34 31 24 21 16 +27 27 26 25 24 +41 41 38 35 34 35 +31 31 28 27 27 +37 37 36 34 31 28 24 +49 49 47 46 43 41 38 33 +63 63 62 59 62 59 58 +9 9 11 10 13 +23 23 21 19 22 21 21 +37 37 40 39 35 +13 13 10 11 5 +78 78 78 75 72 71 70 +73 73 73 70 71 +10 10 8 5 5 4 4 +22 22 21 21 20 16 +96 96 96 95 89 +41 41 40 36 33 32 29 28 +57 57 53 51 54 +58 58 57 56 55 51 51 +46 46 42 39 35 +21 21 18 14 12 11 5 +40 40 38 36 29 27 25 +97 97 94 89 91 +36 36 34 29 27 25 22 22 +87 87 84 79 75 +35 35 32 26 24 21 16 +52 48 45 42 40 37 +49 45 42 40 42 +95 91 88 86 86 +88 84 82 81 80 78 75 71 +83 79 78 76 70 +17 13 11 10 9 10 7 +45 41 43 41 42 +97 93 90 89 92 89 89 +17 13 15 14 12 8 +36 32 29 27 29 27 22 +50 46 45 44 41 41 40 39 +93 89 87 84 84 87 +23 19 17 16 16 16 +29 25 24 22 22 20 18 14 +14 10 9 9 8 3 +20 16 13 9 6 +22 18 16 13 9 11 +73 69 66 62 62 +16 12 8 5 1 +58 54 50 47 45 40 +46 42 40 33 30 +65 61 54 51 54 +25 21 20 19 17 15 9 9 +38 34 31 25 24 20 +71 67 66 64 58 56 51 +78 73 70 69 68 65 +32 26 24 22 21 19 22 +92 86 84 83 81 78 78 +67 62 59 56 53 49 +64 58 56 55 54 52 51 46 +82 76 74 72 74 71 69 66 +37 32 30 32 29 32 +65 58 57 59 56 55 52 52 +94 87 84 87 83 +54 47 49 48 45 39 +74 68 65 62 62 60 59 +53 47 44 43 43 46 +96 89 86 86 85 85 +62 55 53 52 52 51 47 +34 28 28 25 20 +25 20 16 13 11 9 +58 52 49 45 42 39 40 +56 49 45 43 40 37 37 +32 26 22 20 16 +94 88 84 82 75 +65 59 58 52 50 49 47 +68 61 55 53 55 +53 47 45 40 40 +73 66 61 59 57 53 +66 60 57 56 51 46 +32 34 37 38 40 43 44 42 +9 11 14 17 19 20 20 +18 19 21 24 26 27 30 34 +68 71 73 75 77 79 80 85 +75 76 79 81 80 81 84 +40 43 44 41 40 +17 20 23 25 22 23 23 +89 91 94 93 97 +20 23 26 25 28 30 37 +28 30 30 31 32 33 +23 25 25 26 25 +70 73 75 76 78 78 81 81 +18 19 19 21 22 23 27 +78 80 82 83 86 86 91 +7 10 11 12 14 18 19 +92 93 97 98 95 +33 36 40 41 41 +77 78 79 83 87 +49 50 54 57 60 63 65 72 +75 78 79 80 83 90 92 +82 84 85 92 94 91 +47 50 53 55 61 63 63 +47 48 49 50 56 59 63 +23 24 26 28 31 36 41 +28 26 27 28 31 +5 2 5 6 3 +82 81 83 85 87 88 91 91 +30 28 29 31 32 36 +39 37 38 40 42 45 51 +16 14 13 16 19 +40 37 38 40 39 41 39 +62 59 62 64 62 62 +72 70 71 68 71 74 76 80 +27 26 25 28 29 31 37 +30 28 31 34 34 35 +50 47 47 48 50 52 51 +41 40 41 44 44 46 47 47 +48 47 48 51 52 52 56 +6 3 5 7 10 10 15 +3 2 4 5 6 8 12 15 +88 85 86 87 91 94 91 +59 56 59 63 65 65 +44 43 45 49 53 +47 46 49 53 55 56 62 +32 30 36 38 39 41 +27 24 27 30 31 38 39 38 +64 61 68 70 70 +54 52 55 60 61 63 67 +23 20 27 30 37 +21 21 23 26 27 28 30 33 +22 22 23 24 23 +36 36 39 42 44 44 +48 48 51 53 57 +19 19 21 24 27 29 30 35 +35 35 33 34 35 38 39 +74 74 73 75 72 +75 75 77 74 75 76 76 +24 24 27 28 25 28 30 34 +67 67 65 67 70 72 79 +44 44 45 46 49 49 51 52 +69 69 72 72 69 +28 28 28 31 34 36 36 +53 53 53 55 56 60 +51 51 51 52 54 59 +82 82 84 88 89 +66 66 67 70 71 75 73 +82 82 85 89 91 91 +32 32 35 37 40 44 48 +27 27 30 33 37 42 +77 77 80 86 89 92 95 +65 65 68 70 77 79 77 +86 86 87 89 91 97 99 99 +31 31 36 39 41 45 +6 6 9 14 17 18 25 +27 31 32 34 36 +55 59 62 65 67 70 71 69 +4 8 11 14 15 16 16 +3 7 8 11 13 17 +73 77 80 82 85 87 89 96 +85 89 91 94 93 94 96 97 +8 12 13 12 15 18 17 +51 55 52 53 53 +21 25 28 30 29 31 34 38 +48 52 55 52 57 +1 5 6 6 8 9 +37 41 44 45 47 47 45 +44 48 50 50 50 +72 76 76 77 78 79 83 +54 58 59 60 63 64 64 70 +18 22 24 27 31 32 +33 37 40 44 45 43 +13 17 21 22 24 26 26 +24 28 31 33 37 39 42 46 +19 23 24 28 30 35 +39 43 45 52 53 +57 61 62 68 70 68 +40 44 51 54 54 +60 64 69 70 71 75 +40 44 46 53 56 59 66 +25 32 35 37 40 43 +63 68 70 72 73 74 73 +34 41 42 43 43 +1 6 7 10 11 15 +23 28 31 34 39 +91 96 95 96 98 +81 88 85 88 86 +54 60 62 59 60 60 +30 37 36 39 43 +44 51 53 51 57 +25 30 31 33 33 36 37 39 +85 92 94 94 93 +2 8 9 11 11 11 +41 46 49 49 51 54 58 +58 63 65 67 67 68 69 74 +45 50 52 56 57 60 63 +29 34 38 40 37 +67 74 76 80 80 +57 64 68 70 71 75 +6 13 16 19 23 26 29 36 +26 31 32 37 39 41 44 47 +67 72 77 80 78 +76 81 86 88 90 90 +65 72 75 78 80 85 86 90 +32 38 43 44 45 47 52 +61 60 58 57 56 54 51 53 +92 90 89 86 86 +70 68 67 65 61 +87 84 81 80 79 78 75 68 +41 38 37 40 38 35 34 +99 96 95 96 98 +53 51 50 52 50 50 +17 15 14 13 14 10 +16 15 17 14 13 6 +31 30 30 27 24 23 +43 42 39 39 38 41 +58 55 52 49 47 46 46 46 +61 59 58 58 55 51 +58 55 55 54 52 49 46 39 +43 42 38 37 35 +96 93 89 88 87 90 +80 79 76 75 74 70 70 +53 50 49 47 46 42 38 +38 35 31 29 28 22 +87 84 81 79 74 73 71 +23 22 20 19 12 11 8 10 +20 17 16 9 9 +20 18 16 10 7 3 +21 19 17 11 8 7 2 +64 65 62 61 60 59 57 +17 18 17 14 12 15 +61 64 63 60 59 57 57 +88 91 88 86 83 79 +97 99 97 94 88 +69 70 72 69 66 +23 24 23 24 25 +43 46 44 42 44 43 41 41 +19 20 19 21 19 15 +62 63 64 63 61 59 57 51 +90 91 89 86 86 83 81 78 +73 74 72 71 71 68 66 69 +37 40 38 36 36 33 31 31 +35 38 38 35 31 +76 77 77 74 68 +76 78 75 73 69 66 +67 68 64 61 58 55 53 55 +53 55 51 49 46 44 44 +47 48 44 43 39 +89 92 89 86 82 76 +26 29 27 21 20 +78 80 74 73 70 69 70 +75 78 72 70 70 +16 18 15 9 5 +73 76 70 67 65 63 58 +37 37 35 32 29 26 23 +41 41 39 36 33 32 30 33 +75 75 72 70 67 66 65 65 +26 26 24 23 22 21 17 +98 98 97 94 93 91 88 82 +14 14 15 13 10 8 5 3 +33 33 31 29 26 28 29 +31 31 29 26 28 28 +91 91 88 86 87 85 81 +72 72 70 73 70 67 64 57 +22 22 22 20 19 17 +74 74 74 71 69 66 63 64 +25 25 22 22 22 +33 33 32 30 28 28 24 +84 84 81 78 78 76 71 +19 19 15 12 9 8 7 +74 74 71 70 66 63 65 +46 46 45 41 41 +38 38 34 33 29 +44 44 43 39 32 +17 17 10 8 5 +89 89 87 85 84 79 78 80 +21 21 18 17 12 12 +66 66 65 62 60 54 50 +81 81 78 76 69 66 63 57 +40 36 34 33 30 29 +30 26 25 23 22 20 21 +24 20 17 14 13 11 10 10 +84 80 77 75 72 71 67 +64 60 58 55 53 48 +88 84 83 85 82 +33 29 32 30 31 +73 69 67 70 70 +95 91 88 87 85 84 85 81 +34 30 29 26 27 22 +61 57 57 55 52 49 47 +48 44 43 43 46 +75 71 70 68 68 65 64 64 +46 42 42 40 36 +79 75 72 72 69 64 +96 92 91 87 85 83 82 80 +80 76 75 71 69 67 70 +61 57 53 51 51 +21 17 15 13 11 7 3 +26 22 19 15 12 6 +73 69 67 60 58 57 56 +57 53 47 46 48 +65 61 59 56 51 48 48 +35 31 28 22 21 17 +49 45 43 36 34 33 30 24 +46 41 40 37 34 32 +79 73 70 67 65 63 60 63 +37 32 31 30 30 +66 59 58 56 55 54 50 +42 36 34 32 26 +74 67 66 68 65 64 62 +34 27 24 22 25 23 21 23 +24 19 16 17 15 15 +40 33 32 30 33 31 30 26 +73 66 65 66 60 +23 16 14 14 12 9 6 +74 69 67 64 64 65 +47 40 37 35 32 29 29 29 +37 31 28 25 22 20 20 16 +60 53 51 49 48 48 46 39 +76 70 66 63 60 +59 52 48 47 50 +49 43 42 38 38 +84 79 75 73 72 71 67 +75 69 68 64 62 60 55 +71 66 60 57 56 +70 64 57 54 53 51 48 51 +89 83 77 75 72 69 69 +50 45 44 42 40 38 31 27 +40 33 27 26 23 20 13 +12 13 16 19 22 24 23 +45 48 51 52 53 54 56 56 +30 31 33 35 38 42 +81 82 84 87 93 +10 13 14 15 13 16 +46 49 51 52 53 54 51 49 +13 14 17 16 16 +36 39 42 39 42 45 47 51 +11 13 15 12 19 +54 55 55 57 60 +30 33 33 36 39 37 +42 44 45 45 46 48 48 +34 35 35 36 39 43 +32 34 37 37 40 43 48 +22 25 29 32 33 +6 9 10 11 15 14 +77 78 82 84 87 87 +24 26 30 33 34 35 37 41 +45 48 51 52 56 63 +18 21 22 23 28 29 31 33 +9 10 13 16 22 21 +31 32 38 41 43 43 +72 74 77 84 85 88 89 93 +37 39 42 49 51 52 59 +73 72 73 76 77 79 +6 3 6 7 10 11 9 +67 66 69 72 74 76 78 78 +8 5 6 9 10 11 12 16 +60 57 59 60 63 64 70 +35 32 30 33 36 39 41 42 +29 28 26 28 31 32 29 +94 92 90 91 92 92 +23 20 18 20 22 23 27 +10 9 11 8 14 +38 35 38 38 41 42 +23 20 23 23 26 29 26 +61 58 58 61 63 63 +64 61 62 65 65 68 72 +7 5 6 6 13 +12 9 13 15 18 19 22 23 +16 14 17 21 24 23 +71 68 72 73 75 75 +12 11 14 16 20 22 23 27 +10 8 11 15 16 22 +54 53 55 58 63 64 +89 87 90 95 92 +17 15 17 19 21 22 29 29 +46 43 50 51 55 +48 45 48 49 54 59 +82 82 85 88 89 91 +11 11 12 13 14 17 14 +77 77 78 79 80 81 84 84 +74 74 77 79 80 81 85 +22 22 23 24 31 +82 82 81 83 85 88 89 92 +26 26 29 30 27 30 29 +56 56 57 60 63 65 62 62 +72 72 71 74 78 +5 5 8 6 11 +74 74 77 78 78 80 83 +27 27 30 30 33 31 +16 16 16 19 20 22 22 +77 77 79 81 81 85 +37 37 39 40 41 41 43 48 +67 67 68 69 73 74 76 79 +3 3 6 8 12 15 14 +83 83 87 88 89 92 92 +22 22 26 29 30 31 34 38 +2 2 5 8 10 13 17 23 +68 68 71 78 81 +15 15 17 22 24 21 +10 10 13 16 23 23 +16 16 17 18 24 27 31 +25 25 27 29 32 39 46 +23 27 28 31 33 36 37 +83 87 89 91 88 +44 48 49 50 53 53 +46 50 52 54 57 61 +9 13 16 17 19 22 29 +31 35 36 33 35 +81 85 84 86 85 +3 7 9 12 15 18 15 15 +50 54 53 55 59 +46 50 51 54 52 58 +58 62 63 66 66 67 70 +35 39 39 41 43 41 +29 33 36 37 37 37 +13 17 17 20 24 +71 75 75 76 79 81 84 89 +21 25 29 31 32 33 35 +33 37 40 44 46 48 47 +5 9 12 13 15 19 19 +23 27 30 34 37 38 42 +51 55 56 60 63 64 70 +50 54 57 60 62 63 68 69 +24 28 31 32 33 36 43 40 +24 28 29 30 36 37 38 38 +52 56 61 63 66 70 +15 19 25 27 29 34 +26 33 36 38 41 +23 30 33 34 32 +65 70 72 74 75 75 +13 18 20 22 26 +43 50 53 55 58 59 66 +12 17 19 18 21 24 +27 33 36 37 38 37 40 38 +74 81 82 84 85 84 86 86 +19 26 23 25 26 28 32 +77 83 84 81 88 +48 55 58 61 64 64 65 +40 45 47 47 48 49 47 +58 63 65 67 70 71 71 71 +30 35 36 39 39 42 46 +74 81 83 86 86 92 +67 72 74 76 79 83 84 85 +49 54 56 60 61 62 61 +62 67 71 73 74 74 +38 43 47 48 51 54 57 61 +19 26 27 30 34 40 +41 46 52 54 55 56 +36 42 44 50 51 50 +11 17 20 21 26 29 30 30 +24 30 33 39 41 42 46 +3 8 11 17 19 25 +85 84 81 80 79 76 79 +17 14 12 10 10 +30 27 25 23 19 +23 20 18 15 14 7 +38 36 35 33 34 32 +59 58 56 57 56 54 56 +45 44 46 44 41 41 +24 21 19 16 18 16 13 9 +79 77 75 73 72 74 72 66 +43 42 39 39 37 +33 30 28 28 25 24 22 24 +15 13 11 10 10 9 6 6 +94 93 91 91 88 84 +52 50 50 47 42 +59 57 54 50 47 46 44 42 +16 14 12 8 6 8 +91 88 84 82 82 +34 31 30 26 24 23 19 +85 84 81 79 78 74 68 +94 93 91 90 87 82 79 78 +28 25 18 16 17 +78 75 69 68 67 64 61 61 +37 34 28 26 22 +98 96 95 90 83 +48 51 50 47 46 +38 39 37 36 38 +49 51 49 47 45 44 44 +61 64 63 60 58 54 +18 19 18 16 11 +88 89 88 90 87 85 84 82 +6 8 11 10 11 +91 93 90 87 84 87 85 85 +77 80 78 81 80 78 74 +21 24 25 22 17 +86 88 85 82 82 80 +51 53 51 51 50 48 49 +53 55 55 52 50 49 49 +27 30 29 29 25 +54 55 52 50 50 47 41 +59 61 60 56 53 50 48 +82 85 81 79 78 75 77 +59 61 59 55 53 53 +27 29 26 22 20 19 18 14 +51 52 49 45 38 +39 40 38 37 36 35 29 26 +65 66 65 59 57 56 54 56 +93 94 92 87 86 86 +39 41 40 37 31 28 24 +43 46 39 36 30 +46 46 44 43 40 +42 42 41 38 36 34 37 +45 45 44 42 39 37 34 34 +81 81 80 78 75 71 +29 29 28 26 25 22 16 +83 83 82 85 84 +70 70 67 66 69 67 70 +31 31 29 32 32 +47 47 44 46 42 +11 11 12 11 10 3 +81 81 79 77 77 76 +79 79 79 76 78 +28 28 25 25 25 +66 66 63 60 60 58 55 51 +18 18 18 15 14 12 5 +35 35 33 31 30 26 24 +7 7 3 2 5 +76 76 74 73 69 67 66 66 +30 30 28 24 23 22 18 +92 92 89 85 84 82 75 +42 42 41 38 36 30 29 26 +74 74 73 70 68 61 58 60 +74 74 72 67 67 +76 76 69 66 63 59 +66 66 63 62 61 55 54 49 +97 93 91 88 85 82 79 76 +58 54 52 49 46 43 46 +92 88 85 82 80 79 79 +86 82 80 79 76 74 70 +32 28 25 22 20 13 +68 64 61 59 57 54 55 53 +72 68 70 67 64 67 +96 92 95 94 92 92 +58 54 52 54 51 47 +39 35 33 32 34 32 26 +87 83 81 79 76 76 73 70 +54 50 49 49 50 +78 74 74 73 73 +53 49 49 47 45 41 +64 60 60 59 57 55 52 46 +64 60 59 55 54 +13 9 5 4 7 +23 19 17 15 11 11 +86 82 80 79 75 72 69 65 +88 84 80 77 76 70 +89 85 83 82 75 73 70 +86 82 77 76 74 72 69 70 +32 28 25 23 22 21 14 14 +77 73 70 68 61 59 57 53 +58 54 48 47 46 45 39 +16 11 9 7 4 1 +24 17 14 13 14 +71 65 63 61 58 56 53 53 +85 80 79 77 76 75 71 +83 77 76 74 73 70 67 60 +88 83 80 81 79 77 75 +72 67 69 67 65 63 61 64 +18 12 9 12 12 +89 83 81 78 75 76 74 70 +46 41 39 36 37 32 +83 78 75 74 71 71 69 67 +47 40 40 39 38 35 32 35 +46 41 39 37 37 37 +58 51 48 48 47 45 41 +65 60 60 59 53 +26 19 15 13 12 +17 11 9 8 4 5 +89 83 82 80 76 75 73 73 +71 66 63 62 59 55 51 +78 73 69 67 60 +39 32 26 24 23 22 19 +70 64 59 57 54 51 50 51 +30 23 18 15 15 +79 72 69 64 60 +25 19 16 11 8 2 +48 44 42 39 39 +4 8 14 17 17 +92 91 90 89 86 86 +16 19 15 13 12 6 +39 36 29 28 31 +78 75 77 78 79 79 +20 24 26 29 33 37 +81 78 82 85 86 +41 37 30 29 28 26 24 24 +68 73 80 82 81 +90 88 87 84 81 78 76 +86 84 83 80 77 76 75 73 +69 67 65 62 60 +22 24 26 29 32 34 +14 12 10 7 4 +69 71 73 75 78 81 +51 48 45 42 40 38 37 36 +50 51 53 56 58 +96 94 93 92 89 +59 58 55 54 53 51 +43 42 41 38 35 +61 63 65 67 70 +97 96 95 94 93 91 89 88 +17 16 15 12 11 8 5 +17 19 20 23 26 +44 47 48 49 52 53 54 57 +53 52 51 49 48 47 44 +84 83 81 80 77 74 73 71 +24 22 21 18 15 12 +71 74 75 76 77 79 82 85 +5 8 11 12 14 15 18 21 +35 37 39 41 43 46 +41 44 47 48 50 51 53 +46 43 40 37 35 34 31 +30 27 24 22 19 16 13 +54 57 58 61 63 66 +77 80 82 85 86 +60 63 65 68 70 71 +83 85 86 88 89 90 91 94 +72 71 68 65 64 63 60 +59 60 63 64 66 +60 57 56 55 53 50 +54 57 60 61 62 +56 59 60 61 63 66 68 71 +57 58 61 64 66 +61 62 63 65 68 69 +32 30 27 26 23 22 +54 52 49 48 46 43 41 +20 22 23 24 25 +28 27 24 21 20 18 15 +75 73 70 67 65 +80 78 75 74 73 72 +37 36 34 31 28 26 25 24 +24 26 29 32 34 +86 89 91 93 94 95 97 +57 54 52 49 46 44 42 41 +75 72 70 69 68 65 62 61 +27 29 30 31 34 35 38 41 +17 20 23 25 28 +38 36 33 31 29 26 +98 95 92 91 89 +14 13 10 9 6 4 2 +24 26 29 31 33 +44 46 48 50 52 54 55 +46 45 43 40 39 38 36 +16 17 18 20 22 24 27 +89 90 93 96 97 +55 56 58 59 60 61 64 65 +68 69 72 75 77 +75 74 72 69 68 +63 65 68 69 70 73 +65 67 70 73 75 76 +72 71 68 65 62 60 58 56 +19 22 23 25 27 29 +35 37 38 40 41 43 46 +22 24 25 26 28 29 31 +54 52 49 47 45 42 +8 9 12 13 14 17 18 +55 53 52 51 48 +27 24 23 20 17 16 13 +65 67 70 73 76 77 80 83 +9 11 12 14 16 18 20 22 +18 20 21 23 25 28 31 32 +65 67 68 70 72 74 76 +17 20 23 26 28 31 +38 39 41 43 45 46 +69 71 73 76 77 79 +89 90 91 94 96 97 98 +87 89 92 94 96 98 99 +34 36 37 38 40 42 45 +13 15 18 21 24 26 28 +81 80 79 77 74 73 72 69 +51 50 49 46 45 +68 65 64 61 58 +56 55 54 52 51 50 +14 13 11 10 8 7 5 +9 12 15 16 17 18 21 +86 87 90 91 94 +87 86 83 82 80 77 75 74 +16 17 18 21 22 24 27 30 +38 40 42 45 48 49 +69 67 66 65 62 59 56 54 +60 58 55 54 52 50 +48 46 44 41 38 +96 94 92 90 89 +77 79 81 82 84 +99 97 94 93 92 89 87 +82 81 78 76 73 70 67 +75 72 71 70 68 65 63 +17 16 15 12 11 +41 43 45 48 51 54 56 +36 34 31 30 29 28 26 23 +69 68 67 65 62 +83 81 80 77 76 73 70 68 +44 41 38 36 35 32 30 +77 78 79 80 83 86 89 +45 46 48 51 53 55 +12 14 17 18 21 23 25 27 +39 40 42 43 44 45 47 +7 9 11 14 15 17 20 22 +65 64 63 61 58 55 +35 32 30 28 25 24 +7 8 10 12 14 17 18 +54 53 51 49 46 +39 38 37 36 33 +83 84 85 86 89 92 +23 20 17 14 13 11 9 6 +31 30 29 28 26 23 21 +37 39 41 42 43 45 +90 87 86 85 83 80 77 75 +55 54 52 50 49 47 +10 12 13 16 19 21 +51 53 54 56 58 59 61 +46 49 51 53 55 +83 85 88 90 93 96 97 99 +25 27 28 31 32 +34 35 38 41 44 47 49 51 +16 15 12 10 8 5 +52 50 47 44 41 39 +80 83 85 86 88 89 92 +34 37 38 39 40 41 42 +54 56 58 61 62 63 65 +23 26 28 30 32 33 +28 26 23 22 19 16 15 13 +52 55 57 59 62 +76 73 71 70 69 66 64 62 +73 75 78 81 83 86 87 90 +57 55 52 50 49 47 44 +74 76 79 82 84 85 87 +27 25 24 23 20 +91 92 93 96 98 99 +40 41 44 46 48 50 +45 48 50 53 54 56 +91 89 88 87 86 +13 16 18 20 22 24 26 +87 89 90 91 94 +81 82 84 86 89 90 +59 61 62 64 67 69 +32 29 28 25 24 22 20 +34 32 30 28 27 26 +21 22 25 27 28 31 34 35 +59 56 53 50 49 48 47 +74 71 69 67 65 64 63 61 +48 45 42 41 38 35 32 +51 50 47 44 43 42 +35 34 33 32 31 30 29 26 +83 80 77 75 74 72 69 +32 29 28 27 26 24 21 +68 70 71 74 75 78 80 +23 25 27 30 33 36 39 42 +35 36 38 40 41 42 43 45 +58 60 61 62 63 +35 37 40 42 45 47 50 +87 86 84 82 81 +33 36 39 40 42 44 +8 5 4 2 1 +68 66 63 61 58 +34 32 31 29 26 25 22 +3 6 7 9 10 11 12 +42 41 39 37 35 33 32 +54 56 59 61 64 67 68 71 +28 30 32 35 37 38 39 +23 21 20 19 17 +78 77 74 71 69 68 +38 36 34 33 30 27 24 +65 68 71 73 74 76 +41 38 37 35 32 +53 54 55 57 58 59 62 65 +59 62 65 67 70 71 72 +70 73 74 77 80 81 83 +97 96 94 91 90 89 88 +47 44 41 38 37 36 +62 61 59 56 55 +95 92 90 89 88 87 85 +14 11 8 7 6 3 2 1 +75 78 81 84 86 88 90 +37 35 33 30 29 28 26 24 +75 76 79 81 84 86 88 90 +78 76 74 72 69 66 +59 57 56 55 52 51 48 +50 53 54 56 59 +45 43 40 38 37 +12 14 16 18 21 23 26 +80 81 84 87 88 91 93 +59 58 56 54 53 50 +16 14 13 12 9 7 5 2 +18 21 24 25 26 29 +14 16 17 19 22 23 26 +92 89 86 85 82 81 +60 63 66 69 71 72 74 75 +62 64 65 68 71 74 75 78 +48 49 51 53 55 +78 75 74 72 71 +67 68 69 70 72 73 +21 23 24 27 29 30 32 +2 5 8 10 12 14 +30 28 25 23 21 18 15 +66 65 64 62 59 56 +48 49 52 54 55 +80 83 85 86 87 89 91 +52 50 48 47 44 42 40 +54 53 50 49 47 +86 89 91 92 95 98 +27 30 31 33 36 +32 35 38 41 43 46 49 50 +24 23 21 20 18 16 +89 86 83 80 77 76 75 74 +26 29 30 31 34 35 +45 44 41 39 37 +32 33 36 38 40 43 45 +74 76 79 82 85 +16 17 20 22 25 27 28 +28 30 32 33 36 37 38 39 +76 73 71 70 67 66 64 63 +16 13 12 9 6 5 +27 24 22 20 19 16 14 11 +24 26 27 29 31 33 +44 47 49 50 52 54 55 58 +29 27 25 22 20 17 16 14 +31 32 34 35 36 39 41 43 +44 41 38 35 32 29 28 +27 29 31 33 35 36 39 +89 88 85 84 83 81 78 76 +70 71 72 75 76 77 80 +69 67 66 63 60 57 56 +20 22 24 26 28 30 +86 83 81 80 77 75 72 69 +18 17 14 11 9 8 5 4 +53 54 56 59 60 61 63 65 +13 16 19 20 22 23 24 25 +42 45 47 50 51 +35 32 29 27 26 +28 29 32 35 37 39 40 43 +82 79 77 74 72 70 69 67 +66 63 60 57 54 +20 23 26 28 30 31 34 35 \ No newline at end of file From b340277d76aa7501e9f91b48e57b784f7c097d50 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Mon, 2 Dec 2024 21:58:59 +0900 Subject: [PATCH 2/5] solution: solve Day 2 Part 1 --- .../src/main/kotlin/com/eunicehong/Day02.kt | 27 ++++++++++++++++--- .../test/kotlin/com/eunicehong/Day02Test.kt | 2 +- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt index 4174bb1..6d61fc6 100644 --- a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt +++ b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt @@ -1,5 +1,7 @@ package com.eunicehong +import kotlin.math.abs + /** * # 2024 Day 2: Red-Nosed Reports * @@ -46,10 +48,12 @@ internal class Day02 { * * Analyze the unusual data from the engineers. How many reports are safe? */ - fun solution1(puzzle: String): String { - val lines = puzzle.lines().filter { it.isNotEmpty() } - return "" - } + fun solution1(puzzle: String) = + puzzle + .lines() + .map { it.split(" ").map { level -> level.toInt() } } + .count(::isReportValid) + .toString() /** * ## Part 2 @@ -60,3 +64,18 @@ internal class Day02 { return "" } } + +/** + * Check if the report is valid. + * + * @param report The report to check. + * + * @return `true` if the report is valid, `false` otherwise. + */ +private fun isReportValid(report: List): Boolean { + val diffs = report.windowed(2).map { abs(it[1] - it[0]) } + val isIncreasing = report == report.sorted() + val isDecreasing = report == report.sortedDescending() + val isChangeSmall = diffs.all { it in 1..3 } + return (isIncreasing || isDecreasing) && isChangeSmall +} diff --git a/eunice-hong/src/test/kotlin/com/eunicehong/Day02Test.kt b/eunice-hong/src/test/kotlin/com/eunicehong/Day02Test.kt index 2f664ca..7f4165f 100644 --- a/eunice-hong/src/test/kotlin/com/eunicehong/Day02Test.kt +++ b/eunice-hong/src/test/kotlin/com/eunicehong/Day02Test.kt @@ -11,7 +11,7 @@ internal class Day02Test : DailyTest(2) { @Test override fun puzzleSolution1() { - val expected = "Not implemented" + val expected = "246" val actual = testDay02.solution1(input) assertEquals(expected, actual, "🎅 Solution for Puzzle 1 Day $dayString has not been implemented yet.") println("🌲 Day $dayString Puzzle 1 completed! $actual") From a36227796b38de9ed135ce20e3ce83a962ace3fc Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Mon, 2 Dec 2024 23:47:05 +0900 Subject: [PATCH 3/5] solution: solve Day 2 Part 2 --- .../src/main/kotlin/com/eunicehong/Day02.kt | 51 +++++++++++++++++-- .../test/kotlin/com/eunicehong/Day02Test.kt | 2 +- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt index 6d61fc6..ca61099 100644 --- a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt +++ b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt @@ -39,10 +39,15 @@ internal class Day02 { * In the example above, the reports can be found safe or unsafe by checking those rules: * * `7 6 4 2 1`: Safe because the levels are all decreasing by 1 or 2. + * * `1 2 7 8 9`: Unsafe because 2 7 is an increase of 5. + * * `9 7 6 2 1`: Unsafe because 6 2 is a decrease of 4. + * * `1 3 2 4 5`: Unsafe because 1 3 is increasing but 3 2 is decreasing. + * * `8 6 4 4 1`: Unsafe because 4 4 is neither an increase or a decrease. + * * `1 3 6 7 9`: Safe because the levels are all increasing by 1, 2, or 3. * So, in this example, 2 reports are safe. * @@ -58,13 +63,51 @@ internal class Day02 { /** * ## Part 2 * + * The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about the Problem Dampener. + * + * The Problem Dampener is a reactor-mounted module that lets the reactor safety systems tolerate a single bad level in what would otherwise be a safe report. It's like the bad level never happened! + * + * Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, the report instead counts as safe. + * + * More of the above example's reports are now safe: + * + * `7 6 4 2 1`: Safe without removing any level. + * + * `1 2 7 8 9`: Unsafe regardless of which level is removed. + * + * `9 7 6 2 1`: Unsafe regardless of which level is removed. + * + * `1 3 2 4 5`: Safe by removing the second level, 3. + * + * `8 6 4 4 1`: Safe by removing the third level, 4. + * + * `1 3 6 7 9`: Safe without removing any level. + * Thanks to the Problem Dampener, 4 reports are actually safe! + * + * Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. How many reports are now safe? + * + * */ - fun solution2(puzzle: String): String { - val lines = puzzle.lines().filter { it.isNotEmpty() } - return "" - } + fun solution2(puzzle: String) = + puzzle + .lines() + .map { it.split(" ").map { level -> level.toInt() } } + .count(::isReportValidWithTolerance) + .toString() } +/** + * Check if the report is valid with tolerance. + * + * @param report The report to check. + * + * @return `true` if the report is valid with tolerance, `false` otherwise. + */ +private fun isReportValidWithTolerance(report: List): Boolean = + (0..report.lastIndex) + .map { indexToRemove -> report.filterIndexed { index, _ -> index != indexToRemove } } + .any { dampenedReport -> isReportValid(dampenedReport) } + /** * Check if the report is valid. * diff --git a/eunice-hong/src/test/kotlin/com/eunicehong/Day02Test.kt b/eunice-hong/src/test/kotlin/com/eunicehong/Day02Test.kt index 7f4165f..fed3416 100644 --- a/eunice-hong/src/test/kotlin/com/eunicehong/Day02Test.kt +++ b/eunice-hong/src/test/kotlin/com/eunicehong/Day02Test.kt @@ -19,7 +19,7 @@ internal class Day02Test : DailyTest(2) { @Test override fun puzzleSolution2() { - val expected = "Not implemented" + val expected = "318" val actual = testDay02.solution2(input) assertEquals(expected, actual, "🎅 Solution for Puzzle 2 Day $dayString has not been implemented yet.") println("🌲 Day $dayString Puzzle 2 completed! $actual") From 2a4503234ff28c905c857e0bd054a9d9ed719099 Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Tue, 3 Dec 2024 00:02:12 +0900 Subject: [PATCH 4/5] refactor: use zipWithNext() to check consecutive value changes without sorting --- eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt index ca61099..ee18ae0 100644 --- a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt +++ b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt @@ -117,8 +117,8 @@ private fun isReportValidWithTolerance(report: List): Boolean = */ private fun isReportValid(report: List): Boolean { val diffs = report.windowed(2).map { abs(it[1] - it[0]) } - val isIncreasing = report == report.sorted() - val isDecreasing = report == report.sortedDescending() + val isIncreasing = report.zipWithNext().all { it.first < it.second } + val isDecreasing = report.zipWithNext().all { it.first > it.second } val isChangeSmall = diffs.all { it in 1..3 } return (isIncreasing || isDecreasing) && isChangeSmall } From 8bdfecd5c9209081a9cba475930b37a69d5c627f Mon Sep 17 00:00:00 2001 From: eunice-hong Date: Tue, 3 Dec 2024 00:04:18 +0900 Subject: [PATCH 5/5] refactor: extract repetitive input parsing logic into `parseInput` function to remove duplication --- .../src/main/kotlin/com/eunicehong/Day02.kt | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt index ee18ae0..5e6d8f9 100644 --- a/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt +++ b/eunice-hong/src/main/kotlin/com/eunicehong/Day02.kt @@ -55,8 +55,7 @@ internal class Day02 { */ fun solution1(puzzle: String) = puzzle - .lines() - .map { it.split(" ").map { level -> level.toInt() } } + .parseInput() .count(::isReportValid) .toString() @@ -90,8 +89,7 @@ internal class Day02 { */ fun solution2(puzzle: String) = puzzle - .lines() - .map { it.split(" ").map { level -> level.toInt() } } + .parseInput() .count(::isReportValidWithTolerance) .toString() } @@ -122,3 +120,17 @@ private fun isReportValid(report: List): Boolean { val isChangeSmall = diffs.all { it in 1..3 } return (isIncreasing || isDecreasing) && isChangeSmall } + +/** + * Parse the input. + * + * @return The parsed input. + */ +private fun String.parseInput() = + this + .split("\n") + .map { report -> + report + .split(" ") + .map { level -> level.toInt() } + }