diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-04 11:35:40 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-04 11:35:40 +0800 |
commit | b6f02f841a26a5dc8c405416a57a4c8bee51b699 (patch) | |
tree | a9562bbd74978287d82b7391d9e134b5143ae7a4 /src | |
parent | db6c4680263981594cacd464e4abfea38586b9d5 (diff) | |
download | advent-of-code-b6f02f841a26a5dc8c405416a57a4c8bee51b699.tar.gz advent-of-code-b6f02f841a26a5dc8c405416a57a4c8bee51b699.zip |
2019 day1
Diffstat (limited to 'src')
-rw-r--r-- | src/2016/day1/README.md | 5 | ||||
-rw-r--r-- | src/2017/day1/README.md | 12 | ||||
-rw-r--r-- | src/2019/day1/README.md | 31 | ||||
-rw-r--r-- | src/2019/day1/aoc.cpp | 46 | ||||
-rw-r--r-- | src/2019/day1/aoc.h | 2 | ||||
-rw-r--r-- | src/2019/day1/input | 101 | ||||
-rw-r--r-- | src/2020/day1/README.md | 20 | ||||
-rw-r--r-- | src/2020/day1/input | 2 | ||||
-rw-r--r-- | src/2021/day1/README.md | 20 | ||||
-rw-r--r-- | src/2021/day1/input | 2 |
10 files changed, 187 insertions, 54 deletions
diff --git a/src/2016/day1/README.md b/src/2016/day1/README.md index 64299f9..acdb696 100644 --- a/src/2016/day1/README.md +++ b/src/2016/day1/README.md @@ -18,4 +18,9 @@ For example: How many blocks away is Easter Bunny HQ? +--- Part Two --- +Then, you notice the instructions continue on the back of the Recruiting Document. Easter Bunny HQ is actually at the first location you visit twice. +For example, if your instructions are R8, R4, R4, R8, the first location you visit twice is 4 blocks away, due East. + +How many blocks away is the first location you visit twice? diff --git a/src/2017/day1/README.md b/src/2017/day1/README.md index 370fe17..95b6b14 100644 --- a/src/2017/day1/README.md +++ b/src/2017/day1/README.md @@ -19,4 +19,16 @@ For example: 91212129 produces 9 because the only digit that matches the next one is the last digit, 9. What is the solution to your captcha? +--- Part Two --- +You notice a progress bar that jumps to 50% completion. Apparently, the door isn't yet satisfied, but it did emit a star as encouragement. The instructions change: +Now, instead of considering the next digit, it wants you to consider the digit halfway around the circular list. That is, if your list contains 10 items, only include a digit in your sum if the digit 10/2 = 5 steps forward matches it. Fortunately, your list has an even number of elements. + +For example: + +1212 produces 6: the list contains 4 items, and all four digits match the digit 2 items ahead. +1221 produces 0, because every comparison is between a 1 and a 2. +123425 produces 4, because both 2s match each other, but no other digit has a match. +123123 produces 12. +12131415 produces 4. +What is the solution to your new captcha? diff --git a/src/2019/day1/README.md b/src/2019/day1/README.md index 64299f9..6b97284 100644 --- a/src/2019/day1/README.md +++ b/src/2019/day1/README.md @@ -1,21 +1,32 @@ ---- Day 1: No Time for a Taxicab --- - -Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th. +--- Day 1: The Tyranny of the Rocket Equation --- +Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars. Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! -You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further. +The Elves quickly load you into a spacecraft and prepare to launch. -The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given number of blocks, ending at a new intersection. +At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet. -There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the destination? +Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2. For example: - Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away. - R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away. - R5, L5, R5, R3 leaves you 12 blocks away. +For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2. +For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2. +For a mass of 1969, the fuel required is 654. +For a mass of 100756, the fuel required is 33583. +The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values. + +What is the sum of the fuel requirements for all of the modules on your spacecraft? + +--- Part Two --- +During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added. -How many blocks away is Easter Bunny HQ? +Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation. +So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example: +A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2. +At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966. +The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346. +What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.) diff --git a/src/2019/day1/aoc.cpp b/src/2019/day1/aoc.cpp index 26fed89..933228d 100644 --- a/src/2019/day1/aoc.cpp +++ b/src/2019/day1/aoc.cpp @@ -1,3 +1,47 @@ #include "aoc.h" -namespace aoc2019 {} +namespace aoc2019 { + +int parse_day1(line_view lv) { + int d{0}; + const char* p = lv.line; + while (*p >= '0' && *p <= '9') { + d = d * 10 + *p - '0'; + p++; + } + return d; +} + +int day1(line_view file) { + int fuel{0}; + auto cal = [](int m) -> int { return m / 3 - 2; }; + per_line(file, [&fuel, &cal](line_view lv) { + fuel += cal(parse_day1(lv)); + return true; + }); + + return fuel; +} + +void add_fuel(int base, int* total) { + if (base > 0) { + // printf("%d\n", base); + *total += base; + add_fuel(base / 3 - 2, total); + } +} + +int day1part2(line_view file) { + int fuel{0}; + per_line(file, [&fuel](line_view lv) { + int total{0}; + int base = parse_day1(lv); + add_fuel(base / 3 - 2, &total); + fuel += total; + return true; + }); + + return fuel; +} + +} // namespace aoc2019 diff --git a/src/2019/day1/aoc.h b/src/2019/day1/aoc.h index 959d6d8..6b27fce 100644 --- a/src/2019/day1/aoc.h +++ b/src/2019/day1/aoc.h @@ -3,4 +3,6 @@ namespace aoc2019 { +int day1(line_view); +int day1part2(line_view); } diff --git a/src/2019/day1/input b/src/2019/day1/input index 22adf2a..3b6b4c4 100644 --- a/src/2019/day1/input +++ b/src/2019/day1/input @@ -1 +1,100 @@ -R1, L3, R5, R5, R5, L4, R5, R1, R2, L1, L1, R5, R1, L3, L5, L2, R4, L1, R4, R5, L3, R5, L1, R3, L5, R1, L2, R1, L5, L1, R1, R4, R1, L1, L3, R3, R5, L3, R4, L4, R5, L5, L1, L2, R4, R3, R3, L185, R3, R4, L5, L4, R48, R1, R2, L1, R1, L4, L4, R77, R5, L2, R192, R2, R5, L4, L5, L3, R2, L4, R1, L5, R5, R4, R1, R2, L3, R4, R4, L2, L4, L3, R5, R4, L2, L1, L3, R1, R5, R5, R2, L5, L2, L3, L4, R2, R1, L4, L1, R1, R5, R3, R3, R4, L1, L4, R1, L2, R3, L3, L2, L1, L2, L2, L1, L2, R3, R1, L4, R1, L1, L4, R1, L2, L5, R3, L5, L2, L2, L3, R1, L4, R1, R1, R2, L1, L4, L4, R2, R2, R2, R2, R5, R1, L1, L4, L5, R2, R4, L3, L5, R2, R3, L4, L1, R2, R3, R5, L2, L3, R3, R1, R3 +129192 +58561 +57267 +95382 +84995 +127372 +93598 +97264 +138550 +79327 +135661 +139468 +108860 +149642 +72123 +128333 +69002 +98450 +86267 +70171 +101333 +79822 +142539 +142743 +51371 +111381 +62073 +72210 +125168 +135952 +131060 +121842 +88234 +146774 +136571 +126719 +50644 +75696 +51195 +77171 +118052 +83691 +133779 +149814 +64847 +110697 +92695 +59453 +139517 +129487 +79271 +97896 +146987 +149822 +71866 +90797 +104732 +54997 +50139 +134115 +133017 +144979 +89428 +124750 +91833 +57252 +67195 +121624 +102706 +138245 +127700 +124098 +110382 +121557 +103613 +133576 +122801 +112306 +120203 +134696 +76129 +84576 +80854 +147237 +71025 +127513 +143631 +125090 +115698 +57979 +84880 +120177 +147389 +88380 +114688 +56355 +126265 +58220 +63523 +130179 diff --git a/src/2020/day1/README.md b/src/2020/day1/README.md index 64299f9..8b13789 100644 --- a/src/2020/day1/README.md +++ b/src/2020/day1/README.md @@ -1,21 +1 @@ ---- Day 1: No Time for a Taxicab --- - -Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th. - -Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! - -You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further. - -The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given number of blocks, ending at a new intersection. - -There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the destination? - -For example: - - Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away. - R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away. - R5, L5, R5, R3 leaves you 12 blocks away. - -How many blocks away is Easter Bunny HQ? - diff --git a/src/2020/day1/input b/src/2020/day1/input index 22adf2a..8b13789 100644 --- a/src/2020/day1/input +++ b/src/2020/day1/input @@ -1 +1 @@ -R1, L3, R5, R5, R5, L4, R5, R1, R2, L1, L1, R5, R1, L3, L5, L2, R4, L1, R4, R5, L3, R5, L1, R3, L5, R1, L2, R1, L5, L1, R1, R4, R1, L1, L3, R3, R5, L3, R4, L4, R5, L5, L1, L2, R4, R3, R3, L185, R3, R4, L5, L4, R48, R1, R2, L1, R1, L4, L4, R77, R5, L2, R192, R2, R5, L4, L5, L3, R2, L4, R1, L5, R5, R4, R1, R2, L3, R4, R4, L2, L4, L3, R5, R4, L2, L1, L3, R1, R5, R5, R2, L5, L2, L3, L4, R2, R1, L4, L1, R1, R5, R3, R3, R4, L1, L4, R1, L2, R3, L3, L2, L1, L2, L2, L1, L2, R3, R1, L4, R1, L1, L4, R1, L2, L5, R3, L5, L2, L2, L3, R1, L4, R1, R1, R2, L1, L4, L4, R2, R2, R2, R2, R5, R1, L1, L4, L5, R2, R4, L3, L5, R2, R3, L4, L1, R2, R3, R5, L2, L3, R3, R1, R3 + diff --git a/src/2021/day1/README.md b/src/2021/day1/README.md index 64299f9..8b13789 100644 --- a/src/2021/day1/README.md +++ b/src/2021/day1/README.md @@ -1,21 +1 @@ ---- Day 1: No Time for a Taxicab --- - -Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th. - -Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! - -You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further. - -The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given number of blocks, ending at a new intersection. - -There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the destination? - -For example: - - Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away. - R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away. - R5, L5, R5, R3 leaves you 12 blocks away. - -How many blocks away is Easter Bunny HQ? - diff --git a/src/2021/day1/input b/src/2021/day1/input index 22adf2a..8b13789 100644 --- a/src/2021/day1/input +++ b/src/2021/day1/input @@ -1 +1 @@ -R1, L3, R5, R5, R5, L4, R5, R1, R2, L1, L1, R5, R1, L3, L5, L2, R4, L1, R4, R5, L3, R5, L1, R3, L5, R1, L2, R1, L5, L1, R1, R4, R1, L1, L3, R3, R5, L3, R4, L4, R5, L5, L1, L2, R4, R3, R3, L185, R3, R4, L5, L4, R48, R1, R2, L1, R1, L4, L4, R77, R5, L2, R192, R2, R5, L4, L5, L3, R2, L4, R1, L5, R5, R4, R1, R2, L3, R4, R4, L2, L4, L3, R5, R4, L2, L1, L3, R1, R5, R5, R2, L5, L2, L3, L4, R2, R1, L4, L1, R1, R5, R3, R3, R4, L1, L4, R1, L2, R3, L3, L2, L1, L2, L2, L1, L2, R3, R1, L4, R1, L1, L4, R1, L2, L5, R3, L5, L2, L2, L3, R1, L4, R1, R1, R2, L1, L4, L4, R2, R2, R2, R2, R5, R1, L1, L4, L5, R2, R4, L3, L5, R2, R3, L4, L1, R2, R3, R5, L2, L3, R3, R1, R3 + |