-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
082962c
commit 9d48f32
Showing
15 changed files
with
233 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.