Skip to content

Commit cedef99

Browse files
committed
fixes
1 parent fbabc13 commit cedef99

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

scripts/runall.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ def main():
675675
parser.add_argument("-x", "--exclude", type=str, action="append", metavar="Y:D", help="exclude day")
676676
parser.add_argument("--verified", action="store_true", help="only inputs with solution")
677677
parser.add_argument("--no-slow", action="store_true", help="exclude slow solutions")
678+
parser.add_argument("--alt", action="store_true", help="run alternarive solutions too")
678679

679680
parser.add_argument("-r", "--refresh", action="store_true", help="relaunch solutions")
680681
parser.add_argument("-n", "--dry-run", action="store_true", help="do not run")
@@ -783,7 +784,9 @@ def main():
783784
continue
784785

785786
day_solutions = list(Path(f"src/year{year}").glob(f"day{day}"))
786-
day_solutions += Path(f"src/year{year}").glob(f"day{day}_*")
787+
788+
if args.alt:
789+
day_solutions += Path(f"src/year{year}").glob(f"day{day}_*")
787790

788791
for mday in day_solutions:
789792
mday = mday.name.removeprefix("day")
@@ -811,7 +814,7 @@ def main():
811814
print(end=f"{CR}{CLEAR_EOL}")
812815

813816
for lang, e in elapsed.items():
814-
stats_elapsed[year, day, lang] = (e, nb_samples)
817+
stats_elapsed[year, day, mday, lang] = (e, nb_samples)
815818

816819
if filter_year == 0:
817820
print(
@@ -829,16 +832,17 @@ def main():
829832

830833
finally:
831834
if stats_elapsed:
832-
languages = sorted(set(map(itemgetter(2), stats_elapsed.keys())))
835+
languages = sorted(set(map(itemgetter(3), stats_elapsed.keys())))
833836

834-
nb_puzzles = len(set((y, d) for y, d, _ in stats_elapsed.keys()))
837+
nb_puzzles = len(set((y, d) for y, d, _, _ in stats_elapsed.keys()))
835838
nb_solutions = 0
836839

837840
# compute elapsed time by language
838841
total_time = 0
839842
lines = []
840843
for lang in languages:
841-
t = list(t for (_, _, i), (t, _) in stats_elapsed.items() if lang == i)
844+
845+
t = list(t for (_, _, _, i), (t, _) in stats_elapsed.items() if lang == i)
842846
n = len(t)
843847
t = sum(t)
844848

src/year2022/day15/day15.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,20 @@ def manhattan(p1, p2):
2727
sensors.append((sx, sy, d))
2828
beacons.add((bx, by))
2929

30-
######################
3130

32-
# the search range
33-
bx_min = min(x for x, _ in beacons) - d_max
34-
bx_max = max(x for x, _ in beacons) + d_max
31+
######################
3532

3633
part1 = 0
3734
y = 10 if filename == "test.txt" else 2_000_000
38-
for x in range(bx_min, bx_max + 1):
39-
if (x, y) in beacons:
40-
continue
41-
42-
ok = True
43-
for sx, sy, nearest_beacon in sensors:
44-
d = manhattan((sx, sy), (x, y))
45-
if d <= nearest_beacon:
46-
# the sensors always report the nearest beacon
47-
# if the distance is less than the distance measured by the sensor,
48-
# there cannot be a beacon at this position
49-
ok = False
50-
break
51-
if not ok:
52-
part1 += 1
53-
54-
print(part1)
5535

36+
bad_pos = set()
37+
for sx, sy, d in sensors:
38+
for x in range(sx - d, sx + d + 1):
39+
if sy - (d - abs(sx - x)) <= y <= sy + (d - abs(sx - x)):
40+
if (x, y) not in beacons:
41+
bad_pos.add((x, y))
42+
43+
print(len(bad_pos))
5644

5745
######################
5846

src/year2022/day15/day15.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,17 @@ impl Puzzle {
7070
continue;
7171
}
7272

73-
let mut ok = true;
7473
for (sx, sy, nearest_beacon) in &self.sensors {
7574
let d = manhattan(x, y, *sx, *sy);
7675
if d <= *nearest_beacon {
7776
// the sensors always report the nearest beacon
7877
// if the distance is less than the distance measured by the sensor,
7978
// there cannot be a beacon at this position
8079

81-
ok = false;
80+
part1 += 1;
8281
break;
8382
}
8483
}
85-
if !ok {
86-
part1 += 1;
87-
}
8884
}
8985
part1
9086
}

0 commit comments

Comments
 (0)