Skip to content

Commit

Permalink
day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 4, 2024
1 parent 4441441 commit c82933e
Show file tree
Hide file tree
Showing 13 changed files with 804 additions and 249 deletions.
2 changes: 1 addition & 1 deletion src/2024/day01.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1 class="title-global"><a href="index.html">Advent of Code</a></h1>
</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">6*</span></div>
title="Member of sponsor: Wix Engineering">(Sponsor)</a> <span class="star-count">8*</span></div>
</div>
<div>
<h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=</span><a
Expand Down
2 changes: 1 addition & 1 deletion src/2024/day02.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1 class="title-global"><a href="index.html">Advent of Code</a></h1>
</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">6*</span></div>
title="Member of sponsor: Wix Engineering">(Sponsor)</a> <span class="star-count">8*</span></div>
</div>
<div>
<h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=</span><a
Expand Down
2 changes: 1 addition & 1 deletion src/2024/day03.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1 class="title-global"><a href="index.html">Advent of Code</a></h1>
</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">6*</span></div>
title="Member of sponsor: Wix Engineering">(Sponsor)</a> <span class="star-count">8*</span></div>
</div>
<div>
<h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=</span><a
Expand Down
100 changes: 100 additions & 0 deletions src/2024/day04.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<title>Day 4 - Advent of Code 2024</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">8*</span></div>
</div>
<div>
<h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=</span><a
href="index.html">2024</a><span class="title-event-wrap">;</span></h1>
</div>
</header>
<main>
<article class="day-desc"><h2>--- Day 4: Ceres Search ---</h2><p>"Looks like the Chief's not here. Next!" One of The Historians pulls out a device and pushes the only button on it. After a brief flash, you recognize the interior of the <a href="/2019/day/10">Ceres monitoring station</a>!</p>
<p>As the search for the Chief continues, a small Elf who lives on the station tugs on your shirt; she'd like to know if you could help her with her <em>word search</em> (your puzzle input). She only has to find one word: <code>XMAS</code>.</p>
<p>This word search allows words to be horizontal, vertical, diagonal, written backwards, or even overlapping other words. It's a little unusual, though, as you don't merely need to find one instance of <code>XMAS</code> - you need to find <em>all of them</em>. Here are a few ways <code>XMAS</code> might appear, where irrelevant characters have been replaced with <code>.</code>:<p>
<pre><code>..X...
.SAMX.
.A..A.
XMAS.S
.X....
</code></pre>
<p>The actual word search will be full of letters instead. For example:</p>
<pre><code>MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
</code></pre>
<p>In this word search, <code>XMAS</code> occurs a total of <code><em>18</em></code> times; here's the same word search again, but where letters not involved in any <code>XMAS</code> have been replaced with <code>.</code>:</p>
<pre><code>....XXMAS.
.SAMXMS...
...S..A...
..A.A.MS.X
XMASAMX.MM
X.....XA.A
S.S.S.S.SS
.A.A.A.A.A
..M.M.M.MM
.X.X.XMASX
</code></pre>
<p>Take a look at the little Elf's word search. <em>How many times does <code>XMAS</code> appear?</em></p>
</article>
<p>Your puzzle answer was <code>2603</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>The Elf looks quizzically at you. Did you misunderstand the assignment?</p>
<p>Looking for the instructions, you flip over the word search to find that this isn't actually an <code><em>XMAS</em></code> puzzle; it's an <span title="This part originally involved searching for something else, but this joke was too dumb to pass up."><code><em>X-MAS</em></code></span> puzzle in which you're supposed to find two <code>MAS</code> in the shape of an <code>X</code>. One way to achieve that is like this:</p>
<pre><code>M.S
.A.
M.S
</code></pre>
<p>Irrelevant characters have again been replaced with <code>.</code> in the above diagram. Within the <code>X</code>, each <code>MAS</code> can be written forwards or backwards.</p>
<p>Here's the same example from before, but this time all of the <code>X-MAS</code>es have been kept instead:</p>
<pre><code>.M.S......
..A..MSMS.
.M.S.MAA..
..A.ASMSM.
.M.S.M....
..........
S.S.S.S.S.
.A.A.A.A..
M.M.M.M.M.
..........
</code></pre>
<p>In this example, an <code>X-MAS</code> appears <code><em>9</em></code> times.</p>
<p>Flip the word search from the instructions back over to the word search side and try again. <em>How many times does an <code>X-MAS</code> appear?</em></p>
</article>
<p>Your puzzle answer was <code>1965</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="day04.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://bsky.app/intent/compose?text=I%27ve+completed+%22Ceres+Search%22+%2D+Day+4+%2D+Advent+of+Code+2024+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F4" target="_blank">Bluesky</a>
<a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Ceres+Search%22+%2D+Day+4+%2D+Advent+of+Code+2024&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F4&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+%22Ceres+Search%22+%2D+Day+4+%2D+Advent+of+Code+2024+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F4';try{localStorage.setItem('mastodon.server',ms);}finally{}}else{return false;}" target="_blank">Mastodon</a
></span>]</span> this puzzle.</p>
</main>
</body>
</html>
31 changes: 31 additions & 0 deletions src/2024/day04.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export function part1(input) {
let count = 0;
const m = input.split("\n");
for (let i = 0; i < m.length; i++) {
for (let j = 0; j < m[i].length; j++) {
const lookups = [
[m[i][j], m[i + 1]?.[j], m[i + 2]?.[j], m[i + 3]?.[j]],
[m[i][j], m[i]?.[j + 1], m[i]?.[j + 2], m[i]?.[j + 3]],
[m[i][j], m[i + 1]?.[j + 1], m[i + 2]?.[j + 2], m[i + 3]?.[j + 3]],
[m[i][j], m[i - 1]?.[j + 1], m[i - 2]?.[j + 2], m[i - 3]?.[j + 3]],
].map(lookup => lookup.join(""));
count += lookups.filter(x => x === "XMAS" || x === "SAMX").length;
}
}
return count;
}

export function part2(input) {
let count = 0;
const m = input.split("\n");
for (let i = 0; i < m.length; i++) {
for (let j = 0; j < m[i].length; j++) {
const lookups = [
[m[i][j], m[i + 1]?.[j + 1], m[i + 2]?.[j + 2]],
[m[i][j + 2], m[i + 1]?.[j + 1], m[i + 2]?.[j]],
].map(lookup => lookup.join(""));
if (lookups.filter(x => x === "MAS" || x === "SAM").length === 2) count++;
}
}
return count;
}
56 changes: 56 additions & 0 deletions src/2024/day04.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { part1, part2 } from "./day04.js";
import readInput from "../utils/read-input.js";

const input = readInput(import.meta.url);

describe("day04 2024", () => {
describe("part1", () => {
test("it should work for part 1 examples", () => {
expect(
part1(
[
"MMMSXXMASM",
"MSAMXMSMSA",
"AMXSXMAAMM",
"MSAMASMSMX",
"XMASAMXAMM",
"XXAMMXXAMA",
"SMSMSASXSS",
"SAXAMASAAA",
"MAMMMXMMMM",
"MXMXAXMASX",
].join("\n"),
),
).toEqual(18);
});

test("it should work for part 1 input", () => {
expect(part1(input)).toEqual(2603);
});
});

describe("part2", () => {
test("it should work for part 2 examples", () => {
expect(
part2(
[
"MMMSXXMASM",
"MSAMXMSMSA",
"AMXSXMAAMM",
"MSAMASMSMX",
"XMASAMXAMM",
"XXAMMXXAMA",
"SMSMSASXSS",
"SAXAMASAAA",
"MAMMMXMMMM",
"MXMXAXMASX",
].join("\n"),
),
).toEqual(9);
});

test("it should work for part 2 input", () => {
expect(part2(input)).toEqual(1965);
});
});
});
Loading

0 comments on commit c82933e

Please sign in to comment.