-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
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,39 @@ | ||
import unittest | ||
|
||
from collections import Counter | ||
from functools import cache | ||
|
||
|
||
@cache | ||
def howmany(bottles: tuple): | ||
if len(bottles) == 1: | ||
return bottles[0] + 1 | ||
|
||
def sub(ix): | ||
bottles1 = sorted((jb - (jx == ix) for jx, jb in enumerate(bottles))) | ||
return tuple(b for b in bottles1 if b) | ||
|
||
return sum(howmany(sub(i)) for i in range(len(bottles))) + 1 | ||
|
||
|
||
def main(letters): | ||
c = sorted(Counter(letters).values()) | ||
return howmany(tuple(c)) - 1 | ||
|
||
|
||
class TestHowMany(unittest.TestCase): | ||
def test_howmany(self): | ||
self.assertEqual(howmany((5,)), 6) | ||
self.assertEqual(howmany((1, 1)), 5) | ||
self.assertEqual(howmany((1, 1, 1)), 16) | ||
|
||
|
||
class TestMain(unittest.TestCase): | ||
def test_main(self): | ||
self.assertEqual(main("ab"), 4) | ||
self.assertEqual(main("abc"), 15) | ||
self.assertEqual(main("aaaaa"), 5) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(main(input().strip())) |