Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 3, 2023
1 parent ca947a3 commit 5434b44
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 196 deletions.
83 changes: 83 additions & 0 deletions src/2023/day03.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<title>Day 3 - Advent of Code 2023</title>
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet'
type='text/css'>
<link rel="stylesheet" type="text/css" href="../static/style.css" />
<link rel="stylesheet alternate" type="text/css" href="../static/highcontrast.css" title="High Contrast" />
<link rel="shortcut icon" href="../static/favicon.png" />
</head>
<body>
<header>
<div>
<h1 class="title-global"><a href="index.html">Advent of Code</a></h1>
<nav>
<ul>
<li><a href="events.html">[Events]</a></li>
<li><a href="leaderboard.html">[Leaderboard]</a></li>
<li><a href="solver.html">[Solver]</a></li>
</ul>
</nav>
<div class="user">Shahar Talmi <a class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <a
href="https://www.wix.engineering/" target="_blank" class="sponsor-badge"
title="Member of sponsor: Wix Engineering">(Sponsor)</a> <span class="star-count">50*</span></div>
</div>
<div>
<h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=</span><a
href="index.html">2023</a><span class="title-event-wrap">;</span></h1>
</div>
</header>
<main>
<article class="day-desc"><h2>--- Day 3: Gear Ratios ---</h2><p>You and the Elf eventually reach a <a href="https://en.wikipedia.org/wiki/Gondola_lift" target="_blank">gondola lift</a> station; he says the gondola lift will take you up to the <em>water source</em>, but this is as far as he can bring you. You go inside.</p>
<p>It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.</p>
<p>"Aaah!"</p>
<p>You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.</p>
<p>The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can <em>add up all the part numbers</em> in the engine schematic, it should be easy to work out which part is missing.</p>
<p>The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently <em>any number adjacent to a symbol</em>, even diagonally, is a "part number" and should be included in your sum. (Periods (<code>.</code>) do not count as a symbol.)</p>
<p>Here is an example engine schematic:</p>
<pre><code>467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
</code></pre>
<p>In this schematic, two numbers are <em>not</em> part numbers because they are not adjacent to a symbol: <code>114</code> (top right) and <code>58</code> (middle right). Every other number is adjacent to a symbol and so <em>is</em> a part number; their sum is <code><em>4361</em></code>.</p>
<p>Of course, the actual engine schematic is much larger. <em>What is the sum of all of the part numbers in the engine schematic?</em></p>
</article>
<p>Your puzzle answer was <code>528799</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.</p>
<p>You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.</p>
<p>Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola.</p>
<p>The missing part wasn't the only issue - one of the gears in the engine is wrong. A <em>gear</em> is any <code>*</code> symbol that is adjacent to <em>exactly two part numbers</em>. Its <em>gear ratio</em> is the result of <span title="They're magic gears.">multiplying</span> those two numbers together.</p>
<p>This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.</p>
<p>Consider the same engine schematic again:</p>
<pre><code>467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
</code></pre>
<p>In this schematic, there are <em>two</em> gears. The first is in the top left; it has part numbers <code>467</code> and <code>35</code>, so its gear ratio is <code>16345</code>. The second gear is in the lower right; its gear ratio is <code>451490</code>. (The <code>*</code> adjacent to <code>617</code> is <em>not</em> a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces <code><em>467835</em></code>.</p>
<p><em>What is the sum of all of the gear ratios in your engine schematic?</em></p>
</article>
<p>Your puzzle answer was <code>84907174</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
<p>At this point, you should <a href="index.html">return to your Advent calendar</a> and try another puzzle.</p>
<p>If you still want to see it, you can <a href="day03.txt" target="_blank">get your puzzle input</a>.</p>
<p>You can also <span class="share">[Share<span class="share-content">on
<a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Gear+Ratios%22+%2D+Day+3+%2D+Advent+of+Code+2023&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F3&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
<a href="javascript:void(0);" onclick="var ms; try{ms=localStorage.getItem('mastodon.server')}finally{} if(typeof ms!=='string')ms=''; ms=prompt('Mastodon Server?',ms); if(typeof ms==='string' && ms.length){this.href='https://'+ms+'/share?text=I%27ve+completed+%22Gear+Ratios%22+%2D+Day+3+%2D+Advent+of+Code+2023+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F3';try{localStorage.setItem('mastodon.server',ms);}finally{}}else{return false;}" target="_blank">Mastodon</a
></span>]</span> this puzzle.</p>
</main>
</body>
</html>
123 changes: 37 additions & 86 deletions src/2023/day03.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,51 @@
function signed(map, i, j) {
const neighbors = [
map[i - 1]?.[j - 1],
map[i - 1]?.[j],
map[i - 1]?.[j + 1],
map[i]?.[j - 1],
map[i]?.[j + 1],
map[i + 1]?.[j - 1],
map[i + 1]?.[j],
map[i + 1]?.[j + 1],
];
return neighbors.some(n => n !== '.' && (n < '0' || n > '9'));
function signed(map, i, j, check = c => c && c !== '.' && Number.isNaN(+c)) {
return [
{ pos: `${i - 1},${j - 1}`, c: map[i - 1]?.[j - 1] },
{ pos: `${i - 1},${j}`, c: map[i - 1]?.[j] },
{ pos: `${i - 1},${j + 1}`, c: map[i - 1]?.[j + 1] },
{ pos: `${i},${j - 1}`, c: map[i]?.[j - 1] },
{ pos: `${i},${j + 1}`, c: map[i]?.[j + 1] },
{ pos: `${i + 1},${j - 1}`, c: map[i + 1]?.[j - 1] },
{ pos: `${i + 1},${j}`, c: map[i + 1]?.[j] },
{ pos: `${i + 1},${j + 1}`, c: map[i + 1]?.[j + 1] },
].find(({ c }) => check(c))?.pos;
}

function gear(map, i, j) {
const neighbors = [
{ x: i - 1, y: j - 1, c: map[i - 1]?.[j - 1] },
{ x: i - 1, y: j, c: map[i - 1]?.[j] },
{ x: i - 1, y: j + 1, c: map[i - 1]?.[j + 1] },
{ x: i, y: j - 1, c: map[i]?.[j - 1] },
{ x: i, y: j + 1, c: map[i]?.[j + 1] },
{ x: i + 1, y: j - 1, c: map[i + 1]?.[j - 1] },
{ x: i + 1, y: j, c: map[i + 1]?.[j] },
{ x: i + 1, y: j + 1, c: map[i + 1]?.[j + 1] },
];
const gears = neighbors.filter(n => n.c === '*');
if (gears.length > 1) {
console.log(gears); //?
}
return gears.length === 0 ? undefined : `${gears[0].x},${gears[0].y}`;
}

export function part1(input) {
function parse(input, check) {
const map = input.split('\n').map(line => line.split(''));
let current = '';
let sign = false;
let sum = 0;
let signs = {};
for (let i = 0; i < map.length; i++) {
const line = map[i];
for (let j = 0; j < line.length; j++) {
const char = line[j];
if (char >= '0' && char <= '9') {
current += char;
if (signed(map, i, j)) {
sign = true;
}
let current = '';
let pos = undefined;
for (let j = 0; j < map[i].length; j++) {
if (Number.isInteger(+map[i][j])) {
current += map[i][j];
pos = pos || signed(map, i, j, check);
} else {
if (current) {
if (sign) sum += +current;
current = '';
sign = false;
if (current && pos) {
signs[pos] = signs[pos] ? signs[pos].concat(+current) : [+current];
}
current = '';
pos = undefined;
}
}
if (current) {
if (sign) sum += +current;
current = '';
sign = false;
if (current && pos) {
signs[pos] = signs[pos] ? signs[pos].concat(+current) : [+current];
}
}
return sum;
return signs;
}

export function part1(input) {
const signs = parse(input);
return Object.values(signs)
.flat()
.reduce((a, b) => a + b, 0);
}

export function part2(input) {
const map = input.split('\n').map(line => line.split(''));
let current = '';
let pos = undefined;
let gears = {};
for (let i = 0; i < map.length; i++) {
const line = map[i];
for (let j = 0; j < line.length; j++) {
const char = line[j];
if (char >= '0' && char <= '9') {
current += char;
pos = pos || gear(map, i, j);
} else {
if (current) {
if (pos)
gears[pos] = gears[pos] ? gears[pos].concat(current) : [+current];
current = '';
pos = undefined;
}
}
}
if (current) {
if (pos)
gears[pos] = gears[pos] ? gears[pos].concat(current) : [+current];
current = '';
pos = undefined;
}
}
gears = Object.values(gears).map(gear => {
if (gear.length > 1) {
return gear.reduce((a, b) => a * b, 1);
} else {
return 0;
}
});
return Object.values(gears).reduce((a, b) => a + b, 0);
const gears = parse(input, c => c === '*');
return Object.values(gears)
.map(gear => (gear.length > 1 ? gear.reduce((a, b) => a * b, 1) : 0))
.reduce((a, b) => a + b, 0);
}
4 changes: 2 additions & 2 deletions src/2023/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
<main>
<article>
<p>From here, you can access all of the events (and the corresponding puzzles, leaderboards, stats, etc) ever run on Advent of Code:</p>
<div class="eventlist-event"><a href="../2023/index.html">[2023]</a> <span class="star-count"> 4*</span> <a href="/2023/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2023/index.html">[2023]</a> <span class="star-count"> 6*</span> <a href="/2023/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2022/index.html">[2022]</a> <span class="star-count">50*</span> <a href="/2022/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2021/index.html">[2021]</a> <span class="star-count">50*</span> <a href="/2021/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2020/index.html">[2020]</a> <span class="star-count">50*</span> <a href="/2020/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
Expand All @@ -41,7 +41,7 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
<div class="eventlist-event"><a href="../2017/index.html">[2017]</a> <span class="star-count">50*</span> <a href="/2017/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2016/index.html">[2016]</a> <span class="star-count">50*</span> <a href="/2016/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<div class="eventlist-event"><a href="../2015/index.html">[2015]</a> <span class="star-count">50*</span> <a href="/2015/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a></div>
<p>Total stars: <span class="star-count">404*</span></article>
<p>Total stars: <span class="star-count">406*</span></article>
</main>
</body>
</html>
25 changes: 13 additions & 12 deletions src/2023/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
</header>
<main>
<style>
.calendar .calendar-color-n { color:#9b715b; }
.calendar .calendar-color-w { color:#ffffff; }
.calendar .calendar-color-y { color:#ffff66; text-shadow:0 0 5px #ffff66, 0 0 10px #ffff66; }
.calendar .calendar-color-m { color:#d4dde4; }
.calendar .calendar-color-l { color:#ccccff; }
.calendar .calendar-color-n { color:#9b715b; }
.calendar .calendar-color-w { color:#ffffff; }
</style>
<pre class="calendar"><span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> </span>
Expand All @@ -53,17 +54,13 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
<span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> <span class="calendar-color-w">'</span> <span class="calendar-color-l">-</span> </span>
<span aria-hidden="true" class="calendar-day3"> <span class="calendar-color-w">'</span> <span class="calendar-color-w">.</span> <span class="calendar-color-l">-</span> <span class="calendar-color-l">-</span> * <span class="calendar-color-w">.</span> <span class="calendar-day"> 3</span><span id="calendar-countdown"></span><script>
<span aria-hidden="true" class=""> <span class="calendar-color-w">...''''</span> </span>
<span aria-hidden="true" class="calendar-day4"> <span class="calendar-color-w">.''</span> * <span class="calendar-day"> 4</span><span id="calendar-countdown"></span><script>
(function(){
var countdown = document.getElementById("calendar-countdown");
if (!countdown) return;
var server_eta = 74251;
var key = "2023-3-"+server_eta;
var server_eta = 82242;
var key = "2023-4-"+server_eta;
var now = Math.floor(new Date().getTime()/1000);
var target = server_eta + now;
if (sessionStorage) {
Expand All @@ -86,8 +83,8 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
countdown.textContent = "";

var a = document.createElement("a");
a[String.fromCharCode(104,114,101,102)] = "/2023" + String.fromCharCode(47,100,97,121,47) + "3";
a.className = "calendar-day3 calendar-day-new";
a[String.fromCharCode(104,114,101,102)] = "/2023" + String.fromCharCode(47,100,97,121,47) + "4";
a.className = "calendar-day4 calendar-day-new";
var span = countdown.parentNode;
while (span.firstChild) {
a.appendChild(span.firstChild);
Expand All @@ -109,6 +106,10 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
update_countdown();
})();
</script></span>
<span aria-hidden="true" class=""> <span class="calendar-color-w">.'</span> <span class="calendar-color-m">/</span> </span>
<span aria-hidden="true" class=""> <span class="calendar-color-w">:</span> <span class="calendar-color-w">/\</span> <span class="calendar-color-l">-</span><span class="calendar-color-m">/</span> <span class="calendar-color-w">:</span> </span>
<span aria-hidden="true" class=""> <span class="calendar-color-w">'.</span> <span class="calendar-color-l">-</span> <span class="calendar-color-l">-</span> <span class="calendar-color-m">/</span> <span class="calendar-color-w">.'</span> </span>
<a aria-label="Day 3, two stars" href="day03.html" class="calendar-day3 calendar-verycomplete"> <span class="calendar-color-w">'..</span> <span class="calendar-color-l">-</span> <span class="calendar-color-l">-</span> <span class="calendar-color-y">*</span><span class="calendar-color-w">..'</span> <span class="calendar-day"> 3</span> <span class="calendar-mark-complete">*</span><span class="calendar-mark-verycomplete">*</span></a>
<a aria-label="Day 2, two stars" href="day02.html" class="calendar-day2 calendar-verycomplete"> <span class="calendar-color-n">----@</span> <span class="calendar-color-w">'''..</span><span class="calendar-color-y">*</span><span class="calendar-color-w">......'''</span> <span class="calendar-day"> 2</span> <span class="calendar-mark-complete">*</span><span class="calendar-mark-verycomplete">*</span></a>
<a aria-label="Day 1, two stars" href="day01.html" class="calendar-day1 calendar-verycomplete"> <span class="calendar-color-y">*</span> <span class="calendar-color-n">!</span> <span class="calendar-color-n">/^\</span> <span class="calendar-day"> 1</span> <span class="calendar-mark-complete">*</span><span class="calendar-mark-verycomplete">*</span></a>
</pre>
Expand Down
379 changes: 283 additions & 96 deletions src/2023/leaderboard.html

Large diffs are not rendered by default.

0 comments on commit 5434b44

Please sign in to comment.