From 9650cea992017f93b87faa6c05cd14f3222c5ee8 Mon Sep 17 00:00:00 2001 From: Travis Hardiman Date: Fri, 6 Dec 2024 09:23:38 -0500 Subject: [PATCH 1/8] day 6, part 1 --- package.json | 2 +- public/funs.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++- views/index.html | 2 +- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index c70fd20..f441f6b 100644 --- a/package.json +++ b/package.json @@ -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/", diff --git a/public/funs.js b/public/funs.js index 2195eb8..1af4638 100644 --- a/public/funs.js +++ b/public/funs.js @@ -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: { diff --git a/views/index.html b/views/index.html index 9765750..40b870c 100644 --- a/views/index.html +++ b/views/index.html @@ -25,8 +25,8 @@

solutions:

  • day 03
  • day 04
  • day 05
  • -