4
\$\begingroup\$

This question already has an answer here:

At work I've been acting as quality control on our public documents, to make sure that they are WCAG 2.0 compliant. Given the length of some of these documents there tend to be issues, and so I record the issues and the pages they occur on.

The challenge: Given a string input, output a count of all distinct pages within the string.

Rules:

  • Input will consist of of a non-empty string containing the pages, page ranges, and the issue. Multiple pages and page ranges are guaranteed to be separated by commas ,.
pgs 810-812, 897, 1043, 1047, 1219, 1308, 1317, 1323 restructure reading boxes
pgs 810-812,897,1043, 1047,1219, 1308, 1317, 1323 restructure reading boxes

Both of the above are possible valid inputs.

  • This input can be multiple lines, with each line representing a different issue.
pgs 810-812, 897, 1043, 1047, 1219, 1308, 1317, 1323 restructure reading boxes
pgs 816, 819, 826, 827, 829, 853, 861, 866, 885, 1043, 1142, 1143, 1164 table needs to be read as table or figure

You can take a multiline input as a list of strings, with each item in the list being a line. The output is still based on the entirety of the list.

  • The input format is not necessarily pgs <page numbers> <issue>.

    Both pg 1324 read all and Read all are valid inputs.

    If the string consists of only <Issue>, this represents that the document is a single page, and thus the output would be 1 (no matter how many lines of input there are). If there is more than one page, you are guaranteed to not have this format as a line of the input.

Test Cases:

Input:
------
pgs 810-812, 897, 1043, 1047, 1219, 1308, 1317, 1323 restructure reading boxes

Output: 
-------
10


Input:
------
pgs 587,897, 936, 939,946, 1308 single figure
pgs 810-812, 897, 1043, 1047, 1219,1308,1317, 1323 restructure reading boxes

Output:
-------
14


Input:
------
pgs 824, 873, 941-943, 1032, 1034, 1036-1040, 1055, 1071, 1153, 1154, 1228 figures as figures
pgs 1036-1040 Color Contrast for negative balance numbers does not meet WGAC 2.0 guidelines
pg 1324 read all

Output:
-------
18

Input:
------
Incorrect Reading Order

Output:
-------
1

Input:
------
Incorrect Reading Order
Pg 1 Read Table as Table
Pgs 1 Color Contrast for negative balance numbers does not meet WGAC 2.0 guidelines

Output:
-------
1


This is , so shortest answer in bytes wins.

\$\endgroup\$

marked as duplicate by Sriotchilism O'Zaic code-golf Jul 20 at 18:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Inverse of this? \$\endgroup\$ – Veskah Jul 19 at 14:19
  • \$\begingroup\$ @Veskah Not really an inverse. That one condenses pages into page ranges, the inverse would take page ranges, and output a list of pages. Mine is just a count of involved pages. \$\endgroup\$ – william porter Jul 19 at 14:25
  • 1
    \$\begingroup\$ Possible duplicate \$\endgroup\$ – Giuseppe Jul 19 at 16:09
  • \$\begingroup\$ Definitely strongly related, but I'd say the difference in input and output make it not a duplicate. \$\endgroup\$ – william porter Jul 19 at 16:16
  • 3
    \$\begingroup\$ @williamporter the core of the challenge is still parsing a format like a,b-c,d; adding a count unique and removing the other characters does not, in my estimation, make it different enough, but the community is welcome to override that. \$\endgroup\$ – Giuseppe Jul 19 at 16:37
3
\$\begingroup\$

05AB1E, 51 38 bytes

-13 bytes thanks to Kevin Cruijssen!

',ð:#ʒ'-м€.ïß}D.ï≠ÅÏ'-¡¤LsнUʒX<›]˜Ùg1M

Try it online!

Explanation

                                      // Implicitly Read in multiline string 
 ',ð:#                                //Swap commas with spaces and split on spaces
      ʒ'-м€.ïß}                       //Filter list to only those with - removed are numbers
               D                      //Duplicate
                .ï≠ÅÏ                 //For all the ones that aren't numbers (so the ranges)
                    '-¡               //Split on dashes
                       ¤L             //Generate list from 1 to the highest number
                         sнU          //Stick the lower number in the variable U
 ʒ®<›]                                //Filter out numbers less than the lower number
      ˜                               //deep flatten
        Ùg                            //Uniquify and count 
          1M                          //Push 1 then output the largest number in the stack
\$\endgroup\$
  • 1
    \$\begingroup\$ 39 bytes. Summary of changes: input as a multi-line string, so can be removed. ' has a builtin: ð. The can be ß (minimum). '-' :# can simply be a split: '-¡. The D is unnecessary by using ¤ instead of θ (both get the last item, but ¤ doesn't pop, and θ does pop). The ©, ® can be U/X (or alternatively V/Y) so the s can be removed before the filter; these pop and store, whereas © stores without popping. ï can be removed (I think); and D>i1 can be simplified to 1‚à (pair with 1, and then pop+push maximum). \$\endgroup\$ – Kevin Cruijssen Aug 1 at 7:55
  • \$\begingroup\$ Thanks @KevinCruijssen I'll update when I get a chance, some excellent ideas I can use in the future in there too! \$\endgroup\$ – Expired Data Aug 1 at 7:59
  • \$\begingroup\$ Yep, that's why I tried to explain my changes instead of just giving the 39 byter, so you can learn from it for future challenges. :) Nice answer though. That initial ',' :# had me confused for a bit when I changed it to ',¡ like the split on - later on. It will leave leading spaces before the numbers, causing all kind of issues later on in the first filter.. And the before the is to also filter out the 2.0 I noticed. Nice work-around! :) \$\endgroup\$ – Kevin Cruijssen Aug 1 at 8:03
  • 1
    \$\begingroup\$ Too bad ʒ'-мDþQ} also leaves the empty items, which is how I usually only leave numbers. ;) \$\endgroup\$ – Kevin Cruijssen Aug 1 at 8:06
  • \$\begingroup\$ oh, one last byte to golf (for now ;p), so it becomes 38: my suggestion of 1‚à can be 1M (push 1, push largest number on the stack). \$\endgroup\$ – Kevin Cruijssen Aug 1 at 8:08
1
\$\begingroup\$

Perl 5 (-p), 63 bytes

s/-/../g;s/^pgs?|[^., \d].*//g;map$h{$_}=1,eval}{$\=(keys%h)||1

TIO

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.