The Brits aren't exactly known for being good at memorizing stuff. That's why scientists need newspaper articles to find those who are, and that's why it shouldn't be a surprise that they forget more than a 1000 things per year, including phone numbers, addresses, birthdays and more. Now, it is your duty to help the Brits remember their numbers.
In a 2010 video by the BBC (from 0:32 to 1:10), a trick is explained on how to do just that:
First, the base-10 string of the number is partitioned into several smaller non-overlapping parts, which then individually fit into one of four categories (three in the video, including one residual category for this challenge). No part of the partition is allowed to start with a 0
, except individual digits (This makes the challenge fairer for languages that only work with numbers and not with strings). Each category then has an associated cost. Your goal is to find the partition of the string such that the total cost is minimal, and output that partition. The four categories are:
1. Addition or multiplication:
Three or four digits in which the first two add or multiply to the third or, in case of a carry, the third and fourth digit.
Examples:
112 # 1 + 1 = 2
347 # 3 + 4 = 7
549 # 5 + 4 = 9
5510 # 5 + 5 = 10
606 # 6 + 0 = 6
9817 # 9 + 8 = 17
111 # 1 * 1 = 1
3412 # 3 * 4 = 12
5420 # 5 * 4 = 20
5525 # 5 * 5 = 25
600 # 6 * 0 = 0
9872 # 9 * 8 = 72
Cost:
- Addition without carry:
2
- Addition with carry:
3
- Multiplication without carry:
3
- Multiplication with carry:
5
2. Relation to a year:
Four digits that form an year considered to contain an important date. For the sake of this challenge, this is every year in which a British monarch died.
Exhaustive list:
1714, 1727, 1760, 1820, 1830, 1837, 1901, 1910, 1936, 1952, 1972, 2022
Cost:
- 18th and 19th century:
4
- 20th century:
2
- 21st century:
1
3. Similarity to a symbol:
A sequence of numbers that look like a symbol. Since Unicode is too vast and symbols are ill-defined, we will instead use any sequence of four digits that is a British word if typed on a 7-segment display calculator that is then rotated 180°.
Exhaustive list (taken from this file, filtered by whether there is a C1-level Oxford Dictionary entry of the same exact name):
3215, 3507, 3573, 3705, 5507, 5508, 5537, 7105, 7108, 7718, 7735, 7738
Cost: 3
4. Residual digit
Any digit not part of such a pattern will be treated as a residual digit and be assigned a cost of 2
.
Challenge
In this challenge, you will be given a non-negative number as input and will output the partition of the number such that it is as easy to remember as possible for a Brit, minimizing the total cost. If multiple equivalent partitions exit, you may choose arbitrarily.
You may take the input as a 64-bit (or wider) integer, a string containing the decimal representation or a list of decimal digits. You may output the partition as a list of numbers, or, if your language doesn't support lists, a Gödel-encoded list or similar. You may additionally output the cost, but this is neither required nor beneficial (I'd assume).
If your language has a builtin for generating optimal partitions for Brits to remember numbers, usage of it is not discouraged.
This is code-golf, so the shortest code (as measured in bytes) wins.
Test cases
Note that the test case's outputs are only examples and you might output a different output with the same cost. Note that if you calculate a partition with a different cost, your solution is most likely faulty.
Input | Output | Cost | Cost explanation | Explanation |
---|---|---|---|---|
1972 | [1972] | 2 = 2 | Monarch died 20th century | Death of Edward VIII |
19729 | [1972, 9] | 4 = 2 + 2 | Monarch died 20th century + Single digit | Death of Edward VIII followed by a 9 |
1972022 | [1, 9, 7, 2022] | 7 = 2 + 2 + 2 + 1 | Single digit x3 + Monarch died 21st century | Lone 1, 9 and 7 followed by the death of Elizabeth II |
92183721 | [9218, 3721] | 10 = 5 + 5 | Multiplication with carry x2 | 9 * 2 = 18, 3 * 7 = 21 |
123 | [123] | 2 = 2 | Addition without carry | 1 + 2 = 3 |
44812 | [4, 4812] | 5 = 2 + 3 | Single digit + Addition with carry | 4 followed by 4 + 8 = 12 |
48123 | [4812, 3] | 5 = 3 + 2 | Addition with carry + Single digit | 4 + 8 = 12 followed by 3 |
1820224610 | [1, 8, 2022, 4610] | 8 = 2 + 2 + 1 + 3 | Single digit x2 + Monarch died 21st century + Addition with carry | 1 and 8 followed by the death of Elizabeth II and 4 + 6 = 10 |
4312370550707 | [4312, 3705, 5, 0, 707] | 14 = 5 + 3 + 2 + 2 + 2 | Multiplication with carry + British word + Single digit x2 + Addition without carry | 4 * 3 = 12, then the word 'sole', a five, a zero and 7 + 0 = 7 |
54202243215 | [5420, 224, 3215] | 10 = 5 + 2 + 3 | Multiplication with carry + Addition without carry + British word | 5 * 4 = 20, 2 + 2 = 4, and the word 'size' |
171419012022 | [1714, 1901, 2022] | 7 = 4 + 2 + 1 | Monarch died 18th-19th century + Monarch died 20th century + Monarch died 21st century | The death of every female British monarch |
171450119010112022 | [1714, 5, 0, 1, 1901, 0, 1, 1, 2022] | 19 = 4 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 1 | Monarch died 18th-19th century + Single digit x3 + Monarch died 20th century + Single digit x3 + Monarch died 21st century | The death of every female British monarch, interrupted by 5, 0, 1 in the first and 0, 1, 1 in the second position |
99819918182022 | [9981, 9918, 1, 8, 2022] | 13 = 5 + 3 + 2 + 2 + 1 | Multiplication with carry + Addition with carry + Single digit x2 + Monarch died 21st century | 9 * 9 = 81, 9 + 9 = 18, lone 1 and 8 followed by the death of Elizabeth II |
1248163264128 | [1, 248, 1, 6, 326, 4, 1, 2, 8] | 20 = 2 + 3 + 2 + 2 + 3 + 2 + 2 + 2 + 2 | Single digit + Multiplication without carry + Single digit x2 + Multiplication without carry + Single digit x4 | |
123512351235 | [1, 235, 1, 235, 1, 235] | 12 = 2 + 2 + 2 + 2 + 2 + 2 | Single digit + Addition without carry + Single digit + Addition without carry + Single digit + Addition without carry | |
23550877187738 | [2, 3, 5508, 7718, 7738] | 13 = 2 + 2 + 3 + 3 + 3 | Single digit x2 + British word x3 | 2 and 3, 'boss', 'bill', 'bell' |
732133933917141760 | [7321, 339, 339, 1714, 1760] | 19 = 5 + 3 + 3 + 4 + 4 | Multiplication with carry + Multiplication without carry x2 + Monarch died 18th-19th century x2 |
Computer-readable test cases without explanations
1972 -> [1972]
19729 -> [1972, 9]
1972022 -> [1, 9, 7, 2022]
92183721 -> [9218, 3721]
123 -> [123]
44812 -> [4, 4812]
48123 -> [4812, 3]
1820224610 -> [1, 8, 2022, 4610]
4312370550707 -> [4312, 3705, 5, 0, 707]
54202243215 -> [5420, 224, 3215]
171419012022 -> [1714, 1901, 2022]
171450119010112022 -> [1714, 5, 0, 1, 1901, 0, 1, 1, 2022]
99819918182022 -> [9981, 9918, 1, 8, 2022]
1248163264128 -> [1, 248, 1, 6, 326, 4, 1, 2, 8]
123512351235 -> [1, 235, 1, 235, 1, 235]
23550877187738 -> [2, 3, 5508, 7718, 7738]
732133933917141760 -> [7321, 339, 339, 1714, 1760]
I express my deepest gratitude for your unwavering support during these challenging times faced by Great Britain. Your contributions to assisting impoverished, forgetful Britons are truly cherished and immensely valued. May God save the King! 🇬🇧 🇬🇧 🇬🇧 🇬🇧 🇬🇧
input -> output
. \$\endgroup\$