@ericksong/

solve2017

C#

https://github.com/IntelliTect-Samples/Solve2017

fork
loading
Files
  • main.cs
  • BinaryOperator.cs
  • Digits.cs
  • IntermediateResults.cs
  • main.exe
  • UnaryOperator.cs

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.22997628273990367","path":"main.cs","file":{"path":"main.cs","content":{"asEncoding":{"base64":"dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkxpbnE7CnVzaW5nIFN5c3RlbS5UZXh0Owp1c2luZyBTeXN0ZW0uVGhyZWFkaW5nLlRhc2tzOwoKbmFtZXNwYWNlIFNvbHZlMjAxNwp7CiAgICBjbGFzcyBQcm9ncmFtCiAgICB7CiAgICAgICAgc3RhdGljIHZvaWQgTWFpbihzdHJpbmdbXSBhcmdzKQogICAgICAgIHsKICAgICAgICAgICAgRGF0ZVRpbWUgc3RhcnRUaW1lID0gRGF0ZVRpbWUuTm93OwogICAgICAgICAgICB2YXIgaW50ZXJtZWRpYXRlUmVzdWx0cyA9IG5ldyBJbnRlcm1lZGlhdGVSZXN1bHRzKCk7CiAgICAgICAgICAgIHZhciBtYXhOdW1iZXIgPSAxMDA7CgogICAgICAgICAgICB2YXIgdW5hcnlPcGVyYXRvcnMgPSBuZXcgTGlzdDxVbmFyeU9wZXJhdG9yPigpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIG5ldyBVbmFyeU9wZXJhdG9yKCIoezB9KSEiLCAoYXJnKSA9PiBGYWN0b3JpYWwoYXJnKSwgKGFyZykgPT4gRmFjdG9yaWFsSXNWYWxpZChhcmcpKSwKICAgICAgICAgICAgICAgIG5ldyBVbmFyeU9wZXJhdG9yKCIoezB9KSEhIiwgKGFyZykgPT4gRG91YmxlRmFjdG9yaWFsKGFyZyksIChhcmcpID0+IEZhY3RvcmlhbElzVmFsaWQoYXJnKSksCiAgICAgICAgICAgICAgICAvL25ldy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oZj0+Zi5Ub1N0cmluZygpKS5Ub0xpc3QoKTsKCiAgICAgICAgICAgIC8vIEdlbmVyYXRlIGFsbCB0aGUgUGVybXV0YXRpb25zCiAgICAgICAgICAgIGZvcmVhY2ggKHZhciBudW1zIGluIEdldFBlcm11dGF0aW9ucyhzdGFydCkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8vQ29uc29sZS5Xcml0ZUxpbmUoc3RyaW5nLkpvaW4oIiAiLCBudW1zKSk7CiAgICAgICAgICAgICAgICBpbnRlcm1lZGlhdGVSZXN1bHRzLkFkZChuZXcgRGlnaXRzKG51bXMpKTsKICAgICAgICAgICAgICAgIEFkZEFsbERlY2ltYWxzKG51bXMuVG9MaXN0KCksIGludGVybWVkaWF0ZVJlc3VsdHMpOwoKICAgICAgICAgICAgICAgIC8vIENvbWJpbmUgbnVtYmVycwogICAgICAgICAgICAgICAgdmFyIHJlc3VsdHMgPSBDb21iaW5lT25lKG51bXMuVG9MaXN0KCkpOwoKICAgICAgICAgICAgICAgIGZvcmVhY2ggKHZhciByZXN1bHQgaW4gcmVzdWx0cykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpbnRlcm1lZGlhdGVSZXN1bHRzLkFkZChuZXcgRGlnaXRzKHJlc3VsdCkpOwoKICAgICAgICAgICAgICAgICAgICAvLyBIYW5kbGUgdGhlIGRlY2ltYWwgcG9pbnQKICAgICAgICAgICAgICAgICAgICBBZGRBbGxEZWNpbWFscyhyZXN1bHQsIGludGVybWVkaWF0ZVJlc3VsdHMpOwoKICAgICAgICAgICAgICAgICAgICAvLyBHZXQgY29tYmluYXRpb25zCiAgICAgICAgICAgICAgICAgICAgLy9mb3JlYWNoICh2YXIgY29tYm8gaW4gR2V0QWxsQ29tYm9zKHJlc3VsdCkpCiAgICAgICAgICAgICAgICAgICAgLy97CiAgICAgICAgICAgICAgICAgICAgLy8gICAgaW50ZXJtZWRpYXRlUmVzdWx0cy5BZGQobmV3IERpZ2l0cyhjb21ibykpOwogICAgICAgICAgICAgICAgICAgIC8vfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICAvL0hhbmRsZSBOZWdhdGl2ZXMKICAgICAgICAgICAgLy9mb3JlYWNoICh2YXIgZGlnaXRzIGluIGludGVybWVkaWF0ZVJlc3VsdHMuVmFsdWVzLlRvQXJyYXkoKSApCiAgICAgICAgICAgIC8vewogICAgICAgICAgICAvLyAgICBBZGRBbGxOZWdhdGl2ZXMoZGlnaXRzLk9yaWdpbmFsRGlnaXRzLCBpbnRlcm1lZGlhdGVSZXN1bHRzKTsKICAgICAgICAgICAgLy99CgoKICAgICAgICAgICAgLy9mb3JlYWNoICh2YXIgcmVzdWx0IGluIGludGVybWVkaWF0ZVJlc3VsdHMpCiAgICAgICAgICAgIC8vewogICAgICAgICAgICAvLyAgICBDb25zb2xlLldyaXRlTGluZSgkIntyZXN1bHQuS2V5fSAiKTsKICAgICAgICAgICAgLy99CgoKCiAgICAgICAgICAgIGJvb2wgZ290UmVzdWx0cyA9IGZhbHNlOwogICAgICAgICAgICBkbwogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB2YXIgdGhpc0l0ZXJhdGlvbkxpc3QgPSBpbnRlcm1lZGlhdGVSZXN1bHRzLlZhbHVlcy5XaGVyZShmID0+ICFmLklzUHJvY2Vzc2VkKS5Ub0FycmF5KCk7CiAgICAgICAgICAgICAgICAvL3ZhciB0aGlzSXRlcmF0aW9uTGlzdCA9IGludGVybWVkaWF0ZVJlc3VsdHMuVmFsdWVzLlRvQXJyYXkoKTsKICAgICAgICAgICAgICAgIGdvdFJlc3VsdHMgPSBmYWxzZTsKICAgICAgICAgICAgICAgIC8vIEhhbmRsZSB1bmFyeSAgc3FydCwgZmFjdG9yaWFsLCBldGMuIAogICAgICAgICAgICAgICAgZm9yZWFjaCAoRGlnaXRzIGRpZ2l0cyBpbiB0aGlzSXRlcmF0aW9uTGlzdCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBmb3JlYWNoIChVbmFyeU9wZXJhdG9yIHVuYXJ5T3BlcmF0b3IgaW4gdW5hcnlPcGVyYXRvcnMpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGRpZ2l0cy5Db3VudDsgaSsrKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBnb3RSZXN1bHRzIHw9IGludGVybWVkaWF0ZVJlc3VsdHMuQWRkKGRpZ2l0cywgdW5hcnlPcGVyYXRvciwgaSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgLy8gSGFuZGxlIGJpbmFyeSAgKywgLSwgKiwgZXRjLgogICAgICAgICAgICAgICAgZm9yZWFjaCAoRGlnaXRzIGRpZ2l0cyBpbiB0aGlzSXRlcmF0aW9uTGlzdCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBmb3JlYWNoIChCaW5hcnlPcGVyYXRvciBiaW5hcnlPcGVyYXRvciBpbiBiaW5hcnlPcGVyYXRvcnMpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8IGRpZ2l0cy5Db3VudDsgaSsrKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBnb3RSZXN1bHRzIHw9IGludGVybWVkaWF0ZVJlc3VsdHMuQWRkKGRpZ2l0cywgYmluYXJ5T3BlcmF0b3IsIGkpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGRpZ2l0cy5Jc1Byb2Nlc3NlZCA9IHRydWU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAvLyBMb29wIHVudGlsIHdlIGRvbid0IGZpbmQgYW55IG5ldyBkaWdpdCBjb21iaW5hdGlvbnMuIAogICAgICAgICAgICB9IHdoaWxlIChnb3RSZXN1bHRzKTsKCiAgICAgICAgICAgIC8vIEdldCBhIGxpc3Qgb2YgYWxsIG51bWJlcnMgZm91bmQuIAogICAgICAgICAgICB2YXIgZmluYWxSZXN1bHQgPSBuZXcgTGlzdDxEaWdpdHM+KCk7CiAgICAgICAgICAgIGZvcmVhY2ggKHZhciByZXN1bHQgaW4gaW50ZXJtZWRpYXRlUmVzdWx0cykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgLy9Db25zb2xlLldyaXRlTGluZSgkIntyZXN1bHQuS2V5fSAiKTsKICAgICAgICAgICAgICAgIGlmIChyZXN1bHQuVmFsdWUuQ291bnQgPT0gMSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBmaW5hbFJlc3VsdC5BZGQocmVzdWx0LlZhbHVlKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQoKICAgICAgICAgICAgaW50IGNvdW50ID0gMDsKICAgICAgICAgICAgZm9yIChkb3VibGUgaSA9IDE7IGkgPD0gbWF4TnVtYmVyOyBpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHZhciBkaWdpdCA9IGZpbmFsUmVzdWx0LkZpcnN0T3JEZWZhdWx0KGYgPT4gZlswXSA9PSBpKTsKICAgICAgICAgICAgICAgIGlmIChkaWdpdCAhPSBudWxsKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICAgICAgICAgICAgdmFyIHZhbHVlID0gZGlnaXRbMF07CiAgICAgICAgICAgICAgICAgICAgdmFyIGV4cHJlc3Npb24gPSBkaWdpdC5EaWdpdENhbGN1bGF0aW9uc1swXTsKICAgICAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgkInsgaSB9ID0geyBleHByZXNzaW9uIH0iKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgkInsgaSB9ID0gPyIpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgkIlRvdGFsIGJldHdlZW4gMSBhbmQge21heE51bWJlcn06IHtjb3VudH0iKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoJCJUb3RhbCBmb3VuZDoge2ZpbmFsUmVzdWx0LkNvdW50fSIpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgkIlRvdGFsIFRpbWU6IHsoRGF0ZVRpbWUuTm93IC0gc3RhcnRUaW1lKS5Ub3RhbE1pbGxpc2Vjb25kc31tcyIpOwoKICAgICAgICB9CgogICAgICAgIHByaXZhdGUgc3RhdGljIExpc3Q8TGlzdDxzdHJpbmc+PiBDb21iaW5lT25lKExpc3Q8c3RyaW5nPiBkaWdpdHMpCiAgICAgICAgewogICAgICAgICAgICB2YXIgcmVzdWx0cyA9IG5ldyBMaXN0PExpc3Q8c3RyaW5nPj4oKTsKCiAgICAgICAgICAgIGZvciAoaW50IHggPSAwOyB4IDwgZGlnaXRzLkNvdW50KCkgLSAxOyB4KyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHZhciByZXN1bHQgPSBuZXcgTGlzdDxzdHJpbmc+KCk7CiAgICAgICAgICAgICAgICBmb3IgKGludCB5ID0gMDsgeSA8IGRpZ2l0cy5Db3VudCgpOyB5KyspCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYgKHggPT0geSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIC8vIENvbmNhdGVuYXRlIHRoaXMgZGlnaXQgYW5kIHRoZSBuZXh0IG9uZQogICAgICAgICAgICAgICAgICAgICAgICB2YXIgbmV3SXRlbSA9IGRpZ2l0c1t5XSArIGRpZ2l0c1t5ICsgMV07CiAgICAgICAgICAgICAgICAgICAgICAgIHJlc3VsdC5BZGQobmV3SXRlbSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKHkgPT0geCArIDEpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAvLyBEbyBub3RoaW5nCiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIC8vIGp1c3QgYWRkIHRoZSBkaWdpdAogICAgICAgICAgICAgICAgICAgICAgICByZXN1bHQuQWRkKGRpZ2l0c1t5XSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgcmVzdWx0cy5BZGQocmVzdWx0KTsKICAgICAgICAgICAgICAgIGlmIChyZXN1bHRzLkNvdW50ID4gMSkgcmVzdWx0cy5BZGRSYW5nZShDb21iaW5lT25lKHJlc3VsdCkpOwogICAgICAgICAgICB9CgogICAgICAgICAgICByZXR1cm4gcmVzdWx0czsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBzdGF0aWMgZG91YmxlIEZhY3RvcmlhbChkb3VibGUgYXJnKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKGFyZyA9PSAwKSByZXR1cm4gMTsKICAgICAgICAgICAgZG91YmxlIHJlc3VsdCA9IDE7CiAgICAgICAgICAgIGZvciAoZG91YmxlIHggPSBhcmc7IHggPiAxOyB4LS0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHJlc3VsdCAqPSB4OwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgfQoKICAgICAgICBwdWJsaWMgc3RhdGljIGRvdWJsZSBEb3VibGVGYWN0b3JpYWwoZG91YmxlIGFyZykKICAgICAgICB7CiAgICAgICAgICAgIGlmIChhcmcgPT0gMCkgcmV0dXJuIDE7CiAgICAgICAgICAgIGRvdWJsZSByZXN1bHQgPSAxOwogICAgICAgICAgICBmb3IgKGRvdWJsZSB4ID0gYXJnOyB4ID4gMTsgeCA9IHggLSAyKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByZXN1bHQgKj0geDsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgICAgIH0KICAgICAgICBwdWJsaWMgc3RhdGljIGRvdWJsZSBUcmlwcGxlRmFjdG9yaWFsKGRvdWJsZSBhcmcpCiAgICAgICAgewogICAgICAgICAgICBpZiAoYXJnID09IDApIHJldHVybiAxOwogICAgICAgICAgICBkb3VibGUgcmVzdWx0ID0gMTsKICAgICAgICAgICAgZm9yIChkb3VibGUgeCA9IGFyZzsgeCA+IDE7IHggPSB4IC0gMykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmVzdWx0ICo9IHg7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBzdGF0aWMgYm9vbCBGYWN0b3JpYWxJc1ZhbGlkKGRvdWJsZSBhcmcpCiAgICAgICAgewogICAgICAgICAgICBpZiAoYXJnIDwgMCkgcmV0dXJuIGZhbHNlOwogICAgICAgICAgICBpZiAoKGludClhcmcgPT0gYXJnKSByZXR1cm4gdHJ1ZTsKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KCgogICAgICAgIC8vLyA8c3VtbWFyeT4KICAgICAgICAvLy8gR2V0cyBhbGwgcGVybXV0YXRpb25zIG9mIHRoZSBhcnJheS4KICAgICAgICAvLy8gPC9zdW1tYXJ5PgogICAgICAgIC8vLyA8dHlwZXBhcmFtIG5hbWU9IlQiPjwvdHlwZXBhcmFtPgogICAgICAgIC8vLyA8cGFyYW0gbmFtZT0ibGlzdCI+PC9wYXJhbT4KICAgICAgICAvLy8gPHJldHVybnM+PC9yZXR1cm5zPgogICAgICAgIHN0YXRpYyBJRW51bWVyYWJsZTxMaXN0PFQ+PiBHZXRQZXJtdXRhdGlvbnM8VD4oSUVudW1lcmFibGU8VD4gbGlzdCkKICAgICAgICB7CiAgICAgICAgICAgIHZhciByZXN1bHQgPSBuZXcgRGljdGlvbmFyeTxzdHJpbmcsIExpc3Q8VD4+KCk7CiAgICAgICAgICAgIGlmIChsaXN0LkNvdW50KCkgPT0gMSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcmVzdWx0LkFkZCgiIiwgbGlzdC5Ub0xpc3QoKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3JlYWNoICh2YXIgYyBpbiBsaXN0KQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHZhciBhbGxCdXRUaGlzID0gbmV3IExpc3Q8VD4obGlzdCk7CiAgICAgICAgICAgICAgICAgICAgYWxsQnV0VGhpcy5SZW1vdmUoYyk7CiAgICAgICAgICAgICAgICAgICAgdmFyIGNoaWxkUGVybXV0YXRpb25zID0gR2V0UGVybXV0YXRpb25zKGFsbEJ1dFRoaXMpOwogICAgICAgICAgICAgICAgICAgIGZvcmVhY2ggKHZhciBjaGlsZCBpbiBjaGlsZFBlcm11dGF0aW9ucykKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGNoaWxkLkluc2VydCgwLCBjKTsKICAgICAgICAgICAgICAgICAgICAgICAgdmFyIGtleSA9IG5ldyBzdHJpbmcoY2hpbGQuU2VsZWN0KGYgPT4gQ29udmVydC5Ub0NoYXIoZikpLlRvQXJyYXkoKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmICghcmVzdWx0LkNvbnRhaW5zS2V5KGtleSkpIHJlc3VsdC5BZGQoa2V5LCBjaGlsZCk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiByZXN1bHQuVmFsdWVzOwogICAgICAgIH0KCiAgICAgICAgLy8gQ29waWVkIGZyb20gU3RhY2sgT3ZlcmZsb3cKICAgICAgICBwdWJsaWMgc3RhdGljIExpc3Q8TGlzdDxUPj4gR2V0QWxsQ29tYm9zPFQ+KExpc3Q8VD4gbGlzdCkKICAgICAgICB7CiAgICAgICAgICAgIExpc3Q8TGlzdDxUPj4gcmVzdWx0ID0gbmV3IExpc3Q8TGlzdDxUPj4oKTsKICAgICAgICAgICAgLy8gaGVhZAogICAgICAgICAgICByZXN1bHQuQWRkKG5ldyBMaXN0PFQ+KCkpOwogICAgICAgICAgICByZXN1bHQuTGFzdCgpLkFkZChsaXN0WzBdKTsKICAgICAgICAgICAgaWYgKGxpc3QuQ291bnQgPT0gMSkKICAgICAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgICAgIC8vIHRhaWwKICAgICAgICAgICAgTGlzdDxMaXN0PFQ+PiB0YWlsQ29tYm9zID0gR2V0QWxsQ29tYm9zKGxpc3QuU2tpcCgxKS5Ub0xpc3QoKSk7CiAgICAgICAgICAgIHRhaWxDb21ib3MuRm9yRWFjaChjb21ibyA9PgogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByZXN1bHQuQWRkKG5ldyBMaXN0PFQ+KGNvbWJvKSk7CiAgICAgICAgICAgICAgICBjb21iby5BZGQobGlzdFswXSk7CiAgICAgICAgICAgICAgICByZXN1bHQuQWRkKG5ldyBMaXN0PFQ+KGNvbWJvKSk7CiAgICAgICAgICAgIH0pOwogICAgICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgICAgIH0KCiAgICAgICAgLy8vIDxzdW1tYXJ5PgogICAgICAgIC8vLyBBZGRzIGFsbCBjb21iaW5hdGlvbnMgb2YgZGVjaW1hbHMuCiAgICAgICAgLy8vIDwvc3VtbWFyeT4KICAgICAgICAvLy8gPHBhcmFtIG5hbWU9Imxpc3QiPjwvcGFyYW0+CiAgICAgICAgLy8vIDxwYXJhbSBuYW1lPSJpbnRlcm1lZGlhdGVSZXN1bHRzIj48L3BhcmFtPgogICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBBZGRBbGxEZWNpbWFscyhMaXN0PHN0cmluZz4gbGlzdCwgSW50ZXJtZWRpYXRlUmVzdWx0cyBpbnRlcm1lZGlhdGVSZXN1bHRzKQogICAgICAgIHsKICAgICAgICAgICAgZm9yIChpbnQgeCA9IDA7IHggPCBsaXN0LkNvdW50OyB4KyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHZhciBuZXdMaXN0ID0gbmV3IExpc3Q8c3RyaW5nPihsaXN0KTsKICAgICAgICAgICAgICAgIHZhciBkaWdpdCA9IGxpc3RbeF07CiAgICAgICAgICAgICAgICBpZiAoIWRpZ2l0LkNvbnRhaW5zKCIuIikpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaW50IHN0YXJ0ID0gMDsKICAgICAgICAgICAgICAgICAgICBpZiAoZGlnaXQuU3RhcnRzV2l0aCgiLSIpKSBzdGFydCA9IDE7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IHN0YXJ0OyBpIDwgZGlnaXQuTGVuZ3RoOyBpKyspCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICB2YXIgbmV3RGlnaXQgPSAkIntkaWdpdC5TdWJzdHJpbmcoMCwgaSl9LntkaWdpdC5TdWJzdHJpbmcoaSwgZGlnaXQuTGVuZ3RoIC0gaSl9IjsKICAgICAgICAgICAgICAgICAgICAgICAgbmV3TGlzdFt4XSA9IG5ld0RpZ2l0OwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoaW50ZXJtZWRpYXRlUmVzdWx0cy5BZGQobmV3IERpZ2l0cyhuZXdMaXN0KSkpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIEFkZEFsbERlY2ltYWxzKG5ld0xpc3QsIGludGVybWVkaWF0ZVJlc3VsdHMpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICAvL3B1YmxpYyBzdGF0aWMgdm9pZCBBZGRBbGxOZWdhdGl2ZXMoTGlzdDxzdHJpbmc+IGxpc3QsIEludGVybWVkaWF0ZVJlc3VsdHMgaW50ZXJtZWRpYXRlUmVzdWx0cykKICAgICAgICAvL3sKICAgICAgICAvLyAgICBmb3IgKGludCB4ID0gMDsgeCA8IGxpc3QuQ291bnQ7IHgrKykKICAgICAgICAvLyAgICB7CiAgICAgICAgLy8gICAgICAgIHZhciBuZXdMaXN0ID0gbmV3IExpc3Q8c3RyaW5nPihsaXN0KTsKICAgICAgICAvLyAgICAgICAgdmFyIGRpZ2l0ID0gbGlzdFt4XTsKICAgICAgICAvLyAgICAgICAgaWYgKCFkaWdpdC5Db250YWlucygiLSIpKQogICAgICAgIC8vICAgICAgICB7CiAgICAgICAgLy8gICAgICAgICAgICB2YXIgbmV3RGlnaXQgPSAkIi17ZGlnaXR9IjsKICAgICAgICAvLyAgICAgICAgICAgIG5ld0xpc3RbeF0gPSBuZXdEaWdpdDsKICAgICAgICAvLyAgICAgICAgICAgIGlmIChpbnRlcm1lZGlhdGVSZXN1bHRzLkFkZChuZXcgRGlnaXRzKG5ld0xpc3QpKSkKICAgICAgICAvLyAgICAgICAgICAgIHsKICAgICAgICAvLyAgICAgICAgICAgICAgICBBZGRBbGxOZWdhdGl2ZXMobmV3TGlzdCwgaW50ZXJtZWRpYXRlUmVzdWx0cyk7CiAgICAgICAgLy8gICAgICAgICAgICB9CiAgICAgICAgLy8gICAgICAgIH0KICAgICAgICAvLyAgICB9CiAgICAgICAgLy99CiAgICB9CgoKCgp9Cg=="},"asBuffer":null},"loaded":true}}
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Solve2017
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime startTime = DateTime.Now;
            var intermediateResults = new IntermediateResults();
            var maxNumber = 100;

            var unaryOperators = new List<UnaryOperator>()
            {
                new UnaryOperator("({0})!", (arg) => Factorial(arg), (arg) => FactorialIsValid(arg)),
                new UnaryOperator("({0})!!", (arg) => DoubleFactorial(arg), (arg) => FactorialIsValid(arg)),
                //new UnaryOperator("({0})!!!", (arg) => TrippleFactorial(arg), (arg) => FactorialIsValid(arg)),
                new UnaryOperator("sqrt({0})", (arg) => Math.Sqrt(arg), (arg) => arg>=0 ? true: false),
                //new UnaryOperator("abs({0})", (arg) => Math.Abs(arg), (arg) => arg>=0 ? true: false)
            };

            var binaryOperators = new List<BinaryOperator>()
            {
                new BinaryOperator("({0} + {1})", (arg1, arg2) => arg1+arg2),
                new BinaryOperator("({0} - {1})", (arg1, arg2) => arg1-arg2),
                new BinaryOperator("({0} * {1})", (arg1, arg2) => arg1*arg2),
                new BinaryOperator("({0} / {1})", (arg1, arg2) => arg1/arg2),
                new BinaryOperator("({0} ^ {1})", (arg1, arg2) => Math.Pow(arg1,arg2)),
                // This doesn't work because there isn't a simple modulus operator in the Math library. And Mod isn't allowed in this solution.
                //new BinaryOperator("({0}%{1})", (arg1, arg2) => Mod(arg1,arg2)),
            };

            Console.WriteLine($"This program determines the simplest way to calculate every number from 1 to {maxNumber} from a set of numbers.");
            Console.WriteLine("For example from 2017, 10 could be calculated from 02+1+7. 19=02+17.");
            int number = 2017;
            Console.WriteLine($"Number to calculate from (enter for {number})");
            string input = Console.ReadLine();
            if (!string.IsNullOrWhiteSpace(input)){
              int.TryParse(input, out number);
            }
            Console.WriteLine($"Calculating on {number}");

            var start = number.ToString().Select(f=>f.ToString()).ToList();

            // Generate all the Permutations
            foreach (var nums in GetPermutations(start))
            {
                //Console.WriteLine(string.Join(" ", nums));
                intermediateResults.Add(new Digits(nums));
                AddAllDecimals(nums.ToList(), intermediateResults);

                // Combine numbers
                var results = CombineOne(nums.ToList());

                foreach (var result in results)
                {
                    intermediateResults.Add(new Digits(result));

                    // Handle the decimal point
                    AddAllDecimals(result, intermediateResults);

                    // Get combinations
                    //foreach (var combo in GetAllCombos(result))
                    //{
                    //    intermediateResults.Add(new Digits(combo));
                    //}
                }
            }

            //Handle Negatives
            //foreach (var digits in intermediateResults.Values.ToArray() )
            //{
            //    AddAllNegatives(digits.OriginalDigits, intermediateResults);
            //}


            //foreach (var result in intermediateResults)
            //{
            //    Console.WriteLine($"{result.Key} ");
            //}



            bool gotResults = false;
            do
            {
                var thisIterationList = intermediateResults.Values.Where(f => !f.IsProcessed).ToArray();
                //var thisIterationList = intermediateResults.Values.ToArray();
                gotResults = false;
                // Handle unary  sqrt, factorial, etc. 
                foreach (Digits digits in thisIterationList)
                {
                    foreach (UnaryOperator unaryOperator in unaryOperators)
                    {
                        for (int i = 0; i < digits.Count; i++)
                        {
                            gotResults |= intermediateResults.Add(digits, unaryOperator, i);
                        }
                    }
                }

                // Handle binary  +, -, *, etc.
                foreach (Digits digits in thisIterationList)
                {
                    foreach (BinaryOperator binaryOperator in binaryOperators)
                    {
                        for (int i = 1; i < digits.Count; i++)
                        {
                            gotResults |= intermediateResults.Add(digits, binaryOperator, i);
                        }
                    }
                    digits.IsProcessed = true;
                }
                // Loop until we don't find any new digit combinations. 
            } while (gotResults);

            // Get a list of all numbers found. 
            var finalResult = new List<Digits>();
            foreach (var result in intermediateResults)
            {
                //Console.WriteLine($"{result.Key} ");
                if (result.Value.Count == 1)
                {
                    finalResult.Add(result.Value);
                }
            }

            int count = 0;
            for (double i = 1; i <= maxNumber; i++)
            {
                var digit = finalResult.FirstOrDefault(f => f[0] == i);
                if (digit != null)
                {
                    count++;
                    var value = digit[0];
                    var expression = digit.DigitCalculations[0];
                    Console.WriteLine($"{ i } = { expression }");
                }
                else
                {
                    Console.WriteLine($"{ i } = ?");
                }
            }

            Console.WriteLine($"Total between 1 and {maxNumber}: {count}");
            Console.WriteLine($"Total found: {finalResult.Count}");
            Console.WriteLine($"Total Time: {(DateTime.Now - startTime).TotalMilliseconds}ms");

        }

        private static List<List<string>> CombineOne(List<string> digits)
        {
            var results = new List<List<string>>();

            for (int x = 0; x < digits.Count() - 1; x++)
            {
                var result = new List<string>();
                for (int y = 0; y < digits.Count(); y++)
                {
                    if (x == y)
                    {
                        // Concatenate this digit and the next one
                        var newItem = digits[y] + digits[y + 1];
                        result.Add(newItem);
                    }
                    else if (y == x + 1)
                    {
                        // Do nothing
                    }
                    else
                    {
                        // just add the digit
                        result.Add(digits[y]);
                    }
                }
                results.Add(result);
                if (results.Count > 1) results.AddRange(CombineOne(result));
            }

            return results;
        }

        public static double Factorial(double arg)
        {
            if (arg == 0) return 1;
            double result = 1;
            for (double x = arg; x > 1; x--)
            {
                result *= x;
            }
            return result;
        }

        public static double DoubleFactorial(double arg)
        {
            if (arg == 0) return 1;
            double result = 1;
            for (double x = arg; x > 1; x = x - 2)
            {
                result *= x;
            }
            return result;
        }
        public static double TrippleFactorial(double arg)
        {
            if (arg == 0) return 1;
            double result = 1;
            for (double x = arg; x > 1; x = x - 3)
            {
                result *= x;
            }
            return result;
        }

        public static bool FactorialIsValid(double arg)
        {
            if (arg < 0) return false;
            if ((int)arg == arg) return true;
            return false;
        }


        /// <summary>
        /// Gets all permutations of the array.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        static IEnumerable<List<T>> GetPermutations<T>(IEnumerable<T> list)
        {
            var result = new Dictionary<string, List<T>>();
            if (list.Count() == 1)
            {
                result.Add("", list.ToList());
            }
            else
            {
                foreach (var c in list)
                {
                    var allButThis = new List<T>(list);
                    allButThis.Remove(c);
                    var childPermutations = GetPermutations(allButThis);
                    foreach (var child in childPermutations)
                    {
                        child.Insert(0, c);
                        var key = new string(child.Select(f => Convert.ToChar(f)).ToArray());
                        if (!result.ContainsKey(key)) result.Add(key, child);
                    }
                }
            }
            return result.Values;
        }

        // Copied from Stack Overflow
        public static List<List<T>> GetAllCombos<T>(List<T> list)
        {
            List<List<T>> result = new List<List<T>>();
            // head
            result.Add(new List<T>());
            result.Last().Add(list[0]);
            if (list.Count == 1)
                return result;
            // tail
            List<List<T>> tailCombos = GetAllCombos(list.Skip(1).ToList());
            tailCombos.ForEach(combo =>
            {
                result.Add(new List<T>(combo));
                combo.Add(list[0]);
                result.Add(new List<T>(combo));
            });
            return result;
        }

        /// <summary>
        /// Adds all combinations of decimals.
        /// </summary>
        /// <param name="list"></param>
        /// <param name="intermediateResults"></param>
        public static void AddAllDecimals(List<string> list, IntermediateResults intermediateResults)
        {
            for (int x = 0; x < list.Count; x++)
            {
                var newList = new List<string>(list);
                var digit = list[x];
                if (!digit.Contains("."))
                {
                    int start = 0;
                    if (digit.StartsWith("-")) start = 1;
                    for (int i = start; i < digit.Length; i++)
                    {
                        var newDigit = $"{digit.Substring(0, i)}.{digit.Substring(i, digit.Length - i)}";
                        newList[x] = newDigit;
                        if (intermediateResults.Add(new Digits(newList)))
                        {
                            AddAllDecimals(newList, intermediateResults);
                        }
                    }
                }
            }
        }

        //public static void AddAllNegatives(List<string> list, IntermediateResults intermediateResults)
        //{
        //    for (int x = 0; x < list.Count; x++)
        //    {
        //        var newList = new List<string>(list);
        //        var digit = list[x];
        //        if (!digit.Contains("-"))
        //        {
        //            var newDigit = $"-{digit}";
        //            newList[x] = newDigit;
        //            if (intermediateResults.Add(new Digits(newList)))
        //            {
        //                AddAllNegatives(newList, intermediateResults);
        //            }
        //        }
        //    }
        //}
    }




}