10
\$\begingroup\$

Inspired by Draw a timeline.

Given a timeline of events, output the date for each event.

A timeline will be given in the form

    2000
--AF-C-------E--D---B--

Where the first line shows a known point in time. The known year is always the character below the first digit of the known year

You may assume:

  • On the timeline itself, each character represents one year.
  • There will always be at least one event
  • The events don't have to be in order
  • You may assume every character in the range between A and the furthest character is present
  • There will be at most 26 events
  • The known point will not have any padding to the right
  • There can be negative numbers in both the known point and the output
  • You will not have to handle numbers bigger than 2^32
  • You will not have to handle ranges bigger than 2^8
  • You can take input as lowercase letters instead of uppercase

You should write a program that takes a timeline in this form and outputs the key dates in order (A, B, C, D...)

You may output in a convenient form but you must take input in the format given.

Test cases:

    2000
--AF-C-------E--D---B--
[1998, 2016, 2001, 2012, 2009, 1999]

     10
--C-AB--D
[9, 10, 7, 13]

     0
--ABCDEFG--
[-3, -2, -1, 0, 1, 2, 3]

          5
--ABCDEFG--
[-3, -2, -1, 0, 1, 2, 3]
\$\endgroup\$
2
  • 1
    \$\begingroup\$ ... input in the format given. That is? A single string with a newline? or 2 strings? \$\endgroup\$
    – edc65
    Feb 22, 2016 at 21:04
  • 1
    \$\begingroup\$ 2 strings or a string with a newline \$\endgroup\$
    – Blue
    Feb 22, 2016 at 21:08

9 Answers 9

2
\$\begingroup\$

05AB1E, 19 18 bytes

Code:

ð¢>UágF²N>.bkX-¹+,

Explanation:

ð¢                   # Count the number of spaces before the year starts
  >                  # Increment by 1
   U                 # Pop and store this into X
    á                # Keep the alphabetic characters of the second input
     g               # Take the length
      F              # For N in range(0, length), do...
       ²             #   Take the second input
        N>           #   Push N and increment by 1
          .b         #   Converts 1 to A, 2 to B, etc.
            k        #   Find the index of that letter in the second input
             X       #   Push X
              -      #   index - X
               ¹     #   Get the first input, which contains the year
                +    #   Add to the difference of the index
                 ,   #   Pop and output the sum

Try it online!

Uses CP-1252 encoding.

\$\endgroup\$
1
\$\begingroup\$

Pyth, 16 bytes

Takes input as lowercase letters instead of uppercase.

VS@GJw+-sz/zdxJN
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 72

(a,b,r=[])=>[...b].map((x,i)=>r[parseInt(x,36)-10]=+a+i-a.search`\\d`)&&r

Test

f=(a,b,r=[])=>[...b].map((x,i)=>r[parseInt(x,36)-10]=+a+i-a.search`\\d`)&&r

console.log=x=>O.textContent+=x+'\n'

;[['    2000','--AF-C-------E--D---B--'],
['     10','--C-AB--D'],
['     0','--ABCDEFG--'],
['          5','--ABCDEFG--']]
.forEach(t=>{
  var a=t[0],b=t[1],r=f(a,b)
  console.log(a+'\n'+b+'\n'+r+'\n')
})  
<pre id=O></pre>

\$\endgroup\$
1
\$\begingroup\$

Bash + coreutils, 68

Line 1 is input as a quoted command-line option and line 2 is input from STDIN:

s="${1//[0-9]}"
fold -1|nl -v$[$1-${#s}]|sort -k2|sed '/-$/d;s/.$//'
\$\endgroup\$
1
\$\begingroup\$

Perl, 58 + 1 = 59 bytes

/\d/;$b=<>;for$c(A..Z){($z=index$b,$c)>-1&&say$_+$z-$-[0]}

Breakdown:

/\d/;                    # Match first digit in input string, this will set $-[0]
$b=<>;                   # Read next line (--A--CB--...) into $b
for $c (A..Z){           # Iterate over A, B, C, ... Z
  ($z=index$b,$c) >-1 && # If the character is found inside $b
  say $_+$z-$-[0]        #   then print
}

Requires -n and the free -M5.010:

# Added line breaks for each test case
$ perl -nE'/\d/;$b=<>;for$c(A..Z){($z=index$b,$c)>-1&&say$_+$z-$-[0]}' tl
1998
2016
2001
2012
2009
1999

9
10
7
13

-3
-2
-1
0
1
2
3

-3
-2
-1
0
1
2
3
$ cat tl
    2000
--AF-C-------E--D---B--
     10
--C-AB--D
     0
--ABCDEFG--
          5
--ABCDEFG--
\$\endgroup\$
0
\$\begingroup\$

Pyth, 22 bytes

V+r\AJeSKwJ+xKN-izT/zd

No I did not write this before I posted the challenge.

Explanation:

                       - autoassign z = input()
 +r\AJeSKwJ            -  create range of letters
        Kw             -      autoassign K = input()
      eS               -     sorted(K)[-1] (get the biggest character)
     J                 -    autoassign J = ^
  r\A                  -   range("A", ^)
 +        J            -  ^ + J
V                      - for N in ^: V
               -izT/zd - Get the number at the start
                izT    -   int(z, 10)
               -       -  ^-V
                   /zd -   z.count(" ")
           +           - V+^
            xKN        -  K.index(N)

Try it here

\$\endgroup\$
0
\$\begingroup\$

Python 3, 118

Man, today is the day of long Python answers.

def f(p,l):o=sum(x<'0'for x in p);e={x:i-o+int(p[o:])for i,x in enumerate(l)if'@'<x};return list(map(e.get,sorted(e)))
\$\endgroup\$
0
\$\begingroup\$

Seriously, 40 bytes

' ,c,;)l@;±)@-(x@;╗@Z`i3╤τ+@┐`MX╜ú∩S`└`M

Try it online!

Explanation to come later after further golfing.

\$\endgroup\$
0
\$\begingroup\$

Perl, 80 79 71 67 bytes

($a=<>)=~/\d/;$b=<>;say$a+$_-$-[0]for grep{$_+1}map{index$b,$_}A..Z

Thanks to @dev-null for 12 bytes!

($a=<>)=~/\d/;  # read first line of input, find position of first digit
                # (saved in the $- variable)
$b=<>;          # read the second line
                                           A..Z  # generate range 'A','B',...
                            map{index$b,$_}      # find index for each
                  grep{$_+1}                     # select only those != -1
              for                                # foreach of remaining...
say$a+$_-$-[0]                                   # calculate proper date
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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