Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 9 #8

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meowing-holy-carbon",
"version": "2024.12.08",
"version": "2024.12.09",
"description": "Travis's Advent of Code 2024",
"author": "Travis Hardiman",
"homepage": "https://github.com/dieseltravis/aoc2024/",
Expand Down
129 changes: 127 additions & 2 deletions public/funs.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,133 @@
}
},
day9: {
part1: d => d,
part2: d => d
part1: (data) => {
let fileNum = 0;
const files = [];
const input = data.trim().split('').map((c, i) => {
const block = {
startIndex: i,
isSpace: i % 2,
size: +c
};
if (!block.isSpace) {
block.fileIndex = fileNum++;
files.push(block);
}
return block;
});
console.log(input, files);
const disk = input.reduce((acc, b) => {
for (let l = b.size; l--;) {
acc.push(b.isSpace ? '.' : b.fileIndex);
}
return acc;
}, []);
console.log(disk, disk.join(''));
for (let l = disk.length; l--;) {
const c = disk[l];
if (c !== '.') {
const free = disk.indexOf('.');
if (free < l) {
disk[l] = '.';
disk[free] = c;
} else {
break;
}
}
}
console.log(disk, disk.join(''));
const checksum = disk.reduce((sum, c, i) => {
if (c !== '.') {
sum += (i * c);
}
return sum;
}, 0);
return checksum;
},
part2: (data) => {
let fileNum = 0;
const files = [];
const input = data.trim().split('').map((c, i) => {
const block = {
startIndex: i,
isSpace: i % 2,
size: +c
};
if (!block.isSpace) {
block.fileIndex = fileNum++;
files.push(block);
}
return block;
});
console.log(input, files);
const disk = input.reduce((acc, b) => {
for (let l = b.size; l--;) {
acc.push(b.isSpace ? '.' : b.fileIndex);
}
return acc;
}, []);
console.log(disk, disk.join(''));
const findFree = () => {
let freeIndex = -1;
let lastFreeStart = -1;
return disk.reduce((acc, c, i) => {
if (c === '.') {
if (lastFreeStart === -1) {
lastFreeStart = i;
acc.push({
start: i,
freeLength: 0
});
freeIndex++;
}
acc[freeIndex].freeLength++;
} else {
lastFreeStart = -1;
}
return acc;
}, []);
};
console.log(findFree());
for (let l = disk.length; l--;) {
const c = disk[l];
// console.log(l, c);
if (c !== '.') {
const file = files[c];
const free = findFree();
const firstFree = free.find(f => f.freeLength >= file.size);
// console.log(file, firstFree);
if (firstFree) {
let newStart = firstFree.start;
if (newStart < l) {
// move file
for (let ll = file.size; ll--;) {
disk[newStart++] = c;
disk[l--] = '.';
}
l++;
} else {
// console.log('space is to right, going from ' + l);
l -= (file.size - 1);
// console.log('to ' + l);
}
} else {
// console.log('no space, going from ' + l);
l -= (file.size - 1);
// console.log('to ' + l);
}
}
}
console.log(disk, disk.join(''));
const checksum = disk.reduce((sum, c, i) => {
if (c !== '.') {
sum += (i * c);
}
return sum;
}, 0);
// 6717067113048 is too high
return checksum;
}
},
day10: {
part1: d => d,
Expand Down
2 changes: 1 addition & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ <h2>solutions:</h2>
<li><a href="/day/06">day 06</a>(part 2 broken)</li>
<li><a href="/day/07">day 07</a></li>
<li><a href="/day/08">day 08</a></li>
<!--
<li><a href="/day/09">day 09</a></li>
<!--
<li><a href="/day/10">day 10</a></li>
<li><a href="/day/11">day 11</a></li>
<li><a href="/day/12">day 12</a></li>
Expand Down
Loading