corresponds to a single digit (non-negative) number Ø All letters have unique numbers Ø Letters placed together form a multi-digit number (e.g. SEND) Ø The letter M cannot be 0 (zero) Ø This equality constraint holds true: SEND + MORE = MONEY S E N D M O R E + Y E N O M
N, D, M, O, R, Y Rule/ Constraint Each letter has 10 possibilities (0 to 9), except M. Number of possible solutions 107 * 9 = 90,000,000 Each letter has a unique value. 9 * 9C7 = 1,632,960 M cannot be 0. (That means M is1.) 9C7 = 181,440 SEND + MORE = MONEY ?
most concise implementation. (This is not a code golf.) To write the fastest running program. We do aim: To solve the problem at hand. To keep it verbose enough to be clear and readable. To make it extensible. To learn about Algebraic Data Types and pattern matching in Java.
values for all variables → Generator Ø Evaluate the expressions → Expression evaluator Ø If all the constraints hold true for a combination of values, we have a solution → Puzzle solver Expression Evaluator Generator Puzzle Solver S E ND MO RE + Y E N O M
Ø If the values of S, E, N, D are s, e, n, d respectively, then The value of SEND = s*1000 + e*100 + n*10 + d Grammar: Expression = a constant | a variable | (an expression) + (an expression) | (an expression) * (an expression) d e * 1000 100 10 n + s * * + +
has value for each of its constituent types. Ø Examples Ø A point has two coordinates: x and y. Ø An address has a house-number, a street, a city and a country.
presence of a pattern in a given sequence of tokens. Ø Features in Java 21 Ø Pattern matching for instanceof (JEP-394) Ø Record patterns (JEP-440) Ø Pattern matching for switch (JEP-441) Ø Unnamed patterns and variables - Preview (JEP-443)
digit (non-negative) number Ø Create Variable to represent each letter Ø Create RangeConstraint to represent possible values of a variable Ø Create Expression with evaluation function Ø Generator should generate all possible combinations of variable-values Ø Create more constraint types Ø All values must be unique → UniqueValuesConstraint Ø Equality constraints must be satisfied → Create EqualityConstraint Ø Puzzle solver needs to use Generator and Expression-s to find solution(s). Expression Evaluator Generator Puzzle Solver S E ND MO RE + Y E N O M
Algebraic data types helped us to model data so that only valid states are allowed to exist. Ø We used records and sealed types to implement product and sum types. Ø Pattern matching features helped us with implementing behaviour/ functions that operated on the data.
model data as immutable data and keep the code, that embodies the business logic, separate. Ø Explained by Brian Goetz (Java Language Architect at Oracle) in this article: bit.ly/DOPJava Ø Principles Ø Model the data, the whole data, and nothing but the data. Ø Data is immutable. Ø Validate at the boundary. Ø Make illegal states unrepresentable. Ø ADT and pattern matching (in other words records, sealed types, and pattern matching in Java) work together to enable easier data-oriented programming. Ø OOP and DOP: OOP is useful when modelling large complex systems and DOP is suitable for simpler/ smaller (sub) system.