-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problems.cs
2346 lines (2024 loc) · 109 KB
/
Problems.cs
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using ExtensionMethods;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Xml.Serialization;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Problems
{
/// <summary>
/// This namespace contains all the information about the problems.
/// Each problem is organised into it's own class.
/// The problem class template contains common variables and functions.
/// </summary>
// The Problem class contains everything relevant to one problem
class Problem
{
#region Variables
// Title and discription are set from initializer
public string Title { get; set; }
public string WebData { get; set; }
// Other info is taken from a tooltip on the problem's website
public string miscInfo; // Full tooltip
public int difficulty; // Taken from miscInfo
public int solvedBy; // Taken from miscInfo
// Solution might be stored here someday
public bool solutionKnown = false;
// The description control is allways a stackpanel and is generated during loading.
private FlowDocumentScrollViewer Description;
#endregion
#region Functions
// Constructor
public Problem(string _miscInfo)
{
// Get some info
miscInfo = _miscInfo.Replace("<br>", " ");
solvedBy = Convert.ToInt32(miscInfo.GetBetween("Solved by ", ";"));
difficulty = Convert.ToInt32(miscInfo.GetBetween("Difficulty rating: ", "%"));
}
// Get FlowDoc of description
public FlowDocumentScrollViewer GetDescriptionFDSV(int pNum)
{
if (Description is null)
{
#region FlowDoc preparation
// Flowdoc to hold content
FlowDocument FD = new FlowDocument()
{
IsHyphenationEnabled = true,
IsOptimalParagraphEnabled = true,
FontFamily = new FontFamily("Segoe UI")
};
// Title
TextBlock TitleTB = new TextBlock()
{
FontSize = 30,
FontFamily = new FontFamily("Georgia"),
};
// Get link
string linkPath = "https://projecteuler.net/problem=" + pNum;
// Add hyperlink
var HL = new Hyperlink(new Run($"{pNum}: " + Title))
{
Foreground = new SolidColorBrush(Colors.Black),
TextDecorations = null,
NavigateUri = new Uri(linkPath)
};
HL.RequestNavigate += (sender, e) =>
{
System.Diagnostics.Process.Start(e.Uri.ToString());
};
TitleTB.Inlines.Add(HL);
FD.Blocks.Add(new BlockUIContainer()
{
Child = TitleTB,
BorderBrush = new SolidColorBrush(Colors.Black),
BorderThickness = new Thickness(0,0,0,1),
});
// Subtext
FD.Blocks.Add(new BlockUIContainer()
{
Child = new TextBlock()
{
Text = miscInfo,
FontSize = 10,
Margin = new Thickness(0, 0, 0, 30),
TextWrapping = TextWrapping.Wrap
}
});
#endregion
#region Description content
#region Content prep
// Get content
string openingTag = "<div class=\"problem_content\" role=\"problem\">";
int indexOfFirstDiv = WebData.IndexOf(openingTag);
int indexOfFirstDivClosingTag;
int depth = 0;
int curIndex = indexOfFirstDiv;
string curCode = "<div";
while (true)
{
// Find next code
var firstCodeIndices = new string[] { "<div", "</div" }.Select(code => (code, WebData.IndexOf(code, curIndex + curCode.Length))).Where(x => x.Item2 != -1);
var firstCode = firstCodeIndices.Where(x => x.Item2 == firstCodeIndices.Min(y => y.Item2)).First();
curIndex = firstCode.Item2;
curCode = firstCode.code;
if (firstCode.code == "<div")
{
depth++;
}
else
{
if (depth > 0)
{
depth--;
}
else
{
// Done
indexOfFirstDivClosingTag = firstCode.Item2;
break;
}
}
}
string pContent = WebData.Substring(indexOfFirstDiv + openingTag.Length, indexOfFirstDivClosingTag - indexOfFirstDiv - openingTag.Length);
#endregion
// Format Lines
Block B;
(pContent, B) = HTML.FormatFirstBlock(pContent);
while (pContent.Length > 0)
{
FD.Blocks.Add(B);
(pContent, B) = HTML.FormatFirstBlock(pContent);
}
#endregion
Description = new FlowDocumentScrollViewer()
{
Document = FD,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto
};
}
return Description;
}
#endregion
}
// Class dealing with HTML decoding
public static class HTML
{
// Paragraph tags
public static readonly (string start, string end)[] blockTags = new (string start, string end)[]
{
("<p", "</p>"),
("$$", "$$"),
("<div","</div>"),
("<blockquote", "</blockquote>"),
("<ul", "</ul>")
};
// Inline tag chars
public static readonly char[] inlineTagChars = new char[]
{
'<',
'$'
};
// Replace exit codes that can be replaced at the start of the parsing
public static string PreparationReplace(string str)
{
new (string c, string r)[]
{
("\n", ""),
(" (right click and 'Save Link/Target As...')", "")
}
.ToList().ForEach(x => str = str.Replace(x.c, x.r));
return str;
}
// Create formula control from string
public static InlineUIContainer GetFormulaControl(string str)
{
var C = new InlineUIContainer()
{
Child = new WpfMath.Controls.FormulaControl()
{
Formula = str,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5, 0, 5, 0)
},
BaselineAlignment = BaselineAlignment.Bottom,
};
return C;
}
// Replace exit codes that should only be replaced at the VERY END of the parsing
public static Run GetFinishedRun(string str)
{
(string c, string r) code = ("<", "<");
return new Run(str.Replace(code.c, code.r));
}
public static (string, Block) FormatFirstBlock(string str)
{
char[] blockTagChars = blockTags.Select(x => x.start[0]).Distinct().ToArray();
// Return empty string if str contains no blockTags
if (!blockTags.Any(x => str.Contains(x.start)) )
{
return ("", new Paragraph());
}
// Get open tag index
int openTagI = blockTags.Select(x => str.IndexOf(x.start)).Where(x => x != -1).Min();
// Trim block
str = str.Substring(openTagI);
// Get first char of open tag
char firstCharOfOpenTag = str[0];
// Switch for first char of open tag (cases terminate)
switch (firstCharOfOpenTag)
{
case '<':
{
// Get tag name
string tagName = blockTags.Where(x => str.IndexOf(x.start) == 0).First().start.Substring(1);
// Determine fullTag
string fullTag = str.GetBetweenInclusive("<", "</" + tagName + ">");
// Get open tag
string openTag = fullTag.GetBetweenInclusive("<", ">");
// Get tag content
string tagContent = fullTag.GetBetween(openTag, "</" + tagName + ">");
// Switch for tag name
switch (tagName)
{
case "blockquote": // Indented paragraph
case "p": // Paragraph
case "div": // Section (same as paragraph as far as i can see)
{
// Create paragraph
Paragraph P = new Paragraph();
// Indent if blockquote
if (tagName == "blockquote")
{
P.Padding = new Thickness(50, 0, 0, 0);
}
// Class="class"
if (openTag.Contains("class="))
{
// Switch for class
switch (openTag.GetBetween("class=\"", "\""))
{
// Centered
case "center":
{
P.TextAlignment = TextAlignment.Center;
break;
}
// Monospaced centered
case "monospace center":
{
// Centered
P.TextAlignment = TextAlignment.Center;
// Monospaced font
P.FontFamily = new FontFamily("Courier New");
// Reduced fontsize
//P.FontSize *= 0.9;
// Remove linespacing
P.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
P.LineHeight = P.FontSize + 3;
// Special padding
//P.Padding = new Thickness(0, 0, 0, 0);
break;
}
// Monospaced
case "monospace":
{
// Monospaced font
P.FontFamily = new FontFamily("Courier New");
// Reduced fontsize
//P.FontSize *= 0.75;
// Remove linespacing
P.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
P.LineHeight = P.FontSize + 3;
// Special padding
P.Padding = new Thickness(50, 0, 0, 0);
break;
}
// Left margin
case "margin_left":
{
P.Padding = new Thickness(50, 0, 0, 0);
break;
}
// Note (nothing)
case "note":
{
break;
}
// Smaller
case "smaller":
{
P.FontSize *= 0.9;
break;
}
// Unknown
default:
{
throw new Exception($"Unkown class in: {str}");
}
}
}
// Format content and add to P
FormatLine(tagContent).ToList().ForEach(x => P.Inlines.Add(x));
// Return
return (str.Substring(fullTag.Length), P);
}
case "ul":
{
// Create list
var contentList = new List
{
MarkerOffset = 10,
MarkerStyle = TextMarkerStyle.Disc,
Margin = new Thickness(30, 10, 10, 10)
};
// Put all <li> items into list
while (tagContent.Contains("<li>"))
{
int liCloseIndex = tagContent.IndexOf("</li>");
string liItem = tagContent.GetBetween("<li>", "</li>");
tagContent = tagContent.Remove(0, liCloseIndex + "</li>".Length);
var Par = new Paragraph();
FormatLine(liItem.Replace("<br />", "")).ForEach(x => Par.Inlines.Add(x));
var lItem = new ListItem(Par);
contentList.ListItems.Add(lItem);
}
// Add block to FD
return (str.Substring(fullTag.Length), contentList);
}
default:
{
throw new Exception($"Unknown blockTag tagName: {tagName}");
}
}
}
case '$':
{
// Get full tag
string fullTag = str.GetBetweenInclusive("$$", "$$");
// Create centered paragraph
Paragraph P = new Paragraph()
{
TextAlignment = TextAlignment.Center
};
// Add formula to P
P.Inlines.Add(GetFormulaControl(fullTag.GetBetween("$$","$$")));
// Return
return (str.Substring(fullTag.Length), P);
}
default:
{
throw new Exception($"Unknown block tag: {str}");
}
}
}
// Return the string as a list of Inlines
public static List<Inline> FormatLine(string line)
{
/// This function processes the html code string "line" into a list of WPF Inline's.
/// Inlines can hold pretty much anything, including images
/// The function processes one tag, and then calls itself to format the remainder string
/// and concatonates the result of the remainder to the Inline list.
// Create inline list
List<Inline> inlines = new List<Inline>();
// Do prep replace
line = PreparationReplace(line);
// If line contains no tags ==> return line as Inline
if (!(inlineTagChars.Any(x => line.Contains(x))))
{
inlines.Add(GetFinishedRun(line));
return inlines;
}
// Line contains tags ==> get index of first tag
(int index, char firstChar) tag;
tag.index = inlineTagChars.Select(x => line.IndexOf(x)).Where(x => x != -1).Min();
tag.firstChar = line[tag.index];
// Format part before first escape and add to inlines
// Also remove beforePart from line, now tag.index should be 0
if (tag.index > 0)
{
string beforePart = line.Substring(0, tag.index);
inlines.Add(GetFinishedRun(beforePart));
line = line.Remove(0, tag.index);
tag.index = 0;
}
// The complete tag string is determined separately, but gets removed after the operations
string fullTag;
// Switch for first char (cases must determine fullTag)
switch (tag.firstChar)
{
case '$':
{
// Inline formula
fullTag = line.GetBetweenInclusive("$", "$");
// Add inline of formula
inlines.Add(GetFormulaControl(fullTag.GetBetween("$", "$")));
break;
}
case '<':
{
// Get open tag
string openTag = line.GetBetweenInclusive("<",">");
// Switch for if tag is container
switch (openTag.EndsWith("/>"))
{
// Non-container tag
case true:
{
// Set fullTag
fullTag = openTag;
// Get tag name
string tagName = openTag.Substring(1, new string[]
{
openTag.GetBetween("<", " "), openTag.GetBetween("<", "/>")
}.Min(x => x.Length));
// Switch for tag name
switch (tagName)
{
// Linebreak
case "br":
{
if (line.Substring(fullTag.Length) != "")
{
inlines.Add(new Run("\n"));
}
break;
}
// Image
case "img":
{
// Content
string imagePath = "https://projecteuler.net/" + fullTag.GetBetween("\"", "\"");
// Get image
BitmapImage BMI = new BitmapImage(new Uri(imagePath));
Image img = new Image()
{
Stretch = Stretch.Uniform
};
BMI.DownloadCompleted += delegate (object sender, EventArgs e)
{
img.Source = BMI;
img.Width = BMI.Width * 2;
img.Height = BMI.Height * 2;
};
// Add content
inlines.Add(new InlineUIContainer()
{
Child = img
});
break;
}
default:
{
throw new Exception($"Unknown non-container tag: {fullTag}");
}
}
break;
}
// Container tag
case false:
{
// Get tag name
string tagName = openTag.Substring(1, new string[]
{
openTag.GetBetween("<", " "), openTag.GetBetween("<", ">")
}.Where(x => x.Length > 0).Min(x => x.Length));
// Set fullTag
fullTag = line.GetBetweenInclusive(openTag, "</" + tagName + ">");
//// Throw error if tag contains tag with identical name
//if (fullTag.Contains("<" + tagName))
//{
// throw new Exception($"Tag: {fullTag} contains tag with same tagName: {tagName}");
//}
// Get tag content
string tagContent = fullTag.GetBetween(openTag, "</" + tagName + ">");
// Switch for tag name (known container tags)
switch (tagName)
{
case "a": // Hyperlink
{
// Get link
string linkPath = "https://projecteuler.net/" + fullTag.GetBetween("href=\"", "\"");
// Add hyperlink
var HL = new Hyperlink(GetFinishedRun(tagContent))
{
NavigateUri = new Uri(linkPath)
};
HL.RequestNavigate += (sender, e) =>
{
System.Diagnostics.Process.Start(e.Uri.ToString());
};
inlines.Add(HL);
break;
}
case "b": // Bold text
{
// Add content
inlines.Add(new Bold(GetFinishedRun(tagContent)));
break;
}
case "dfn": // Italic text with tooltip
{
// Get tooltip
string tooltip = fullTag.GetBetween("title=\"", "\"");
// Add italic part with tooltip
inlines.Add(new Italic(GetFinishedRun(tagContent))
{
ToolTip = new ToolTip()
{
Content = $" ({tooltip})"
}
});
break;
}
case "sub": // Subscript
{
// Convert content to subscript
var content = FormatLine(tagContent);
content.ForEach(x => x.BaselineAlignment = BaselineAlignment.Subscript);
content.ForEach(x => x.FontSize *= 0.75);
//content.ForEach(x => Typography.SetVariants(x, FontVariants.Subscript));
// Add content
content.ForEach(x => inlines.Add(x));
break;
}
case "sup": // Superscript
{
// Convert content to subscript
var content = FormatLine(tagContent);
content.ForEach(x => x.BaselineAlignment = BaselineAlignment.TextTop);
content.ForEach(x => x.FontSize *= 0.75);
//content.ForEach(x => Typography.SetVariants(x, FontVariants.Superscript));
// Add content
content.ForEach(x => inlines.Add(x));
break;
}
case "span": // Color
{
// Getting color
string color = fullTag.GetBetween("<span class=\"", "\"");
// Get content inlines
List<Inline> contentInlines = FormatLine(tagContent);
// Switch for colors
switch (color) // Terminates
{
case "red":
{
// Apply red
contentInlines.ForEach(x => x.Foreground = new SolidColorBrush(Colors.Red));
}
break;
case "red strong":
{
// Apply red and bold
contentInlines.ForEach(x => x.Foreground = new SolidColorBrush(Colors.Red));
contentInlines.ForEach(x => x.FontWeight = FontWeights.Bold);
break;
}
default: // Unknown
{
throw new Exception($"Unknown span color: {color}");
}
}
// Put inlines into inlines
inlines = inlines.Concat(contentInlines).ToList();
break;
}
// Italic
case "i":
case "var":
{
// Add content as italic
inlines.Add(new Italic(GetFinishedRun(tagContent)));
break;
}
// Table
case "table":
{
Grid G = new Grid();
// Go over rows
int curRow = 0;
while (tagContent.Contains("<tr>"))
{
int curCol = 0;
// Add row
G.RowDefinitions.Add(new RowDefinition());
// Get row
string rowStr = tagContent.GetBetweenInclusive("<tr>", "</tr>");
// Remove row
tagContent = tagContent.Remove(0, rowStr.Length);
while (rowStr.Contains("<td>"))
{
if (curCol >= G.ColumnDefinitions.Count)
{
G.ColumnDefinitions.Add(new ColumnDefinition());
}
string cellStr = rowStr.GetBetweenInclusive("<td>", "</td>");
rowStr = rowStr.Remove(0, cellStr.Length);
string cellStrContent = cellStr.GetBetween("<td>", "</td>");
TextBlock TB = new TextBlock();
FormatLine(cellStrContent).ForEach(x => TB.Inlines.Add(x));
Grid.SetColumn(TB, curCol);
Grid.SetRow(TB, curRow);
G.Children.Add(TB);
curCol++;
}
curRow++;
}
inlines.Add(new InlineUIContainer()
{
Child = G
});
break;
}
// Section
case "div":
{
FormatLine(tagContent).ForEach(x => inlines.Add(x));
break;
}
default:
{
throw new Exception($"Unknown inline tag: {tagName}");
}
}
break;
}
default:
{
throw new Exception($"If you're reading this, there is something very wrong");
}
}
break;
}
default:
{
throw new Exception($"No known escape char: {tag.firstChar}");
}
}
// Remove tag from line
line = line.Remove(0, fullTag.Length);
// Format remainder and add to list
if (line != "")
{
FormatLine(line).ForEach(x => inlines.Add(x));
}
// Return
return inlines;
}
}
// Problem datastructure and loading functions
static class ProblemData
/// Static class containing the problem data
{
public static int totalProblemAmount, problemsLoaded;
public static Dictionary<int, Problem> Problems;
private static System.Net.WebClient webClient;
private static string archivesWebData;
static ProblemData()
{
#region Initialize variables
// Set the amount of problems to load initially
problemsLoaded = 0;
// Create problem dictionairy
Problems = new Dictionary<int, Problem>();
#endregion
#region Load problems
// Create WebClient
webClient = new System.Net.WebClient();
// Get the number of problems
archivesWebData = Encoding.UTF8.GetString( webClient.DownloadData("https://projecteuler.net/archives") );
// Find "The problems archives table shows problems 1 to xxx."
string totalProblemAmountString = archivesWebData.GetBetween("The problems archives table shows problems 1 to ", ".");
totalProblemAmount = Convert.ToInt32(totalProblemAmountString);
// Load the first 10 problems
LoadNext(10);
#endregion
}
public static void LoadNext(int loadAm)
{
// For each problem
for (int pI = problemsLoaded; pI < problemsLoaded + loadAm; pI++)
{
// Get problem number
int pN = pI + 1;
// Get webData
byte[] rawBytes = webClient.DownloadData("http://projecteuler.net/problem=" + pN);
string webData = System.Text.Encoding.UTF8.GetString(rawBytes);
// Add problem to dictionairy
Problems.Add(pN, new Problem(webData.GetBetween("<span class=\"tooltiptext_right\">", "</span>"))
{
Title = webData.GetBetween("<h2>", "</h2>"),
WebData = webData
}) ;
}
problemsLoaded += loadAm;
}
public static void LoadAtLeast(int amount)
{
while (Problems.Count < amount)
{
LoadNext(1);
}
}
public static UserProfile LoadUserData(string userDataFileName)
{
using (FileStream fs = new FileStream(userDataFileName, FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(UserProfile));
return (UserProfile)serializer.Deserialize(fs);
}
}
}
// Class structure for user profiles (xml storage)
[Serializable]
public class UserProfile
{
public string userName;
public string userDataFileName;
public List<KeyValuePair<int, string>> userProblemState;
public string clientVersion;
// Default constructor for serialization
public UserProfile() { }
// Actual contructor
public UserProfile( string _userName, string _clientVersion )
{
userName = _userName;
userDataFileName = userName + "_Data.xml";
userProblemState = new List<KeyValuePair<int, string>>();
clientVersion = _clientVersion;
}
public void SaveUserData()
{
using (FileStream fs = new FileStream(userDataFileName, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(UserProfile));
serializer.Serialize(fs, this);
}
}
}
//class P_1 // Multiples of 3 and 5
//{
// string Title = "Multiples of 3 and 5";
// int Difficulty = 5;
// int Solved = 964994;
// StackPanel Discription()
// {
// StackPanel SP = new StackPanel();
// SP.Children.Add(new TextBlock()
// {
// Margin = new Thickness(10, 10, 10, 10),
// Text = "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23."
// });
// SP.Children.Add(new TextBlock()
// {
// Margin = new Thickness(10, 10, 10, 10),
// Text = "Find the sum of all the multiples of 3 or 5 below 1000."
// });
// return SP;
// }
// long Solution()
// {
// // Find the sum of all the multiples of 3 or 5 below 1000.
// int upperLimit = 1000;
// int sum = 0;
// int[] divisors = new int[] { 3, 5 };
// foreach (int d in divisors)
// {
// int m = 1;
// for (int prod = m * d; prod < upperLimit; prod = m * d)
// {
// sum += prod;
// m++;
// }
// }
// return sum;
// }
//}
//class P_2 // Even Fibonacci numbers
//{
// string Title = "Even Fibonacci numbers";
// int Difficulty = 5;
// int Solved = 768279;
// StackPanel Discription()
// {
// StackPanel SP = new StackPanel();
// SP.Children.Add(new TextBlock()
// {
// Margin = new Thickness(10, 10, 10, 10),
// Text = "Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:"
// });
// SP.Children.Add(new TextBlock()
// {
// Margin = new Thickness(10, 10, 10, 10),
// Text = "1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...",
// HorizontalAlignment = HorizontalAlignment.Center
// });
// SP.Children.Add(new TextBlock()
// {
// Margin = new Thickness(10, 10, 10, 10),
// Text = "By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms."
// });
// return SP;
// }
// long Solution()
// {
// // By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
// List<int> Fib = new List<int>() { 1, 2 };
// int upperLimit = 4000000;
// long sum = 2;
// for (int nextFib = Fib[Fib.Count - 1] + Fib[Fib.Count - 2];
// nextFib < upperLimit;
// nextFib = Fib[Fib.Count - 1] + Fib[Fib.Count - 2])
// {
// Fib.Add(nextFib);
// if (nextFib % 2 == 0) // If even
// {
// sum += nextFib;
// }
// }
// return sum;
// }
//}
//class P_3 // Largest prime factor
//{
// string Title = "Largest prime factor";
// int Difficulty = 5;
// int Solved = 550872;
// StackPanel Discription()
// {
// StackPanel SP = new StackPanel();
// SP.Children.Add(new TextBlock()
// {
// Margin = new Thickness(10, 10, 10, 10),
// Text = "The prime factors of 13195 are 5, 7, 13 and 29."
// });
// SP.Children.Add(new TextBlock()
// {
// Margin = new Thickness(10, 10, 10, 10),
// Text = "What is the largest prime factor of the number 600851475143 ?"
// });
// return SP;