-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexprtk_riddle.cpp
129 lines (106 loc) · 6.21 KB
/
exprtk_riddle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* ExprTk Logical Deduction Riddle *
* Author: Arash Partow (1999-2024) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* SPDX-License-Identifier: MIT *
* *
**************************************************************
*/
/*
The Broken Window Riddle
A window has been broken, there are 5 suspects (Annie, Betsy, Chloe,
Tori and Zoey). Each suspect is asked who they think broke the window.
The following is each suspect's statement:
Tori : It wasn't Zoey, It was Annie
Annie: It wasn't Betsy, It wasn't Zoey
Betsy: It was Zoey, It wasn't Tori
Chloe: It was Betsy, It was Annie
Zoey : It was Chloe, It wasn't Tori
Given each of the suspects spoke only one lie and one truth, and only
one person is responsible for the broken window:
Who then was it that broke the window?
*/
#include <string>
#include "exprtk.hpp"
template <typename T>
void logical_deducation_riddle()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
typedef exprtk::function_compositor<T> compositor_t;
typedef typename compositor_t::function function_t;
symbol_table_t symbol_table;
exprtk::rtl::io::println<T> println;
symbol_table.add_function("println", println);
compositor_t compositor(symbol_table);
compositor.add(function_t()
.name("constraint")
.var("person_x").var("person_y")
.expression
(
" person_x xor person_y; "
));
const std::string logical_deducation_riddle_program =
" var number_of_culprits := 1; "
" "
" for (var Tori := false; Tori <= true; Tori += true) "
" { "
" for (var Annie := false; Annie <= true; Annie += true) "
" { "
" for (var Betsy := false; Betsy <= true; Betsy += true) "
" { "
" for (var Chloe := false; Chloe <= true; Chloe += true) "
" { "
" for (var Zoey := false; Zoey <= true; Zoey += true) "
" { "
" if (sum(Annie, Betsy, Chloe, Tori, Zoey) != number_of_culprits) "
" continue; "
" "
" var solution := "
" constraint( not(Zoey) , Annie ) and /* Tori */ "
" constraint( not(Betsy) , not(Zoey) ) and /* Annie */ "
" constraint( Zoey , not(Tori) ) and /* Betsy */ "
" constraint( Betsy , Annie ) and /* Chloe */ "
" constraint( Chloe , not(Tori) ) ; /* Zoey */ "
" "
" if (solution == true) "
" { "
" var culprit := ''; "
" [*] "
" { "
" case Annie : culprit := 'Annie'; "
" case Betsy : culprit := 'Betsy'; "
" case Chloe : culprit := 'Chloe'; "
" case Tori : culprit := 'Tori' ; "
" case Zoey : culprit := 'Zoey' ; "
" }; "
" "
" println(culprit,' broke the window!'); "
" } "
" } "
" } "
" } "
" } "
" } "
" ";
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(logical_deducation_riddle_program,expression);
expression.value();
}
int main()
{
logical_deducation_riddle<double>();
return 0;
}