Skip to content

Commit b420cbf

Browse files
committed
Working part one
1 parent b962b05 commit b420cbf

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

2019/7/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Answers
22

3-
| Part 1 | Part 2 |
4-
|-----------|-----------|
5-
| ` ` | ` ` |
3+
| Part 1 | Part 2 |
4+
|---------|-----------|
5+
| `87138` | ` ` |
66

77
## --- Day 7: Amplification Circuit ---
88

2019/7/computer.js 2019/7/circuit.js

+42-17
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,33 @@ const POSITION_MODE = '0';
1212
const IMMEDIATE_MODE = '1';
1313

1414
class Computer {
15-
constructor(input, input_value = 1) {
16-
this.original_input = input.slice(0);
17-
this.input = input.slice(0);
15+
constructor(memory, inputs, clone_memory = false) {
16+
this.original_memory = clone_memory && memory.slice(0);
17+
this.memory = memory.slice(0);
1818
this.pointer = 0;
1919

20-
this.input_value = input_value;
21-
this.output_value = null;
20+
this.inputs = Array.isArray(inputs) ? inputs.slice(0) : [inputs];
21+
this.outputs = [];
2222

2323
this.OPS = {
2424
[ADD]: {
2525
name: ADD,
2626
params: 3,
27-
fn: (a, b, c) => (this.input[c] = a + b),
27+
fn: (a, b, c) => (this.memory[c] = a + b),
2828
write: true,
2929
},
3030

3131
[MUL]: {
3232
name: MUL,
3333
params: 3,
34-
fn: (a, b, c) => (this.input[c] = a * b),
34+
fn: (a, b, c) => (this.memory[c] = a * b),
3535
write: true,
3636
},
3737

3838
[INP]: {
3939
name: INP,
4040
params: 1,
41-
fn: a => (this.input[a] = this.input_value),
41+
fn: a => (this.memory[a] = this.inputs.shift()),
4242
write: true,
4343
},
4444

@@ -83,14 +83,14 @@ class Computer {
8383
[LTH]: {
8484
name: LTH,
8585
params: 3,
86-
fn: (a, b, c) => (this.input[c] = a < b ? 1 : 0),
86+
fn: (a, b, c) => (this.memory[c] = a < b ? 1 : 0),
8787
write: true,
8888
},
8989

9090
[EQU]: {
9191
name: EQU,
9292
params: 3,
93-
fn: (a, b, c) => (this.input[c] = a === b ? 1 : 0),
93+
fn: (a, b, c) => (this.memory[c] = a === b ? 1 : 0),
9494
write: true,
9595
},
9696
};
@@ -103,10 +103,12 @@ class Computer {
103103
this.runOp(op);
104104
op = this.parseOp();
105105
}
106+
107+
return this.outputs;
106108
}
107109

108110
parseOp() {
109-
let temp_op = String(this.input[this.pointer]).padStart(2, '0');
111+
let temp_op = String(this.memory[this.pointer]).padStart(2, '0');
110112
let op = this.OPS[temp_op.substr(-2, 2)];
111113

112114
let full_op = temp_op.padStart(op.params + 2, '0');
@@ -128,11 +130,11 @@ class Computer {
128130
let values = [];
129131
for (let i = 0; i < modes.length; i++) {
130132
let mode = modes[i];
131-
let value = this.input[this.pointer + i];
133+
let value = this.memory[this.pointer + i];
132134

133135
const can_switch_to_position = !write || i < modes.length - 1;
134136
if (can_switch_to_position && mode === POSITION_MODE) {
135-
value = this.input[value];
137+
value = this.memory[value];
136138
}
137139

138140
values.push(value);
@@ -147,9 +149,7 @@ class Computer {
147149
}
148150

149151
output(v) {
150-
if (v !== 0) {
151-
console.log(v);
152-
}
152+
this.outputs.push(v);
153153
}
154154

155155
// For debugging
@@ -158,4 +158,29 @@ class Computer {
158158
// }
159159
}
160160

161-
module.exports = Computer;
161+
class Circuit {
162+
constructor(memory, phase_settings) {
163+
this.memory = memory;
164+
this.phase_settings = phase_settings;
165+
}
166+
167+
run(return_last_output = true) {
168+
// First computer gets 0 as its second input
169+
let last_computer_output = [0];
170+
for (let phase_seting of this.phase_settings) {
171+
let computer = new Computer(this.memory, [
172+
phase_seting,
173+
...last_computer_output,
174+
]);
175+
last_computer_output = computer.run();
176+
}
177+
178+
179+
return return_last_output ? last_computer_output.pop() : last_computer_output;
180+
}
181+
}
182+
183+
module.exports = {
184+
Computer,
185+
Circuit,
186+
};

2019/7/part-one.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1+
const G = require('generatorics');
12
const { input } = require('./input');
2-
const Computer = require('./computer');
3+
const { Circuit } = require('./circuit');
4+
5+
let max_output = Number.MIN_SAFE_INTEGER;
6+
for (var phase_settings of G.permutation([0, 1, 2, 3, 4])) {
7+
let circuit = new Circuit(input, phase_settings);
8+
let output = circuit.run();
9+
if (output > max_output) {
10+
max_output = output;
11+
}
12+
}
13+
14+
console.log(max_output);

0 commit comments

Comments
 (0)