mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 7 part 1.
This commit is contained in:
@@ -1,8 +1,31 @@
|
||||
#include <iostream>
|
||||
#include "days.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace {
|
||||
int simulate(const std::vector<int>& program, const std::vector<int> phases) {
|
||||
int state = 0;
|
||||
for (int phase : phases) {
|
||||
auto copy = program;
|
||||
auto result = aoc2019::run_intcode(copy, { phase, state });
|
||||
state = result.front();
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
void aoc2019::day07_part1(std::istream &input, std::ostream &output) {
|
||||
output << "Not implemented\n";
|
||||
const auto program = aoc2019::read_intcode(input);
|
||||
std::vector<int> phases{0, 1, 2, 3, 4};
|
||||
|
||||
int best = 0;
|
||||
|
||||
do {
|
||||
best = std::max(simulate(program, phases), best);
|
||||
} while (std::next_permutation(phases.begin(), phases.end()));
|
||||
|
||||
output << best << std::endl;
|
||||
}
|
||||
|
||||
void aoc2019::day07_part2(std::istream &input, std::ostream &output) {
|
||||
|
||||
1
2019/tests/samples/07-1-1.in
Normal file
1
2019/tests/samples/07-1-1.in
Normal file
@@ -0,0 +1 @@
|
||||
3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0
|
||||
1
2019/tests/samples/07-1-1.out
Normal file
1
2019/tests/samples/07-1-1.out
Normal file
@@ -0,0 +1 @@
|
||||
43210
|
||||
2
2019/tests/samples/07-1-2.in
Normal file
2
2019/tests/samples/07-1-2.in
Normal file
@@ -0,0 +1,2 @@
|
||||
3,23,3,24,1002,24,10,24,1002,23,-1,23,
|
||||
101,5,23,23,1,24,23,23,4,23,99,0,0
|
||||
1
2019/tests/samples/07-1-2.out
Normal file
1
2019/tests/samples/07-1-2.out
Normal file
@@ -0,0 +1 @@
|
||||
54321
|
||||
2
2019/tests/samples/07-1-3.in
Normal file
2
2019/tests/samples/07-1-3.in
Normal file
@@ -0,0 +1,2 @@
|
||||
3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,
|
||||
1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0
|
||||
1
2019/tests/samples/07-1-3.out
Normal file
1
2019/tests/samples/07-1-3.out
Normal file
@@ -0,0 +1 @@
|
||||
65210
|
||||
Reference in New Issue
Block a user