Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]
share|improve this question
1  
... input in the format given. That is? A single string with a newline? or 2 strings? – edc65 Feb 22 at 21:04
1  
2 strings or a string with a newline – muddyfish Feb 22 at 21:08
up vote 1 down vote accepted

Pyth, 16 bytes

Takes input as lowercase letters instead of uppercase.

VS@GJw+-sz/zdxJN
share|improve this answer

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.

share|improve this answer

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>

share|improve this answer

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/.$//'
share|improve this answer

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--
share|improve this answer

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

share|improve this answer

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)))
share|improve this answer

Seriously, 40 bytes

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

Try it online!

Explanation to come later after further golfing.

share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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