Skip to content

Commit

Permalink
Day 7-9 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-ardini committed Dec 31, 2019
1 parent beedc3c commit 2c8f925
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 59 deletions.
70 changes: 43 additions & 27 deletions 7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const IMM_MODE = 1;
class Amplifier {
pos: number = 0;
output: number = 0;
numbers: Array<number>;
numbers: number[];
halted: boolean;

constructor(numbers: Array<number>) {
constructor(numbers: number[]) {
this.pos = 0;
this.output = 0;
this.halted = false;
Expand All @@ -30,7 +30,7 @@ class Amplifier {
return this.halted;
}

solve(inputs: Array<number>): number {
solve(inputs: number[]): number {
let inputPos = 0;
while (true) {
const op = this.numbers[this.pos] % 100;
Expand Down Expand Up @@ -124,41 +124,57 @@ class Amplifier {
}
}

export async function solve(): Promise<number> {
const lines = await readlines('./data/7.txt');
const numbers: Array<number> = lines[0].split(',').map(Number);
function findMaxSignal(program: number[], part2: boolean): number {
let maxOut = -Infinity;
for (let phaseA = 5; phaseA <= 9; ++phaseA) {
for (let phaseB = 5; phaseB <= 9; ++phaseB) {
for (let phaseC = 5; phaseC <= 9; ++phaseC) {
for (let phaseD = 5; phaseD <= 9; ++phaseD) {
for (let phaseE = 5; phaseE <= 9; ++phaseE) {
let phases: Set<number> = new Set([phaseA, phaseB, phaseC, phaseD, phaseE]);
let min = part2 ? 5 : 0;
for (let phaseA = min; phaseA < min + 5; ++phaseA) {
for (let phaseB = min; phaseB < min + 5; ++phaseB) {
for (let phaseC = min; phaseC < min + 5; ++phaseC) {
for (let phaseD = min; phaseD < min + 5; ++phaseD) {
for (let phaseE = min; phaseE < min + 5; ++phaseE) {
let phases = new Set([phaseA, phaseB, phaseC, phaseD, phaseE]);
if (phases.size != 5) {
continue;
}
let ampA = new Amplifier([...numbers]);
let ampB = new Amplifier([...numbers]);
let ampC = new Amplifier([...numbers]);
let ampD = new Amplifier([...numbers]);
let ampE = new Amplifier([...numbers]);
let ampA = new Amplifier([...program]);
let ampB = new Amplifier([...program]);
let ampC = new Amplifier([...program]);
let ampD = new Amplifier([...program]);
let ampE = new Amplifier([...program]);
let input = 0;
let first = true;
while (!ampE.isHalted()) {
input = ampA.solve(first ? [phaseA, input] : [input]);
input = ampB.solve(first ? [phaseB, input] : [input]);
input = ampC.solve(first ? [phaseC, input] : [input]);
input = ampD.solve(first ? [phaseD, input] : [input]);
input = ampE.solve(first ? [phaseE, input] : [input]);
first = false;
}
if (input > maxOut) {
maxOut = input;
if (part2) {
while (!ampE.isHalted()) {
input = ampA.solve(first ? [phaseA, input] : [input]);
input = ampB.solve(first ? [phaseB, input] : [input]);
input = ampC.solve(first ? [phaseC, input] : [input]);
input = ampD.solve(first ? [phaseD, input] : [input]);
input = ampE.solve(first ? [phaseE, input] : [input]);
first = false;
}
if (input > maxOut) {
maxOut = input;
}
} else {
input = ampA.solve([phaseA, input]);
input = ampB.solve([phaseB, input]);
input = ampC.solve([phaseC, input]);
input = ampD.solve([phaseD, input]);
input = ampE.solve([phaseE, input]);
if (input > maxOut) {
maxOut = input;
}
}
}
}
}
}
}
return maxOut;
}

export async function solve(): Promise<number> {
const lines = await readlines('./data/7.txt');
const numbers: number[] = lines[0].split(',').map(Number);
return findMaxSignal(numbers, true);
}
43 changes: 25 additions & 18 deletions 8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,36 @@ const TRANSPARENT = 2;
const W = 25;
const H = 6;

function getImage(input: string): Array<string> {
function getImage(input: string, part2: boolean): number[] {
let min0 = Infinity;
let min1 = Infinity;
let min2 = Infinity;
let image: Array<string> = new Array(25 * 6);
let image: number[] = new Array(25 * 6);
for (let x = 0; x < W; ++x) {
for (let y = 0; y < H; ++y) {
image[x + y*W] = '3';
image[x + y*W] = 3;
}
}
console.log(image);
while (input) {
let digits0: number = 0;
let digits1: number = 0;
let digits2: number = 0;
for (let y = 0; y < H; ++y) {
for (let x = 0; x < W; ++x) {
let char = input.charAt(x + y * W);
console.log(x+y*W);
//console.log(char);
if (char == '0') {
if (image[x + y * W] == '2' || image[x + y * W] == '3') {
image[x + y * W] = char;
if (char === '0') {
if (image[x + y * W] === 2 || image[x + y * W] === 3) {
image[x + y * W] = Number(char);
}
++digits0;
} else if (char == '1') {
if (image[x + y * W] == '2' || image[x + y * W] == '3') {
image[x + y * W] = char;
} else if (char === '1') {
if (image[x + y * W] === 2 || image[x + y * W] == 3) {
image[x + y * W] = Number(char);
}
++digits1;
} else if (char == '2') {
if (image[x + y * W] == '3') {
image[x + y * W] = char;
} else if (char === '2') {
if (image[x + y * W] === 3) {
image[x + y * W] = Number(char);
}
++digits2;
}
Expand All @@ -52,11 +49,21 @@ function getImage(input: string): Array<string> {
}
input = input.substr(W * H);
}
return image;
return part2 ? image : [min0, min1, min2];
}

export async function solve(): Promise<string> {
const lines = await readlines('./data/8.txt');
let img = getImage(lines[0]);
return img.join('');
let part2 = true;
let img = getImage(lines[0], part2);
if (!part2) {
return String(img[1] * img[2]);
}

const drawImg = () => img.map((v, i) => {
let out = v === 0 ? ' ' : '#';
return i % W === W - 1 ? `${out}\n` : out;
}).join('');
console.log(drawImg());
return drawImg();
}
29 changes: 16 additions & 13 deletions 9.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const REL_MODE = 2;
class Amplifier {
pos: number = 0;
output: number = 0;
numbers: Array<number>;
numbers: number[];
halted: boolean;
relBase: number = 0;

constructor(numbers: Array<number>) {
constructor(numbers: number[]) {
this.pos = 0;
this.output = 0;
this.halted = false;
Expand All @@ -46,16 +46,16 @@ class Amplifier {
throw Error('invalid op');
}

getPs(mode: number, pos: number, count: number): Array<number> {
let out: Array<number> = [];
getPs(mode: number, pos: number, count: number): number[] {
let out: number[] = [];
for (let i = 1; i <= count; ++i) {
out.push(this.getP(mode % 10, pos + i));
mode = Math.floor(mode / 10);
}
return out;
}

solve(inputs: Array<number>): number {
solve(inputs: number[]): number {
while (true) {
const op = this.numbers[this.pos] % 100;
let mode = Math.floor(this.numbers[this.pos] / 100);
Expand Down Expand Up @@ -133,15 +133,18 @@ class Amplifier {
}
}

export async function solve(): Promise<string> {
const lines = await readlines('./data/9.txt');
const numbers: Array<number> = lines[0].split(',').map(Number);
// const numbers = [109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99];
const padded = numbers.concat(Array(1000000).fill(0));
let amp = new Amplifier([...padded]);
let out = '';
function runProgram(program: number[], part2: boolean): number {
let amp = new Amplifier([...program]);
let out = 0;
while (!amp.isHalted()) {
out += String(amp.solve([2])) + ', ';
out = amp.solve([part2 ? 2 : 1]);
}
return out;
}

export async function solve(): Promise<number> {
const lines = await readlines('./data/9.txt');
const numbers: number[] = lines[0].split(',').map(Number);
const padded = numbers.concat(Array(1000000).fill(0));
return runProgram(padded, true);
}
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import {solve} from './6';
import {solve} from './9';

const app = express();

Expand Down

0 comments on commit 2c8f925

Please sign in to comment.