Skip to content

Commit

Permalink
day 6, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
dieseltravis committed Dec 6, 2024
1 parent df43b10 commit 9650cea
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
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.05",
"version": "2024.12.06",
"description": "Travis's Advent of Code 2024",
"author": "Travis Hardiman",
"homepage": "https://github.com/dieseltravis/aoc2024/",
Expand Down
55 changes: 54 additions & 1 deletion public/funs.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,60 @@
}
},
day6: {
part1: d => d,
part1: (data) => {
const guard = {
'^': { dy: -1, dx: 0 },
'>': { dy: 0, dx: 1 },
v: { dy: 1, dx: 0 },
'<': { dy: 0, dx: -1 }
};
const dirs = Object.keys(guard);
let guardpos = { y: -1, x: -1 };
let guardchar = 'x';
let guardrotation = -1;
const input = data.trim().split('\n').map((r, y) => {
const row = r.split('');
const found = row.findIndex(c => dirs.includes(c));
if (found >= 0) {
guardpos = { y, x: found };
guardchar = row[found];
guardrotation = dirs.indexOf(guardchar);
}
return row;
});
const ymax = input.length;
const xmax = input[0].length;
const inRange = p => p.y >= 0 && p.y < ymax && p.x >= 0 && p.x < xmax;

console.log(dirs, guardpos, guardchar, guardrotation, ymax, xmax, input);
let safety = 10000;
let steps = 0;
while (safety--) {
input[guardpos.y][guardpos.x] = 'X';
let change = guard[guardchar];
let nextpos = { y: guardpos.y + change.dy, x: guardpos.x + change.dx };
let safety2 = 5;
if (!inRange(nextpos)) {
break;
}
while (input[nextpos.y][nextpos.x] === '#' && safety2--) {
// rotate right
guardrotation = (guardrotation + 1) % 4;
guardchar = dirs[guardrotation];
change = guard[guardchar];
nextpos = { y: guardpos.y + change.dy, x: guardpos.x + change.dx };
if (!inRange(nextpos)) {
break;
}
}
guardpos = nextpos;
steps++;
}
const grid = input.map(r => r.join('')).join('\n');
const count = grid.match(/X/g).length;
console.log(safety, steps, '\n' + grid, count);
return count;
},
part2: d => d
},
day7: {
Expand Down
2 changes: 1 addition & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ <h2>solutions:</h2>
<li><a href="/day/03">day 03</a></li>
<li><a href="/day/04">day 04</a></li>
<li><a href="/day/05">day 05</a></li>
<!--
<li><a href="/day/06">day 06</a></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>
Expand Down

0 comments on commit 9650cea

Please sign in to comment.