Day 2
This commit is contained in:
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"iosfwd": "cpp",
|
||||
"vector": "cpp"
|
||||
}
|
||||
}
|
||||
5
2019/1stDay/.vscode/settings.json
vendored
5
2019/1stDay/.vscode/settings.json
vendored
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"cmath": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,24 @@
|
||||
--- 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!
|
||||
|
||||
The Elves quickly load you into a spacecraft and prepare to launch.
|
||||
|
||||
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.
|
||||
|
||||
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:
|
||||
|
||||
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.
|
||||
|
||||
Binary file not shown.
@@ -1,20 +0,0 @@
|
||||
--- 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!
|
||||
|
||||
The Elves quickly load you into a spacecraft and prepare to launch.
|
||||
|
||||
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.
|
||||
|
||||
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:
|
||||
|
||||
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?
|
||||
Binary file not shown.
64
2019/2ndDay/challenge.txt
Normal file
64
2019/2ndDay/challenge.txt
Normal file
@@ -0,0 +1,64 @@
|
||||
--- Day 2: 1202 Program Alarm ---
|
||||
On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't worry, that's perfectly norma--" The ship computer bursts into flames.
|
||||
|
||||
You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!"
|
||||
|
||||
An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode - either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.
|
||||
|
||||
Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.
|
||||
|
||||
For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.
|
||||
|
||||
Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.
|
||||
|
||||
Once you're done processing an opcode, move to the next one by stepping forward 4 positions.
|
||||
|
||||
For example, suppose you have the following program:
|
||||
|
||||
1,9,10,3,2,3,11,0,99,30,40,50
|
||||
For the purposes of illustration, here is the same program split into multiple lines:
|
||||
|
||||
1,9,10,3,
|
||||
2,3,11,0,
|
||||
99,
|
||||
30,40,50
|
||||
The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:
|
||||
|
||||
1,9,10,70,
|
||||
2,3,11,0,
|
||||
99,
|
||||
30,40,50
|
||||
Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:
|
||||
|
||||
3500,9,10,70,
|
||||
2,3,11,0,
|
||||
99,
|
||||
30,40,50
|
||||
Stepping forward 4 more positions arrives at opcode 99, halting the program.
|
||||
|
||||
Here are the initial and final states of a few more small programs:
|
||||
|
||||
1,0,0,0,99 becomes 2,0,0,0,99 (1 + 1 = 2).
|
||||
2,3,0,3,99 becomes 2,3,0,6,99 (3 * 2 = 6).
|
||||
2,4,4,5,99,0 becomes 2,4,4,5,99,9801 (99 * 99 = 9801).
|
||||
1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99.
|
||||
Once you have a working computer, the first step is to restore the gravity assist program (your puzzle input) to the "1202 program alarm" state it had just before the last computer caught fire. To do this, before running the program, replace position 1 with the value 12 and replace position 2 with the value 2. What value is left at position 0 after the program halts?
|
||||
|
||||
--- Part Two ---
|
||||
"Good, the new computer seems to be working correctly! Keep it nearby during this mission - you'll probably use it again. Real Intcode computers support many more features than your new one, but we'll let you know what they are as you need them."
|
||||
|
||||
"However, your current priority should be to complete your gravity assist around the Moon. For this mission to succeed, we should settle on some terminology for the parts you've already built."
|
||||
|
||||
Intcode programs are given as a list of integers; these values are used as the initial state for the computer's memory. When you run an Intcode program, make sure to start by initializing memory to the program's values. A position in memory is called an address (for example, the first value in memory is at "address 0").
|
||||
|
||||
Opcodes (like 1, 2, or 99) mark the beginning of an instruction. The values used immediately after an opcode, if any, are called the instruction's parameters. For example, in the instruction 1,2,3,4, 1 is the opcode; 2, 3, and 4 are the parameters. The instruction 99 contains only an opcode and has no parameters.
|
||||
|
||||
The address of the current instruction is called the instruction pointer; it starts at 0. After an instruction finishes, the instruction pointer increases by the number of values in the instruction; until you add more instructions to the computer, this is always 4 (1 opcode + 3 parameters) for the add and multiply instructions. (The halt instruction would increase the instruction pointer by 1, but it halts the program instead.)
|
||||
|
||||
"With terminology out of the way, we're ready to proceed. To complete the gravity assist, you need to determine what pair of inputs produces the output 19690720."
|
||||
|
||||
The inputs should still be provided to the program by replacing the values at addresses 1 and 2, just like before. In this program, the value placed in address 1 is called the noun, and the value placed in address 2 is called the verb. Each of the two input values will be between 0 and 99, inclusive.
|
||||
|
||||
Once the program has halted, its output is available at address 0, also just like before. Each time you try a pair of inputs, make sure you first reset the computer's memory to the values in the program (your puzzle input) - in other words, don't reuse memory from a previous attempt.
|
||||
|
||||
Find the input noun and verb that cause the program to produce the output 19690720. What is 100 * noun + verb? (For example, if noun=12 and verb=2, the answer would be 1202.)
|
||||
79
2019/2ndDay/challenge1.cpp
Normal file
79
2019/2ndDay/challenge1.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<int> tokenise( std::string input )
|
||||
{
|
||||
std::stringstream ssInput( input );
|
||||
std::vector<int> stream;
|
||||
|
||||
std::string s;
|
||||
while( std::getline( ssInput, s, ',' ) ) {
|
||||
stream.push_back( std::stoi( s ) );
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
enum Opcodes
|
||||
{
|
||||
Add = 1,
|
||||
Multiply = 2,
|
||||
End = 99
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
|
||||
std::ifstream infile( argv[1] );
|
||||
|
||||
std::string input;
|
||||
std::getline( infile, input );
|
||||
|
||||
std::vector<int> program = tokenise( input );
|
||||
|
||||
program[1] = 12;
|
||||
program[2] = 2;
|
||||
|
||||
for ( int i = 0; i < program.size(); i += 4 )
|
||||
{
|
||||
|
||||
int opcode = program[i];
|
||||
|
||||
std::cout << "Opcode : " << opcode << std::endl;
|
||||
|
||||
int operandLocA = program[i + 1];
|
||||
int operandLocB = program[i + 2];
|
||||
|
||||
std::cout << "Operand locations : " << operandLocA << ", " << operandLocB << std::endl;
|
||||
|
||||
int operandA = program[operandLocA];
|
||||
int operandB = program[operandLocB];
|
||||
|
||||
int resLoc = program[i + 3];
|
||||
|
||||
std::cout << "Result location : " << resLoc << std::endl;
|
||||
|
||||
if ( opcode == Add )
|
||||
program[resLoc] = operandA + operandB;
|
||||
else if ( opcode == Multiply )
|
||||
program[resLoc] = operandA * operandB;
|
||||
else if ( opcode == End )
|
||||
{
|
||||
std::cout << "Program end" << std::endl << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
std::cout << "Program end state : ";
|
||||
for ( auto& i : program )
|
||||
std::cout << i << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
}
|
||||
BIN
2019/2ndDay/challenge1.exe
Normal file
BIN
2019/2ndDay/challenge1.exe
Normal file
Binary file not shown.
80
2019/2ndDay/challenge2.cpp
Normal file
80
2019/2ndDay/challenge2.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<int> tokenise( std::string input )
|
||||
{
|
||||
std::stringstream ssInput( input );
|
||||
std::vector<int> stream;
|
||||
|
||||
std::string s;
|
||||
while( std::getline( ssInput, s, ',' ) ) {
|
||||
stream.push_back( std::stoi( s ) );
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
enum Opcodes
|
||||
{
|
||||
Add = 1,
|
||||
Multiply = 2,
|
||||
End = 99
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
|
||||
std::ifstream infile( argv[1] );
|
||||
|
||||
std::string input;
|
||||
std::getline( infile, input );
|
||||
|
||||
std::vector<int> program;
|
||||
|
||||
for ( int noun = 0; noun < 100; noun++ )
|
||||
for ( int verb = 0; verb < 100; verb++ )
|
||||
{
|
||||
|
||||
program = tokenise( input );
|
||||
|
||||
// Noun
|
||||
program[1] = noun;
|
||||
// Verb
|
||||
program[2] = verb;
|
||||
|
||||
for ( int i = 0; i < program.size(); i += 4 )
|
||||
{
|
||||
|
||||
int opcode = program[i];
|
||||
|
||||
int operandA = program[program[i + 1]];
|
||||
int operandB = program[program[i + 2]];
|
||||
|
||||
int resLoc = program[i + 3];
|
||||
|
||||
if ( opcode == Add )
|
||||
program[resLoc] = operandA + operandB;
|
||||
else if ( opcode == Multiply )
|
||||
program[resLoc] = operandA * operandB;
|
||||
else if ( opcode == End )
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if ( program[0] == 19690720 )
|
||||
{
|
||||
std::cout << "Solution found : " << noun << " " << verb << std::endl;
|
||||
std::cout << "Solution answer : " << 100 * noun + verb << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::cout << std::endl << "Program end state : ";
|
||||
for ( auto& i : program )
|
||||
std::cout << i << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
}
|
||||
BIN
2019/2ndDay/challenge2.exe
Normal file
BIN
2019/2ndDay/challenge2.exe
Normal file
Binary file not shown.
1
2019/2ndDay/input.txt
Normal file
1
2019/2ndDay/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,5,19,23,1,23,5,27,2,27,10,31,1,5,31,35,2,35,6,39,1,6,39,43,2,13,43,47,2,9,47,51,1,6,51,55,1,55,9,59,2,6,59,63,1,5,63,67,2,67,13,71,1,9,71,75,1,75,9,79,2,79,10,83,1,6,83,87,1,5,87,91,1,6,91,95,1,95,13,99,1,10,99,103,2,6,103,107,1,107,5,111,1,111,13,115,1,115,13,119,1,13,119,123,2,123,13,127,1,127,6,131,1,131,9,135,1,5,135,139,2,139,6,143,2,6,143,147,1,5,147,151,1,151,2,155,1,9,155,0,99,2,14,0,0
|
||||
Reference in New Issue
Block a user