This repository has been archived by the owner on Dec 25, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 89741 316108 7641 756 9 7832357 91 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import os.path | ||
|
||
import pytest | ||
|
||
import support | ||
|
||
INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt') | ||
|
||
|
||
def compute(s: str) -> int: | ||
numbers = support.parse_numbers_split(s) | ||
|
||
def _transmute(n: int) -> tuple[int, ...]: | ||
if n == 0: | ||
return (1,) | ||
elif len(str(n)) % 2 == 0: | ||
nstr = str(n) | ||
midp = len(nstr) // 2 | ||
return int(nstr[:midp]), int(nstr[midp:]) | ||
else: | ||
return (2024 * n,) | ||
|
||
for _ in range(25): | ||
numbers = [m for n in numbers for m in _transmute(n)] | ||
|
||
return len(numbers) | ||
|
||
|
||
INPUT_S = '''\ | ||
125 17 | ||
''' | ||
EXPECTED = 55312 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('input_s', 'expected'), | ||
( | ||
(INPUT_S, EXPECTED), | ||
), | ||
) | ||
def test(input_s: str, expected: int) -> None: | ||
assert compute(input_s) == expected | ||
|
||
|
||
def main() -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('data_file', nargs='?', default=INPUT_TXT) | ||
args = parser.parse_args() | ||
|
||
with open(args.data_file) as f, support.timing(): | ||
print(compute(f.read())) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
raise SystemExit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import functools | ||
import os.path | ||
|
||
import pytest | ||
|
||
import support | ||
|
||
INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt') | ||
|
||
|
||
@functools.cache | ||
def _compute(n: int, m: int) -> int: | ||
if m == 0: | ||
return 1 | ||
elif n == 0: | ||
return _compute(1, m - 1) | ||
elif len(str(n)) % 2 == 0: | ||
nstr = str(n) | ||
midp = len(nstr) // 2 | ||
return ( | ||
_compute(int(nstr[:midp]), m - 1) + | ||
_compute(int(nstr[midp:]), m - 1) | ||
) | ||
else: | ||
return _compute(2024 * n, m - 1) | ||
|
||
|
||
def compute(s: str, *, iterations: int = 75) -> int: | ||
numbers = support.parse_numbers_split(s) | ||
|
||
return sum(_compute(n, iterations) for n in numbers) | ||
|
||
|
||
INPUT_S = '''\ | ||
125 17 | ||
''' | ||
EXPECTED = 55312 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('input_s', 'expected'), | ||
( | ||
(INPUT_S, EXPECTED), | ||
), | ||
) | ||
def test(input_s: str, expected: int) -> None: | ||
assert compute(input_s, iterations=25) == expected | ||
|
||
|
||
def main() -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('data_file', nargs='?', default=INPUT_TXT) | ||
args = parser.parse_args() | ||
|
||
with open(args.data_file) as f, support.timing(): | ||
print(compute(f.read())) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
raise SystemExit(main()) |