Skip to content

Commit dd97298

Browse files
committed
day 13 with ocr
1 parent 091ad86 commit dd97298

File tree

6 files changed

+1208
-34
lines changed

6 files changed

+1208
-34
lines changed

2021/Day13/README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
original source: [https://adventofcode.com/2021/day/13](https://adventofcode.com/2021/day/13)
2+
## --- Day 13: Transparent Origami ---
3+
You reach another volcanically active part of the cave. It would be nice if you could do some kind of thermal imaging so you could tell ahead of time which caves are too hot to safely enter.
4+
5+
Fortunately, the submarine seems to be equipped with a thermal camera! When you activate it, you are greeted with:
6+
7+
<pre>
8+
<code>Congratulations on your purchase! To activate this infrared thermal imaging
9+
camera system, please enter the code found on page 1 of the manual.
10+
</code>
11+
</pre>
12+
13+
Apparently, the Elves have never used this feature. To your surprise, you manage to find the manual; as you go to open it, page 1 falls out. It's a large sheet of [transparent paper](https://en.wikipedia.org/wiki/Transparency_(projection))! The transparent paper is marked with random dots and includes instructions on how to fold it up (your puzzle input). For example:
14+
15+
<pre>
16+
<code>6,10
17+
0,14
18+
9,10
19+
0,3
20+
10,4
21+
4,11
22+
6,0
23+
6,12
24+
4,1
25+
0,13
26+
10,12
27+
3,4
28+
3,0
29+
8,4
30+
1,10
31+
2,14
32+
8,10
33+
9,0
34+
35+
fold along y=7
36+
fold along x=5
37+
</code>
38+
</pre>
39+
40+
The first section is a list of dots on the transparent paper. <code>0,0</code> represents the top-left coordinate. The first value, <code>x</code>, increases to the right. The second value, <code>y</code>, increases downward. So, the coordinate <code>3,0</code> is to the right of <code>0,0</code>, and the coordinate <code>0,7</code> is below <code>0,0</code>. The coordinates in this example form the following pattern, where <code>#</code> is a dot on the paper and <code>.</code> is an empty, unmarked position:
41+
42+
<pre>
43+
<code>...#..#..#.
44+
....#......
45+
...........
46+
#..........
47+
...#....#.#
48+
...........
49+
...........
50+
...........
51+
...........
52+
...........
53+
.#....#.##.
54+
....#......
55+
......#...#
56+
#..........
57+
#.#........
58+
</code>
59+
</pre>
60+
61+
Then, there is a list of <em>fold instructions</em>. Each instruction indicates a line on the transparent paper and wants you to fold the paper <em>up</em> (for horizontal <code>y=...</code> lines) or <em>left</em> (for vertical <code>x=...</code> lines). In this example, the first fold instruction is <code>fold along y=7</code>, which designates the line formed by all of the positions where <code>y</code> is <code>7</code> (marked here with <code>-</code>):
62+
63+
<pre>
64+
<code>...#..#..#.
65+
....#......
66+
...........
67+
#..........
68+
...#....#.#
69+
...........
70+
...........
71+
-----------
72+
...........
73+
...........
74+
.#....#.##.
75+
....#......
76+
......#...#
77+
#..........
78+
#.#........
79+
</code>
80+
</pre>
81+
82+
Because this is a horizontal line, fold the bottom half <em>up</em>. Some of the dots might end up overlapping after the fold is complete, but dots will never appear exactly on a fold line. The result of doing this fold looks like this:
83+
84+
<pre>
85+
<code>#.##..#..#.
86+
#...#......
87+
......#...#
88+
#...#......
89+
.#.#..#.###
90+
...........
91+
...........
92+
</code>
93+
</pre>
94+
95+
Now, only <code>17</code> dots are visible.
96+
97+
Notice, for example, the two dots in the bottom left corner before the transparent paper is folded; after the fold is complete, those dots appear in the top left corner (at <code>0,0</code> and <code>0,1</code>). Because the paper is transparent, the dot just below them in the result (at <code>0,3</code>) remains visible, as it can be seen through the transparent paper.
98+
99+
Also notice that some dots can end up <em>overlapping</em>; in this case, the dots merge together and become a single dot.
100+
101+
The second fold instruction is <code>fold along x=5</code>, which indicates this line:
102+
103+
<pre>
104+
<code>#.##.|#..#.
105+
#...#|.....
106+
.....|#...#
107+
#...#|.....
108+
.#.#.|#.###
109+
.....|.....
110+
.....|.....
111+
</code>
112+
</pre>
113+
114+
Because this is a vertical line, fold <em>left</em>:
115+
116+
<pre>
117+
<code>#####
118+
#...#
119+
#...#
120+
#...#
121+
#####
122+
.....
123+
.....
124+
</code>
125+
</pre>
126+
127+
The instructions made a square!
128+
129+
The transparent paper is pretty big, so for now, focus on just completing the first fold. After the first fold in the example above, <code><em>17</em></code> dots are visible - dots that end up overlapping after the fold is completed count as a single dot.
130+
131+
<em>How many dots are visible after completing just the first fold instruction on your transparent paper?</em>
132+
133+
134+
## --- Part Two ---
135+
Finish folding the transparent paper according to the instructions. The manual says the code is always <em>eight capital letters</em>.
136+
137+
<em>What code do you use to activate the infrared thermal imaging camera system?</em>
138+
139+

2021/Day13/Solution.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace AdventOfCode.Y2021.Day13;
6+
7+
[ProblemName("Transparent Origami")]
8+
class Solution : Solver {
9+
10+
public object PartOne(string input) => GetFolds(input).First().Count();
11+
public object PartTwo(string input) => Ocr(GetFolds(input).Last());
12+
13+
IEnumerable<HashSet<Point>> GetFolds(string input) {
14+
var blocks = input.Split("\n\n");
15+
// parse to points into a hashset
16+
var points = (
17+
from line in blocks[0].Split("\n")
18+
let coords = line.Split(",")
19+
select new Point(int.Parse(coords[0]), int.Parse(coords[1]))
20+
).ToHashSet();
21+
22+
// fold line by line, yield result
23+
foreach (var line in blocks[1].Split("\n")) {
24+
var rule = line.Split("=");
25+
if (rule[0].EndsWith("x")) {
26+
points = FoldX(int.Parse(rule[1]), points);
27+
} else {
28+
points = FoldY(int.Parse(rule[1]), points);
29+
}
30+
yield return points;
31+
}
32+
}
33+
34+
HashSet<Point> FoldX(int x, HashSet<Point> d) =>
35+
d.Select(p => p.x > x ? p with { x = 2 * x - p.x } : p).ToHashSet();
36+
37+
HashSet<Point> FoldY(int y, HashSet<Point> d) =>
38+
d.Select(p => p.y > y ? p with { y = 2 * y - p.y } : p).ToHashSet();
39+
40+
// Ocr for fun
41+
string Ocr(HashSet<Point> points) {
42+
var dict = new Dictionary<long, string>{
43+
{0x19297A52, "A"},
44+
{0x725C94B8, "B"},
45+
{0x32508498, "C"},
46+
{0x7A1C843C, "E"},
47+
{0x7A1C8420, "F"},
48+
{0x3D0E4210, "F"},
49+
{0x3250B49C, "G"},
50+
{0x252F4A52, "H"},
51+
{0x0C210A4C, "J"},
52+
{0x18421498, "J"},
53+
{0x2108421E, "L"},
54+
{0x7252E420, "P"},
55+
{0x7252E524, "R"},
56+
{0x462A2108, "Y"},
57+
{0x3C22221E, "Z"},
58+
{0, ""},
59+
};
60+
61+
var charWidth = 5;
62+
var width = points.MaxBy(p => p.x).x;
63+
var height = points.MaxBy(p => p.y).y;
64+
65+
var res = "";
66+
for (var ch = 0; ch < Math.Ceiling(width / (double)charWidth); ch++) {
67+
var hash = 0L;
68+
var st = "";
69+
for (var irow = 0; irow <= height; irow++) {
70+
for (var i = 0; i < charWidth; i++) {
71+
var icol = (ch * charWidth) + i;
72+
73+
if (points.Contains(new Point(icol, irow))) {
74+
hash += 1;
75+
st += "#";
76+
} else {
77+
st += ".";
78+
}
79+
hash <<= 1;
80+
}
81+
st += "\n";
82+
}
83+
if (!dict.ContainsKey(hash)) {
84+
throw new Exception($"Unrecognized letter with hash: 0x{hash.ToString("X")}\n{st}");
85+
}
86+
res += dict[hash];
87+
}
88+
return res;
89+
}
90+
}
91+
92+
record Point(int x, int y);

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