-
Notifications
You must be signed in to change notification settings - Fork 0
/
rosetta.txt
665 lines (563 loc) · 14.6 KB
/
rosetta.txt
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
// Fizzbuz
import io;
proc FizzFuzz() -> void
for (i in 1..100) do
switch
case i % 15 == 0:
println("FizzBuzz");
case i % 3 == 0:
println("Fizz");
case i % 5 == 0:
println("Buzz");
else:
println("${i}");
end
end
end
proc FizzFuzz() -> void
for i in 1..100 do
switch
case i % 15 == 0: println("FizzBuzz");
case i % 3 == 0: println("Fizz");
case i % 5 == 0: println("Buzz");
else: println("${i}");
end
end
end
proc FizzBuzz() -> void
{
for (i in 1..100)
{
switch
{
case i % 15 == 0:
println("FizzBuzz");
case i % 3 == 0:
println("Fizz");
case i % 5 == 0:
println("Buzz");
default:
println("{i}");
}
}
}
proc FizzBuzz() -> void
{
for (i in 1..100) do
if i % 15 == 0 do
println("FizzBuzz");
elif i % 3 == 0 do
println("Fizz");
elif i % 5 == 0 do
println("Buzz");
else
println("{i}");
end
end
}
if foo == 1 do
test();
else
something_else();
end;
const GAMES = 3000000
program monty
random : Random;
random.init(time(NULL))
for (int i = 0, i < GAMES, i++) do
door : bool[3];
door[random.next(3)] = true;
choice : int = random.next(3);
if (((!(door[((choice + 1)%3)])) && (door[((choice + 2) % 3)])) || (!(door[((choice+2)%3)]) &&
(door[((choice+1)%3)]))) -> winsbyswitch++;
end;
printf("\nAfter ${GAMES}, I won ${win_by_switch} by switching. That is %f. ",
(float)winsbyswitch*100.0/(float)i);
end
proc FizzBuzz() -> void
{
for (i in 1..100)
{
if (i % 15 == 0)
{
println("FizzBuzz");
}
elif (i % 3 == 0)
{
println("Fizz");
}
elif (i % 5 == 0)
{
println("Buzz");
}
else
{
println("{i}");
}
}
}
proc mod_inverse(a : int, m : int) -> int
{
if (m == 1) -> return 0;
m0 := m;
x := 1;
y := 0;
while (a > 1)
{
q := a / m;
temp := m;
m = a % m;
a = temp;
temp = y;
y = x - q * y;
x = temp;
/*
Should we support this??
(a, m) = (m, a % m);
(x, y) = (y, x - q * y);
*/
}
return x < 0 ? x + m0 : x;
}
proc mod_inverse(a : i32, m : i32) -> i32
{
if (m == 1) -> return 0;
i32 m0 = m;
i32 x = 1;
i32 y = 0;
while (a > 1)
{
i32 q = a / m;
i32 temp = m;
m = a % m;
a = temp;
temp = y;
y = x - q * y;
x = temp;
/*
Should we support this??
(a, m) = (m, a % m);
(x, y) = (y, x - q * y);
*/
}
return x < 0 ? x + m0 : x;
}
proc mod_inverse(a : i32, m : i32) -> i32
if (m == 1) -> return 0;
i32 m0 = m;
i32 x = 1;
i32 y = 0;
while a > 1 do
i32 q = a / m;
i32 temp = m;
m = a % m;
a = temp;
temp = y;
y = x - q * y;
x = temp;
(a, m) = (m, a % m);
(x, y) = (y, x - q * y);
end
return x < 0 ? x + m0 : x;
end
proc mod_inverse(a : i32, m : i32) -> i32
if (m == 1) -> return 0;
m0 := m;
x := 1;
y := 0;
while a > 1 do
q := a / m;
temp := m;
m = a % m;
a = temp;
temp = y;
y = x - q * y;
x = temp;
(a, m) = (m, a % m);
(x, y) = (y, x - q * y);
end
return x < 0 ? x + m0 : x;
end
// Swap:
tmp := a;
a := b;
b := tmp;
// OR a, b = b, a
// Anagram
program Anagram
{
stream := FileStream.new().init("unixdict.txt");
anagramMap := Map.new().init();
u32 count = 0;
while (line, eof := stream->getLine(); !eof)
{
chars := line->getChars();
Array.sort(chars);
key := String.new()->chars).autorelease();
list := anagramMap->get(key);
if (!list)
{
list := Array.new().autorelease();
anagramMap->put(key, list);
}
list->addObject(line);
count = max(count, list->count());
}
stream->close();
for (key, value in list)
{
if (value->count() >= count)
{
println(String.join(value, ", "));
}
}
}
struct string @packed
{
type : u8;
length : u48;
union
{
data : u8[@size(u8*)];
pointer : u8*
}
}
proc chars(string *self) -> u8[]
{
if (!self) -> return nil;
data : u8[] = alloc(u8[self.length]);
switch type
case 0:
memcpy(self.data, data, 0, self.length);
else:
memcpy(self.pointer, data.pointer, 0, self.length);
end
return data;
}
program Anagram
FileStream stream;
stream.load("unixdict.txt")
catch NOT_FOUND:
println("Can't find dictionary");
catch NOT_READABLE | FOO_TEST:
println("Can't read file");
catch ANY as error:
println("${error}");
end;
stream.load("unixdict.txt") => rethrow;
defer -> stream.close();
Map anagramMap;
anagramMap.init();
u32 count = 0;
while (line : string, eof : bool = stream.getLine() rethrow; !eof) do
chars : char[] = line.chars();
Array.sort(chars);
key : string = chars.createString();
list : string[...] = anagramMap.get(key);
if !list do
list = alloc(string[...])
anagramMap.put(key, list);
end
list.append(line);
count = max(count, list.size());
end catch ignore
for key : string, value : list[...] in list do
if value.size() >= count do
println(value.join(", "));
end
end
end
program Anagram
FileStream stream = FileStream.new()->init("unixdict.txt");
defer -> stream->close();
Map anagramMap = Map.new()->init();
u32 count = 0;
while i32 line, i32 eof = stream->getLine(); !eof do
id chars = line->getChars();
Array.sort(chars);
String key = String.new()->chars().autorelease();
id list = anagramMap->get(key);
if !list do
list := Array.new().autorelease();
anagramMap->put(key, list);
end
list->addObject(line);
count = max(count, list->count());
end
for key, value in list do
if (value->count() >= count do
println(String.join(value, ", "));
end
end
end
program Anagram
begin
FileStream stream;
stream.init("unixdict.txt") !!
catch
println("Failed to open dictionary");
exit(-1);
end;
defer -> stream.close();
Map anagramMap;
anagramMap.init();
u32 count = 0;
while i32 line, i32 eof = stream.getLine(); !eof
begin
String chars = line.getChars();
chars.sort();
list : List* = anagramMap->get(chars);
if !list do
list = alloc(Array);
anagramMap.put(key, list);
end
list.addObject(line);
count = max(count, list.count());
end
for key, value in list do
if (value.count() >= count do
println(value.join(", "));
end
end
end
program Anagram
begin
FileStream stream;
err != stream.init("unixdict.txt");
if err do
println("Failed to open dictionary");
exit(-1);
end
switch (err)
case NOT_FOUND: panic("Can't find dictionary");
case NOT_READABLE | FOO_TEST: panic("Can't read file");
end
defer
stream.close();
end
anagramMap : Map[String][List[String]];
Map::init(&anagramMap);
count : u32 = 0;
while (line : string, eof : bool = stream.getLine(); !eof) do
chars : u8[] = line.getChars();
chars.sort();
list : List[String] = anagramMap.get(chars);
if !list do
list = [];
anagramMap.put(key, list);
end
list []= line;
count = max(count, list.count());
end
for key, value in list do
if (value.count() >= count do
println(value.join(", "));
end
end
end
program Anagram
{
stream := FileStream.new()->init("unixdict.txt");
anagramMap := Map.init();
u32 count = 0;
while (line, eof := stream->getLine(); !eof)
{
chars := line->getChars();
Array.sort(chars);
key := String(chars);
list := anagramMap->get(key);
if (!list)
{
list := Array.new().autorelease();
anagramMap->put(key, list);
}
list->addObject(line);
count = max(count, list->count());
}
stream->close();
for (key, value in list)
{
if (value->count() >= count)
{
println(String.join(value, ", "));
}
}
}
program SendMail
{
msg : MailMessage = MailMessage.stack()->init();
msg->addRecipient(name: "Alice Moralis",
email: "[email protected]",
MailRecipient.PRIMARY);
msg->addRecipient(name: "Patrick Kilpatrick",
email: "[email protected]",
MailRecipient.CC);
msg->addRecipient(name: "Alice Moralis",
email: "",
MailRecipient.);
msg.addRecipient (MailRecipient (MailRecipient::PRIMARY_RECIPIENT,
"Alice Moralis"));
msg.addRecipient (MailRecipient (MailRecipient::,
"",
""));
msg.addRecipient (MailRecipient (MailRecipient::BCC_RECIPIENT,
"Michael Carmichael"));
msg.setSender ("Roy Kilroy <[email protected]>");
msg.setSubject ("Rosetta Code");
msg.setContent ("Sending mail from C++ using POCO C++ Libraries");
SMTPClientSession smtp ("mail.example.com"); // SMTP server name
smtp.login ();
smtp.sendMessage (msg);
smtp.close ();
std::cerr << "Sent mail successfully!" << std::endl;
}
catch (std::exception &e)
{
std::cerr << "failed to send mail: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
program SendMail
{
msg : MailMessage;
msg.addRecipient(name: "Alice Moralis", email: "[email protected]", MailRecipient.PRIMARY);
msg.addRecipient(name: "Patrick Kilpatrick", email: "[email protected]", MailRecipient.CC);
msg.addRecipient(name: "Alice Moralis", email: "", MailRecipient.BCC);
msg.sender = "Roy Kilroy <[email protected]>";
msg.subject = "Rosetta Code";
msg.content = "Testing";
SMTPClientSession smtp;
err != smtp.init("mail.example.com");
err !!= smtp.login()!;
err !!= smtp.sendMail(msg);
err !!= smtp.close();
if err do
println("Failed to send mail: ${err}");
exit(-1);
end
println("Sent message successfully!");
exit(0);
}
proc GameMapModel.getOwnerType(Position position) -> Owner
locationData : LocationData = getLocationData(position).data;
ownerId : LeaderId = locationData.ownerId;
switch
case locationData.freezone: return Owner.FREEZONE;
case ownerId == LeaderNone: return Owner.NONE;
case isPlayer(ownerId): return Owner.SELF;
case Player().getAlignment() == Alignment.GOOD
&& leaderById(ownerId).getAlignament() == Alignment.GOOD: return Owner.ALLY;
case Player().areTheyAllied(ownerId): return Owner.ALLY;
else return Owner.OTHER
end
end
Owner GameMapModel::getOwnerType(Position position) const
{
auto locationData = getLocationData(position)->data;
if (locationData->freezone) return Owner::FREEZONE;
LeaderId ownerId = locationData->ownerId;
if (ownerId == LeaderNone) return Owner::NONE;
if (Game::isPlayer(ownerId)) return Owner::SELF;
if (Game::player().getAlignment() == Alignment::GOOD &&
Game::model().getLeaderRegistry()->byId(ownerId)->getAlignment() == Alignment::GOOD)
{
return Owner::ALLY;
}
return Game::player().areTheyAllied(ownerId) ? Owner::ALLY : Owner::OTHER;
}
proc GameMapModel.getAreaArmies() -> Army
totalArmy : Army = 0;
foreach key, value in $locations do
if isPlayer(value.data.ownerId) -> totalArmy.addArmy(value.data.army);
end
end
Army GameMapModel::getAreaArmies()
{
Army totalArmy;
for (auto pair : m_locations)
{
if (Game::isPlayer(pair.second->data->ownerId))
{
totalArmy.addArmy(pair.second->data->army);
}
}
return totalArmy;
}
void GameMapModel::locationUpdated(const LocationUpdatedPacket &packet)
{
setDirty();
auto currentData = getLocationData(packet.position);
++currentData->version;
const Object &value = packet.value;
switch (packet.property)
{
case AreaProperty::NAME:
currentData->data->nameId = value.intValue();
break;
case AreaProperty::QUEST_MONSTER:
currentData->data->questMonster = Monster::forType(value.stringValue());
break;
case AreaProperty::PRICES:
currentData->data->prices.reset(value.isNull() ? nullptr : new Prices(value));
break;
case AreaProperty::DESTINATIONS:
currentData->data->destinations = value.mapValue();
break;
case AreaProperty::SPELLS:
currentData->data->spells = value.arrayValue();
break;
case AreaProperty::OWNER:
currentData->data->ownerId = (LeaderId)value.intValue();
break;
case AreaProperty::MAGICIAN:
currentData->data->magician = (MagicianType)value.intValue();
break;
case AreaProperty::PORTAL:
currentData->data->portal = value.intValue();
break;
case AreaProperty::ARMY:
currentData->data->army = Army(value);
break;
case AreaProperty::DRAGON:
currentData->data->dragon = value.isNull() ? std::shared_ptr<NPC>() : std::make_shared<NPC>(value);
break;
case AreaProperty::DRAGON_TASK:
currentData->data->dragonKillTask = (LeaderId)value.intValue();
break;
case AreaProperty::SHRINE:
currentData->data->shrineBuilderId = (LeaderId)value.intValue();
break;
case AreaProperty::BORDER_ARMY:
currentData->data->borderArmy = Army(value);
break;
case AreaProperty::FREEZONE:
currentData->data->freezone = value.boolValue();
break;
}
}
proc GameMapModel.locationUpdated(LocationUpdatedPacket *packet) -> void
self.setDirty();
currentData : *LocationData = getLocationData(packet.position);
current = ¤tData.data;
++currentData.version;
Object *value = &packet.value;
switch packet.property
case AreaProperty::NAME: current.nameId = value.intValue();
case AreaProperty::QUEST_MONSTER: current.questMonster = Monster.forType(value.stringValue());
case AreaProperty::PRICES: current.prices = value.isNull() ? nil : new Prices(value));
case AreaProperty::DESTINATIONS: current.destinations = value.mapValue();
case AreaProperty::SPELLS: current.spells = value.arrayValue();
case AreaProperty::OWNER: current.ownerId = @cast(value.intValue(), LeaderId);
case AreaProperty::MAGICIAN: current.magician = @cast(value.intValue(), MagicianType);
case AreaProperty::PORTAL: current.portal = value.intValue();
case AreaProperty::ARMY: current.army = Army(value);
case AreaProperty::DRAGON: current.dragon = value.isNull() ? NPC() : NPC(value);
case AreaProperty::DRAGON_TASK: current.dragonKillTask = @cast(value.intValue(), LeaderId);
case AreaProperty::SHRINE: current.shrineBuilderId = @cast(value.intValue(), LeaderId);
case AreaProperty::BORDER_ARMY: current.borderArmy = Army(value);
case AreaProperty::FREEZONE: current.freezone = value.boolValue();
end
end