-
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
Showing
12 changed files
with
561 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
console.log(input.map(Number).map(i => Math.floor(i/3)-2).reduce((a,b)=>a+b,0)) | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
console.log(input.map(Number).map(calc).reduce((a,b)=>a+b,0)) | ||
|
||
function calc(c: number) { | ||
let sum = 0 | ||
let fuel = Math.floor(c/3)-2 | ||
while(fuel > 0) { | ||
sum += fuel | ||
fuel = Math.floor(fuel/3)-2 | ||
} | ||
return sum | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
const list: Record<number, number> = {} | ||
input[0].split(',').map(Number).forEach((i,idx) => list[idx] = i) | ||
list[1] = 12 | ||
list[2] = 2 | ||
|
||
console.log(run(list)[0]) | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
const list: Record<number, number> = {} | ||
input[0].split(',').map(Number).forEach((i,idx) => list[idx] = i) | ||
for (let i = 0; i <= 99; i++) { | ||
for (let j = 0; j <= 99; j++) { | ||
const copy = JSON.parse(JSON.stringify(list)) | ||
copy[1] = i | ||
copy[2] = j | ||
if (run(copy)[0] == 19690720) { | ||
console.log(i * 100 + j) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
function run(list: Record<number, number>) { | ||
let i = 0 | ||
while(list[i] != 99) { | ||
if (list[i] == 1) { | ||
list[list[i+3]] = list[list[i+1]] + list[list[i+2]] | ||
} else if (list[i] == 2) { | ||
list[list[i+3]] = list[list[i+1]] * list[list[i+2]] | ||
} else { | ||
throw i | ||
} | ||
i+=4 | ||
} | ||
return list | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
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 |
---|---|---|
@@ -0,0 +1,156 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
const parts = input[0].split(',') | ||
|
||
let cX = 0 | ||
let cY = 0 | ||
|
||
const lines: Line[] = [] | ||
for (const p of parts) { | ||
let dir = [0,0] | ||
if (p[0] == 'U') dir = [0,1] | ||
if (p[0] == 'D') dir = [0,-1] | ||
if (p[0] == 'L') dir = [-1,0] | ||
if (p[0] == 'R') dir = [1,0] | ||
const mov = Number(p.substring(1)) | ||
const newLine = { | ||
x1: cX, | ||
y1: cY, | ||
x2: cX + dir[0] * mov, | ||
y2: cY + dir[1] * mov | ||
} | ||
lines.push(newLine) | ||
cX = newLine.x2 | ||
cY = newLine.y2 | ||
} | ||
|
||
const parts2 = input[1].split(',') | ||
let minD = Infinity | ||
cX = 0 | ||
cY = 0 | ||
for (const p of parts2) { | ||
let dir = [0,0] | ||
if (p[0] == 'U') dir = [0,1] | ||
if (p[0] == 'D') dir = [0,-1] | ||
if (p[0] == 'L') dir = [-1,0] | ||
if (p[0] == 'R') dir = [1,0] | ||
const mov = Number(p.substring(1)) | ||
const newLine = { | ||
x1: cX, | ||
y1: cY, | ||
x2: cX + dir[0] * mov, | ||
y2: cY + dir[1] * mov | ||
} | ||
|
||
for (const l of lines) { | ||
const i = intersect(l, newLine) | ||
if (i == null) continue | ||
const d = Math.abs(i[0]) + Math.abs(i[1]) | ||
if (d == 0) continue | ||
if (d < minD) minD = d | ||
} | ||
|
||
cX = newLine.x2 | ||
cY = newLine.y2 | ||
} | ||
console.log(minD) | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
interface Line2 extends Line { | ||
goneDistance: number | ||
} | ||
|
||
const parts = input[0].split(',') | ||
|
||
let cX = 0 | ||
let cY = 0 | ||
let cD = 0 | ||
|
||
const lines: Line2[] = [] | ||
for (const p of parts) { | ||
let dir = [0,0] | ||
if (p[0] == 'U') dir = [0,1] | ||
if (p[0] == 'D') dir = [0,-1] | ||
if (p[0] == 'L') dir = [-1,0] | ||
if (p[0] == 'R') dir = [1,0] | ||
const mov = Number(p.substring(1)) | ||
const newLine = { | ||
x1: cX, | ||
y1: cY, | ||
x2: cX + dir[0] * mov, | ||
y2: cY + dir[1] * mov, | ||
goneDistance: cD | ||
} | ||
lines.push(newLine) | ||
cX = newLine.x2 | ||
cY = newLine.y2 | ||
cD += mov | ||
} | ||
|
||
const parts2 = input[1].split(',') | ||
let minD = Infinity | ||
cX = 0 | ||
cY = 0 | ||
cD = 0 | ||
for (const p of parts2) { | ||
let dir = [0,0] | ||
if (p[0] == 'U') dir = [0,1] | ||
if (p[0] == 'D') dir = [0,-1] | ||
if (p[0] == 'L') dir = [-1,0] | ||
if (p[0] == 'R') dir = [1,0] | ||
const mov = Number(p.substring(1)) | ||
const newLine = { | ||
x1: cX, | ||
y1: cY, | ||
x2: cX + dir[0] * mov, | ||
y2: cY + dir[1] * mov | ||
} | ||
|
||
for (const l of lines) { | ||
const i = intersect(l, newLine) | ||
if (i == null) continue | ||
if(i[0] == 0 && i[1] == 0) continue | ||
const d = cD + l.goneDistance + Math.abs(l.x1 - i[0]) + Math.abs(l.y1 - i[1]) + Math.abs(newLine.x1 - i[0]) + Math.abs(newLine.y1 - i[1]) | ||
if (d < minD) minD = d | ||
} | ||
|
||
cX = newLine.x2 | ||
cY = newLine.y2 | ||
cD += mov | ||
} | ||
console.log(minD) | ||
} | ||
|
||
interface Line { | ||
x1: number | ||
x2: number | ||
y1: number | ||
y2: number | ||
} | ||
|
||
function intersect(l1: Line, l2: Line): [number, number]|null { | ||
const x1 = l1.x1 | ||
const x2 = l1.x2 | ||
const x3 = l2.x1 | ||
const x4 = l2.x2 | ||
const y1 = l1.y1 | ||
const y2 = l1.y2 | ||
const y3 = l2.y1 | ||
const y4 = l2.y2 | ||
|
||
const pX = ((x1*y2 - y1*x2)*(x3 - x4) - (x1 - x2)*(x3*y4 - y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)) | ||
const pY = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)) | ||
|
||
if (!inRange(pX, x1,x2) || !inRange(pX, x3,x4) || !inRange(pY, y1,y2) || !inRange(pY,y3,y4)) { | ||
return null | ||
} | ||
|
||
return [pX, pY] | ||
} | ||
|
||
function inRange(test: number, n1: number, n2: number) { | ||
let max = n1 > n2 ? n1 : n2 | ||
let min = n1 > n2 ? n2 : n1 | ||
|
||
return test >= min && test <= max | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
const range = input[0].split('-').map(Number) | ||
let count = 0 | ||
for (let i = range[0]; i <= range[1]; i++) { | ||
if (checkValid(i)) count++ | ||
} | ||
console.log(count) | ||
|
||
function checkValid(n: number) { | ||
const nums = getNums(n) | ||
for (let i = 1; i < nums.length; i++) { | ||
if (nums[i] < nums[i-1]) return false | ||
} | ||
for (let i = 1; i < nums.length; i++) { | ||
if (nums[i] == nums[i-1]) return true | ||
} | ||
return false | ||
} | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
const range = input[0].split('-').map(Number) | ||
let count = 0 | ||
for (let i = range[0]; i <= range[1]; i++) { | ||
if (checkValid(i)) count++ | ||
} | ||
console.log(count) | ||
|
||
function checkValid(n: number) { | ||
const nums = getNums(n) | ||
for (let i = 1; i < nums.length; i++) { | ||
if (nums[i] < nums[i-1]) return false | ||
} | ||
const c: number[] = [] | ||
let last = nums[0] | ||
let co = 0 | ||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i] == last) co++ | ||
else { | ||
c.push(co) | ||
co = 1 | ||
last = nums[i] | ||
} | ||
} | ||
c.push(co) | ||
return c.includes(2) | ||
} | ||
} | ||
|
||
|
||
|
||
function getNums(n: number): number[] { | ||
return [100000, 10000, 1000, 100, 10, 1].map(i => Math.floor((n % (i*10)) / i)) | ||
} |
Oops, something went wrong.