Skip to content

Commit

Permalink
Day 1-3 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-ardini committed Dec 30, 2019
1 parent 082962c commit 9d48f32
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 146 deletions.
26 changes: 11 additions & 15 deletions 1.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import readlines from './util/readlines';

function massToFuelRecursive(mass: number): number {
return fuelToFuel(requiredFuel(mass));
}

function fuelToFuel(fuel: number): number {
return fuel <= 0 ? 0 : fuel + fuelToFuel(requiredFuel(fuel));
}

function massToFuelIterative(mass: number): number {
function massToFuel(mass: number, part2: boolean): number {
let remainingFuel = requiredFuel(mass);
let totalFuel = 0;
while (remainingFuel > 0) {
totalFuel += remainingFuel;
remainingFuel = requiredFuel(remainingFuel);
if (part2) {
while (remainingFuel > 0) {
totalFuel += remainingFuel;
remainingFuel = requiredFuel(remainingFuel);
}
return totalFuel;
} else {
return remainingFuel;
}
return totalFuel;
}

function requiredFuel(massOrFuel: number): number {
return Math.floor(massOrFuel / 3.0) - 2;
}

export async function day1(): Promise<number> {
export async function solve(): Promise<number> {
const lines = await readlines('./data/1.txt');
const masses: Array<number> = lines.map(Number);
return masses.map(massToFuelRecursive).reduce((a, b) => a + b);
return masses.map((m) => massToFuel(m, true)).reduce((a, b) => a + b);
}
2 changes: 1 addition & 1 deletion 10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function part2(asteroids: Array<Array<number>>, pairs: Array<[number, number]>):
return c * 100 + r;
}

export async function day10(): Promise<string> {
export async function solve(): Promise<string> {
const lines = await readlines('./data/10.txt');
let asteroids: Array<Array<number>> = [];
for (let line of lines) {
Expand Down
2 changes: 1 addition & 1 deletion 11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class Robot {
}
}

export async function day11(): Promise<string> {
export async function solve(): Promise<string> {
const lines = await readlines('./data/11.txt');
const numbers: Array<number> = lines[0].split(',').map(Number);
const padded = numbers.concat(Array(1000000).fill(0));
Expand Down
6 changes: 3 additions & 3 deletions 12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function energy(moon: Vec3, v: Vec3) {
return ke * pe;
}

function solve(moons: Array<Vec3>): number {
function part2(moons: Array<Vec3>): number {
const startMoons: Array<Vec3> = [...moons];
const vs: Array<Vec3> = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
const cycleLengths: Array<number> = [-1, -1, -1];
Expand All @@ -76,7 +76,7 @@ function solve(moons: Array<Vec3>): number {
return cycleLengths.reduce((a, b) => lcm(a, b));
}

export async function day12(): Promise<string> {
export async function solve(): Promise<string> {
const moons: Array<Vec3> = [[-19, -4, 2],
[-9, 8, -16],
[-4, 5, -11],
Expand All @@ -87,5 +87,5 @@ export async function day12(): Promise<string> {
[4, -8, 8],
[3, 5, -1]];
*/
return String(solve(moons));
return String(part2(moons));
}
43 changes: 25 additions & 18 deletions 2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ const ADD = 1;
const MULT = 2;
const HALT = 99;

function solve(numbers: Array<number>): Array<number> {
function run(numbers: number[]): number[] {
let pos = 0;
while (true) {
const op = numbers[pos];
switch (op) {
case ADD:
const sum = numbers[numbers[pos + 1]] + numbers[numbers[pos + 2]];
numbers[numbers[pos + 3]] = sum;
break;
case MULT:
const product = numbers[numbers[pos + 1]] * numbers[numbers[pos + 2]];
numbers[numbers[pos + 3]] = product;
break;
case HALT:
return numbers;
case ADD:
const sum = numbers[numbers[pos + 1]] + numbers[numbers[pos + 2]];
numbers[numbers[pos + 3]] = sum;
break;
case MULT:
const product = numbers[numbers[pos + 1]] * numbers[numbers[pos + 2]];
numbers[numbers[pos + 3]] = product;
break;
case HALT:
return numbers;
}
pos += 4;
}
throw Error("Invalid program");
}

function findOutput(numbers: Array<number>, output: number): Array<number> {
function findOutput(numbers: number[], output: number): number[] {
let input = numbers.slice(0);
let noun = 0;
let verb = 0;
Expand All @@ -35,18 +35,25 @@ function findOutput(numbers: Array<number>, output: number): Array<number> {
let input = numbers.slice(0);
input[1] = noun;
input[2] = verb;
let program = solve(input);
let program = run(input);
if (program[0] == output) {
return program;
return program;
}
}
}
throw Error("Output is not possible");
}

export async function day2(): Promise<number> {
export async function solve(): Promise<number> {
const lines = await readlines('./data/2.txt');
const numbers: Array<number> = lines[0].split(',').map(Number);
const program = findOutput(numbers, 19690720);
return 100 * program[1] + program[2];
const numbers: number[] = lines[0].split(',').map(Number);
let part2 = true;
if (part2) {
const program = findOutput(numbers, 19690720);
return 100 * program[1] + program[2];
} else {
numbers[1] = 12;
numbers[2] = 2;
return run(numbers)[0];
}
}
152 changes: 56 additions & 96 deletions 3.ts
Original file line number Diff line number Diff line change
@@ -1,119 +1,79 @@
import readlines from './util/readlines';
import Map2d from './util/map2d';
import Vec2d from './util/vec2d';

/** <x, <y, steps>> */
const grid: Map<number, Map<number, number>> = new Map();
const INTERSECTION_INDICATOR = 1e8;

function initCell(m: Map<number, Map<number, number>>, x: number): Map<number, number> {
if (!m.has(x)) {
m.set(x, new Map());
}
return m.get(x) as Map<number, number>;
}

const HACK = 1e8;

function addV(m: Map<number, number>, k: number, v: number) {
if (m.has(k)) {
m.set(k, (m.get(k) as number) + v + HACK);
}
}

function setV(m: Map<number, number>, k: number, v: number) {
if (!m.has(k)) {
m.set(k, v);
function getDir(op: string): Vec2d {
switch (op) {
case 'L':
return new Vec2d(-1, 0);
case 'R':
return new Vec2d(1, 0);
case 'D':
return new Vec2d(0, -1);
case 'U':
return new Vec2d(0, 1);
}
throw Error("Invalid move");
}

function markMove([x, y]: [number, number], move: string, baseDist: number, first: boolean): [number, number] {
function markMove(grid: Map2d<number>, start: Vec2d, move: string, baseDistance: number, firstWire: boolean): Vec2d {
const op = move.charAt(0);
const dist = Number(move.substr(1));
switch (op) {
case 'R': {
for (let i = 1; i <= dist; ++i) {
let yToVal = initCell(grid, x + i);
if (first) {
setV(yToVal, y, baseDist + i);
} else {
addV(yToVal, y, baseDist + i);
}
}
return [x + dist, y];
}
case 'L': {
for (let i = 1; i <= dist; ++i) {
let yToVal = initCell(grid, x - i);
if (first) {
setV(yToVal, y, baseDist + i);
} else {
addV(yToVal, y, baseDist + i);
}
const distance = Number(move.substr(1));
const dir = getDir(op);
let pos: Vec2d;
for (let i = 1; i <= distance; ++i) {
pos = start.add(dir.mult(i));
if (firstWire) {
grid.set(pos, baseDistance + i);
} else {
const firstDistance = grid.get(pos);
if (firstDistance !== undefined) {
grid.set(pos, INTERSECTION_INDICATOR + firstDistance + baseDistance + i);
}
return [x - dist, y];
}
case 'U': {
let yToVal = initCell(grid, x);
for (let i = 1; i <= dist; ++i) {
if (first) {
setV(yToVal, y + i, baseDist + i);
} else {
addV(yToVal, y + i, baseDist + i);
}
}
return [x, y + dist];
}
case 'D': {
let yToVal = initCell(grid, x);
for (let i = 1; i <= dist; ++i) {
if (first) {
setV(yToVal, y - i, baseDist + i);
} else {
addV(yToVal, y - i, baseDist + i);
}
}
return [x, y - dist];
}
}
throw Error("Invalid move");
return pos!;
}

function markPath(wire: Array<string>, first: boolean) {
let [x, y] = [0, 0];
let dist = 0;
function markPath(grid: Map2d<number>, wire: string[], firstWire: boolean) {
let start = new Vec2d(0, 0);
let distance = 0;
wire.forEach((move) => {
let [xn, yn] = markMove([x, y], move, dist, first);
dist += Math.abs(xn - x) + Math.abs(yn - y);
x = xn;
y = yn;
let next = markMove(grid, start, move, distance, firstWire);
distance += next.manhattanDistance(start);
start = next;
});
}

function distance(x: number, y: number) {
return Math.abs(x) + Math.abs(y);
}

function solve(wireA: Array<string>, wireB: Array<string>): number {
//markPath(["R75","D30","R83","U83","L12","D49","R71","U7","L72"], true);
//markPath(["U62","R66","U55","R34","D71","R55","D58","R83"], false);
markPath(wireA, true);
markPath(wireB, false);
let minDist: number = Infinity;
grid.forEach((yToVal, x) => {
yToVal.forEach((val, y) => {
if (val > HACK) {
let dist = val - HACK;
if (dist < minDist) {
minDist = dist;
}
function minDistance(wireA: string[], wireB: string[], part2: boolean): number {
const grid: Map2d<number> = new Map2d();
markPath(grid, wireA, true);
markPath(grid, wireB, false);
let minDistance: number = Infinity;
if (part2) {
grid.forEach((val) => {
let distance = val - INTERSECTION_INDICATOR;
if (distance > 0 && distance < minDistance) {
minDistance = distance;
}
});
});
return minDist;
} else {
let origin = new Vec2d(0, 0);
for (let [k, v] of grid.entries()) {
if (v > INTERSECTION_INDICATOR) {
minDistance = Math.min(minDistance, k.manhattanDistance(origin));
}
}
}
return minDistance;
}

export async function day3(): Promise<number> {
export async function solve(): Promise<number> {
const lines = await readlines('./data/3.txt');
const wireA: Array<string> = lines[0].split(',');
const wireB: Array<string> = lines[1].split(',');
const wireA: string[] = lines[0].split(',');
const wireB: string[] = lines[1].split(',');

return solve(wireA, wireB);
return minDistance(wireA, wireB, true);
}
2 changes: 1 addition & 1 deletion 4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function isMatch(n: number): boolean {
return isAscending(digits) && hasDigitsPair(digits);
}

export function day4(): number {
export function solve(): number {
let matches = 0;

for (let i = 193651; i <= 649729; ++i) {
Expand Down
6 changes: 3 additions & 3 deletions 5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const IMM_MODE = 1;

const INPUT = 5;

function solve(numbers: Array<number>): Array<number> {
function run(numbers: Array<number>): Array<number> {
let pos = 0;
let mode = 0;
while (true) {
Expand Down Expand Up @@ -108,9 +108,9 @@ function solve(numbers: Array<number>): Array<number> {
throw Error("Invalid program");
}

export async function day5(): Promise<number> {
export async function solve(): Promise<number> {
const lines = await readlines('./data/5.txt');
const numbers: Array<number> = lines[0].split(',').map(Number);
const program = solve(numbers);
const program = run(numbers);
return 0;
}
2 changes: 1 addition & 1 deletion 6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function getOrbitSteps(key: string): Map<string, number> {
return steps;
}

export async function day6(): Promise<number> {
export async function solve(): Promise<number> {
const lines = await readlines('./data/6.txt');
const pairs = lines.map(l => l.split(')'));
for (let [a, b] of pairs) {
Expand Down
2 changes: 1 addition & 1 deletion 7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Amplifier {
}
}

export async function day7(): Promise<number> {
export async function solve(): Promise<number> {
const lines = await readlines('./data/7.txt');
const numbers: Array<number> = lines[0].split(',').map(Number);
let maxOut = -Infinity;
Expand Down
Loading

0 comments on commit 9d48f32

Please sign in to comment.