From 13d6fb2553cbd8231277127efb619a161959a8b7 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 9 Dec 2022 18:55:30 -0500 Subject: [PATCH] day9: cpp changes, add part2 basic --- day9/part1.cpp | 24 +++++++++++++------- day9/part2.bas | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ day9/part2.cpp | 3 ++- 3 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 day9/part2.bas diff --git a/day9/part1.cpp b/day9/part1.cpp index ecfbdd6..61b6f8d 100644 --- a/day9/part1.cpp +++ b/day9/part1.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -8,6 +7,7 @@ int main() { std::vector> steps; + std::vector hashes (10000, 0); while (1) { std::string line; @@ -19,13 +19,9 @@ int main() } int hx = 0, hy = 0, tx = 0, ty = 0; - std::list> locs; - locs.emplace_back(0, 0); for (const auto& [dir, count] : steps) { for (int i = 0; i < count; ++i) { - //std::cout << hx << ", " << hy << " " << tx << ", " << ty << std::endl; - if (dir == 'U') ++hy; else if (dir == 'D') @@ -41,14 +37,26 @@ int main() if (hy != ty) ty += hy > ty ? 1 : -1; - if (std::find(locs.cbegin(), locs.cend(), std::pair{tx, ty}) == locs.cend()) { - locs.emplace_back(tx, ty); + int hash = tx * 1000 + ty; + for (int i = 0; i < hashes.size(); ++i) { + if (hashes[i] == 0) { + hashes[i] = hash; + break; + } else if (hashes[i] == hash) { + break; + } } } } } - std::cout << locs.size() << std::endl; + int count = 0; + for (int i = 0; i < hashes.size(); ++i) { + ++count; + if (hashes[i] == 0) + break; + } + std::cout << count << std::endl; return 0; } diff --git a/day9/part2.bas b/day9/part2.bas new file mode 100644 index 0000000..917c83a --- /dev/null +++ b/day9/part2.bas @@ -0,0 +1,61 @@ +REM Advent of Code 2022: Day 9, part 1 +REM Written in Applesoft BASIC + +REM Same as part1, but track positions in SX and SY arrays. +REM At 405, we reuse HX/HY/TX/TY to minimize changes from part1. + + 10 DIM HS(7000) + 20 DIM SX(10) + 30 DIM SY(10) + 40 FOR I = 0 TO 9:SX(I) = 0: NEXT I + 50 FOR I = 0 TO 9:SY(I) = 0: NEXT I + 100 S$ = "" + 110 GET C$ + 120 IF ASC (C$) < 32 GOTO 200 + 130 S$ = S$ + C$ + 140 GOTO 110 + 200 IF LEN (S$) = 0 THEN GOTO 1000 + 210 D$ = LEFT$ (S$,1) + 220 S = VAL ( MID$ (S$,3, LEN (S$) - 2)) + 230 PRINT ">>",D$;" ";S + 300 FOR I = 1 TO S + 310 IF D$ = "U" THEN SY(0) = SY(0) + 1: GOTO 400 + 320 IF D$ = "D" THEN SY(0) = SY(0) - 1: GOTO 400 + 330 IF D$ = "R" THEN SX(0) = SX(0) + 1: GOTO 400 + 340 IF D$ = "L" THEN SX(0) = SX(0) - 1: GOTO 400 + 350 PRINT "ERROR: ";D$ + 360 END + 400 FOR K = 1 TO 9 + 405 HX = SX(K - 1):HY = SY(K - 1) + 410 TX = SX(K):TY = SY(K) + 415 DX = HX - TX:DY = HY - TY + 420 IF ABS (DX) < = 1 AND ABS (DY) < = 1 GOTO 460 + 430 IF HX < > TX THEN GOSUB 500 + 435 IF HY < > TY THEN GOSUB 550 + 440 SX(K) = TX:SY(K) = TY + 445 NEXT K + 450 HH = TX * 1000 + TY + 455 GOSUB 600 + 460 NEXT I + 470 GOTO 100 + 500 Z = - 1 + 510 IF HX > TX THEN Z = 1 + 520 TX = TX + Z + 530 RETURN + 550 Z = - 1 + 560 IF HY > TY THEN Z = 1 + 570 TY = TY + Z + 580 RETURN + 600 FOR J = 0 TO 6999 + 610 IF HS(J) = HH THEN J = 6999: GOTO 630 + 620 IF HS(J) = 0 THEN HS(J) = HH:J = 9999 + 630 NEXT J + 640 RETURN + 1000 CO = 0 + 1010 FOR I = 0 TO 9999 + 1020 CO = CO + 1 + 1030 IF HS(I) = 0 THEN I = 9999 + 1040 NEXT I + 1050 PRINT "TOTAL: ";CO + 1060 END + diff --git a/day9/part2.cpp b/day9/part2.cpp index d6da81b..bb7fda2 100644 --- a/day9/part2.cpp +++ b/day9/part2.cpp @@ -46,9 +46,10 @@ int main() ty += hy > ty ? 1 : -1; } + } + if (std::find(locs.cbegin(), locs.cend(), knots.back()) == locs.cend()) locs.emplace_back(knots.back()); - } } }