previndexinfonext

code guessing, round #85 (completed)

started at ; stage 2 at ; ended at

specification

they woke me up and told me I have to do my job. not good. play this multiplayer snake. submissions can be written in most languages.

overview

we're going to play snake, a game where you are a snake on a grid and eat apples. this snake is different because there can be multiple snakes at once. (and no, this isn't slither.io...) you will play the role of only one of the snakes, while the others will be represented by other players' submissions.

much like rounds 61 and 78, your program will interact with other programs, facilitated by a central "host" program (gaming.js). for this reason, it should follow the following fixed protocol over stdin and stdout.

the beginning

begin by writing, followed by a newline, one of the following encodings:

utf8
utf16
utf16ne
utf16le
utf16be
ucs2
ucs2ne
ucs2le
ucs2be
ascii
latin1

when reading, you will receive only the encoding you request here from gaming.js. the ucs2* and utf16* options are equivalent, as no character requiring surrogates is used. ascii and latin1 are also equivalent. this specification only expects you to write ASCII, and NUL characters are ignored, so it does not matter which encoding you use when writing, nor whether it matches your initial choice.

in base-10 decimal, in your chosen encoding (as with all future communication), gaming.js will send you the width and height of the game board on separate lines, like this:

16
5

game loop

the board

while 1, gaming.js will show you the game board, using a set of characters that depends on the encoding:

$.╋╬0O╔═╚╗║╝┏━┗┓┃┛  (utf8, utf16*, ucs2*)
$.+#0O7=19|3q-ze:c  (ascii, latin1)

the board consists of the following entities represented by the following symbols:

the process to parse a snake's body, differentiating it from other snakes' bodies, is simple. follow these steps using a gnome.

  1. begin standing at the head (O0)
  2. walk to some neighbouring double pipe (╬╔═╚╗║╝) connected to the head
  3. look at the tile on which you will walk next. on junctions (╋╬), you must go straight. otherwise, just follow the visual path of the current tile without turning back on yourself, like following a road.
  4. if you are looking at any pipe (╋╬╔═╚╗║╝┏━┗┓┃┛) connected to where you are standing, regardless of whether it is doubled or not, walk there and go back to step 3. otherwise, you are done.

the gnome's path following this process traces the body of the snake.

your action

after giving you the board, gaming.js waits for your response. write any of the characters rdluRDLU.. (a newline is not necessary.) they have the following effects:

other characters will be ignored. if you close stdout/terminate instead of answering, you will die, dropping apples for other snakes to eat.

moving onto an apple, obviously, will grow you by 1 segment. moving into another snake, obviously, will kill you. but movement is asynchronous! if you try to move into the same empty space as another snake on the same turn, both of you will die unless exactly 1 of you is sprinting, in which case only the one not sprinting will die. if you move into a snake's tail the same turn that it moves away, you will live.

unobviously, moving into yourself doesn't always kill you. if you move perpendicularly into one of your own straight segments, you live and move again so that you land on the other side. this can repeat as many times as is necessary across several straight segments, but if you hit a corner of yourself or another snake while jumping like this, you die. here's an example where going left can move you a great distance across many tiles:

..┏┓┏┓┏┓┏┓┏┓┏┓..
.┃┃┃┃┃┃┃┃┃┃┃┃┃0.
.┗┛┗┛┗┛┗┛┗┛┗┛┗╝.

....┏┓┏┓┏┓┏┓┏┓..
....0╬╋╋╋╋╋╋╋╋┓.
.....┗┛┗┛┗┛┗┛┗┛.

note that additional moves may be made to keep your body unambiguous, like in this case where moving down causes you to move once more than apparently necessary:

.........
.........
...┃0╗...
...┗━┛...
.........
.........

.........
.........
....┏┓...
....┃┛...
....║....
....0....

see also this queer case where moving into a junction tile is safe, because it is our own tail and moves away in the same turn, leaving a straight segment to jump over:

....┏━┓...
..┏━╋━╋┓..
..┗━╋┓┃┃..
...╔0┃┃┃..
...┗━┛┃┃..
......┗┛..

....0━┓...
..┏━╬━╋┓..
..┗━╋┓┃┃..
...┏┛┃┃┃..
...┗━┛┃┃..
......┗┛..

the challenge

we're finally here. your challenge is to play this game and achieve... anything! your exact goal is unspecified. have fun!

kimapr's original specification, as well as the implementation of gaming.js to test against, is available here.

results

  1. 🅿️ essaie +5 -3 = 2
    1. *Ada
    2. kimapr
    3. oleander
    4. Indigo
    5. pyrotelekinetic
  2. oleander +3 -1 = 2
    1. Indigo (was *Ada)
    2. kimapr
    3. essaie
    4. *Ada (was Indigo)
    5. pyrotelekinetic
  3. kimapr +3 -2 = 1
    1. *Ada
    2. essaie
    3. pyrotelekinetic (was oleander)
    4. Indigo
    5. oleander (was pyrotelekinetic)
  4. Indigo +1 -2 = -1
    1. kimapr (was *Ada)
    2. *Ada (was kimapr)
    3. essaie
    4. pyrotelekinetic (was oleander)
    5. oleander (was pyrotelekinetic)
  5. *Ada +0 -2 = -2
    1. essaie (was kimapr)
    2. kimapr (was essaie)
    3. pyrotelekinetic (was oleander)
    4. oleander (was Indigo)
    5. Indigo (was pyrotelekinetic)
  6. pyrotelekinetic +0 -2 = -2
    1. kimapr (was *Ada)
    2. essaie (was kimapr)
    3. Indigo (was essaie)
    4. *Ada (was oleander)
    5. oleander (was Indigo)

entries

you can download all the entries

entry #1

written by *Ada
submitted at
1 like

guesses
comments 1
essaie

\Did you know? The humble JAVASCRIPT is a kind of snake!


post a comment


snakebot69.js ASCII text
 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
#!/usr/bin/env node

/**
 *
 *     @@@@@@@  @@@@@@  @@@@@@@  @@@@@@@@  @@@@@@@  @@@  @@@ @@@@@@@@  @@@@@@  @@@@@@    @@@  @@@  @@@  @@@@@@  @@@@@@@  @@@@@@@@ @@@@@@@@
 *    !@@      @@!  @@@ @@!  @@@ @@!      !@@       @@!  @@@ @@!      !@@     !@@        @@!  @@!  @@! @@!  @@@ @@!  @@@ @@!           @@!
 *    !@!      @!@  !@! @!@  !@! @!!!:!   !@! @!@!@ @!@  !@! @!!!:!    !@@!!   !@@!!     @!!  !!@  @!@ @!@!@!@! @!@!!@!  @!!!:!      @!!
 *    :!!      !!:  !!! !!:  !!! !!:      :!!   !!: !!:  !!! !!:          !:!     !:!     !:  !!:  !!  !!:  !!! !!: :!!  !!:       !!:
 *     :: :: :  : :. :  :: :  :  : :: ::   :: :: :   :.:: :  : :: ::  ::.: :  ::.: :       ::.:  :::    :   : :  :   : : : :: ::  :.::.: :
 *
 *
 *    @@@@@@@  @@@@@@@  @@@@@@@@  @@@@@@ @@@@@@@@ @@@  @@@ @@@@@@@  @@@@@@
 *    @@!  @@@ @@!  @@@ @@!      !@@     @@!      @@!@!@@@   @!!   !@@
 *    @!@@!@!  @!@!!@!  @!!!:!    !@@!!  @!!!:!   @!@@!!@!   @!!    !@@!!
 *    !!:      !!: :!!  !!:          !:! !!:      !!:  !!!   !!:       !:!
 *     :        :   : : : :: ::  ::.: :  : :: ::  ::    :     :    ::.: :
 *
 *
 *    @@@      @@@@@@@@  @@@@@@@   @@@@@@  @@@          @@@@@@ @@@  @@@  @@@@@@  @@@  @@@ @@@@@@@@    @@@@@@@   @@@@@@  @@@@@@@
 *    @@!      @@!      !@@       @@!  @@@ @@!         !@@     @@!@!@@@ @@!  @@@ @@!  !@@ @@!         @@!  @@@ @@!  @@@   @!!
 *    @!!      @!!!:!   !@! @!@!@ @!@!@!@! @!!          !@@!!  @!@@!!@! @!@!@!@! @!@@!@!  @!!!:!      @!@!@!@  @!@  !@!   @!!
 *    !!:      !!:      :!!   !!: !!:  !!! !!:             !:! !!:  !!! !!:  !!! !!: :!!  !!:         !!:  !!! !!:  !!!   !!:
 *    : ::.: : : :: ::   :: :: :   :   : : : ::.: :    ::.: :  ::    :   :   : :  :   ::: : :: ::     :: : ::   : :. :     :
 *
 */

const readline = require('readline');
let process = require("node:process");
process.stdin.setEncoding('ascii');

(async () => {
	console.log('ascii');
	let width = 0;
	let height = 0;
	{
		const rl = readline.createInterface({
			input: process.stdin
		})
		for await (const line of rl) {
			if (width == 0) {
				width = parseInt(line, 10);
				continue;
			} else {
				height = parseInt(line, 10);
				break;
			}
		}
	}

	cmd = async (s) => {
		console.log(s);

		let r = new Set();
		{
			const rl = readline.createInterface({
				input: process.stdin
			})
			let i = 0;
			for await (const line of rl) {
				r = r.union(new Set(line.split('$').map(d => d.length + i * width)));
				let head = line.indexOf('0');
				if (head > -1) {
					if (line.indexOf('.') == -1 && ((line + line).indexOf('0$') == -1)) {
						return null;
					}
				}

				if (i == height) {
					break;
				}

				i += 1;
			}
		}
		return r;
	}

	while(1) {
		let last = await cmd('d') ?? new Set();
		for(let i = 0; i < 16; ++i) {
			let n = await cmd('r');
			if (n == null || n.isDisjointFrom(last)) {
				break;
			}
		}
	}
})();

entry #2

written by kimapr
submitted at
0 likes

guesses
comments 1
essaie

\Did you know? The humble CPLUSPLUS is a kind of snake!


post a comment


worm.cpp Unicode text, UTF-8 text
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <clocale>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iterator>
#include <optional>
#include <stdexcept>
#include <type_traits>
#include <vector>

#if (__cplusplus >= 202302L)
#  include <utility>
#  define UNREACHABLE std::unreachable();
#else
#  define UNREACHABLE __builtin_unreachable();
#endif

int_least32_t get2char()
{
  char16_t buf;

  if (!fread(&buf, 2, 1, stdin))
    return -1;

  return buf;
}

char *read2line1()
{
  static char line[4096];
  int i = 0;
  int_least32_t c;

  while ((c = get2char()), c != -1 && c != '\n') {
    if (i < sizeof(line) - 1)
      line[i++] = c;
  }

  line[i] = 0;

  return line;
}

bool is_le()
{
  union {
    short h;
    char c[2];
  } num;

  num.h = 1;

  return num.c[0];  // undifined behavior
}

struct Pos {
  int x, y;

  constexpr Pos(int x, int y) : x(x), y(y) {}
  constexpr Pos() : x(0), y(0) {}
};

struct Dir {
  char i;

  constexpr Dir(int i) : i(i) {}
  constexpr Dir() : i(4) {}

  constexpr char to_cmd()
  {
    constexpr std::array<char, 4> cmds{'r', 'd', 'l', 'u'};

    return cmds[i];
  }

  constexpr char to_cmd_fast()
  {
    return to_cmd() + ('A' - 'a');
  }
};

constexpr bool is_cstr(int c, const char16_t *str)
{
  for (; *str != '\0'; str++)
    if (c == *str)
      return true;

  return false;
}

Pos operator+(Pos pos, Dir dir)
{
  switch (dir.i) {
    case 0:
      return Pos(pos.x + 1, pos.y + 0);
    case 1:
      return Pos(pos.x + 0, pos.y + 1);
    case 2:
      return Pos(pos.x - 1, pos.y + 0);
    case 3:
      return Pos(pos.x + 0, pos.y - 1);
  }

  UNREACHABLE
}

Dir operator-(Pos dst, Pos src)
{
  Pos off(dst.x - src.x, dst.y - src.y);

  struct H {
    static constexpr int i(int x, int y)
    {
      return (y + 1) * 3 + (x + 1);
    }
  };

  switch (H::i(off.x, off.y)) {
    case H::i(1, 0):
      return {0};
    case H::i(0, 1):
      return {1};
    case H::i(-1, 0):
      return {2};
    case H::i(0, -1):
      return {3};
  }

  fprintf(stderr, "bad offset %i %i\n", off.x, off.y);

  UNREACHABLE
}

bool operator==(Pos a, Pos b)
{
  return a.x == b.x && a.y == b.y;
}
bool operator!=(Pos a, Pos b)
{
  return !(a == b);
}
bool operator==(Dir a, Dir b)
{
  return a.i == b.i;
}
bool operator!=(Dir a, Dir b)
{
  return a.i != b.i;
}
Dir operator-(Pos dst)
{
  return dst - Pos();
}
Dir operator+(Dir dir, int n)
{
  return {(char)(((dir.i + n) % 4 + 4) % 4)};
}
Dir operator-(Dir dir, int n)
{
  return dir + -n;
}
Dir operator~(Dir dir)
{
  return dir + 2;
}

struct Cell {
  char16_t c;

  constexpr Cell() : c(u'\0') {}
  constexpr Cell(char16_t c) : c(c) {}

  operator bool()
  {
    return c != u'\0';
  }
  operator char16_t()
  {
    return c;
  }

  constexpr bool is_apple()
  {
    return c == u'$';
  }
  constexpr bool is_safe()
  {
    return c == u'.' || is_apple();
  }
  constexpr bool is_neck()
  {
    return is_cstr(c, u"╬╔═╚╗║╝");
  }

  static constexpr Cell apple()
  {
    return {u'$'};
  }
  static constexpr Cell space()
  {
    return {u'.'};
  }
  static constexpr Cell bodybase(const char16_t *charset, std::bitset<4> conns)
  {
    for (const char16_t *s = charset; *s != 0; s++)
      if (Cell(*s).connections() == conns)
        return Cell(*s);

    UNREACHABLE
  }
  static constexpr Cell neck(std::bitset<4> conns)
  {
    return bodybase(u"╬╔═╚╗║╝", conns);
  }
  static constexpr Cell body(std::bitset<4> conns)
  {
    return bodybase(u"╋┏━┗┓┃┛", conns);
  }
  static Cell neck(Dir a, Dir b)
  {
    std::bitset<4> conns{0};
    conns[a.i] = 1;
    conns[b.i] = 1;
    return neck(conns);
  }
  static Cell body(Dir a, Dir b)
  {
    std::bitset<4> conns{0};
    conns[a.i] = 1;
    conns[b.i] = 1;
    return body(conns);
  }
  static constexpr Cell neck(Cell base)
  {
    return neck(base.connections());
  }
  static constexpr Cell body(Cell base)
  {
    return body(base.connections());
  }
  static constexpr Cell neck()
  {
    return neck(std::bitset<4>(0b1111));
  }
  static constexpr Cell body()
  {
    return body(std::bitset<4>(0b1111));
  }

  constexpr std::bitset<4> connections()
  {
    switch (c) {
      case u'╬':
      case u'╋':
        return {0b1111};
      case u'╔':
      case u'┏':
        return {0b0011};
      case u'╗':
      case u'┓':
        return {0b0110};
      case u'╝':
      case u'┛':
        return {0b1100};
      case u'╚':
      case u'┗':
        return {0b1001};
      case u'║':
      case u'┃':
        return {0b1010};
      case u'═':
      case u'━':
        return {0b0101};
    }

    return {0};
  }

  constexpr bool is_connected(Dir dir)
  {
    const auto state = connections();

    return state[dir.i];
  }

  constexpr Dir walkdir(Dir dir)
  {
    const auto state = connections();

    if (state[dir.i])
      return dir;

    if (state[(dir - 1).i])
      return dir - 1;

    if (state[(dir + 1).i])
      return dir + 1;

    fprintf(stderr, "walktf! (%lc) %i\n", (int)c, (int)dir.i);

    UNREACHABLE
  }

  constexpr std::bitset<2> player()
  {
    if (c == u'0')
      return {0b01};
    if (c == u'O')
      return {0b10};

    return {0};
  }
};

bool operator==(Cell a, char16_t b)
{
  return a.c == b;
}
bool operator!=(Cell a, char16_t b)
{
  return a.c != b;
}

template<class T> class Grid {
 protected:
  int m_width, m_height;
  std::vector<T> data;

 public:
  Grid(int width, int height) : m_width(width), m_height(height)
  {
    data.reserve((m_width + 1) * m_height + 1);
    data.resize((m_width + 1) * m_height + 1);

    if constexpr (std::is_same_v<T, Cell>) {
      for (int y = 0; y < m_height; y++) {
        data[y * (m_width + 1) + m_width] = {u'\n'};
      }

      data[(m_width + 1) * m_height] = {u'\n'};
    }
  }

  void copy_from(Grid &other)
  {
    if (other.width() != width() || other.height() != height())
      UNREACHABLE

    data = other.data;
  }

  bool copy_io(FILE *io = stdin)
  {
    if (!fread(data.data(), data.size() * sizeof(typename decltype(data)::value_type), 1, io))
      return false;

    return true;
  }

  int width()
  {
    return m_width;
  }
  int height()
  {
    return m_height;
  }

  Pos norm(Pos pos)
  {
    return {(pos.x % m_width + m_width) % m_width, (pos.y % m_height + m_height) % m_height};
  }

  Pos norm(Pos pos, Pos base)
  {
    Pos off(pos.x - base.x, pos.y - base.y);

    return {base.x + off.x % m_width, base.y + off.y % m_height};
  }

  decltype(data[0]) operator[](Pos pos)
  {
    pos = norm(pos);

    return data[pos.y * (m_width + 1) + pos.x];
  }

  class iterator {
    Pos pos;
    Grid &grid;

    friend class Grid;
    iterator(Pos pos, Grid &grid) : pos(pos), grid(grid) {}

   public:
    using difference_type = std::ptrdiff_t;
    using value_type = std::pair<Pos, decltype(grid.data[0])>;

    value_type operator*()
    {
      return {pos, grid[pos]};
    }
    bool operator==(const iterator &other)
    {
      return pos == other.pos;
    }
    bool operator!=(const iterator &other)
    {
      return !(*this == other);
    }

    iterator &operator++()
    {
      if (pos.x == grid.width() - 1) {
        pos.x = 0;
        pos.y++;
        return *this;
      }

      pos.x++;
      return *this;
    }

    iterator operator++(int)
    {
      auto tmp = *this;
      ++*this;
      return tmp;
    }
  };

  iterator begin()
  {
    return {Pos(), *this};
  }

  iterator end()
  {
    return {Pos(0, m_height), *this};
  }

  void validate()
  {
    if constexpr (std::is_same_v<T, Cell>) {
      for (int y = 0; y < m_height; y++) {
        if (data[y * (m_width + 1) + m_width] != u'\n')
          throw std::runtime_error("corrupted board");
      }

      if (data[(m_width + 1) * m_height] != u'\n')
        throw std::runtime_error("corrupted board");
    }
  }
};

struct Player {
  Pos pos;
  int debt;
};

enum MoveResult {
  MRESULT_NOOP,
  MRESULT_DEAD,
  MRESULT_FOOD,
  MRESULT_CONT,
};

enum MoveStage {
  MSTAGE_FUME = 0,
  MSTAGE_FAPL = 1,
  MSTAGE_MOVE = 2,
  MSTAGE_FIN = 3,
};

struct Move {
  Dir dir;
  bool fast;
};

class Game : public Grid<Cell> {
  std::vector<Player> players;
  Grid<int> density;
  int apple_count;

  Game(int width, int height) : Grid(width, height), density(width, height) {}

  void init_state()
  {
    players.resize(0);
    apple_count = 0;

    for (auto [pos, cell] : *this) {
      if (cell.player()[0])
        players.insert(players.begin(), Player{.pos = pos, .debt = 0});
      else if (cell.player()[1])
        players.push_back(Player{.pos = pos, .debt = 0});

      if (cell.is_apple())
        apple_count++;
    }

    players.shrink_to_fit();
  }

  std::deque<Pos> snake_body(Pos pos)
  {
    if (!(*this)[pos].player().any()) {
      for (int y = 0; y < height(); y++) {
        for (int x = 0; x < width(); x++) {
          fprintf(stderr,
                  "%lc",
                  (x == pos.x - 1 && y == pos.y) ? '[' :
                  (x == pos.x + 1 && y == pos.y) ? ']' :
                                                   (int)(*this)[Pos(x, y)].c);
        }
        fprintf(stderr, "\n");
      }

      throw std::runtime_error("bad snake");
    }

    Dir dir;
    std::deque<Pos> body{pos};

    for (int i = 0; i < 4; i++) {
      if ((*this)[pos + Dir(i)].is_neck() && (*this)[pos + Dir(i)].is_connected(~Dir(i))) {
        dir = Dir(i);
        break;
      }
    }

    if (dir.i == 4)
      throw std::runtime_error("bad snake");

    for (;;) {
      pos = pos + dir;
      dir = (*this)[pos].walkdir(dir);

      body.push_back(pos);

      if (!(*this)[pos + dir].is_connected(~dir))
        break;
    }

    if (body.size() < 3)
      throw std::runtime_error("bad snake");

    return body;
  }

  void kill(Pos pos)
  {
    for (Pos segpos : snake_body(pos))
      (*this)[segpos] = ((*this)[segpos].connections() == 4 || rand() > RAND_MAX / 2) ?
                            Cell::apple() :
                            Cell::space();
  }

  static bool is_ambiguous(Pos pos, Cell neck, Grid<int> &density)
  {
    for (int i = 0; i < 4; i++)
      if (neck.is_connected(Dir(i)) && !density[pos + Dir(i)])
        return true;

    return false;
  }

  void init_snake_density()
  {
    for (auto [pos, cell] : *this)
      density[pos] = cell.player().any() ? 1 : cell.connections().count() / 2;
  }

  struct MoveState {
    Pos pos;
    int debt;
    int ate;
    std::deque<Pos> body;
    std::vector<Pos> nheads;
    Dir dir;
    bool fast;
    Grid<Cell> tmp;

    MoveState(Game &game) : tmp(game.width(), game.height()) {}
  };

  enum MoveResult move_single(MoveState &state, int stage)
  {
    return move_single(
        state.pos, state.debt, state.body, state.nheads, state.dir, stage, state.tmp, state.ate);
  }

  enum MoveResult move_single(Pos pos,
                              int &debt,
                              std::deque<Pos> &body,
                              std::vector<Pos> &nheads,
                              Dir dir,
                              int stage,
                              Grid<Cell> &tmp,
                              int &ate)
  {
    switch (stage) {
      case MSTAGE_FUME:
        while (debt >= 2 && std::distance(body.begin(), body.end()) >= 5 &&
               (*this)[*std::prev(body.end())].connections().count() < 4)
        {
          Pos tailpos = *std::prev(body.end());
          body.pop_back();

          density[tailpos]--;
          tmp[tailpos] = Cell::apple();

          debt -= 2;
        }

        return MRESULT_CONT;

      case MSTAGE_FAPL:
        for (auto [pos, cell] : tmp) {
          if (cell) {
            (*this)[pos] = cell;
            tmp[pos] = {};
          }
        }

        return MRESULT_CONT;

      case MSTAGE_MOVE: {
        Grid<int> local_density(width(), height());

        for (Pos segpos : body) {
          Cell cell = (*this)[segpos];

          local_density[segpos] = cell.player().any() ? 1 : cell.connections().count() / 2;
        }

        int i = 0;

        do {
          Pos tail = *std::prev(body.end());
          body.pop_back();
          Pos head = *body.begin();
          Pos neck = *std::next(body.begin());
          Pos nexthead = head + dir;
          Cell headval = tmp[head] ? tmp[head] : (*this)[head];
          Cell neckval = tmp[neck] ? tmp[neck] : (*this)[neck];
          Cell nextheadval = tmp[nexthead] ? tmp[nexthead] : (*this)[nexthead];
          Cell tailval = tmp[tail] ? tmp[tail] : (*this)[tail];
          Dir taildir = tail - *std::prev(body.end());

          if (!nextheadval.is_apple()) {
            tmp[tail] = local_density[tail] == 2 ?
                            (tailval.is_neck() ? Cell::neck(taildir + 1, taildir - 1) :
                                                 Cell::body(taildir + 1, taildir - 1)) :
                            Cell::space();
            neckval = tmp[neck] ? tmp[neck] : (*this)[neck];
            headval = tmp[head] ? tmp[head] : (*this)[head];
            local_density[tail]--;
            density[tail]--;
          }
          else {
            ate++;
            body.push_back(tail);
          }

          tmp[neck] = Cell::body(neckval);
          tmp[head] = headval.connections().any() ? Cell::neck(headval) :
                                                    Cell::neck(dir, neck - head);

          if (local_density[nexthead] &&
              nextheadval.connections() != Cell::body(dir + 1, dir - 1).connections())
          {
            return MRESULT_DEAD;
          }
          else if (local_density[nexthead]) {
            tmp[nexthead] = Cell::neck();
          }
          else {
            tmp[nexthead] = (*this)[pos];
            nheads.push_back(nexthead);
          }

          body.push_front(nexthead);
          local_density[nexthead]++;
          density[nexthead]++;

          if (++i > 1000) {
            std::string info;

            for (int y = 0; y < height(); y++) {
              for (int x = 0; x < width(); x++) {
                char buf[16];

                sprintf(
                    buf, "%lc", (char16_t)(tmp[Pos(x, y)] ? tmp[Pos(x, y)] : (*this)[Pos(x, y)]));

                info += buf;
              }

              info += "\n";
            }

            info += "vs: (orig)\n";

            for (int y = 0; y < height(); y++) {
              for (int x = 0; x < width(); x++) {
                char buf[16];

                sprintf(buf, "%lc", (char16_t)((*this)[Pos(x, y)]));

                info += buf;
              }

              info += "\n";
            }

            throw std::runtime_error(std::string("wtf? {\n") + info + "}\n");
          }
        } while (tmp[body[0]] != (*this)[pos] ||
                 is_ambiguous(body[1], tmp[body[1]], local_density));

        return MRESULT_CONT;
      }

      case MSTAGE_FIN:
        for (auto pos : nheads) {
          if (density[pos] > 1)
            return MRESULT_DEAD;
        }

        for (auto [pos, cell] : tmp) {
          if (cell && !(cell.is_safe() && density[pos])) {
            (*this)[pos] = cell;
          }
          tmp[pos] = {};
        }

        return ate ? MRESULT_FOOD : MRESULT_NOOP;

      default:
        UNREACHABLE
    }
  }

 public:
  static Game from_io()
  {
    int width, height;

    setvbuf(stdout, NULL, _IONBF, 0);
    printf("utf16%s\n", is_le() ? "le" : "be");

    const char *ln;
    if (sscanf(ln = read2line1(), "%i", &width) != 1) {
      fprintf(stderr, "line read(%s)\n", ln);
      throw std::runtime_error("failed to read width");
    }

    if (sscanf(read2line1(), "%i", &height) != 1)
      throw std::runtime_error("failed to read height");

    Game game(width, height);

    if (!fread(game.data.data(),
               game.data.size() * sizeof(decltype(game.data)::value_type),
               1,
               stdin))
      throw std::runtime_error("failed to read board");

    game.validate();
    game.init_state();

    return game;
  }

  Pos player_pos(int i = 0)
  {
    return players[i].pos;
  }

  int player_length(int i = 0)
  {
    return snake_body(players[i].pos).size();
  }

  int avail_space(int i = 0)
  {
    Grid<bool> seen(width(), height());
    Grid<bool> gsnake(width(), height());
    std::vector<Pos> queue{players[i].pos};
    seen[queue[0]] = true;
    std::vector<Pos> queue_next;
    int amount = 0;

    if (!(*this)[queue[0]].player().any())
      return amount;

    for (auto pos : snake_body(queue[0]))
      gsnake[pos] = true;

    while (queue.size()) {
      for (auto pos : queue) {
        for (int i = 0; i < 4; i++) {
          Dir dir = Dir(i);
          Pos npos = norm(pos + dir);

          if ((*this)[pos].connections()[dir.i])
            continue;
          if (seen[npos])
            continue;
          if (!((*this)[npos].is_safe() ||
                (gsnake[pos] &&
                 (*this)[npos].connections() == Cell::body(dir + 1, dir - 1).connections())))
            continue;

          if ((*this)[npos].is_safe())
            amount++;

          queue_next.push_back(npos);
          seen[npos] = true;
        }
      }

      queue = queue_next;
      queue_next = {};
    }

    return amount;
  }

  Pos closest_apple(Pos base)
  {
    Pos apple = base;
    unsigned int dist = -1;

    for (auto [pos, cell] : *this) {
      pos = norm(pos, base);
      unsigned int ndist = abs(pos.x - base.x) + abs(pos.y - base.y);

      if (cell.is_apple() && ndist < dist) {
        apple = pos;
        dist = ndist;
      }
    }

    return apple;
  }

  template<class F> void closest_dirs(Pos base, Pos target, F on_dir)
  {
    for (int i = 0; i < 4; i++) {
      Pos off{target.x - base.x, target.y - base.y};
      off.x = off.x > 0 ? 1 : off.x < 0 ? -1 : 0;
      off.y = off.y > 0 ? 1 : off.y < 0 ? -1 : 0;

      Pos doff = Pos() + Dir(i);

      if (off.x && doff.x == off.x || off.y && doff.y == off.y) {
        on_dir(Dir(i));
      }
    }
  }

  template<class T> void rand_moves(T &moves)
  {
    for (int pi = moves.size(); pi < player_count(); pi++) {
      Move move = {rand() % 4, (bool)(rand() % 2)};
      moves.push_back(move);
    }
  }

  int player_count()
  {
    return players.size();
  }

  template<class Moves> int move(Moves &moves)
  {
    std::vector<bool> alive;
    alive.resize(players.size(), true);

    for (int i = 0; i < players.size(); i++) {
      auto body = snake_body(players[i].pos);

      if (moves[i].dir == body[1] - body[0]) {
        moves[i].dir = body[0] - body[1];
      }

      if (body.size() <= 5)
        moves[i].fast = false;

      if (moves[i].fast)
        players[i].debt++;
    }

    int body_size = 0;

    for (int mi = 0; mi < 2; mi++) {
      init_snake_density();

      struct MStateExtra {
        MoveState ms;
        int stage = 0;

        MStateExtra(Game &game) : ms(game) {}
      };

      std::vector<std::optional<MStateExtra>> mstates;
      mstates.resize(players.size(), {});
      int count = 0;

      for (int i = 0; i < players.size(); i++) {
        if ((!alive[i]) || (!(mi || moves[i].fast)))
          continue;

        auto &mstate = mstates[i].emplace(*this);
        mstate.ms.pos = players[i].pos;
        mstate.ms.debt = players[i].debt;
        mstate.ms.body = snake_body(players[i].pos);
        mstate.ms.dir = moves[i].dir;
        mstate.ms.fast = moves[i].fast;

        count++;
      }

      while (count) {
        for (int i = 0; i < mstates.size(); i++) {
          if (!mstates[i])
            continue;

          auto &state = *mstates[i];

          int result = move_single(state.ms, state.stage);

          if (result == MRESULT_CONT) {
            state.stage++;
          }
          else {
            if (result == MRESULT_DEAD) {
              kill(state.ms.pos);
              alive[i] = false;
            }

            if (result == MRESULT_DEAD && i == 0)
              return 0;

            if (i == 0)
              body_size = state.ms.body.size();

            players[i].pos = state.ms.body[0];
            players[i].debt = state.ms.debt;

            count--;
            mstates[i] = {};
          }
        }
      }
    }

    {
      int i = 0;
      int removed = 0;

      players.erase(std::remove_if(players.begin(),
                                   players.end(),
                                   [&](const Player &pl) { return !alive[i++]; }),
                    players.end());
    }

    return body_size * 2 - players[0].debt;
  }

  int move_io(Move move)
  {
    printf("%c", move.fast ? move.dir.to_cmd_fast() : move.dir.to_cmd());

    Grid<Cell> next_state(width(), height());
    if (!next_state.copy_io())
      return 0;

    std::vector<bool> alive;
    alive.resize(players.size(), true);

    int err = 0;

    for (auto [it, i] = std::pair{players.begin(), (int)0}; it != players.end(); it++, i++) {
      auto player = *it;

      if (next_state[player.pos].is_safe()) {
        alive[i] = false;
        continue;
      }

      MoveState mstate(*this);
      mstate.pos = player.pos;
      mstate.debt = player.debt;
      mstate.body = snake_body(player.pos);
      mstate.dir = next_state[player.pos].walkdir(mstate.body[0] - mstate.body[1]);

      Pos nextpos = player.pos;

      while (next_state[nextpos].is_connected(mstate.dir)) {
        nextpos = nextpos + mstate.dir;
      }

      Game snap = *this;

      do {
        do {
          if ((err = move_single(mstate, MSTAGE_FUME) != MRESULT_CONT))
            break;
          if ((err = move_single(mstate, MSTAGE_MOVE) != MRESULT_CONT))
            break;
        } while (0);

        if (mstate.body[0] == nextpos && !err) {
          break;
        }

        err = 0;

        Dir dir = mstate.dir;
        mstate = {*this};
        mstate.pos = player.pos;
        mstate.debt = player.debt + 1;
        mstate.body = snake_body(player.pos);
        mstate.dir = dir;

        Grid<bool> is_snake(width(), height());

        for (Pos pos : mstate.body) {
          is_snake[pos] = true;
        }

        for (auto [pos, cell] : *this) {
          if (!(cell.is_safe() || is_snake[pos]))
            (*this)[pos] = Cell::space();
        }

        do {
          init_snake_density();

          if ((err = move_single(mstate, MSTAGE_FUME) != MRESULT_CONT))
            break;
          if ((err = move_single(mstate, MSTAGE_FAPL) != MRESULT_CONT))
            break;
          if ((err = move_single(mstate, MSTAGE_MOVE) != MRESULT_CONT))
            break;
          if ((err = move_single(mstate, MSTAGE_FIN) == MRESULT_DEAD))
            break;

          mstate.pos = mstate.body[0];

          if ((err = move_single(mstate, MSTAGE_FUME) != MRESULT_CONT))
            break;
          if ((err = move_single(mstate, MSTAGE_MOVE) != MRESULT_CONT))
            break;
        } while (0);

        if (err) {
          break;
        }

        if ((err = mstate.body[0] != nextpos))
          break;
      } while (0);

      if (err) {
        std::string info;
        Grid<Cell> gsnake(width(), height());

        for (auto pos : mstate.body) {
          gsnake[pos] = mstate.tmp[pos] ? mstate.tmp[pos] : (*this)[pos];
        }

        for (int y = 0; y < height(); y++) {
          for (int x = 0; x < width(); x++) {
            char buf[16];

            sprintf(buf, "%lc", (char16_t)(gsnake[Pos(x, y)] ? gsnake[Pos(x, y)] : Cell::space()));

            info += buf;
          }

          info += "\n";
        }

        fprintf(stderr,
                "worm.cpp: STATE DESYNC at snake (%i,%i) -> (%i,%i) {\n%s}\n",
                player.pos.x,
                player.pos.y,
                nextpos.x,
                nextpos.y,
                info.data());
        copy_from(next_state);
        validate();
        init_state();

        return 1;
      }

      *this = snap;

      players[i].debt = mstate.debt;
      players[i].pos = norm(nextpos);
    }

    {
      int i = 0;

      players.erase(std::remove_if(players.begin(),
                                   players.end(),
                                   [&](const Player &pl) { return !alive[i++]; }),
                    players.end());
    }

    copy_from(next_state);
    validate();

    return alive[0] ? 1 : 0;
  }
};

int main()
{
  setlocale(LC_ALL, "");
  char buf[65536];
  Game game = Game::from_io();
  while (1) {
    struct MoveInfo {
      Move move;
      double score;
      double total;
    };

    std::vector<MoveInfo> moves;

    for (int i = 0; i < 8; i++) {
      MoveInfo move;
      move.move.dir = {i % 4};
      move.move.fast = i / 4;
      move.score = 0;
      move.total = 0;

      moves.push_back(move);
    }

    Game vgame = game;
    Game vgame2 = game;

    Pos pos = game.player_pos();
    Pos apple = game.closest_apple(pos);
    int cur_length = game.player_length();

    std::bitset<4> adirs{0};
    game.closest_dirs(pos, apple, [&](Dir dir) { adirs[dir.i] = true; });

    for (auto &move : moves) {
      int max_fullh = 5;
      int max_onlyf = 25;

      for (int i = 0; i < max_onlyf; i++) {
        vgame = game;

        int mcount = 0;
        int max_mc = 5;
        double exp = 64.0;
        double extent;
        int res;

        {
          std::vector<Move> vmoves;

          vmoves.push_back(move.move);

          for (int pi = 0; pi < vgame.player_count() - 1; pi++)
            vmoves.push_back({rand() % 4, (bool)(rand() % 2)});

          res = vgame.move(vmoves);

          if (!(res)) {
            move.score = 0;
            move.total = 1;
            break;
          }
        }

        for (int mi = 0; i < max_fullh && mi < max_mc && res; mi++) {
          std::vector<Move> vmoves;

          vgame.rand_moves(vmoves);

          int nres;

          for (int mj = 0; mj < 8; mj++,
                   vmoves[0].dir = vmoves[0].dir + 1,
                   vmoves[0].fast = vmoves[0].dir.i ? vmoves[0].fast : !vmoves[0].fast)
          {
            vgame2 = vgame;

            nres = vgame2.move(vmoves);

            if (nres) {
              vgame = vgame2;
              break;
            }
          }

          mcount++;
          res = nres;
        }

        extent = vgame.avail_space();

        move.score += (move.move.fast ? std::min(cur_length, res) : res) * extent;
        move.total += 1.0;
      }

      if (adirs[move.move.dir.i])
        move.total *= 0.8;
    }

    auto move = *std::max_element(moves.begin(), moves.end(), [&](const auto &a, const auto &b) {
      return a.score / a.total < b.score / b.total;
    });

    if (!game.move_io(move.move)) {
      fprintf(stderr, "i am dead.\n");
      return 0;
    }
  }
}

entry #3

written by essaie
submitted at
1 like

guesses
comments 2
essaie

\Did you know? The humble PYTHON is a kind of snake!


*Ada

i would hope a python is a snake wtf do u mean


post a comment


tfbm.py ASCII text
 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
# i'm Throughly Fascinated By sMakes

## specification
pass

## overview
pass

## the beginning
print('kimapr\n',end='')

width = int(input())
height = int(input())

## game loop
### the board
while 1:
  board = [input() for _ in range(height)]
  games = [[board[r][c] for r in range(height)] for c in range(width)]
  input()
  
  head = [(y, r.index('0')) for y, r in enumerate(board) if '0' in r][0]
  
  goalS = ([i for i in range(height-head[0]) if '$' in board[head[0]+i]]+[4])[0]
  goalW = ([j for j in range(head[1]+1) if '$' in games[head[1]-j]]+[4])[0]
  goalA = ([j for j in range(width-head[1]) if '$' in games[head[1]+j]]+[4])[0]
  goalN = ([i for i in range(head[0]+1) if '$' in board[head[0]-i]]+[4])[0]
  
  goals = [goalA or 5, goalS or 6, goalW or 7, goalN or 8]
### your action
  goal = [114,100,108,117][goals.index(min(goals))]
  
  danger = ''.join((row[-2:]+row+row[:2])[head[1]-2:head[1]+3]
  for row in (board[-2:]+board+board[:2])[head[0]-2:head[0]+3])

## the challenge
  print(chr(goal - 32 * ('O' in danger)),end='')
#0

entry #4

written by oleander
submitted at
0 likes

guesses
comments 1
essaie

\Did you know? The humble PYTHON is a kind of snake!


post a comment


snake.py Unicode text, UTF-8 text
 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
print('utf8')
width=int(input())
height=int(input())

def grab(coord):
    return board[coord[1]*width+coord[0]]
def find(value):
    return (value%width,value//width)
def grabbox(coord):
    x,y=coord
    return ({i[2]:i[1] for i in [(x>0, (x-1,y), 'l'), (x<width-1, (x+1,y), 'r'), (y>0, (x,y-1), 'u'), (y<height-1, (x,y+1), 'd')] if i[0]})

rev = {'l':'r','r':'l','u':'d','d':'u'}
side = {'┃':'rl','━':'ud','.':'rlud'}

while 1:
    board = ''
    for line in range(0, height):
        board+=input()
    head = grabbox(find(board.index('0')))

    go=''
    for check in 'rdlu':
        if check in head:
            if grab(head[check]) in side:
                if check in side[grab(head[check])]:
                    go=check
    print(go)
    

entry #5

written by Indigo
submitted at
0 likes

guesses
comments 1
essaie

\Did you know? The SEA snake is not really a snake!


post a comment


noodle.c ASCII text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
  printf("ascii\n");
  fflush(stdout);
  size_t w, h;
  int r;
  r = scanf(" %zu %zu", &w, &h);
  assert(r == 2);
  char *g = malloc(w * h);
  while (true) {
    for (char *p = g; p < g + w * h; ++p) {
      r = scanf(" %c", p);
      assert(r == 1);
    }
    printf("%c\n", "rdlu"[rand() % 4]);
  }
  free(g);
}

entry #6

written by pyrotelekinetic
submitted at
0 likes

guesses
comments 2
jan Tunke

💀


essaie

That's called a "snale", silly! Real snakes have scells!


post a comment


snek ASCII text
1
2
3
4
5
6
7
#!/usr/bin/env sh

echo ascii

while true; do
  echo r
done;