diff --git a/2024/d5.py b/2024/d5.py new file mode 100644 index 0000000..443e4b1 --- /dev/null +++ b/2024/d5.py @@ -0,0 +1,37 @@ +from lib import get_data +from lib import ints + + +def fix(xs): + change = True + while change: + change = False + for rule in rules.splitlines(): + x, y = ints(rule) + if not (x in xs and y in xs): + continue + i, j = xs.index(x), xs.index(y) + if not i < j: + xs[i], xs[j] = xs[j], xs[i] + change = True + return xs + + +rules, pages = get_data(__file__).split("\n\n") + +t1, t2 = 0, 0 +for page in pages.splitlines(): + xs = ints(page) + for rule in rules.splitlines(): + x, y = ints(rule) + if not (x in xs and y in xs): + continue + if not (xs.index(x) < xs.index(y)): + xs = fix(ints(page)) + t2 += xs[len(xs) // 2] + break + else: + t1 += xs[len(xs) // 2] + +print(t1) +print(t2) diff --git a/README.md b/README.md index 3348db0..0750c1b 100644 --- a/README.md +++ b/README.md @@ -295,4 +295,4 @@ and focus. Of course, learning more algorithms and techniques helps. - Day 2: `00:05:34 686 0 00:08:21 531 0` - Day 3: `00:01:48 165 0 00:02:40 56 45` - Day 4: `00:07:47 1101 0 00:24:07 2410 0` -- Day 5: +- Day 5: `00:07:07 679 0 00:23:02 1998 0`