Compare commits
No commits in common. "964e1cd409fdc0142bacae63361bb0b014ff7fcd" and "37169583ff1a10c9ef0ca263cc7959623fe1a130" have entirely different histories.
964e1cd409
...
37169583ff
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,6 +1,6 @@
|
||||
/tmp
|
||||
/out-tsc
|
||||
/dist
|
||||
|
||||
/node_modules
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
@ -9,5 +9,5 @@ yarn-error.log*
|
||||
.pnp.js
|
||||
|
||||
.vscode/*
|
||||
/tryhard
|
||||
|
||||
*.txt
|
||||
|
||||
@ -1,87 +0,0 @@
|
||||
import fs from "fs"
|
||||
const content = fs.readFileSync('input.txt', 'utf-8');
|
||||
|
||||
interface Point {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
const heightMap = content.split("\r\n").map(row => row.split("").map(char => Number(char)))
|
||||
const height = heightMap.length
|
||||
const width = heightMap[0].length
|
||||
const peakSet = new Set();
|
||||
function walk(point: Point) {
|
||||
const pos = heightMap[point.y][point.x]
|
||||
if (pos === 9) {
|
||||
peakSet.add("" + point.x + point.y)
|
||||
} else {
|
||||
const walkable:Point[] = []
|
||||
if (point.y-1 >=0 && (heightMap[point.y-1][point.x]-pos ===1)) {
|
||||
walkable.push({x:point.x, y: point.y-1})
|
||||
}
|
||||
if (point.y+1 < height && (heightMap[point.y+1][point.x]-pos ===1)) {
|
||||
walkable.push({x:point.x, y: point.y+1})
|
||||
}
|
||||
if (point.x-1 >=0 && (heightMap[point.y][point.x-1]-pos ===1)) {
|
||||
walkable.push({x:point.x-1, y: point.y})
|
||||
}
|
||||
if (point.x+1 < width && (heightMap[point.y][point.x+1]-pos === 1)) {
|
||||
walkable.push({x:point.x+1, y: point.y})
|
||||
}
|
||||
if (walkable.length > 0){
|
||||
for (let i = 0; i < walkable.length; i++) {
|
||||
walk(walkable[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function walk2(point: Point) {
|
||||
const pos = heightMap[point.y][point.x]
|
||||
if (pos === 9) {
|
||||
peakSet.add({x: point.x, y:point.y})
|
||||
} else {
|
||||
const walkable:Point[] = []
|
||||
if (point.y-1 >=0 && (heightMap[point.y-1][point.x]-pos ===1)) {
|
||||
walkable.push({x:point.x, y: point.y-1})
|
||||
}
|
||||
if (point.y+1 < height && (heightMap[point.y+1][point.x]-pos ===1)) {
|
||||
walkable.push({x:point.x, y: point.y+1})
|
||||
}
|
||||
if (point.x-1 >=0 && (heightMap[point.y][point.x-1]-pos ===1)) {
|
||||
walkable.push({x:point.x-1, y: point.y})
|
||||
}
|
||||
if (point.x+1 < width && (heightMap[point.y][point.x+1]-pos === 1)) {
|
||||
walkable.push({x:point.x+1, y: point.y})
|
||||
}
|
||||
if (walkable.length > 0){
|
||||
for (let i = 0; i < walkable.length; i++) {
|
||||
walk2(walkable[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let pathcount = 0;
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
const pos = heightMap[y][x]
|
||||
if (pos === 0) {
|
||||
peakSet.clear();
|
||||
walk({x:x,y:y})
|
||||
pathcount += peakSet.size;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(pathcount)
|
||||
pathcount = 0;
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
const pos = heightMap[y][x]
|
||||
if (pos === 0) {
|
||||
peakSet.clear();
|
||||
walk2({x:x,y:y})
|
||||
pathcount += peakSet.size;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(pathcount)
|
||||
@ -1,56 +0,0 @@
|
||||
import fs from "fs"
|
||||
|
||||
const content = fs.readFileSync('input.txt', 'utf-8');
|
||||
const numbers = content.split(" ").map(str => Number(str))
|
||||
interface Stone {
|
||||
value: number;
|
||||
count: number;
|
||||
}
|
||||
|
||||
function blink(numArr: number[]) {
|
||||
for (let i = 0; i < numArr.length; i++) {
|
||||
if (numArr[i] === 0) {
|
||||
numArr[i] = 1
|
||||
} else if (numArr[i].toString(10).length % 2 === 0) {
|
||||
const str = "" + numArr[i].toString(10)
|
||||
const left = str.substring(0, str.length / 2)
|
||||
const right = str.substring(str.length / 2, str.length)
|
||||
numArr[i] = Number(left);
|
||||
i++;
|
||||
numArr.splice(i, 0, Number(right))
|
||||
|
||||
} else {
|
||||
numArr[i] = numArr[i] * 2024
|
||||
}
|
||||
}
|
||||
}
|
||||
const a1 = structuredClone(numbers)
|
||||
for (let i = 0; i < 25; i++) {
|
||||
blink(a1)
|
||||
}
|
||||
console.log(a1.length)
|
||||
let a2: Map<number,number> = new Map;
|
||||
numbers.forEach(num => a2.set(num,1));
|
||||
for (let i = 0; i < 75; i++) {
|
||||
const newMap:Map<number,number> = new Map;
|
||||
a2.forEach((value, number, map) => {
|
||||
if (number === 0) {
|
||||
newMap.set(1, (newMap.get(1) ?? +0) + (value))
|
||||
} else if (number.toString(10).length % 2 === 0) {
|
||||
const str = "" + number.toString(10)
|
||||
const left = Number(str.substring(0, str.length / 2))
|
||||
const right = Number(str.substring(str.length / 2, str.length))
|
||||
newMap.set(left, (newMap.get(left) ?? +0) + value)
|
||||
newMap.set(right, (newMap.get(right) ?? +0) + value)
|
||||
|
||||
} else {
|
||||
newMap.set(number*2024,value);
|
||||
}
|
||||
})
|
||||
a2 = newMap;
|
||||
}
|
||||
let stoneSum = 0;
|
||||
a2.forEach((value, key, map) => {
|
||||
stoneSum += value
|
||||
})
|
||||
console.log(stoneSum)
|
||||
221
src/6/index.ts
221
src/6/index.ts
@ -88,6 +88,7 @@ class Board {
|
||||
const content = fs.readFileSync('input.txt', 'utf-8');
|
||||
const board1 = new Board(content);
|
||||
let outOfBounds = false;
|
||||
let path: Point[] = []
|
||||
while (!outOfBounds) {
|
||||
let free = false
|
||||
while (!free) {
|
||||
@ -126,6 +127,7 @@ while (!outOfBounds) {
|
||||
}
|
||||
}
|
||||
}
|
||||
path.push({x: board1.posisiton.x, y: board1.posisiton.y})
|
||||
board1.move();
|
||||
board1.visited[board1.posisiton.y][board1.posisiton.x] = '?';
|
||||
if (!(board1.posisiton.x < board1.sizeX && board1.posisiton.x > 0 && board1.posisiton.y < board1.sizeY && board1.posisiton.y > 0)) {
|
||||
@ -137,90 +139,155 @@ let count = 0;
|
||||
board1.visited.forEach(row => row.forEach(stop => stop === '?' ? count++ : count))
|
||||
console.log(count)
|
||||
//Bruteforce
|
||||
const board2 = new Board(content);
|
||||
// const board2 = new Board(content);
|
||||
// let obsCounter = 0;
|
||||
// for (let i = 0; i < board2.sizeY + 1; i++) {
|
||||
// for (let j = 0; j < board2.sizeX + 1; j++) {
|
||||
// const field = new Board(content)
|
||||
// if ((i === field.posisiton.y && j === field.posisiton.x)) {
|
||||
// continue;
|
||||
// }
|
||||
// if (field.board[i][j] === '#') {
|
||||
// continue;
|
||||
// }
|
||||
// field.board[i][j] = '#'
|
||||
// field.visited[i][j] = '#'
|
||||
// const visited: Helper[][] = Array(field.sizeY+1).fill(undefined).map(() => Array(field.sizeX+1).fill({
|
||||
// visited: false,
|
||||
// direction: undefined
|
||||
// }))
|
||||
// let limit = false;
|
||||
// let outOfBounds = false;
|
||||
// while (!outOfBounds) {
|
||||
// let free = false;
|
||||
// while (!free) {
|
||||
// switch (field.dir) {
|
||||
// case "UP": {
|
||||
// if (field.posisiton.y - 1 < 0 || field.board[field.posisiton.y - 1][field.posisiton.x] === '#') {
|
||||
// field.nextDir();
|
||||
// } else {
|
||||
// free = true;
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
// case "RIGHT": {
|
||||
// if (field.posisiton.x + 1 > field.sizeX || field.board[field.posisiton.y][field.posisiton.x + 1] === '#') {
|
||||
// field.nextDir();
|
||||
// } else {
|
||||
// free = true;
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
// case "DOWN": {
|
||||
// if (field.posisiton.y + 1 > field.sizeY || field.board[field.posisiton.y + 1][field.posisiton.x] === '#') {
|
||||
// field.nextDir();
|
||||
// } else {
|
||||
// free = true;
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
// case "LEFT": {
|
||||
// if (field.posisiton.x - 1 < 0 || field.board[field.posisiton.y][field.posisiton.x - 1] === '#') {
|
||||
// field.nextDir();
|
||||
// } else {
|
||||
// free = true;
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if (visited[field.posisiton.y][field.posisiton.x].visited && visited[field.posisiton.y][field.posisiton.x].direction === field.dir) {
|
||||
// limit = true;
|
||||
// break;
|
||||
// }
|
||||
// visited[field.posisiton.y][field.posisiton.x] = {visited: true, direction: field.dir}
|
||||
// field.visited[field.posisiton.y][field.posisiton.x] = '?';
|
||||
// field.move();
|
||||
// if (!(field.posisiton.x < field.sizeX && field.posisiton.x >= 0 && field.posisiton.y < field.sizeY && field.posisiton.y >= 0)) {
|
||||
// outOfBounds = true;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// if (limit) {
|
||||
// board2.board[i][j] = "O"
|
||||
// // let output = "";
|
||||
// // field.visited.forEach(row => {
|
||||
// // row.forEach(char => output = output.concat(char))
|
||||
// // output = output.concat('\n');
|
||||
// // })
|
||||
// // writeFileSync(`out\\output${i}${j}.txt`,output);
|
||||
// obsCounter++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// console.log(obsCounter);
|
||||
path.splice(0, 1);
|
||||
let obsCounter = 0;
|
||||
for (let i = 0; i < board2.sizeY + 1; i++) {
|
||||
for (let j = 0; j < board2.sizeX + 1; j++) {
|
||||
const field = new Board(content)
|
||||
if ((i === field.posisiton.y && j === field.posisiton.x)) {
|
||||
continue;
|
||||
}
|
||||
if (field.board[i][j] === '#') {
|
||||
continue;
|
||||
}
|
||||
field.board[i][j] = '#'
|
||||
field.visited[i][j] = '#'
|
||||
const visited: Helper[][] = Array(field.sizeY+1).fill(undefined).map(() => Array(field.sizeX+1).fill({
|
||||
visited: false,
|
||||
direction: undefined
|
||||
}))
|
||||
let limit = false;
|
||||
let outOfBounds = false;
|
||||
while (!outOfBounds) {
|
||||
let free = false;
|
||||
while (!free) {
|
||||
switch (field.dir) {
|
||||
case "UP": {
|
||||
if (field.posisiton.y - 1 < 0) {
|
||||
outOfBounds = true;
|
||||
free = true;
|
||||
} else if (field.board[field.posisiton.y - 1][field.posisiton.x] === '#') {
|
||||
field.nextDir();
|
||||
} else {
|
||||
free = true;
|
||||
}
|
||||
break;
|
||||
for (const point of path) {
|
||||
const field = new Board(content);
|
||||
if (field.posisiton.x === point.x && field.posisiton.y === point.y){
|
||||
continue;
|
||||
}
|
||||
field.board[point.y][point.x] = '#'
|
||||
field.visited[point.y][point.x] = '#'
|
||||
const visited: Helper[][] = Array(field.sizeY + 1).fill(undefined).map(() => Array(field.sizeX + 1).fill({
|
||||
visited: false,
|
||||
direction: undefined
|
||||
}))
|
||||
let limit = false;
|
||||
let outOfBounds = false;
|
||||
while (!outOfBounds) {
|
||||
let free = false;
|
||||
while (!free) {
|
||||
switch (field.dir) {
|
||||
case "UP": {
|
||||
if (field.posisiton.y - 1 < 0 || field.board[field.posisiton.y - 1][field.posisiton.x] === '#') {
|
||||
field.nextDir();
|
||||
} else {
|
||||
free = true;
|
||||
}
|
||||
case "RIGHT": {
|
||||
if (field.posisiton.x + 1 > field.sizeX) {
|
||||
outOfBounds = true;
|
||||
free = true;
|
||||
} else if (field.board[field.posisiton.y][field.posisiton.x + 1] === '#') {
|
||||
field.nextDir();
|
||||
} else {
|
||||
free = true;
|
||||
}
|
||||
break;
|
||||
break;
|
||||
}
|
||||
case "RIGHT": {
|
||||
if (field.posisiton.x + 1 > field.sizeX || field.board[field.posisiton.y][field.posisiton.x + 1] === '#') {
|
||||
field.nextDir();
|
||||
} else {
|
||||
free = true;
|
||||
}
|
||||
case "DOWN": {
|
||||
if (field.posisiton.y + 1 > field.sizeY) {
|
||||
outOfBounds = true;
|
||||
free = true;
|
||||
} else if (field.board[field.posisiton.y + 1][field.posisiton.x] === '#') {
|
||||
field.nextDir();
|
||||
} else {
|
||||
free = true;
|
||||
}
|
||||
break;
|
||||
break;
|
||||
}
|
||||
case "DOWN": {
|
||||
if (field.posisiton.y + 1 > field.sizeY || field.board[field.posisiton.y + 1][field.posisiton.x] === '#') {
|
||||
field.nextDir();
|
||||
} else {
|
||||
free = true;
|
||||
}
|
||||
case "LEFT": {
|
||||
if (field.posisiton.x - 1 < 0) {
|
||||
outOfBounds = true;
|
||||
free = true;
|
||||
} else if (field.board[field.posisiton.y][field.posisiton.x - 1] === '#') {
|
||||
field.nextDir();
|
||||
} else {
|
||||
free = true;
|
||||
}
|
||||
break;
|
||||
break;
|
||||
}
|
||||
case "LEFT": {
|
||||
if (field.posisiton.x - 1 < 0 || field.board[field.posisiton.y][field.posisiton.x - 1] === '#') {
|
||||
field.nextDir();
|
||||
} else {
|
||||
free = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (visited[field.posisiton.y][field.posisiton.x].visited && visited[field.posisiton.y][field.posisiton.x].direction === field.dir) {
|
||||
limit = true;
|
||||
break;
|
||||
}
|
||||
visited[field.posisiton.y][field.posisiton.x] = {visited: true, direction: field.dir}
|
||||
field.visited[field.posisiton.y][field.posisiton.x] = '?';
|
||||
field.move();
|
||||
if (!(field.posisiton.x < field.sizeX && field.posisiton.x >= 0 && field.posisiton.y < field.sizeY && field.posisiton.y >= 0)) {
|
||||
outOfBounds = true;
|
||||
}
|
||||
}
|
||||
if (visited[field.posisiton.y][field.posisiton.x].visited && visited[field.posisiton.y][field.posisiton.x].direction === field.dir) {
|
||||
limit = true;
|
||||
break;
|
||||
}
|
||||
visited[field.posisiton.y][field.posisiton.x] = {visited: true, direction: field.dir}
|
||||
field.visited[field.posisiton.y][field.posisiton.x] = '?';
|
||||
field.move();
|
||||
if (!(field.posisiton.x < field.sizeX && field.posisiton.x >= 0 && field.posisiton.y < field.sizeY && field.posisiton.y >= 0)) {
|
||||
outOfBounds = true;
|
||||
}
|
||||
|
||||
}
|
||||
if (limit) {
|
||||
obsCounter++;
|
||||
}
|
||||
}
|
||||
if (limit) {
|
||||
obsCounter++;
|
||||
}
|
||||
}
|
||||
console.log(obsCounter);
|
||||
console.log(obsCounter);
|
||||
@ -1,72 +0,0 @@
|
||||
import fs from "fs"
|
||||
const content = fs.readFileSync('input.txt', 'utf-8');
|
||||
const rows = content.split("\r\n")
|
||||
interface Calc {
|
||||
result: number,
|
||||
numbers: number[],
|
||||
}
|
||||
|
||||
const calculations = rows.map(row => {
|
||||
const res = Number(row.split(":")[0]);
|
||||
const tail = row.split(":")[1].trim();
|
||||
const num = tail.split(" ").map(numStr => Number(numStr));
|
||||
return {result: res, numbers: num}
|
||||
})
|
||||
let calcualtable = 0;
|
||||
calculations.forEach(calculation => {
|
||||
const operations = calculation.numbers.length-1;
|
||||
const table = []
|
||||
const max = Math.pow(2,operations)
|
||||
for (let i = 0; i < max ; i++) {
|
||||
let toPush = (max-(i+1)).toString(2)
|
||||
while (toPush.length < operations) toPush = "0" + toPush;
|
||||
table.push(toPush.split("").map(str => Number(str)));
|
||||
}
|
||||
for (let c = 0; c < table.length; c++) {
|
||||
const combination = table[c];
|
||||
const calcTable = structuredClone(calculation.numbers);
|
||||
for (let i = 0; i < calcTable.length-1; i++) {
|
||||
if (combination[i] === 0) {
|
||||
calcTable[i+1] = calcTable[i] + calcTable[i+1]
|
||||
} else {
|
||||
calcTable[i+1] = calcTable[i] * calcTable[i+1]
|
||||
}
|
||||
}
|
||||
if (calculation.result === calcTable[calcTable.length-1]) {
|
||||
calcualtable += calculation.result;
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
console.log(calcualtable)
|
||||
let start = new Date().getTime();
|
||||
calcualtable = 0;
|
||||
calculations.forEach(calculation => {
|
||||
const operations = calculation.numbers.length-1;
|
||||
const table = []
|
||||
const max = Math.pow(3,operations)
|
||||
for (let i = 0; i < max ; i++) {
|
||||
let toPush = (max-(i+1)).toString(3)
|
||||
while (toPush.length < operations) toPush = "0" + toPush;
|
||||
table.push(toPush.split("").map(str => Number(str)));
|
||||
}
|
||||
for (let c = 0; c < table.length; c++) {
|
||||
const combination = table[c];
|
||||
const calcTable = structuredClone(calculation.numbers);
|
||||
for (let i = 0; i < calcTable.length-1; i++) {
|
||||
if (combination[i] === 0) {
|
||||
calcTable[i+1] = calcTable[i] + calcTable[i+1]
|
||||
} else if (combination[i] === 1) {
|
||||
calcTable[i+1] = calcTable[i] * calcTable[i+1]
|
||||
} else {
|
||||
calcTable[i+1] = Number("" + calcTable[i] + calcTable[i+1])
|
||||
}
|
||||
}
|
||||
if (calculation.result === calcTable[calcTable.length-1]) {
|
||||
calcualtable += calculation.result;
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
console.log(calcualtable)
|
||||
console.log("Took " + (new Date().getTime() - start))
|
||||
@ -1,80 +0,0 @@
|
||||
import fs from "fs"
|
||||
|
||||
const content = fs.readFileSync('input.txt', 'utf-8');
|
||||
const rows = content.split("\r\n")
|
||||
const field = rows.map(row => row.split(""))
|
||||
const antinodes = structuredClone(field);
|
||||
for (let y = 0; y < field.length; y++) {
|
||||
for (let x = 0; x < field[y].length; x++) {
|
||||
function inBounds(v1:number,v2:number) {
|
||||
return ((v1 >= 0 && v1 < field[y].length) && (v2 >=0 && v2 < field.length))
|
||||
}
|
||||
if (field[y][x] === ".") {
|
||||
continue;
|
||||
} else {
|
||||
const antennaA = field[y][x]
|
||||
for (let y2 = 0; y2 < field.length; y2++) {
|
||||
for (let x2 = 0; x2 < field[y2].length; x2++) {
|
||||
if (field[y2][x2] !== antennaA) {
|
||||
continue;
|
||||
}
|
||||
if (y !== y2 && x !== x2) {
|
||||
const antennaB = field[y2][x2]
|
||||
const posA = {x: x-x2,y: y-y2}
|
||||
const posB = {x: x2-x, y: y2-y}
|
||||
const placeA = {x:x + posA.x, y: y + posA.y}
|
||||
const placeB = {x:x2 + posB.x, y: y2 + posB.y}
|
||||
if (inBounds(placeA.x,placeA.y)) {
|
||||
antinodes[placeA.y][placeA.x] = '#'
|
||||
}
|
||||
if (inBounds(placeB.x,placeB.y)) {
|
||||
antinodes[placeB.y][placeB.x] = '#'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let count = 0;
|
||||
antinodes.forEach(row => row.forEach(char => char === '#' ? count++ : count))
|
||||
console.log(count)
|
||||
for (let y = 0; y < field.length; y++) {
|
||||
for (let x = 0; x < field[y].length; x++) {
|
||||
function inBounds(v1:number,v2:number) {
|
||||
return ((v1 >= 0 && v1 < field[y].length) && (v2 >=0 && v2 < field.length))
|
||||
}
|
||||
if (field[y][x] === ".") {
|
||||
continue;
|
||||
} else {
|
||||
const antennaA = field[y][x]
|
||||
for (let y2 = 0; y2 < field.length; y2++) {
|
||||
for (let x2 = 0; x2 < field[y2].length; x2++) {
|
||||
if (field[y2][x2] !== antennaA) {
|
||||
continue;
|
||||
}
|
||||
if (y !== y2 && x !== x2) {
|
||||
const antennaB = field[y2][x2]
|
||||
const posA = {x: x-x2,y: y-y2}
|
||||
const posB = {x: x2-x, y: y2-y}
|
||||
let placeA = {x:x + posA.x, y: y + posA.y}
|
||||
let placeB = {x:x2 + posB.x, y: y2 + posB.y}
|
||||
antinodes[y][x] = '#'
|
||||
antinodes[y2][x2] = '#'
|
||||
while (inBounds(placeA.x,placeA.y)) {
|
||||
antinodes[placeA.y][placeA.x] = '#'
|
||||
placeA = {x:placeA.x + posA.x, y: placeA.y + posA.y}
|
||||
}
|
||||
if (inBounds(placeB.x,placeB.y)) {
|
||||
antinodes[placeB.y][placeB.x] = '#'
|
||||
placeB = {x:placeB.x + posB.x, y: placeB.y + posB.y}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
count = 0;
|
||||
antinodes.forEach(row => row.forEach(char => char === '#' ? count++ : count))
|
||||
console.log(count)
|
||||
@ -1,75 +0,0 @@
|
||||
import fs from "fs"
|
||||
const content = fs.readFileSync('input.txt', 'utf-8');
|
||||
const disk:string[] = [];
|
||||
let fileId = 0;
|
||||
for (let i = 0; i < content.length; i++) {
|
||||
const amount = Number(content[i]);
|
||||
if (i % 2 === 0) {
|
||||
for (let j = 0; j < amount; j++) {
|
||||
disk.push(fileId.toString(10))
|
||||
}
|
||||
fileId++;
|
||||
} else {
|
||||
for (let j = 0; j < amount; j++) {
|
||||
disk.push(".")
|
||||
}
|
||||
}
|
||||
}
|
||||
const orderedDisk = structuredClone(disk);
|
||||
let backIndex = orderedDisk.length-1
|
||||
for (let i = 0; i < backIndex; i++) {
|
||||
if (orderedDisk[i] === ".") {
|
||||
orderedDisk[i] = orderedDisk[backIndex]
|
||||
orderedDisk[backIndex] = "."
|
||||
while (orderedDisk[backIndex] === ".") {
|
||||
backIndex--
|
||||
}
|
||||
}
|
||||
}
|
||||
let checksum = 0;
|
||||
for (let i = 0; i < orderedDisk.length; i++) {
|
||||
if (orderedDisk[i] !== ".") {
|
||||
checksum += i * Number(orderedDisk[i])
|
||||
}
|
||||
}
|
||||
console.log(checksum)
|
||||
const fragmented = structuredClone(disk);
|
||||
backIndex = fragmented.length-1
|
||||
while (backIndex > 0) {
|
||||
let cursor = 0;
|
||||
let idCount = 1;
|
||||
while (fragmented[backIndex-idCount] === fragmented[backIndex]) {
|
||||
idCount++;
|
||||
}
|
||||
let dotCount = 0
|
||||
while (dotCount < idCount && cursor+idCount < backIndex){
|
||||
while (fragmented[cursor] !== ".") {
|
||||
cursor++;
|
||||
}
|
||||
dotCount = 1;
|
||||
while (fragmented[cursor+dotCount] === ".") {
|
||||
dotCount++;
|
||||
}
|
||||
if (dotCount < idCount) {
|
||||
cursor++
|
||||
}
|
||||
}
|
||||
if (dotCount >= idCount && cursor <backIndex) {
|
||||
for (let j = 0; j < idCount; j++) {
|
||||
fragmented[cursor+j] = fragmented[backIndex-j];
|
||||
fragmented[backIndex-j] = "."
|
||||
}
|
||||
} else {
|
||||
backIndex -= idCount;
|
||||
}
|
||||
while (fragmented[backIndex] === ".") {
|
||||
backIndex--
|
||||
}
|
||||
}
|
||||
checksum = 0;
|
||||
for (let i = 0; i < fragmented.length; i++) {
|
||||
if (fragmented[i] !== ".") {
|
||||
checksum += i * Number(fragmented[i])
|
||||
}
|
||||
}
|
||||
console.log(checksum)
|
||||
@ -8,5 +8,5 @@
|
||||
"skipLibCheck": true,
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": ["tryhard"]
|
||||
"include": ["src"]
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user