aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day24/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-05 17:20:50 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-05 17:20:50 +0800
commit4d40336c960b134093c97a7f01b14ba4b7eb9af6 (patch)
tree65b3b55344e4a7e97ce33bc2b550d8cac93617b1 /src/2022/day24/aoc.cpp
parentd023c6dad70be91ade175e1dfc51d1600fab7ee3 (diff)
downloadadvent-of-code-4d40336c960b134093c97a7f01b14ba4b7eb9af6.tar.gz
advent-of-code-4d40336c960b134093c97a7f01b14ba4b7eb9af6.zip
2022 day24 part1 dp
Diffstat (limited to 'src/2022/day24/aoc.cpp')
-rw-r--r--src/2022/day24/aoc.cpp15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/2022/day24/aoc.cpp b/src/2022/day24/aoc.cpp
index eeb3819..373e4ac 100644
--- a/src/2022/day24/aoc.cpp
+++ b/src/2022/day24/aoc.cpp
@@ -16,15 +16,18 @@ bool is_valid(pos p, const std::map<blizzard, int>&m, valley& v) {
return b && m.find(blizzard{p.x, p.y, '.'}) == m.end() && v.get(p.y, p.x) == '.';
}
-void expedition(int m, pos p, pos target, valley& v, int* min) {
+void expedition(int m, pos p, pos target, valley& v, int* min, bool pass) {
+ if (*min < INT32_MAX && m > *min) return;
if (p == target) {
printf("arrived by %d !!!\n", m);
if (*min > m) *min = m;
}
else {
- if (m < v.get_time(p.y, p.x)) {
- v.get_time(p.y, p.x) = m;
- printf("(%d, %d) in %d\n", p.x, p.y, m);
+ auto& t = v.get_time(p.y, p.x);
+
+ if (m < t || pass) {
+ // printf("(%d, %d) in %d, was %d\n", p.x, p.y, m, t);
+ if (!pass) t = m;
v.next();
std::map<blizzard, int> mp;
@@ -46,7 +49,7 @@ void expedition(int m, pos p, pos target, valley& v, int* min) {
auto mv = mv0[i];
if (is_valid(mv, mp, v)) {
auto blz = v.blz;
- expedition(m + 1, mv, target, v, min);
+ expedition(m + 1, mv, target, v, min, i > 2);
v.blz = blz;
}
}
@@ -66,7 +69,7 @@ std::pair<int, int> day24(line_view file) {
int min{INT32_MAX};
// v.print();
- expedition(0, {1, 0}, {150, 21}, v, &min);
+ expedition(0, {1, 0}, {150, 21}, v, &min, false);
return {min, 0};
}