Skip to content

Commit 12b6a59

Browse files
committed
2023/03
1 parent 72ea645 commit 12b6a59

File tree

6 files changed

+287
-19
lines changed

6 files changed

+287
-19
lines changed

2023/Day03/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
original source: [https://adventofcode.com/2023/day/3](https://adventofcode.com/2023/day/3)
2+
## --- Day 3: Gear Ratios ---
3+
You and the Elf eventually reach a [gondola lift](https://en.wikipedia.org/wiki/Gondola_lift) 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.
4+
5+
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
6+
7+
"Aaah!"
8+
9+
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.
10+
11+
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.
12+
13+
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.)
14+
15+
Here is an example engine schematic:
16+
17+
<pre>
18+
<code>467..114..
19+
...*......
20+
..35..633.
21+
......#...
22+
617*......
23+
.....+.58.
24+
..592.....
25+
......755.
26+
...$.*....
27+
.664.598..
28+
</code>
29+
</pre>
30+
31+
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>.
32+
33+
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>
34+
35+
36+
## --- Part Two ---
37+
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.
38+
39+
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.
40+
41+
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.
42+
43+
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 multiplying those two numbers together.
44+
45+
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.
46+
47+
Consider the same engine schematic again:
48+
49+
<pre>
50+
<code>467..114..
51+
...*......
52+
..35..633.
53+
......#...
54+
617*......
55+
.....+.58.
56+
..592.....
57+
......755.
58+
...$.*....
59+
.664.598..
60+
</code>
61+
</pre>
62+
63+
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>.
64+
65+
<em>What is the sum of all of the gear ratios in your engine schematic?</em>
66+
67+

2023/Day03/Solution.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text.RegularExpressions;
4+
5+
namespace AdventOfCode.Y2023.Day03;
6+
7+
[ProblemName("Gear Ratios")]
8+
class Solution : Solver
9+
{
10+
public object PartOne(string input) {
11+
var rows = input.Split("\n");
12+
var symbols = Parse(rows, new Regex(@"[^.0-9]"));
13+
var numbers = Parse(rows, new Regex(@"\d+"));
14+
15+
return (
16+
from n in numbers
17+
where symbols.Any(s => NextTo(s, n))
18+
select n.Number
19+
).Sum();
20+
}
21+
22+
public object PartTwo(string input) {
23+
var rows = input.Split("\n");
24+
var gears = Parse(rows, new Regex(@"\*"));
25+
var nums = Parse(rows, new Regex(@"\d+"));
26+
27+
return (
28+
from g in gears
29+
let neighbours = from n in nums where NextTo(n, g) select n.Number
30+
where neighbours.Count() == 2
31+
select neighbours.First() * neighbours.Last()
32+
).Sum();
33+
}
34+
35+
// checks that the parts are touching each other, i.e. rows are within 1
36+
// step also the columns (using https://stackoverflow.com/a/3269471).
37+
bool NextTo(Part p1, Part p2) =>
38+
Math.Abs(p2.irow - p1.irow) <= 1 &&
39+
p1.icol <= p2.icol + p2.value.ToString().Length &&
40+
p2.icol <= p1.icol + p1.value.ToString().Length;
41+
42+
// returns the matches of rx with its coordinates
43+
Part[] Parse(string[] rows, Regex rx) => (
44+
from irow in Enumerable.Range(0, rows.Length)
45+
from match in rx.Matches(rows[irow])
46+
select new Part(match.Value, irow, match.Index)
47+
).ToArray();
48+
}
49+
50+
record Part(string value, int irow, int icol) {
51+
public int Number => int.Parse(value);
52+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy