We stayed in Sheerness (where this flight took place), and when my girlfriend saw this she immediately asked “Did pigs fly before women did?”. And the answer turned out to be no, women beat pigs by two weeks: “Sarah Van Deman … was the woman who flew with Wilbur Wright on October 27, 1909” source
I’m cheating a bit by posting this as it does take 11s for the full part 2 solution, but having tracked down and eliminated the excessively long path for part 1, I can’t be bothered to do it again for part 2.
I’m an idiot. Avoiding recursively adding the same points to the seen set dropped total runtime to a hair under 0.5s, so line-seconds are around 35.
Just banging strings together again. Simple enough once I understood that the original reflection may also be valid after desmudging. I don’t know if we were supposed to do something clever for part two, but my part 1 was fast enough that I could just try every possible smudge location and part 2 still ran in 80ms
There is a really simple approach that I describe in my comment on the megathread, but it’s always good to have a nice visualisation so thanks for sharing!
Imagine you’re looking at a grid with your path drawn out on it. On any given row, start from the left and move right, cell by cell. You’re outside the area enclosed by your path at the start of the row. As you move across that row, you remain outside it until you meet and cross the line made by your path. Every non-path cell you now pass can be added to your ‘inside’ count, until you next cross your path, when you stop counting until you cross the path again, and so on.
In this problem, you can tell you’re crossing the path when you encounter one of:
a '|'
a ‘F’ (followed by 0 or more '-'s) followed by 'J’
a ‘L’ (followed by 0 or more '-'s) followed by ‘7’
If you encounter an ‘F’ (followed by 0 or more '-'s) followed by ‘7’, you’ve actually just skimmed along the line and not actually crossed it. Same for the ‘L’/ ‘J’ pair.
Try it out by hand on the example grids and you should get the hang of the logic.
As promised, just a little later than planned. I do like this solution as it’s actually using arrays rather than just imperative programming in fancy dress. Run it here
<span style="color:#323232;">Grid ← =@# [
</span><span style="color:#323232;"> "...#......"
</span><span style="color:#323232;"> ".......#.."
</span><span style="color:#323232;"> "#........."
</span><span style="color:#323232;"> ".........."
</span><span style="color:#323232;"> "......#..."
</span><span style="color:#323232;"> ".#........"
</span><span style="color:#323232;"> ".........#"
</span><span style="color:#323232;"> ".........."
</span><span style="color:#323232;"> ".......#.."
</span><span style="color:#323232;"> "#...#....."
</span><span style="color:#323232;">]
</span><span style="color:#323232;">
</span><span style="color:#323232;">GetDist! ← (
</span><span style="color:#323232;"> # Build arrays of rows, cols of galaxies
</span><span style="color:#323232;"> ⊙(⊃(◿)(⌊÷)⊃(⧻⊢)(⊚=1/⊂)).
</span><span style="color:#323232;"> # check whether each row/col is just space
</span><span style="color:#323232;"> # and so calculate its relative position
</span><span style="color:#323232;"> ∩(++1^1=0/+)⍉.
</span><span style="color:#323232;"> # Map galaxy co-ords to these values
</span><span style="color:#323232;"> ⊏:⊙(:⊏ :)
</span><span style="color:#323232;"> # Map to [x, y] pairs, build cross product,
</span><span style="color:#323232;"> # and sum all topright values.
</span><span style="color:#323232;"> /+≡(/+↘⊗0.)⊠(/+⌵-).⍉⊟
</span><span style="color:#323232;">)
</span><span style="color:#323232;">GetDist!(×1) Grid
</span><span style="color:#323232;">GetDist!(×99) Grid
</span>
Finally got round to solving part 2. Very easy once I realised it’s just a matter of counting line crossings.
Edit: having now read the other comments here, I’m reminded that the line-crossing logic is actually an application of Jordan’s Curve Theorem which looks like a mathematical joke when you first see it, but turns out to be really useful here!
<span style="color:#323232;">var up = Point(0, -1),
</span><span style="color:#323232;"> down = Point(0, 1),
</span><span style="color:#323232;"> left = Point(-1, 0),
</span><span style="color:#323232;"> right = Point(1, 0);
</span><span style="color:#323232;">var pipes = >>{
</span><span style="color:#323232;"> '|': [up, down],
</span><span style="color:#323232;"> '-': [left, right],
</span><span style="color:#323232;"> 'L': [up, right],
</span><span style="color:#323232;"> 'J': [up, left],
</span><span style="color:#323232;"> '7': [left, down],
</span><span style="color:#323232;"> 'F': [right, down],
</span><span style="color:#323232;">};
</span><span style="color:#323232;">late List> grid; // Make grid global for part 2
</span><span style="color:#323232;">Set> buildPath(List lines) {
</span><span style="color:#323232;"> grid = lines.map((e) => e.split('')).toList();
</span><span style="color:#323232;"> var points = {
</span><span style="color:#323232;"> for (var row in grid.indices())
</span><span style="color:#323232;"> for (var col in grid.first.indices()) Point(col, row): grid[row][col]
</span><span style="color:#323232;"> };
</span><span style="color:#323232;"> // Find the starting point.
</span><span style="color:#323232;"> var pos = points.entries.firstWhere((e) => e.value == 'S').key;
</span><span style="color:#323232;"> var path = {pos};
</span><span style="color:#323232;"> // Replace 'S' with assumed pipe.
</span><span style="color:#323232;"> var dirs = [up, down, left, right].where((el) =>
</span><span style="color:#323232;"> points.keys.contains(pos + el) &&
</span><span style="color:#323232;"> pipes.containsKey(points[pos + el]) &&
</span><span style="color:#323232;"> pipes[points[pos + el]]!.contains(Point(-el.x, -el.y)));
</span><span style="color:#323232;"> grid[pos.y][pos.x] = pipes.entries
</span><span style="color:#323232;"> .firstWhere((e) =>
</span><span style="color:#323232;"> (e.value.first == dirs.first) && (e.value.last == dirs.last) ||
</span><span style="color:#323232;"> (e.value.first == dirs.last) && (e.value.last == dirs.first))
</span><span style="color:#323232;"> .key;
</span><span style="color:#323232;">
</span><span style="color:#323232;"> // Follow the path.
</span><span style="color:#323232;"> while (true) {
</span><span style="color:#323232;"> var nd = dirs.firstWhereOrNull((e) =>
</span><span style="color:#323232;"> points.containsKey(pos + e) &&
</span><span style="color:#323232;"> !path.contains(pos + e) &&
</span><span style="color:#323232;"> (points[pos + e] == 'S' || pipes.containsKey(points[pos + e])));
</span><span style="color:#323232;"> if (nd == null) break;
</span><span style="color:#323232;"> pos += nd;
</span><span style="color:#323232;"> path.add(pos);
</span><span style="color:#323232;"> dirs = pipes[points[pos]]!;
</span><span style="color:#323232;"> }
</span><span style="color:#323232;"> return path;
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">part1(List lines) => buildPath(lines).length ~/ 2;
</span><span style="color:#323232;">part2(List lines) {
</span><span style="color:#323232;"> var path = buildPath(lines);
</span><span style="color:#323232;"> var count = 0;
</span><span style="color:#323232;"> for (var r in grid.indices()) {
</span><span style="color:#323232;"> var outside = true;
</span><span style="color:#323232;"> // We're only interested in how many times we have crossed the path
</span><span style="color:#323232;"> // to get to any given point, so mark anything that's not on the path
</span><span style="color:#323232;"> // as '*' for counting, and collapse all uninteresting path segments.
</span><span style="color:#323232;"> var row = grid[r]
</span><span style="color:#323232;"> .indexed()
</span><span style="color:#323232;"> .map((e) => path.contains(Point(e.index, r)) ? e.value : '*')
</span><span style="color:#323232;"> .join('')
</span><span style="color:#323232;"> .replaceAll('-', '')
</span><span style="color:#323232;"> .replaceAll('FJ', '|') // zigzag
</span><span style="color:#323232;"> .replaceAll('L7', '|') // other zigzag
</span><span style="color:#323232;"> .replaceAll('LJ', '') // U-bend
</span><span style="color:#323232;"> .replaceAll('F7', ''); // n-bend
</span><span style="color:#323232;"> for (var c in row.split('')) {
</span><span style="color:#323232;"> if (c == '|') {
</span><span style="color:#323232;"> outside = !outside;
</span><span style="color:#323232;"> } else {
</span><span style="color:#323232;"> if (!outside && c == '*') count += 1;
</span><span style="color:#323232;"> }
</span><span style="color:#323232;"> }
</span><span style="color:#323232;"> }
</span><span style="color:#323232;"> return count;
</span><span style="color:#323232;">}
</span>
It’s so humbling when you’ve hammered out a solution and then realise you’ve been paddling around in waters that have already been mapped out by earlier explorers!
I was getting a bad feeling when it explained in such detail how to solve part 1 that part 2 was going to be some sort of nightmare of traversing all those generated numbers in some complex fashion, but this has got to be one of the shortest solutions I’ve ever written for an AoC challenge.
I’m glad I took the time to read the directions very carefully before starting coding :-)
Top Tip: my ranking of hand types relies on the fact that if you count instances of each face and sort the resulting list from high to low, you get a list that when compared with lists from other hands gives an exact correspondence with the order of the hand types as defined, so no need for a bunch of if/thens, just
<span style="color:#323232;"> var type = Multiset.from(hand).counts.sorted(descending).join('');
</span>
Otherwise it should all be pretty self-explanatory apart from where I chose to map card rank to hex digits in order to facilitate sorting, so ‘b’ means ‘J’!
<span style="color:#323232;">int descending(T a, T b) => b.compareTo(a);
</span><span style="color:#323232;">var cToH = " 23456789TJQKA"; // used to map card rank to hex for sorting.
</span><span style="color:#323232;">
</span><span style="color:#323232;">handType(List hand, {wildcard = false}) {
</span><span style="color:#323232;"> var type = Multiset.from(hand).counts.sorted(descending).join('');
</span><span style="color:#323232;"> var i = hand.indexOf('b');
</span><span style="color:#323232;"> return (!wildcard || i == -1)
</span><span style="color:#323232;"> ? type
</span><span style="color:#323232;"> : '23456789acde'
</span><span style="color:#323232;"> .split('')
</span><span style="color:#323232;"> .map((e) => handType(hand.toList()..[i] = e, wildcard: true))
</span><span style="color:#323232;"> .fold(type, (s, t) => s.compareTo(t) >= 0 ? s : t);
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">solve(List lines, {wildcard = false}) => lines
</span><span style="color:#323232;"> .map((e) {
</span><span style="color:#323232;"> var l = e.split(' ');
</span><span style="color:#323232;"> var hand =
</span><span style="color:#323232;"> l.first.split('').map((e) => cToH.indexOf(e).toRadixString(16));
</span><span style="color:#323232;"> var type = handType(hand.toList(), wildcard: wildcard);
</span><span style="color:#323232;"> if (wildcard) hand = hand.map((e) => e == 'b' ? '0' : e);
</span><span style="color:#323232;"> return (hand.join(), type, int.parse(l.last));
</span><span style="color:#323232;"> })
</span><span style="color:#323232;"> .sorted((a, b) {
</span><span style="color:#323232;"> var c = a.$2.compareTo(b.$2);
</span><span style="color:#323232;"> return (c == 0) ? a.$1.compareTo(b.$1) : c;
</span><span style="color:#323232;"> })
</span><span style="color:#323232;"> .indexed(offset: 1)
</span><span style="color:#323232;"> .map((e) => e.value.$3 * e.index)
</span><span style="color:#323232;"> .sum;
</span><span style="color:#323232;">
</span><span style="color:#323232;">part1(List lines) => solve(lines);
</span><span style="color:#323232;">
</span><span style="color:#323232;">part2(List lines) => solve(lines, wildcard: true);
</span>
It works, but even I can’t understand this code any more as I’m well into my second beer, so don’t put this into production, okay? (Run it here if you dare.)
I decided to use the quadratic formula to solve part 1 which slowed me down while I struggled to remember how it went, but meant that part 2 was a one line change.
This year really is a roller coaster…
<span style="color:#323232;">int countGoodDistances(int time, int targetDistance) {
</span><span style="color:#323232;"> var det = sqrt(time * time - 4 * targetDistance);
</span><span style="color:#323232;"> return (((time + det) / 2).ceil() - 1) -
</span><span style="color:#323232;"> (max(((time - det) / 2).floor(), 0) + 1) +
</span><span style="color:#323232;"> 1;
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">solve(List> data, [param]) {
</span><span style="color:#323232;"> var distances = data.first
</span><span style="color:#323232;"> .indices()
</span><span style="color:#323232;"> .map((ix) => countGoodDistances(data[0][ix], data[1][ix]));
</span><span style="color:#323232;"> return distances.reduce((s, t) => s * t);
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">getNums(l) => l.split(RegExp(r's+')).skip(1);
</span><span style="color:#323232;">
</span><span style="color:#323232;">part1(List lines) =>
</span><span style="color:#323232;"> solve([for (var l in lines) getNums(l).map(int.parse).toList()]);
</span><span style="color:#323232;">
</span><span style="color:#323232;">part2(List lines) => solve([
</span><span style="color:#323232;"> for (var l in lines) [int.parse(getNums(l).join(''))]
</span><span style="color:#323232;"> ]);
</span>
The first pig to fly. 1909. (lemmy.world)
Article and more pig pictures.
Gotcha (lemmy.world)
rule (lemmy.world)
It's canon now. And so is a certain image format. (lemmy.world)
It's a curse (lemmy.world)
🦌 - 2023 DAY 16 SOLUTIONS -🦌
Day 16: The Floor Will Be Lava...
🎄 - 2023 DAY 15 SOLUTIONS -🎄
Day 15: Lens Library...
🍪 - 2023 DAY 14 SOLUTIONS -🍪
Day 14: Parabolic Reflector Dish...
🌟 - 2023 DAY 13 SOLUTIONS -🌟
Day 13: Point of Incidence...
[2023 day 10] part 2 seemed too hard so I visualized part 1 instead (youtu.be)
I wanted to show how the maze follows from individual letters but it was way too large
☃️ - 2023 DAY 11 SOLUTIONS - ☃️
Day 11: Cosmic Expansion...
❄️ - 2023 DAY 10 SOLUTIONS -❄️
Day 10: Pipe Maze...
🦌 - 2023 DAY 9 SOLUTIONS -🦌
Day 9: Mirage Maintenance...
🍪 - 2023 DAY 7 SOLUTIONS -🍪
Day 7: Camel Cards...
🌟 - 2023 DAY 6 SOLUTIONS -🌟
# Day 6: Wait for It...
[2023 Day 5] I might have broken the one second guideline a little (lemmy.world)