Skip to content

Commit 6720841

Browse files
13-2023 Part 1
1 parent e3267f9 commit 6720841

File tree

6 files changed

+207
-31
lines changed

6 files changed

+207
-31
lines changed

2023/Day13/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,66 @@ To <em>summarize</em> your pattern notes, add up <em>the number of columns</em>
7272
Find the line of reflection in each of the patterns in your notes. <em>What number do you get after summarizing all of your notes?</em>
7373

7474

75+
## --- Part Two ---
76+
You resume walking through the valley of mirrors and - <em>SMACK!</em> - run directly into one. Hopefully nobody was watching, because that must have been pretty embarrassing.
77+
78+
Upon closer inspection, you discover that every mirror has exactly one <em>smudge</em>: exactly one <code>.</code> or <code>#</code> should be the opposite type.
79+
80+
In each pattern, you'll need to locate and fix the smudge that causes a <em>different reflection line</em> to be valid. (The old reflection line won't necessarily continue being valid after the smudge is fixed.)
81+
82+
Here's the above example again:
83+
84+
<pre>
85+
<code>#.##..##.
86+
..#.##.#.
87+
##......#
88+
##......#
89+
..#.##.#.
90+
..##..##.
91+
#.#.##.#.
92+
93+
#...##..#
94+
#....#..#
95+
..##..###
96+
#####.##.
97+
#####.##.
98+
..##..###
99+
#....#..#
100+
</code>
101+
</pre>
102+
103+
The first pattern's smudge is in the top-left corner. If the top-left <code>#</code> were instead <code>.</code>, it would have a different, horizontal line of reflection:
104+
105+
<pre>
106+
<code>1 ..##..##. 1
107+
2 ..#.##.#. 2
108+
3v##......#v3
109+
4^##......#^4
110+
5 ..#.##.#. 5
111+
6 ..##..##. 6
112+
7 #.#.##.#. 7
113+
</code>
114+
</pre>
115+
116+
With the smudge in the top-left corner repaired, a new horizontal line of reflection between rows 3 and 4 now exists. Row 7 has no corresponding reflected row and can be ignored, but every other row matches exactly: row 1 matches row 6, row 2 matches row 5, and row 3 matches row 4.
117+
118+
In the second pattern, the smudge can be fixed by changing the fifth symbol on row 2 from <code>.</code> to <code>#</code>:
119+
120+
<pre>
121+
<code>1v#...##..#v1
122+
2^#...##..#^2
123+
3 ..##..### 3
124+
4 #####.##. 4
125+
5 #####.##. 5
126+
6 ..##..### 6
127+
7 #....#..# 7
128+
</code>
129+
</pre>
130+
131+
Now, the pattern has a different horizontal line of reflection between rows 1 and 2.
132+
133+
Summarize your notes as before, but instead use the new different reflection lines. In this example, the first pattern's new horizontal line has 3 rows above it and the second pattern's new horizontal line has 1 row above it, summarizing to the value <code><em>400</em></code>.
134+
135+
In each pattern, fix the smudge and find the different line of reflection. <em>What number do you get after summarizing the new reflection line in each pattern in your notes?</em>
136+
137+

2023/Day13/Solution.cs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,73 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Collections.Immutable;
43
using System.Linq;
5-
using System.Text.RegularExpressions;
6-
using System.Text;
4+
using adventofcode.Utils;
75

86
namespace AdventOfCode.Y2023.Day13;
97

108
[ProblemName("Point of Incidence")]
119
class Solution : Solver
1210
{
11+
// Thx to https://github.com/dpvdberg/aoc2023/blob/master/AdventOfCode2023/Days/Day13/Day13Solution.cs
12+
// Elegant solution, I was trying to do something similar but I was not able to get it to work
13+
private readonly Dictionary<int, (int num, bool isHorizontal)> mirrors = new();
14+
1315
public object PartOne(string input)
1416
{
15-
return 0;
17+
var blocks = ParseBlocks(input);
18+
var result = 0;
19+
for (var i = 0; i < blocks.Length; i++)
20+
{
21+
TryFindMirror(blocks[i], i, out var res);
22+
result += res;
23+
}
24+
25+
return result;
26+
}
27+
28+
private void TryFindMirror(string block, int key, out int result)
29+
{
30+
var lines = block.Split("\n", StringSplitOptions.RemoveEmptyEntries);
31+
var columns = block.SplitIntoColumns().ToList();
32+
33+
for (var i = 1; i < lines.Length; i++)
34+
{
35+
if (lines.Take(i).Reverse().Zip(lines.Skip(i)).All(x => x.First == x.Second))
36+
{
37+
if (mirrors.TryGetValue(key, out (int num, bool isHorizontal) value))
38+
{
39+
if (value.isHorizontal && value.num == i) continue;
40+
}
41+
42+
mirrors[key] = (i, true);
43+
result = i * 100;
44+
return;
45+
}
46+
}
47+
48+
for (var i = 1; i < columns.Count; i++)
49+
{
50+
if (columns.Take(i).Reverse().Zip(columns.Skip(i)).All(x => x.First == x.Second))
51+
{
52+
if (mirrors.TryGetValue(key, out (int num, bool isRow) value))
53+
{
54+
if (!value.isRow && value.num == i) continue;
55+
}
56+
57+
mirrors[key] = (i, false);
58+
result = i;
59+
return;
60+
}
61+
}
62+
63+
result = 0;
1664
}
1765

1866
public object PartTwo(string input)
1967
{
2068
return 0;
2169
}
22-
}
70+
71+
private static string[] ParseBlocks(string input) =>
72+
input.Split("\n\n", StringSplitOptions.RemoveEmptyEntries);
73+
}

2023/Day13/input.refout

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
27742

2023/SplashScreen.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,30 @@ public void Show() {
99

1010
var color = Console.ForegroundColor;
1111
Write(0xcc00, false, " ▄█▄ ▄▄█ ▄ ▄ ▄▄▄ ▄▄ ▄█▄ ▄▄▄ ▄█ ▄▄ ▄▄▄ ▄▄█ ▄▄▄\n █▄█ █ █ █ █ █▄█ █ █ █ █ █ █▄ ");
12-
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ // 2023\n \n ");
13-
Write(0xcc00, false, " ");
14-
Write(0x666666, false, " * ");
12+
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ int y = 2023;\n ");
13+
Write(0xcc00, false, " \n ");
14+
Write(0x666666, false, " . * ");
1515
Write(0xcccccc, false, "14 ");
16-
Write(0x666666, false, "**\n * ");
16+
Write(0x666666, false, "**\n . * ");
1717
Write(0xcccccc, false, "15 ");
1818
Write(0x666666, false, "**\n ");
1919
Write(0xd4dde4, false, "...");
20-
Write(0x666666, false, " * ");
20+
Write(0x666666, false, " . . . ' * ");
2121
Write(0xcccccc, false, "16 ");
2222
Write(0x666666, false, "**\n ");
2323
Write(0xd4dde4, false, ".''....' '..");
24-
Write(0x666666, false, " * ");
24+
Write(0x666666, false, "'. './\\*/\\ ~ ");
2525
Write(0xcccccc, false, "13 ");
26-
Write(0x666666, false, "**\n ");
26+
Write(0xffff66, false, "*");
27+
Write(0x666666, false, "*\n ");
2728
Write(0xd4dde4, false, "'.ZZ");
28-
Write(0x666666, false, " * ");
29+
Write(0x666666, false, " * '.. /\\ ");
2930
Write(0xcccccc, false, "17 ");
3031
Write(0x666666, false, "**\n ");
3132
Write(0xd4dde4, false, ".'''' ZZ");
3233
Write(0xffff66, true, "* ");
33-
Write(0xd4dde4, false, ".'''. ' ");
34+
Write(0xd4dde4, false, ".'''. '");
35+
Write(0x666666, false, "'''.... .. . ' ");
3436
Write(0xcccccc, false, "12 ");
3537
Write(0xffff66, false, "**\n ");
3638
Write(0xd4dde4, false, "'.... '...'");
@@ -52,10 +54,11 @@ public void Show() {
5254
Write(0xe3b585, false, ".");
5355
Write(0xd4dde4, false, "'''");
5456
Write(0xe3b585, false, "~ ~ ~ ~");
55-
Write(0x333333, false, " * ");
57+
Write(0x666666, false, " * ");
5658
Write(0x6b4d3b, false, "### ");
5759
Write(0xe3b585, false, "''. ");
58-
Write(0x666666, false, "19\n ");
60+
Write(0xcccccc, false, "19 ");
61+
Write(0x666666, false, "**\n ");
5962
Write(0xe3b585, false, ".' ~ ");
6063
Write(0xcc00, false, ",");
6164
Write(0xffff66, true, "* ");
@@ -77,9 +80,13 @@ public void Show() {
7780
Write(0xffff66, false, "**\n ");
7881
Write(0xe3b585, false, "'. ~ ");
7982
Write(0xcc00, false, "\" ' ");
80-
Write(0xe3b585, false, "~ ~ ~ ");
83+
Write(0xe3b585, false, "~ ~ ~");
84+
Write(0x666666, false, " * ");
8185
Write(0x6b4d3b, false, "##### ");
82-
Write(0xe3b585, false, ".' \n '.. ~ ~ ");
86+
Write(0xe3b585, false, ".' ");
87+
Write(0xcccccc, false, "20 ");
88+
Write(0x666666, false, "**\n ");
89+
Write(0xe3b585, false, "'.. ~ ~ ");
8390
Write(0xffff66, true, "* ");
8491
Write(0xe3b585, false, "~ ");
8592
Write(0x6b4d3b, false, "##### ");
@@ -101,9 +108,13 @@ public void Show() {
101108
Write(0x5555bb, false, "~ ");
102109
Write(0xcc00, false, "'...' ");
103110
Write(0x5555bb, false, "~");
104-
Write(0xcc00, false, "' '.");
111+
Write(0xcc00, false, "'");
112+
Write(0x333333, false, "* ");
113+
Write(0xcc00, false, "'.");
105114
Write(0x5555bb, false, "~ ");
106-
Write(0xcc00, false, "'.\n : ");
115+
Write(0xcc00, false, "'. ");
116+
Write(0x666666, false, "21\n ");
117+
Write(0xcc00, false, ": ");
107118
Write(0x5555bb, false, "~ ");
108119
Write(0xcc00, false, "'. ");
109120
Write(0xffff66, true, "*");

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