Add solution for 2021 day 17
This commit is contained in:
parent
659a6974c0
commit
8612463f8f
@ -20,7 +20,7 @@
|
||||
| 14 | ✓ | ✓ | [Erlang] |
|
||||
| 15 | ✓ | ✓ | [D] |
|
||||
| 16 | ✓ | ✓ | [Haskell] |
|
||||
| 17 | | | |
|
||||
| 17 | ✓ | ✓ | [Dart] |
|
||||
| 18 | | | |
|
||||
| 19 | | | |
|
||||
| 20 | | | |
|
||||
@ -52,3 +52,5 @@
|
||||
[erlang]: https://www.erlang.org
|
||||
[d]: https://dlang.org
|
||||
[haskell]: https://www.haskell.org
|
||||
[dart]: https://dart.dev
|
||||
[elixir]: https://elixir-lang.org
|
||||
|
2
2021/day-17/Justfile
Normal file
2
2021/day-17/Justfile
Normal file
@ -0,0 +1,2 @@
|
||||
@part PART INPUT_FILE="inputs/puzzle.txt":
|
||||
dart part_{{PART}}.dart {{INPUT_FILE}}
|
79
2021/day-17/common.dart
Normal file
79
2021/day-17/common.dart
Normal file
@ -0,0 +1,79 @@
|
||||
import "dart:io";
|
||||
import "dart:math";
|
||||
|
||||
List<List<int>> parseFile(String path) {
|
||||
final file = File(path);
|
||||
|
||||
final content = file.readAsStringSync();
|
||||
|
||||
final coordinates = content
|
||||
.trim().split(" ")
|
||||
.skip(2).where((e) => e.isNotEmpty)
|
||||
.map((elem) {
|
||||
return elem
|
||||
.split("=").last
|
||||
.replaceFirst(",", "").split("..")
|
||||
.map(int.parse)
|
||||
.toList();
|
||||
})
|
||||
.toList();
|
||||
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
List<int> step(int x, int y, int xv, int yv) {
|
||||
final dx = x + xv;
|
||||
final dy = y + yv;
|
||||
final dxv = xv + (xv > 0 ? -1 : (xv < 0 ? 1 : 0));
|
||||
final dyv = yv - 1;
|
||||
return [dx, dy, dxv, dyv];
|
||||
}
|
||||
|
||||
bool hasMissed(int x, int y, List<List<int>> area) {
|
||||
var missed = (x - area[0][1]) > 0;
|
||||
return missed || (y - area[1][0]) < 0;
|
||||
}
|
||||
|
||||
bool isInTargetArea(int x, int y, List<List<int>> area) {
|
||||
return
|
||||
x >= area[0][0] &&
|
||||
x <= area[0][1] &&
|
||||
y >= area[1][0] &&
|
||||
y <= area[1][1];
|
||||
}
|
||||
|
||||
bool landsInTargetArea(int init_vx, int init_vy, List<List<int>> area) {
|
||||
var x = 0;
|
||||
var y = 0;
|
||||
var vx = init_vx;
|
||||
var vy = init_vy;
|
||||
|
||||
while (true) {
|
||||
final result = step(x, y, vx, vy);
|
||||
if (isInTargetArea(result[0], result[1], area)) return true;
|
||||
if (hasMissed(result[0], result[1], area)) return false;
|
||||
x = result[0];
|
||||
y = result[1];
|
||||
vx = result[2];
|
||||
vy = result[3];
|
||||
}
|
||||
}
|
||||
|
||||
int? findMaxHeight(int init_vx, int init_vy, List<List<int>> area) {
|
||||
var x = 0;
|
||||
var y = 0;
|
||||
var vx = init_vx;
|
||||
var vy = init_vy;
|
||||
var max_height = 0;
|
||||
|
||||
while (true) {
|
||||
final result = step(x, y, vx, vy);
|
||||
max_height = max(result[1], max_height);
|
||||
if (isInTargetArea(result[0], result[1], area)) return max_height;
|
||||
if (hasMissed(result[0], result[1], area)) return null;
|
||||
x = result[0];
|
||||
y = result[1];
|
||||
vx = result[2];
|
||||
vy = result[3];
|
||||
}
|
||||
}
|
1
2021/day-17/inputs/puzzle.txt
Normal file
1
2021/day-17/inputs/puzzle.txt
Normal file
@ -0,0 +1 @@
|
||||
target area: x=277..318, y=-92..-53
|
1
2021/day-17/inputs/sample1.txt
Normal file
1
2021/day-17/inputs/sample1.txt
Normal file
@ -0,0 +1 @@
|
||||
target area: x=20..30, y=-10..-5
|
23
2021/day-17/part_one.dart
Normal file
23
2021/day-17/part_one.dart
Normal file
@ -0,0 +1,23 @@
|
||||
import "dart:math";
|
||||
import "common.dart";
|
||||
|
||||
main(List<String> args) {
|
||||
final path = args.first;
|
||||
|
||||
final target_area = parseFile(path);
|
||||
|
||||
final max_xv = target_area[0][1];
|
||||
final max_yv = target_area[1][0].abs();
|
||||
var max_height = 0;
|
||||
|
||||
Iterable.generate(max_xv, (v) => v + 1).forEach((x) {
|
||||
Iterable.generate(max_yv, (v) => v + 1).forEach((y) {
|
||||
final result = findMaxHeight(x, y, target_area);
|
||||
if (result != null) {
|
||||
max_height = max(result, max_height);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
print(max_height);
|
||||
}
|
30
2021/day-17/part_two.dart
Normal file
30
2021/day-17/part_two.dart
Normal file
@ -0,0 +1,30 @@
|
||||
import "dart:math";
|
||||
import "common.dart";
|
||||
|
||||
main(List<String> args) {
|
||||
final path = args.first;
|
||||
|
||||
final target_area = parseFile(path);
|
||||
|
||||
final max_xv = target_area[0][1];
|
||||
final max_yv = target_area[1][0];
|
||||
var velocity_count = 0;
|
||||
|
||||
var yv = max_yv;
|
||||
|
||||
while (yv < max_yv.abs()) {
|
||||
var xv = max_xv;
|
||||
|
||||
while (xv > 0) {
|
||||
if (landsInTargetArea(xv, yv, target_area)) {
|
||||
velocity_count += 1;
|
||||
}
|
||||
|
||||
xv -= 1;
|
||||
}
|
||||
|
||||
yv += 1;
|
||||
}
|
||||
|
||||
print(velocity_count);
|
||||
}
|
Loading…
Reference in New Issue
Block a user