summary refs log blame commit diff stats
path: root/2020/day-04/README.org
blob: 5456971fc642f23861a1bc05909c9b54abeedffc (plain) (tree)


















































































































































































































                                                                                
#+SETUPFILE: ~/.emacs.d/org-templates/level-3.org
#+HTML_LINK_UP: ../../index.html#2020
#+OPTIONS: toc:1
#+EXPORT_FILE_NAME: index
#+TITLE: Day 04 - Passport Processing

* Puzzle
- This puzzle is taken from: https://adventofcode.com/2020/day/4

You arrive at the airport only to realize that you grabbed your North
Pole Credentials instead of your passport. While these documents are
extremely similar, North Pole Credentials aren't issued by a country and
therefore aren't actually valid documentation for travel in most of the
world.

It seems like you're not the only one having problems, though; a very
long line has formed for the automatic passport scanners, and the delay
could upset your travel itinerary.

Due to some questionable network security, you realize you might be able
to solve both of these problems at the same time.

The automatic passport scanners are slow because they're having trouble
detecting which passports have all required fields. The expected fields
are as follows:

- =byr= (Birth Year)
- =iyr= (Issue Year)
- =eyr= (Expiration Year)
- =hgt= (Height)
- =hcl= (Hair Color)
- =ecl= (Eye Color)
- =pid= (Passport ID)
- =cid= (Country ID)

Passport data is validated in batch files (your puzzle input). Each
passport is represented as a sequence of key:value pairs separated by
spaces or newlines. Passports are separated by blank lines.

Here is an example batch file containing four passports:
#+BEGIN_SRC
ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm

iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929

hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm

hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in
#+END_SRC

The first passport is valid - all eight fields are present. The second
passport is invalid - it is missing hgt (the Height field).

The third passport is interesting; the only missing field is cid, so it
looks like data from North Pole Credentials, not a passport at all!
Surely, nobody would mind if you made the system temporarily ignore
missing cid fields. Treat this "passport" as valid.

The fourth passport is missing two fields, cid and byr. Missing cid is
fine, but missing any other field is not, so this passport is invalid.

According to the above rules, your improved system would report 2 valid
passports.

Count the number of valid passports - those that have all required
fields. Treat cid as optional. In your batch file, how many passports
are valid?
** Part 2
The line is moving more quickly now, but you overhear airport security
talking about how passports with invalid data are getting through.
Better add some data validation, quick!

You can continue to ignore the cid field, but each other field has
strict rules about what values are valid for automatic validation:

- =byr= (Birth Year) - four digits; at least 1920 and at most 2002.
- =iyr= (Issue Year) - four digits; at least 2010 and at most 2020.
- =eyr= (Expiration Year) - four digits; at least 2020 and at most 2030.
- =hgt= (Height) - a number followed by either =cm= or =in=:
  If =cm=, the number must be at least =150= and at most =193=.
  If =in=, the number must be at least =59= and at most =76=.
- =hcl= (Hair Color) - a =#= followed by exactly six characters =0-9= or =a-f=.
- =ecl= (Eye Color) - exactly one of: =amb= =blu= =brn= =gry= =grn= =hzl= =oth=.
- =pid= (Passport ID) - a nine-digit number, including leading zeroes.
- =cid= (Country ID) - ignored, missing or not.

Your job is to count the passports where all required fields are both
present and valid according to the above rules. Here are some example
values:
#+BEGIN_SRC
byr valid:   2002
byr invalid: 2003

hgt valid:   60in
hgt valid:   190cm
hgt invalid: 190in
hgt invalid: 190

hcl valid:   #123abc
hcl invalid: #123abz
hcl invalid: 123abc

ecl valid:   brn
ecl invalid: wat

pid valid:   000000001
pid invalid: 0123456789
#+END_SRC

Here are some invalid passports:
#+BEGIN_SRC
eyr:1972 cid:100
hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926

iyr:2019
hcl:#602927 eyr:1967 hgt:170cm
ecl:grn pid:012533040 byr:1946

hcl:dab227 iyr:2012
ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277

hgt:59cm ecl:zzz
eyr:2038 hcl:74454a iyr:2023
pid:3556412378 byr:2007
#+END_SRC

Here are some valid passports:
#+BEGIN_SRC
pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980
hcl:#623a2f

eyr:2029 ecl:blu cid:129 byr:1989
iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm

hcl:#888785
hgt:164cm byr:2001 iyr:2015 cid:88
pid:545766238 ecl:hzl
eyr:2022

iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719
#+END_SRC

Count the number of valid passports - those that have all required
fields and valid values. Continue to treat cid as optional. In your
batch file, how many passports are valid?
* Solution
The input file is split by =\n\n= & stored in =@passports=. We iterate over
=@passports= & seperate each field in =%field=. Then we check if all
required fields are present, if not then skip the passport. If true then
increment =$valid_passports= which holds the number of valid passports.

#+BEGIN_SRC raku
my $input = slurp "input";
my @passports = $input.split("\n\n");
my $valid_passports = 0;

MAIN: for @passports -> $passport {
    my %fields;

    for $passport.words -> $field {
        my ($key, $value) = $field.split(":");
        %fields{$key} = $value;
    }

    # Check for fields that are strictly required. `cid' can be
    # skipped. Skip this passport if it's not valid.
    for <byr iyr eyr hgt hcl ecl pid> -> $field {
        next MAIN unless %fields{$field};
    }

    # Do validation in part 2.
    if $part == 2 {
        ...
    }
    $valid_passports++;
}
say "Part $part: " ~ $valid_passports;
#+END_SRC
** Part 2
For part 2, more validation is performed. The rules are simple &
well-defined in Puzzle section.

I had forgotten the =default= block in =given/when= & that meant it was
counting passports which don't have =hgt= set to either =cm= or =in= as valid.
In my case it was a single passport.

#+BEGIN_SRC raku
next MAIN unless (
    (1920 ≤ %fields{<byr>} ≤ 2002)
    and (2010 ≤ %fields{<iyr>} ≤ 2020)
    and (2020 ≤ %fields{<eyr>} ≤ 2030)
    and (<amb blu brn gry grn hzl oth> ∋ %fields{<ecl>})
    and (%fields{<pid>} ~~ /^\d ** 9$/)
    and (%fields{<hcl>} ~~ /^'#' <[\d a..f]> ** 6/)
);
given substr(%fields{<hgt>}, *-2) {
    when 'cm' {
        next MAIN unless 150 ≤ substr(%fields{<hgt>}, 0, *-2) ≤ 193;
    }
    when 'in' {
        next MAIN unless 59 ≤ substr(%fields{<hgt>}, 0, *-2) ≤ 76;
    }
    default { next MAIN; }
}
#+END_SRC
604' href='#n604'>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
## running code from the editor and creating sandboxes
#
# Running code in the sandbox editor prepends its contents to a list of
# (non-editable) sandboxes below the editor, showing the result and maybe a
# few other things (later layers).
#
# This layer draws the menubar buttons in non-editable sandboxes but they
# don't do anything yet. Later layers implement each button.

def! main [
  local-scope
  open-console
  clear-screen 0/screen  # non-scrolling app
  env:&:environment <- new-programming-environment 0/filesystem, 0/screen
  env <- restore-sandboxes env, 0/filesystem
  render-all 0/screen, env, render
  event-loop 0/screen, 0/console, env, 0/filesystem
]

container environment [
  sandbox:&:sandbox  # list of sandboxes, from top to bottom. TODO: switch to &:list:sandbox
  render-from:num
  number-of-sandboxes:num
]

after <programming-environment-initialization> [
  *result <- put *result, render-from:offset, -1
]

container sandbox [
  data:text
  response:text
  # coordinates to track clicks
  # constraint: will be 0 for sandboxes at positions before env.render-from
  starting-row-on-screen:num
  code-ending-row-on-screen:num  # past end of code
  screen:&:screen  # prints in the sandbox go here
  next-sandbox:&:sandbox
]

scenario run-and-show-results [
  local-scope
  trace-until 100/app  # trace too long
  assume-screen 100/width, 15/height
  # recipe editor is empty
  assume-resources [
  ]
  # sandbox editor contains an instruction without storing outputs
  env:&:environment <- new-programming-environment resources, screen, [divide-with-remainder 11, 3]
  render-all screen, env, render
  # run the code in the editors
  assume-console [
    press F4
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # check that screen prints the results
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  divide-with-remainder 11, 3                      .
    .                                                  3                                                .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
  screen-should-contain-in-color 7/white, [
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                   divide-with-remainder 11, 3                      .
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
  ]
  screen-should-contain-in-color 245/grey, [
    .                                                                                                    .
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                                                                   .
    .                                                                                                   .
    .                                                  3                                                .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
  # sandbox menu in reverse video
  screen-should-contain-in-color 232/black, [
    .                                                                                                    .
    .                                                                                                    .
    .                                                                                                    .
    .                                                   0   edit       copy       to recipe    delete    .
  ]
  # run another command
  assume-console [
    left-click 1, 80
    type [add 2, 2]
    press F4
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # check that screen prints both sandboxes
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  4                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                  1   edit       copy       to recipe    delete    .
    .                                                  divide-with-remainder 11, 3                      .
    .                                                  3                                                .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
]

after <global-keypress> [
  # F4? load all code and run all sandboxes.
  {
    do-run?:bool <- equal k, 65532/F4
    break-unless do-run?
    screen <- update-status screen, [running...       ], 245/grey
    <begin-run-sandboxes-on-F4>
    error?:bool <- run-sandboxes env, resources, screen
    # we could just render-all, but we do some work to minimize the number of prints to screen
    <end-run-sandboxes-on-F4>
    screen <- render-sandbox-side screen, env, render
    {
      break-if error?
      screen <- update-status screen, [                 ], 245/grey
    }
    screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
    loop +next-event
  }
]

def run-sandboxes env:&:environment, resources:&:resources, screen:&:screen -> errors-found?:bool, env:&:environment, resources:&:resources, screen:&:screen [
  local-scope
  load-ingredients
  errors-found?:bool <- update-recipes env, resources, screen
  jump-if errors-found?, +return
  # check contents of right editor (sandbox)
  <begin-run-sandboxes>
  current-sandbox:&:editor <- get *env, current-sandbox:offset
  {
    sandbox-contents:text <- editor-contents current-sandbox
    break-unless sandbox-contents
    # if contents exist, first save them
    # run them and turn them into a new sandbox
    new-sandbox:&:sandbox <- new sandbox:type
    *new-sandbox <- put *new-sandbox, data:offset, sandbox-contents
    # push to head of sandbox list
    dest:&:sandbox <- get *env, sandbox:offset
    *new-sandbox <- put *new-sandbox, next-sandbox:offset, dest
    *env <- put *env, sandbox:offset, new-sandbox
    # update sandbox count
    sandbox-count:num <- get *env, number-of-sandboxes:offset
    sandbox-count <- add sandbox-count, 1
    *env <- put *env, number-of-sandboxes:offset, sandbox-count
    # save all sandboxes
    # needs to be before running them, in case we die when running
    save-sandboxes env, resources
    # clear sandbox editor
    init:&:duplex-list:char <- push 167/§, 0/tail
    *current-sandbox <- put *current-sandbox, data:offset, init
    *current-sandbox <- put *current-sandbox, top-of-screen:offset, init
  }
  # run all sandboxes
  curr:&:sandbox <- get *env, sandbox:offset
  idx:num <- copy 0
  {
    break-unless curr
    curr <- update-sandbox curr, env, idx
    curr <- get *curr, next-sandbox:offset
    idx <- add idx, 1
    loop
  }
  <end-run-sandboxes>
  +return
  {
    break-if resources  # ignore this in tests
    $system [./snapshot_lesson]
  }
]

# load code from disk
# replaced in a later layer (whereupon errors-found? will actually be set)
def update-recipes env:&:environment, resources:&:resources, screen:&:screen -> errors-found?:bool, env:&:environment, resources:&:resources, screen:&:screen [
  local-scope
  load-ingredients
  recipes:&:editor <- get *env, recipes:offset
  in:text <- editor-contents recipes
  resources <- dump resources, [lesson/recipes.mu], in
  reload in
  errors-found? <- copy 0/false
]

# replaced in a later layer
def update-sandbox sandbox:&:sandbox, env:&:environment, idx:num -> sandbox:&:sandbox, env:&:environment [
  local-scope
  load-ingredients
  data:text <- get *sandbox, data:offset
  response:text, _, fake-screen:&:screen <- run-sandboxed data
  *sandbox <- put *sandbox, response:offset, response
  *sandbox <- put *sandbox, screen:offset, fake-screen
]

def update-status screen:&:screen, msg:text, color:num -> screen:&:screen [
  local-scope
  load-ingredients
  screen <- move-cursor screen, 0, 2
  screen <- print screen, msg, color, 238/grey/background
]

def save-sandboxes env:&:environment, resources:&:resources -> resources:&:resources [
  local-scope
  load-ingredients
  trace 11, [app], [save sandboxes]
  current-sandbox:&:editor <- get *env, current-sandbox:offset
  # first clear previous versions, in case we deleted some sandbox
  $system [rm lesson/[0-9]* >/dev/null 2>/dev/null]  # some shells can't handle '>&'
  curr:&:sandbox <- get *env, sandbox:offset
  idx:num <- copy 0
  {
    break-unless curr
    resources <- save-sandbox resources, curr, idx
    idx <- add idx, 1
    curr <- get *curr, next-sandbox:offset
    loop
  }
]

def save-sandbox resources:&:resources, sandbox:&:sandbox, sandbox-index:num -> resources:&:resources [
  local-scope
  load-ingredients
  data:text <- get *sandbox, data:offset
  filename:text <- append [lesson/], sandbox-index
  resources <- dump resources, filename, data
  <end-save-sandbox>
]

def! render-sandbox-side screen:&:screen, env:&:environment, render-editor:render-recipe -> screen:&:screen, env:&:environment [
  local-scope
  load-ingredients
  trace 11, [app], [render sandbox side]
  old-top-idx:num <- save-top-idx screen
  current-sandbox:&:editor <- get *env, current-sandbox:offset
  row:num, column:num <- copy 1, 0
  left:num <- get *current-sandbox, left:offset
  right:num <- get *current-sandbox, right:offset
  # render sandbox editor
  render-from:num <- get *env, render-from:offset
  {
    render-current-sandbox?:bool <- equal render-from, -1
    break-unless render-current-sandbox?
    row, column, screen, current-sandbox <- call render-editor, screen, current-sandbox
    clear-screen-from screen, row, column, left, right
    row <- add row, 1
  }
  # render sandboxes
  draw-horizontal screen, row, left, right
  sandbox:&:sandbox <- get *env, sandbox:offset
  row, screen <- render-sandboxes screen, sandbox, left, right, row, render-from
  clear-rest-of-screen screen, row, left, right
  #
  assert-no-scroll screen, old-top-idx
]

def render-sandboxes screen:&:screen, sandbox:&:sandbox, left:num, right:num, row:num, render-from:num, idx:num -> row:num, screen:&:screen, sandbox:&:sandbox [
  local-scope
  load-ingredients
  return-unless sandbox
  screen-height:num <- screen-height screen
  hidden?:bool <- lesser-than idx, render-from
  {
    break-if hidden?
    # render sandbox menu
    row <- add row, 1
    at-bottom?:bool <- greater-or-equal row, screen-height
    return-if at-bottom?
    screen <- move-cursor screen, row, left
    screen <- render-sandbox-menu screen, idx, left, right
    # save menu row so we can detect clicks to it later
    *sandbox <- put *sandbox, starting-row-on-screen:offset, row
    # render sandbox contents
    row <- add row, 1
    screen <- move-cursor screen, row, left
    sandbox-data:text <- get *sandbox, data:offset
    row, screen <- render-code screen, sandbox-data, left, right, row
    *sandbox <- put *sandbox, code-ending-row-on-screen:offset, row
    # render sandbox warnings, screen or response, in that order
    sandbox-response:text <- get *sandbox, response:offset
    <render-sandbox-results>
    {
      sandbox-screen:&:screen <- get *sandbox, screen:offset
      empty-screen?:bool <- fake-screen-is-empty? sandbox-screen
      break-if empty-screen?
      row, screen <- render-screen screen, sandbox-screen, left, right, row
    }
    {
      break-unless empty-screen?
      <render-sandbox-response>
      row, screen <- render-text screen, sandbox-response, left, right, 245/grey, row
    }
    +render-sandbox-end
    at-bottom?:bool <- greater-or-equal row, screen-height
    return-if at-bottom?
    # draw solid line after sandbox
    draw-horizontal screen, row, left, right
  }
  # if hidden, reset row attributes
  {
    break-unless hidden?
    *sandbox <- put *sandbox, starting-row-on-screen:offset, 0
    *sandbox <- put *sandbox, code-ending-row-on-screen:offset, 0
    <end-render-sandbox-reset-hidden>
  }
  # draw next sandbox
  next-sandbox:&:sandbox <- get *sandbox, next-sandbox:offset
  next-idx:num <- add idx, 1
  row, screen <- render-sandboxes screen, next-sandbox, left, right, row, render-from, next-idx
]

def render-sandbox-menu screen:&:screen, sandbox-index:num, left:num, right:num -> screen:&:screen [
  local-scope
  load-ingredients
  move-cursor-to-column screen, left
  edit-button-left:num, edit-button-right:num, copy-button-left:num, copy-button-right:num, recipe-button-left:num, recipe-button-right:num, delete-button-left:num <- sandbox-menu-columns left, right
  print screen, sandbox-index, 232/dark-grey, 245/grey
  start-buttons:num <- subtract edit-button-left, 1
  clear-line-until screen, start-buttons, 245/grey
  print screen, [edit], 232/black, 25/background-blue
  clear-line-until screen, edit-button-right, 25/background-blue
  print screen, [copy], 232/black, 58/background-green
  clear-line-until screen, copy-button-right, 58/background-green
  print screen, [to recipe], 232/black, 94/background-orange
  clear-line-until screen, recipe-button-right, 94/background-orange
  print screen, [delete], 232/black, 52/background-red
  clear-line-until screen, right, 52/background-red
]

scenario skip-rendering-sandbox-menu-past-bottom-row [
  trace-until 100/app  # trace too long
  assume-screen 100/width, 6/height
  # recipe editor is empty
  assume-resources [
    [lesson/0] <- [|add 2, 2|]
    [lesson/1] <- [|add 1, 1|]
  ]
  # create two sandboxes such that the top one just barely fills the screen
  env:&:environment <- new-programming-environment resources, screen, []
  env <- restore-sandboxes env, resources
  run [
    render-all screen, env, render
  ]
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  ┊─────────────────────────────────────────────────.
  ]
]

# divide up the menu bar for a sandbox into 3 segments, for edit/copy/delete buttons
# delete-button-right == right
# all left/right pairs are inclusive
def sandbox-menu-columns left:num, right:num -> edit-button-left:num, edit-button-right:num, copy-button-left:num, copy-button-right:num, recipe-button-left:num, recipe-button-right:num, delete-button-left:num [
  local-scope
  load-ingredients
  start-buttons:num <- add left, 4/space-for-sandbox-index
  buttons-space:num <- subtract right, start-buttons
  button-width:num <- divide-with-remainder buttons-space, 4  # integer division
  buttons-wide-enough?:bool <- greater-or-equal button-width, 10
  assert buttons-wide-enough?, [sandbox must be at least 40 or so characters wide]
  edit-button-left:num <- copy start-buttons
  copy-button-left:num <- add start-buttons, button-width
  edit-button-right:num <- subtract copy-button-left, 1
  recipe-button-left:num <- add copy-button-left, button-width
  copy-button-right:num <- subtract recipe-button-left, 1
  delete-button-left:num <- subtract right, button-width, -2  # because 'to recipe' is wider than 'delete'
  recipe-button-right:num <- subtract delete-button-left, 1
]

# print a text 's' to 'editor' in 'color' starting at 'row'
# clear rest of last line, move cursor to next line
# like 'render-code' but without syntax-based colorization
def render-text screen:&:screen, s:text, left:num, right:num, color:num, row:num -> row:num, screen:&:screen [
  local-scope
  load-ingredients
  return-unless s
  column:num <- copy left
  screen <- move-cursor screen, row, column
  screen-height:num <- screen-height screen
  i:num <- copy 0
  len:num <- length *s
  {
    +next-character
    done?:bool <- greater-or-equal i, len
    break-if done?
    done? <- greater-or-equal row, screen-height
    break-if done?
    c:char <- index *s, i
    {
      # newline? move to left rather than 0
      newline?:bool <- equal c, 10/newline
      break-unless newline?
      # clear rest of line in this window
      {
        done?:bool <- greater-than column, right
        break-if done?
        space:char <- copy 32/space
        print screen, space
        column <- add column, 1
        loop
      }
      row <- add row, 1
      column <- copy left
      screen <- move-cursor screen, row, column
      i <- add i, 1
      loop +next-character
    }
    {
      # at right? wrap.
      at-right?:bool <- equal column, right
      break-unless at-right?
      # print wrap icon
      wrap-icon:char <- copy 8617/loop-back-to-left
      print screen, wrap-icon, 245/grey
      column <- copy left
      row <- add row, 1
      screen <- move-cursor screen, row, column
      # don't increment i
      loop +next-character
    }
    i <- add i, 1
    print screen, c, color
    column <- add column, 1
    loop
  }
  was-at-left?:bool <- equal column, left
  clear-line-until screen, right
  {
    break-if was-at-left?
    row <- add row, 1
  }
  move-cursor screen, row, left
]

scenario render-text-wraps-barely-long-lines [
  local-scope
  assume-screen 5/width, 5/height
  run [
    render-text screen, [abcde], 0/left, 4/right, 7/white, 1/row
  ]
  screen-should-contain [
    .     .
    .abcd.
    .e    .
    .     .
  ]
]

# assumes programming environment has no sandboxes; restores them from previous session
def restore-sandboxes env:&:environment, resources:&:resources -> env:&:environment [
  local-scope
  load-ingredients
  # read all scenarios, pushing them to end of a list of scenarios
  idx:num <- copy 0
  curr:&:sandbox <- copy 0
  prev:&:sandbox <- copy 0
  {
    filename:text <- append [lesson/], idx
    contents:text <- slurp resources, filename
    break-unless contents  # stop at first error; assuming file didn't exist
                           # todo: handle empty sandbox
    # create new sandbox for file
    curr <- new sandbox:type
    *curr <- put *curr, data:offset, contents
    <end-restore-sandbox>
    {
      break-if idx
      *env <- put *env, sandbox:offset, curr
    }
    {
      break-unless idx
      *prev <- put *prev, next-sandbox:offset, curr
    }
    idx <- add idx, 1
    prev <- copy curr
    loop
  }
  # update sandbox count
  *env <- put *env, number-of-sandboxes:offset, idx
]

# print the fake sandbox screen to 'screen' with appropriate delimiters
# leave cursor at start of next line
def render-screen screen:&:screen, sandbox-screen:&:screen, left:num, right:num, row:num -> row:num, screen:&:screen [
  local-scope
  load-ingredients
  return-unless sandbox-screen
  # print 'screen:'
  row <- render-text screen, [screen:], left, right, 245/grey, row
  screen <- move-cursor screen, row, left
  # start printing sandbox-screen
  column:num <- copy left
  s-width:num <- screen-width sandbox-screen
  s-height:num <- screen-height sandbox-screen
  buf:&:@:screen-cell <- get *sandbox-screen, data:offset
  stop-printing:num <- add left, s-width, 3
  max-column:num <- min stop-printing, right
  i:num <- copy 0
  len:num <- length *buf
  screen-height:num <- screen-height screen
  {
    done?:bool <- greater-or-equal i, len
    break-if done?
    done? <- greater-or-equal row, screen-height
    break-if done?
    column <- copy left
    screen <- move-cursor screen, row, column
    # initial leader for each row: two spaces and a '.'
    space:char <- copy 32/space
    print screen, space, 245/grey
    print screen, space, 245/grey
    full-stop:char <- copy 46/period
    print screen, full-stop, 245/grey
    column <- add left, 3
    {
      # print row
      row-done?:bool <- greater-or-equal column, max-column
      break-if row-done?
      curr:screen-cell <- index *buf, i
      c:char <- get curr, contents:offset
      color:num <- get curr, color:offset
      {
        # damp whites down to grey
        white?:bool <- equal color, 7/white
        break-unless white?
        color <- copy 245/grey
      }
      print screen, c, color
      column <- add column, 1
      i <- add i, 1
      loop
    }
    # print final '.'
    print screen, full-stop, 245/grey
    column <- add column, 1
    {
      # clear rest of current line
      line-done?:bool <- greater-than column, right
      break-if line-done?
      print screen, space
      column <- add column, 1
      loop
    }
    row <- add row, 1
    loop
  }
]

scenario run-updates-results [
  local-scope
  trace-until 100/app  # trace too long
  assume-screen 100/width, 12/height
  # define a recipe (no indent for the 'add' line below so column numbers are more obvious)
  assume-resources [
    [lesson/recipes.mu] <- [
      ||
      |recipe foo [|
      |  local-scope|
      |  z:num <- add 2, 2|
      |  reply z|
      |]|
    ]
  ]
  # sandbox editor contains an instruction without storing outputs
  env:&:environment <- new-programming-environment resources, screen, [foo]  # contents of sandbox editor
  render-all screen, env, render
  $clear-trace
  # run the code in the editors
  assume-console [
    press F4
  ]
  event-loop screen, console, env, resources
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .recipe foo [                                      ┊─────────────────────────────────────────────────.
    .  local-scope                                     0   edit       copy       to recipe    delete    .
    .  z:num <- add 2, 2                               foo                                              .
    .  reply z                                         4                                                .
    .]                                                 ┊─────────────────────────────────────────────────.
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊                                                 .
    .                                                                                                   .
  ]
  # the new sandbox should be saved to disk
  trace-should-contain [
    app: save sandboxes
  ]
  # no need to update editor
  trace-should-not-contain [
    app: render recipes
  ]
  # make a change (incrementing one of the args to 'add'), then rerun
  $clear-trace
  assume-console [
    left-click 4, 28  # one past the value of the second arg
    press backspace
    type [3]
    press F4
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # check that screen updates the result on the right
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .recipe foo [                                      ┊─────────────────────────────────────────────────.
    .  local-scope                                     0   edit       copy       to recipe    delete    .
    .  z:num <- add 2, 3                               foo                                              .
    .  reply z                                         5                                                .
    .]                                                 ┊─────────────────────────────────────────────────.
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊                                                 .
    .                                                                                                   .
  ]
  # no need to save sandboxes all over again
  trace-should-not-contain [
    app: save sandboxes
  ]
]

scenario run-instruction-manages-screen-per-sandbox [
  local-scope
  trace-until 100/app  # trace too long
  assume-screen 100/width, 20/height
  # empty recipes
  assume-resources [
  ]
  # sandbox editor contains an instruction
  env:&:environment <- new-programming-environment resources, screen, [print screen, 4]  # contents of sandbox editor
  render-all screen, env, render
  # run the code in the editor
  assume-console [
    press F4
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # check that it prints a little toy screen
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  print screen, 4                                  .
    .                                                  screen:                                          .
    .                                                    .4                             .               .
    .                                                    .                              .               .
    .                                                    .                              .               .
    .                                                    .                              .               .
    .                                                    .                              .               .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
]

def editor-contents editor:&:editor -> result:text [
  local-scope
  load-ingredients
  buf:&:buffer:char <- new-buffer 80
  curr:&:duplex-list:char <- get *editor, data:offset
  # skip § sentinel
  assert curr, [editor without data is illegal; must have at least a sentinel]
  curr <- next curr
  return-unless curr, 0
  {
    break-unless curr
    c:char <- get *curr, value:offset
    buf <- append buf, c
    curr <- next curr
    loop
  }
  result <- buffer-to-array buf
]

scenario editor-provides-edited-contents [
  local-scope
  assume-screen 10/width, 5/height
  e:&:editor <- new-editor [abc], 0/left, 10/right
  assume-console [
    left-click 1, 2
    type [def]
  ]
  run [
    editor-event-loop screen, console, e
    s:text <- editor-contents e
    1:@:char/raw <- copy *s
  ]
  memory-should-contain [
    1:array:character <- [abdefc]
  ]
]

# keep the bottom of recipes from scrolling off the screen

scenario scrolling-down-past-bottom-of-recipe-editor [
  local-scope
  trace-until 100/app
  assume-screen 100/width, 10/height
  assume-resources [
  ]
  env:&:environment <- new-programming-environment resources, screen, []
  render-all screen, env, render
  assume-console [
    press enter
    press down-arrow
  ]
  event-loop screen, console, env, resources
  # no scroll
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .                                                  ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊                                                 .
    .                                                                                                   .
  ]
]

scenario cursor-down-in-recipe-editor [
  local-scope
  trace-until 100/app
  assume-screen 100/width, 10/height
  assume-resources [
  ]
  env:&:environment <- new-programming-environment resources, screen, []
  render-all screen, env, render
  assume-console [
    press enter
    press up-arrow
    press down-arrow  # while cursor isn't at bottom
  ]
  event-loop screen, console, env, resources
  cursor:char <- copy 9251/  print screen, cursor
  # cursor moves back to bottom
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .                                                 ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊                                                 .
    .                                                                                                   .
  ]
]

scenario scrolling-down-past-bottom-of-recipe-editor-2 [
  local-scope
  trace-until 100/app
  assume-screen 100/width, 10/height
  assume-resources [
  ]
  env:&:environment <- new-programming-environment resources, screen, []
  render-all screen, env, render
  assume-console [
    # add a line
    press enter
    # cursor back to top line
    press up-arrow
    # try to scroll
    press page-down  # or ctrl-f
  ]
  event-loop screen, console, env, resources
  # no scroll, and cursor remains at top line
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .                                                  ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊                                                 .
    .                                                                                                   .
  ]
]

scenario scrolling-down-past-bottom-of-recipe-editor-3 [
  local-scope
  trace-until 100/app
  assume-screen 100/width, 10/height
  assume-resources [
  ]
  env:&:environment <- new-programming-environment resources, screen, [ab
cd]
  render-all screen, env, render
  assume-console [
    # add a line
    press enter
    # switch to sandbox
    press ctrl-n
    # move cursor
    press down-arrow
  ]
  event-loop screen, console, env, resources
  cursor:char <- copy 9251/  print screen, cursor
  # no scroll on recipe side, cursor moves on sandbox side
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ab                                               .
    .                                                  ┊␣d                                               .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
]

# scrolling through sandboxes

scenario scrolling-down-past-bottom-of-sandbox-editor [
  local-scope
  trace-until 100/app  # trace too long
  assume-screen 100/width, 10/height
  # initialize
  assume-resources [
  ]
  env:&:environment <- new-programming-environment resources, screen, [add 2, 2]
  render-all screen, env, render
  assume-console [
    # create a sandbox
    press F4
  ]
  event-loop screen, console, env, resources
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
  ]
  # switch to sandbox window and hit 'page-down'
  assume-console [
    press ctrl-n
    press page-down
  ]
  run [
    event-loop screen, console, env, resources
    cursor:char <- copy 9251/    print screen, cursor
  ]
  # sandbox editor hidden; first sandbox displayed
  # cursor moves to first sandbox
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊␣   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  4                                                .
  ]
  # hit 'page-up'
  assume-console [
    press page-up
  ]
  run [
    event-loop screen, console, env, resources
    cursor:char <- copy 9251/    print screen, cursor
  ]
  # sandbox editor displays again, cursor is in editor
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊␣                                                .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
  ]
]

# page-down on sandbox side updates render-from to scroll sandboxes
after <global-keypress> [
  {
    break-unless sandbox-in-focus?
    page-down?:bool <- equal k, 65518/page-down
    break-unless page-down?
    sandbox:&:sandbox <- get *env, sandbox:offset
    break-unless sandbox
    # slide down if possible
    {
      render-from:num <- get *env, render-from:offset
      number-of-sandboxes:num <- get *env, number-of-sandboxes:offset
      max:num <- subtract number-of-sandboxes, 1
      at-end?:bool <- greater-or-equal render-from, max
      loop-if at-end?, +next-event  # render nothing
      render-from <- add render-from, 1
      *env <- put *env, render-from:offset, render-from
    }
    screen <- render-sandbox-side screen, env, render
    screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
    loop +next-event
  }
]

# update-cursor takes render-from into account
after <update-cursor-special-cases> [
  {
    break-unless sandbox-in-focus?
    render-from:num <- get *env, render-from:offset
    scrolling?:bool <- greater-or-equal render-from, 0
    break-unless scrolling?
    cursor-column:num <- get *current-sandbox, left:offset
    screen <- move-cursor screen, 2/row, cursor-column  # highlighted sandbox will always start at row 2
    return
  }
]

# 'page-up' on sandbox side is like 'page-down': updates render-from when necessary
after <global-keypress> [
  {
    break-unless sandbox-in-focus?
    page-up?:bool <- equal k, 65519/page-up
    break-unless page-up?
    render-from:num <- get *env, render-from:offset
    at-beginning?:bool <- equal render-from, -1
    break-if at-beginning?
    render-from <- subtract render-from, 1
    *env <- put *env, render-from:offset, render-from
    screen <- render-sandbox-side screen, env, render
    screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
    loop +next-event
  }
]

# sandbox belonging to 'env' whose next-sandbox is 'in'
# return 0 if there's no such sandbox, either because 'in' doesn't exist in 'env', or because it's the first sandbox
def previous-sandbox env:&:environment, in:&:sandbox -> out:&:sandbox [
  local-scope
  load-ingredients
  curr:&:sandbox <- get *env, sandbox:offset
  return-unless curr, 0/nil
  next:&:sandbox <- get *curr, next-sandbox:offset
  {
    return-unless next, 0/nil
    found?:bool <- equal next, in
    break-if found?
    curr <- copy next
    next <- get *curr, next-sandbox:offset
    loop
  }
  return curr
]

scenario scrolling-through-multiple-sandboxes [
  local-scope
  trace-until 100/app  # trace too long
  assume-screen 100/width, 10/height
  # initialize environment
  assume-resources [
  ]
  env:&:environment <- new-programming-environment resources, screen, []
  render-all screen, env, render
  # create 2 sandboxes
  assume-console [
    press ctrl-n
    type [add 2, 2]
    press F4
    type [add 1, 1]
    press F4
  ]
  event-loop screen, console, env, resources
  cursor:char <- copy 9251/  print screen, cursor
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊␣                                                .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  add 1, 1                                         .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                  1   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  4                                                .
  ]
  # hit 'page-down'
  assume-console [
    press page-down
  ]
  run [
    event-loop screen, console, env, resources
    cursor:char <- copy 9251/    print screen, cursor
  ]
  # sandbox editor hidden; first sandbox displayed
  # cursor moves to first sandbox
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊␣   edit       copy       to recipe    delete    .
    .                                                  add 1, 1                                         .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                  1   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  4                                                .
  ]
  # hit 'page-down' again
  assume-console [
    press page-down
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # just second sandbox displayed
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊1   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  4                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
  # hit 'page-down' again
  assume-console [
    press page-down
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # no change
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊1   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  4                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
  # hit 'page-up'
  assume-console [
    press page-up
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # back to displaying both sandboxes without editor
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊0   edit       copy       to recipe    delete    .
    .                                                  add 1, 1                                         .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                  1   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  4                                                .
  ]
  # hit 'page-up' again
  assume-console [
    press page-up
  ]
  run [
    event-loop screen, console, env, resources
    cursor:char <- copy 9251/    print screen, cursor
  ]
  # back to displaying both sandboxes as well as editor
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊␣                                                .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  add 1, 1                                         .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                  1   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  4                                                .
  ]
  # hit 'page-up' again
  assume-console [
    press page-up
  ]
  run [
    event-loop screen, console, env, resources
    cursor:char <- copy 9251/    print screen, cursor
  ]
  # no change
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊␣                                                .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  add 1, 1                                         .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                  1   edit       copy       to recipe    delete    .
    .                                                  add 2, 2                                         .
    .                                                  4                                                .
  ]
]

scenario scrolling-manages-sandbox-index-correctly [
  local-scope
  trace-until 100/app  # trace too long
  assume-screen 100/width, 10/height
  # initialize environment
  assume-resources [
  ]
  env:&:environment <- new-programming-environment resources, screen, []
  render-all screen, env, render
  # create a sandbox
  assume-console [
    press ctrl-n
    type [add 1, 1]
    press F4
  ]
  event-loop screen, console, env, resources
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  add 1, 1                                         .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
  # hit 'page-down' and 'page-up' a couple of times. sandbox index should be stable
  assume-console [
    press page-down
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # sandbox editor hidden; first sandbox displayed
  # cursor moves to first sandbox
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊0   edit       copy       to recipe    delete    .
    .                                                  add 1, 1                                         .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
  # hit 'page-up' again
  assume-console [
    press page-up
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # back to displaying both sandboxes as well as editor
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                                                                   .
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊─────────────────────────────────────────────────.
    .                                                  0   edit       copy       to recipe    delete    .
    .                                                  add 1, 1                                         .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
  # hit 'page-down'
  assume-console [
    press page-down
  ]
  run [
    event-loop screen, console, env, resources
  ]
  # sandbox editor hidden; first sandbox displayed
  # cursor moves to first sandbox
  screen-should-contain [
    .                                                                                 run (F4)           .
    .                                                  ┊─────────────────────────────────────────────────.
    .┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┊0   edit       copy       to recipe    delete    .
    .                                                  add 1, 1                                         .
    .                                                  2                                                .
    .                                                  ┊─────────────────────────────────────────────────.
    .                                                                                                   .
  ]
]