2019 day 7, final version + update RESULTS.txt
This commit is contained in:
@@ -82,3 +82,17 @@ aoc-c : res=453028
|
||||
aoc-c : res=562
|
||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||
context-switch: 0+1, page-faults: 0+417
|
||||
|
||||
=========================================
|
||||
================= day07 =================
|
||||
=========================================
|
||||
|
||||
+++++++++++++++++ part 1
|
||||
aoc-c : res=65464
|
||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||
context-switch: 0+1, page-faults: 0+94
|
||||
|
||||
+++++++++++++++++ part 2
|
||||
aoc-c : res=1518124
|
||||
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||
context-switch: 0+1, page-faults: 0+94
|
||||
|
@@ -1,145 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en-us">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Day 7 - Advent of Code 2019</title>
|
||||
<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
|
||||
<link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'/>
|
||||
<link rel="stylesheet" type="text/css" href="/static/style.css?28"/>
|
||||
<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
|
||||
<link rel="shortcut icon" href="/favicon.png"/>
|
||||
<script>window.addEventListener('click', function(e,s,r){if(e.target.nodeName==='CODE'&&e.detail===3){s=window.getSelection();s.removeAllRanges();r=document.createRange();r.selectNodeContents(e.target);s.addRange(r);}});</script>
|
||||
</head><!--
|
||||
|
||||
|
||||
|
||||
|
||||
Oh, hello! Funny seeing you here.
|
||||
|
||||
I appreciate your enthusiasm, but you aren't going to find much down here.
|
||||
There certainly aren't clues to any of the puzzles. The best surprises don't
|
||||
even appear in the source until you unlock them for real.
|
||||
|
||||
Please be careful with automated requests; I'm not a massive company, and I can
|
||||
only take so much traffic. Please be considerate so that everyone gets to play.
|
||||
|
||||
If you're curious about how Advent of Code works, it's running on some custom
|
||||
Perl code. Other than a few integrations (auth, analytics, social media), I
|
||||
built the whole thing myself, including the design, animations, prose, and all
|
||||
of the puzzles.
|
||||
|
||||
The puzzles are most of the work; preparing a new calendar and a new set of
|
||||
puzzles each year takes all of my free time for 4-5 months. A lot of effort
|
||||
went into building this thing - I hope you're enjoying playing it as much as I
|
||||
enjoyed making it for you!
|
||||
|
||||
If you'd like to hang out, I'm @ericwastl on Twitter.
|
||||
|
||||
- Eric Wastl
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-->
|
||||
<body>
|
||||
<header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2019/about">[About]</a></li><li><a href="/2019/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2019/settings">[Settings]</a></li><li><a href="/2019/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Bruno Raoult <span class="star-count">12*</span></div></div><div><h1 class="title-event"> <span class="title-event-wrap">/^</span><a href="/2019">2019</a><span class="title-event-wrap">$/</span></h1><nav><ul><li><a href="/2019">[Calendar]</a></li><li><a href="/2019/support">[AoC++]</a></li><li><a href="/2019/sponsors">[Sponsors]</a></li><li><a href="/2019/leaderboard">[Leaderboard]</a></li><li><a href="/2019/stats">[Stats]</a></li></ul></nav></div></header>
|
||||
|
||||
<div id="sidebar">
|
||||
<div id="sponsor"><div class="quiet">Our <a href="/2019/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://smartystreets.com/aoc" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">SmartyStreets</a> - Ridiculously -- Fast -------- Address ------ Verification --</div></div>
|
||||
</div><!--/sidebar-->
|
||||
|
||||
<main>
|
||||
<article class="day-desc"><h2>--- Day 7: Amplification Circuit ---</h2><p>Based on the navigational maps, you're going to need to send more power to your ship's thrusters to reach Santa in time. To do this, you'll need to configure a series of <a href="https://en.wikipedia.org/wiki/Amplifier">amplifiers</a> already installed on the ship.</p>
|
||||
<p>There are five <span title="As you can see, I know exactly how rockets work.">amplifiers connected in series</span>; each one receives an input signal and produces an output signal. They are connected such that the first amplifier's output leads to the second amplifier's input, the second amplifier's output leads to the third amplifier's input, and so on. The first amplifier's input value is <code>0</code>, and the last amplifier's output leads to your ship's thrusters.</p>
|
||||
<pre><code> O-------O O-------O O-------O O-------O O-------O
|
||||
0 ->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-> (to thrusters)
|
||||
O-------O O-------O O-------O O-------O O-------O
|
||||
</code></pre>
|
||||
<p>The Elves have sent you some <em>Amplifier Controller Software</em> (your puzzle input), a program that should run on your <a href="5">existing Intcode computer</a>. Each amplifier will need to run a copy of the program.</p>
|
||||
<p>When a copy of the program starts running on an amplifier, it will first use an input instruction to ask the amplifier for its current <em>phase setting</em> (an integer from <code>0</code> to <code>4</code>). Each phase setting is used <em>exactly once</em>, but the Elves can't remember which amplifier needs which phase setting.</p>
|
||||
<p>The program will then call another input instruction to get the amplifier's input signal, compute the correct output signal, and supply it back to the amplifier with an output instruction. (If the amplifier has not yet received an input signal, it waits until one arrives.)</p>
|
||||
<p>Your job is to <em>find the largest output signal that can be sent to the thrusters</em> by trying every possible combination of phase settings on the amplifiers. Make sure that memory is not shared or reused between copies of the program.</p>
|
||||
<p>For example, suppose you want to try the phase setting sequence <code>3,1,2,4,0</code>, which would mean setting amplifier <code>A</code> to phase setting <code>3</code>, amplifier <code>B</code> to setting <code>1</code>, <code>C</code> to <code>2</code>, <code>D</code> to <code>4</code>, and <code>E</code> to <code>0</code>. Then, you could determine the output signal that gets sent from amplifier <code>E</code> to the thrusters with the following steps:</p>
|
||||
<ul>
|
||||
<li>Start the copy of the amplifier controller software that will run on amplifier <code>A</code>. At its first input instruction, provide it the amplifier's phase setting, <code>3</code>. At its second input instruction, provide it the input signal, <code>0</code>. After some calculations, it will use an output instruction to indicate the amplifier's output signal.</li>
|
||||
<li>Start the software for amplifier <code>B</code>. Provide it the phase setting (<code>1</code>) and then whatever output signal was produced from amplifier <code>A</code>. It will then produce a new output signal destined for amplifier <code>C</code>.</li>
|
||||
<li>Start the software for amplifier <code>C</code>, provide the phase setting (<code>2</code>) and the value from amplifier <code>B</code>, then collect its output signal.</li>
|
||||
<li>Run amplifier <code>D</code>'s software, provide the phase setting (<code>4</code>) and input value, and collect its output signal.</li>
|
||||
<li>Run amplifier <code>E</code>'s software, provide the phase setting (<code>0</code>) and input value, and collect its output signal.</li>
|
||||
</ul>
|
||||
<p>The final output signal from amplifier <code>E</code> would be sent to the thrusters. However, this phase setting sequence may not have been the best one; another sequence might have sent a higher signal to the thrusters.</p>
|
||||
<p>Here are some example programs:</p>
|
||||
<ul>
|
||||
<li><p>Max thruster signal <em><code>43210</code></em> (from phase setting sequence <code>4,3,2,1,0</code>):</p><pre><code>3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0</code></pre></li>
|
||||
<li><p>Max thruster signal <em><code>54321</code></em> (from phase setting sequence <code>0,1,2,3,4</code>):</p><pre><code>3,23,3,24,1002,24,10,24,1002,23,-1,23,<br/>101,5,23,23,1,24,23,23,4,23,99,0,0</code></pre></li>
|
||||
<li><p>Max thruster signal <em><code>65210</code></em> (from phase setting sequence <code>1,0,4,3,2</code>):</p><pre><code>3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,<br/>1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0</code></pre></li>
|
||||
</ul>
|
||||
<p>Try every combination of phase settings on the amplifiers. <em>What is the highest signal that can be sent to the thrusters?</em></p>
|
||||
</article>
|
||||
<p>To begin, <a href="7/input" target="_blank">get your puzzle input</a>.</p>
|
||||
<form method="post" action="7/answer"><input type="hidden" name="level" value="1"/><p>Answer: <input type="text" name="answer" autocomplete="off"/> <input type="submit" value="[Submit]"/></p></form>
|
||||
<p>You can also <span class="share">[Share<span class="share-content">on
|
||||
<a href="https://twitter.com/intent/tweet?text=%22Amplification+Circuit%22+%2D+Day+7+%2D+Advent+of+Code+2019&url=https%3A%2F%2Fadventofcode%2Ecom%2F2019%2Fday%2F7&related=ericwastl&hashtags=AdventOfCode" target="_blank">Twitter</a>
|
||||
<a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' && mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=%22Amplification+Circuit%22+%2D+Day+7+%2D+Advent+of+Code+2019+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2019%2Fday%2F7'}else{return false;}" target="_blank">Mastodon</a
|
||||
></span>]</span> this puzzle.</p>
|
||||
</main>
|
||||
|
||||
<!-- ga -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-69522494-1', 'auto');
|
||||
ga('set', 'anonymizeIp', true);
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
<!-- /ga -->
|
||||
</body>
|
||||
</html>
|
@@ -21,26 +21,24 @@
|
||||
#include "list.h"
|
||||
#include "pool.h"
|
||||
|
||||
/* operators codes
|
||||
*/
|
||||
typedef enum {
|
||||
ADD = 1, MUL = 2,
|
||||
INP = 3, OUT = 4,
|
||||
JMP_T = 5, JMP_F = 6,
|
||||
SET_LT = 7, SET_EQ = 8,
|
||||
HLT = 99
|
||||
ADD = 1, MUL = 2, /* CALC: add and mult */
|
||||
INP = 3, OUT = 4, /* I/O: input and output value */
|
||||
JMP_T = 5, JMP_F = 6, /* JUMPS: jump if true / if false */
|
||||
SET_LT = 7, SET_EQ = 8, /* COND SETS: set if true/false */
|
||||
HLT = 99 /* HALT */
|
||||
} opcode_t;
|
||||
|
||||
/**
|
||||
* ops - array of op-codes, mnemo, and number of parameters
|
||||
* @op: An integer, the opcode
|
||||
* @nargs: Opcode number of parameters (unused)
|
||||
* @jump: Next instruction (usually @nargs + 1)
|
||||
* @mnemo: Opcode mnemo (unused, for debug)
|
||||
* @op: An integer, the opcode
|
||||
* @length: Next instruction offset
|
||||
*/
|
||||
typedef struct {
|
||||
int op;
|
||||
u8 nargs;
|
||||
u8 jump;
|
||||
char *mnemo;
|
||||
u8 length;
|
||||
} ops_t;
|
||||
|
||||
typedef struct input {
|
||||
@@ -48,26 +46,20 @@ typedef struct input {
|
||||
struct list_head list;
|
||||
} input_t;
|
||||
|
||||
|
||||
#define MAXOPS 1024
|
||||
typedef struct {
|
||||
int length; /* total program length */
|
||||
int cur; /* current position */
|
||||
struct list_head input;
|
||||
//int input[MAXINPUT]; /* input */
|
||||
struct list_head input; /* process input queue */
|
||||
int mem [MAXOPS]; /* should really be dynamic */
|
||||
} program_t;
|
||||
|
||||
static ops_t ops[] = {
|
||||
[ADD] = { ADD, 3, 4, __stringify(ADD) },
|
||||
[MUL] = { MUL, 3, 4, __stringify(MUL) },
|
||||
[INP] = { INP, 1, 2, __stringify(INP) },
|
||||
[OUT] = { OUT, 1, 2, __stringify(OUT) },
|
||||
[JMP_T] = { JMP_T, 2, 3, __stringify(JMP_T) },
|
||||
[JMP_F] = { JMP_F, 2, 3, __stringify(JMP_F) },
|
||||
[SET_LT] = { SET_LT, 3, 4, __stringify(SET_LT) },
|
||||
[SET_EQ] = { SET_EQ, 3, 4, __stringify(SET_EQ) },
|
||||
[HLT] = { HLT, 0, 1, __stringify(HLT) }
|
||||
[ADD] = { ADD, 4 }, [MUL] = { MUL, 4 },
|
||||
[INP] = { INP, 2 }, [OUT] = { OUT, 2 },
|
||||
[JMP_T] = { JMP_T, 3 }, [JMP_F] = { JMP_F, 3 },
|
||||
[SET_LT] = { SET_LT, 4 }, [SET_EQ] = { SET_EQ, 4 },
|
||||
[HLT] = { HLT, 1 }
|
||||
};
|
||||
|
||||
|
||||
@@ -82,51 +74,9 @@ static int _flag_pow10[] = {1, 100, 1000, 10000};
|
||||
INDIRECT(p, n + i) = val; } \
|
||||
while (0)
|
||||
|
||||
static int print_opcode(program_t *p, int pos)
|
||||
{
|
||||
int op = OP(p, pos);
|
||||
if (ops[op].op) {
|
||||
int nargs = ops[op].nargs;
|
||||
log(3, "%03d [%2d][%6s]\t", pos, op, ops[op].mnemo);
|
||||
for (int i = 1; i <= nargs; ++i) {
|
||||
int direct = p->mem[pos + i];
|
||||
//p->mem[pos + i].param;
|
||||
if (i > 1)
|
||||
log(3, ", ");
|
||||
if (i < 3 && !ISDIRECT(p, pos, i)) {
|
||||
log(3, "*[%d]=", direct);
|
||||
int val = p->mem[direct];
|
||||
log(3, "%d", val);
|
||||
} else {
|
||||
log(3, "%d", direct);
|
||||
}
|
||||
}
|
||||
log(3, "\n");
|
||||
return nargs;
|
||||
} else {
|
||||
log(3, "%03d \t", pos);
|
||||
log(3, "%d\n", p->mem[pos]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
static void print_program_codes(program_t *p)
|
||||
{
|
||||
log(3, "program codes: length=%d\n", p->length);
|
||||
for (int i = 0; i < p->length; ++i)
|
||||
log(3, "%d ", p->mem[i]);
|
||||
log(3, "\n");
|
||||
}
|
||||
static void print_program(program_t *p)
|
||||
{
|
||||
print_program_codes(p);
|
||||
log(3, "program: length=%d\n", p->length);
|
||||
for (int i = 0; i < p->length; ++i) {
|
||||
i += print_opcode(p, i);
|
||||
}
|
||||
}
|
||||
|
||||
static pool_t *pool_input;
|
||||
static int prg_add_input(program_t *prg, int in)
|
||||
static __always_inline int prg_add_input(program_t *prg, int in)
|
||||
{
|
||||
input_t *input = pool_get(pool_input);
|
||||
input->val = in;
|
||||
@@ -134,7 +84,7 @@ static int prg_add_input(program_t *prg, int in)
|
||||
return in;
|
||||
}
|
||||
|
||||
static int prg_get_input(program_t *prg, int *out)
|
||||
static __always_inline int prg_get_input(program_t *prg, int *out)
|
||||
{
|
||||
input_t *input = list_first_entry_or_null(&prg->input, input_t, list);
|
||||
if (!input)
|
||||
@@ -144,15 +94,6 @@ static int prg_get_input(program_t *prg, int *out)
|
||||
pool_add(pool_input, input);
|
||||
return 1;
|
||||
}
|
||||
static void prg_print_input(program_t *prg, int num)
|
||||
{
|
||||
input_t *cur;
|
||||
printf("prg[%d].input: ", num);
|
||||
list_for_each_entry(cur, &prg->input, list) {
|
||||
printf("%d ", cur->val);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* permute - get next permutation of an array of integers
|
||||
@@ -187,12 +128,7 @@ static int permute_next(int len, int *array)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void dup_program(program_t *from, program_t *to)
|
||||
{
|
||||
*to = *from;
|
||||
}
|
||||
|
||||
static int run(int part, program_t *p, int *end)
|
||||
static int run(program_t *p, int *end)
|
||||
{
|
||||
int out = -1;
|
||||
while (1) {
|
||||
@@ -210,13 +146,14 @@ static int run(int part, program_t *p, int *end)
|
||||
poke(p, p->cur, 3, peek(p, p->cur, 1) * peek(p, p->cur, 2));
|
||||
break;
|
||||
case INP:
|
||||
if (prg_get_input(p, &input)) {
|
||||
printf("op=%d\nINP %d\n", op, input);
|
||||
if (prg_get_input(p, &input))
|
||||
poke(p, p->cur, 1, input);
|
||||
} else {
|
||||
printf("NO INPUT\n");
|
||||
return out;
|
||||
}
|
||||
else
|
||||
/* we need an input which is not yet avalable, so we need
|
||||
* to put the program in "waiting mode": We stop it (and
|
||||
* return output value) without setting end flag.
|
||||
*/
|
||||
goto sleep;
|
||||
break;
|
||||
case OUT:
|
||||
out = peek(p, p->cur, 1);
|
||||
@@ -237,10 +174,11 @@ static int run(int part, program_t *p, int *end)
|
||||
break;
|
||||
case HLT:
|
||||
*end = 1;
|
||||
sleep:
|
||||
return out;
|
||||
}
|
||||
if (p->cur == cur)
|
||||
p->cur += ops[op].jump;
|
||||
p->cur += ops[op].length;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,7 +197,7 @@ static int usage(char *prg)
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int phase1[] = {0, 1, 2, 3, 4}, phase2[] = {5, 6, 7, 8, 9}, *phase;
|
||||
int opt, part = 1;
|
||||
int opt, max = 0, part = 1;
|
||||
program_t p = { 0 }, prg[5];
|
||||
|
||||
while ((opt = getopt(ac, av, "d:p:o:")) != -1) {
|
||||
@@ -288,37 +226,32 @@ int main(int ac, char **av)
|
||||
|
||||
phase = part == 1? phase1: phase2;
|
||||
parse(&p);
|
||||
print_program(&p);
|
||||
int out, max = 0, end;
|
||||
|
||||
do {
|
||||
out = 0;
|
||||
int out = 0, end = 0;
|
||||
/* reset programs initial state, and add phase to their input
|
||||
*/
|
||||
for (unsigned i = 0; i < ARRAY_SIZE(prg); ++i) {
|
||||
printf("creating array %d\n", i);
|
||||
dup_program(&p, &prg[i]);
|
||||
prg[i] = p;
|
||||
INIT_LIST_HEAD(&prg[i].input);
|
||||
prg_add_input(&prg[i], phase[i]);
|
||||
}
|
||||
|
||||
// while (1) {
|
||||
end = 0;
|
||||
/* run the 5 processes in order (0, 1, 2, 3, 4, 0, 1, etc...),
|
||||
* until end flag is set by the process 4 (HLT instruction)
|
||||
*/
|
||||
while (!end) {
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
/* add last process output in current process input queue
|
||||
*/
|
||||
prg_add_input(&prg[i], out);
|
||||
prg_print_input(&prg[i], i);
|
||||
//prg[i].input[prg[i].lastinput++] = phase[i];
|
||||
//prg[i].input[prg[i].lastinput++] = out;
|
||||
printf("running program %d\n", i);
|
||||
out = run(part, &prg[i], &end);
|
||||
printf("end program %d out=%d end=%d\n", i, out, end);
|
||||
out = run(&prg[i], &end);
|
||||
}
|
||||
}
|
||||
if (out > max) {
|
||||
max = out;
|
||||
printf("new max: %c%c%c%c%c out=%d max=%d\n", phase[0] + '0',
|
||||
phase[1] + '0', phase[2] + '0', phase[3] + '0', phase[4] + '0',
|
||||
out, max);
|
||||
}
|
||||
max = max(max, out);
|
||||
} while (permute_next(5, phase));
|
||||
|
||||
printf("%s : res=%d\n", *av, max);
|
||||
pool_destroy(pool_input);
|
||||
exit(0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user