先の道路塗装


12

2つのパラメータlane patternroad lengthを指定すると、Roads and Traffic Serviceが道路をペイントするための車線マークのASCII表現を出力します。

入出力の例

入力: BTHMLRPHU、21

2つのパラメーターを取るか、文字列の末尾に数値を連結するかどうかは気にしません。それは明確です。

入力は、関数の引数、環境変数など、言語で意味のあるものとして、STDINから取得できます。

出力:

!   |      x      ##      |      |      x      x      !
! B |  /\  x HOV3 ##  <-  |  ->  |  ^^  x HOV3 x      !
! B |  \/  x HOV3 ##   |  |  |   |  ^^  x HOV3 x      !
!   |      x      ##      |      |      x      x      !
!   |      x      ##      |      |      x      x      !
!   |      |      ##      |      |      |      |      !
!   |      |      ##      |      |      |      |      !
!   |      |      ##      |      |      |      |      !
! B |  /\  | HOV3 ##  <-  |  ->  |  ^^  | HOV3 |      !
! B |  \/  | HOV3 ##   |  |  |   |  ^^  | HOV3 |      !
!   |      x      ##      |      |      x      x      !
! B |  /\  x HOV3 ##  <-  |  ->  |  ^^  x HOV3 x      !
! B |  \/  x HOV3 ##   |  |  |   |  ^^  x HOV3 x      !
!   |      x      ##      |      |      x      x      !
!   |      x      ##      |      |      x      x      !
!   |      |      ##      |      |      |      |      !
!   |      |      ##      |      |      |      |      !
!   |      |      ##      |      |      |      |      !
! B |  /\  | HOV3 ##  <-  |  ->  |  ^^  | HOV3 |      !
! B |  \/  | HOV3 ##   |  |  |   |  ^^  | HOV3 |      !
!   |      x      ##      |      |      x      x      !

各文字は、幅0.5メートル、長さ1キロメートルを示します。

仕様

車線表示

道路が10 km伸びるごとに、2、3、9、10 km(出力の「上」から)にマーキングが描かれます。マーキングはレーンの中央に配置されます。自転車レーンと中央分離帯を除き、すべてのレーンの幅は3メートル(6文字)です。

出力例に示されているように、マーキングの代わりにASCIIのひし形と矢印の文字を使用することはできません

  • B: 自転車専用車線。Bマーキング。幅1.5メートル(3文字)。
  • T:トランジット。ダイヤモンドマーキング
  • H:占有率の高い車線。HOV3マーキング
  • LR:車線を曲がります。矢印マーキング
  • P:追い越し車線。キャレットマーキング
  • U:無制限の車線。マーキングなし

セパレーター(優先順)

  • 中央値:(入力文字列でで##示されM、溝を含む他のセパレータを置き換えます)
  • 溝(左端と右端): !感嘆符
  • 間のHOVレーンの代替x|5キロごと
  • 正常: |

制約

関数またはプログラムは以下を行う必要があります。

  • STDOUTに出力(これはSystem.out.print、Java、console.logJavaScriptなどに相当するものを意味します)
  • 0-10中央値で1-9レーンを印刷できます
  • 最大50 kmの道路(50行の出力)を印刷できる
  • 標準的な抜け穴を使用しない
  • \n出力の末尾のオプションを除き、末尾の空白は受け入れられません

可能な最大の出力:3700バイト(74文字* 50行)。

最小出力:5バイト(入力ありB、1)

仮定

  • 隣接する中央値なし(部分文字列MMは発生しません)
  • マーキングの2行目が途切れる場合があります(たとえば、長さが9または12 kmの場合)
  • レーンは論理的に意味をなさない場合があります(たとえば、道路の左側の右折車線など、任意の順序が可能です)

これはなので、最短のコード(バイト単位)が勝ちです!


1
そこには、等幅フォントが大好きです
WayToDoor

回答:


4

ルビー、245

関連する場合は車線区分を印刷してから、車線を印刷します。

勝つとは思わない。

->(n,i){i.times{|d,t|*e=''
g=e+%w{HOV3 ^^ B}
n.chars{|c|$><<(c==?M?'##':!t ??!:(t+c)[?H]&&d%10<5??x:?|)if(M=t!=?M)
$><<((e+[(%w{/\\ <- ->}+g)[v='TLRUHPB'.index(c)],(%w{\\/ \ | |\ }+g)[v]]+e*4)*2)[d%10].center(v>5?3:6)if(t=c)!=?M}
puts M ? e:?!}}

変更履歴

245は stderrをチョークし、配列を効果的に分割します。

263配列をインデックス化するより良い方法

268は各行を印刷するだけで、正規バージョンを計算しません。

330初期コミット


Rubyが勝つことは期待していませんが、来週以内に他に答えがなければ、勝つと思います:-P
rink.attendant.6


2

JavaScript(ES6)、316バイト

f=(x,n)=>{for(i=0;n>i++;){b=!(r=i%10)|r==3;y=[...`! ${[...x].join` | `} !`[o='replace'](/[\W] ?M [\W]?/g,'##')].map(c=>~(q='LPRTU'.indexOf(c))?` ${'<- |^^^^->| /\\\\/    '.substr(4*q+2*b,2)} `:c=='H'?'HOV3':c).join``;y=r&&r<6?y[o](/\| H/g,'x H')[o](/3 \|/g,'3 x'):y;console.log(b|r==2|r==9?y:y[o](/[^!\|x#]/g,' '))}}

デモ

執筆時点でFirefoxおよびEdgeで動作するはずです。Chrome/ Operaでは、実験的な機能を有効にする必要があります。

console.log = x => O.innerHTML += x + '\n';

f = (x, n) => {
  for (i = 0; n > i++;) {
    b = !(r = i % 10) | r == 3;
    y = [...
      `! ${[...x].join` | `} !` [o = 'replace'](/[\W] ?M [\W]?/g, '##')
    ].map(c => ~(q = 'LPRTU'.indexOf(c)) ? ` ${'<- |^^^^->| /\\\\/    '.substr(4*q+2*b,2)} ` : c == 'H' ? 'HOV3' : c).join ``;
    y = r && r < 6 ? y[o](/\| H/g, 'x H')[o](/3 \|/g, '3 x') : y;
    console.log(b | r == 2 | r == 9 ? y : y[o](/[^!\|x#]/g, ' '))
  }
}

// Snippet stuff
var demo = () => {
  O.innerHTML = '';
  document.forms[0].checkValidity() && f(document.getElementById('P').value, document.getElementById('N').valueAsNumber);
};

document.getElementById('P').addEventListener('change', demo);
document.getElementById('N').addEventListener('change', demo);

demo();
<form action='#'>
  <p>
    <label>Lane pattern:
      <input type=text pattern=^M?([BHLPRTU]M?)+$ maxlength=19 required id=P value=MLTPUMHUTBR>
    </label>
  </p>
  <p>
    <label>Kilometres:
      <input type=number id=N min=1 value=21 max=50 step=1 required>
    </label>
  </p>
  <pre><output id=O></output></pre>
</form>


1

05AB1E175 174 175 バイト

ðTиDU'|TиX'BŽ5ES©ǝX„\/TbSDVè®ǝ€ºX4×"HOV3"®ǝX'<18SǝX„|-Yè®ǝøJDí'<'>:X'^®ǝ2×'#Tи2×'x5и'|5и«'!Tи)I.•o¤[‹‡•uŽDýSтì€ûŽe1ª904ûª8ª₄«ª‡•δ~¬]•2ôDí«Ž
ÿT∍S:ð.ø8ðì‚8:1ðì‚ð:SðT:èεI∍}øJ»

かなり悪いアプローチですが、機能し、作るのは楽しかったです。しかし、間違いなくもう少しゴルフができます。

隣接する2つのHHレーンのバグ修正として+1バイト。

オンラインでお試しください。

説明:

ステップ1:サイズ10のすべての可能なレーンを作成します。

ðTи               # Push a space character, repeated 10 times as list
   DU             # And store a copy in variable `X`
'|Tи             '# Push "|", repeated 10 times as list
X                 # Push the list of spaces of variable `X`
 'B              '# Push a "B"
   Ž5E            # Push compressed integer 1289
      S           # Converted to a list of digits: [1,2,8,9]
       ©          # Store it in variable `®` (without popping)
        ǝ         # Replace the spaces in the pushed `X` with the "B" at these (0-based)
                  # indices
X                 # Push `X` again
 \/              # Push string "\/"
    TbS           # Push 10, converted to binary, as list: [1,0,1,0]
       DV         # Store a copy in variable `Y`
         è        # Index each into this string: ["/","\","/","\"]
          ®       # Push list `®` again ([1,2,8,9])
           ǝ      # And replace the spaces with these characters
            €º    # And then mirror each line (" "→"  "; "/"→"/\"; "\"→"\/")
X                 # Push `X` again
 4×               # Extend each space to four spaces
   "HOV3"         # Push string "HOV3"
         ®ǝ       # And replace the spaces with this string at the indices of `®` again
X                 # Push `X` again
 '<              '# Push string "<"
   18S            # Push 18 as list: [1,8]
      ǝ           # Replace the spaces with "<" at those indices
       X          # Push `X` yet again
        „-|       # Push string "-|"
           Yè     # Use list `Y` ([1,0,1,0]) to index into this string: ["-","|","-","|"]
             ®ǝ   # And replace the spaces at the indices of `®` again
               ø  # Then zip-pair the two lists together
                J # And join each pair of characters to a string
Dí                # Create a copy and reverse each string
  '<'>:           # And replace all "<" with ">"
X'^®ǝ            '# Push `X` with the spaces at indices `®` replaced with "^" 
     2×           # Extend each character to size 2
'#Tи             '# Push "#", repeated 10 times as list
    2×            # And extend each character to size 2
'x5и             '# Push "x" repeated 5 times as list
    '|5и         '# Push "|" repeated 5 times as list
        «         # And merge the lists together
'!Tи             '# Push "!", repeated 10 times as list
)                 # And finally wrap all lists of the stack into one big list of lanes

ステップ2:入力文字列をインデックスに変換します(これを使用して、ステップ1で作成したリストにインデックスを付けます)。

I                 # Push the input-string
 .•o¤[‹‡•         # Push compressed string "tlrpbhmu"
         u        # And uppercase it
ŽDý               # Push compressed integer 3567
   S              # Converted to a list of digits: [3,5,6,7]
    тì            # Prepend each with "100": ["1003","1005","1006","1007"]
      €û          # And palindromize each: ["1003001","1005001","1006001","1007001"]
Že1               # Push compressed integer 10201
   ª              # And append it to the list
904ûª             # Push 904 palindromized to "90409", and also append it to the list
8ª                # Append 8 to the list
₄Â                # Push 1000, and bifurcate it (short for Duplicate & Reverse copy)
  «               # Merge them together: "10000001"
   ª              # And also append it to the list
                 # Now transliterate all uppercase characters in the input to these numbers
•δ~¬]•            # Push compressed integer 1119188999
      2ô          # Split into parts of size 2: [11,19,18,89,99]
        Dí        # Create a copy, and reverse each item: [11,91,81,98,99]
          «       # And merge the lists together: [11,19,18,89,99,11,91,81,98,99]
Ž\nÿ              # Push compressed integer 19889
    T            # Extended to size 10: 1988919889
      S           # As a list of digits: [1,9,8,8,9,1,9,8,8,9]
:                 # Replace all [11,19,18,89,99,11,91,81,98,99] with [1,9,8,8,9,1,9,8,8,9]
                  # in the converted string
ð.ø               # Surround the string with spaces
8ðì               # Push 8 with a prepended space: " 8"
   ‚             # Bifurcate and pair: [" 8","8 "]
     8:           # And replace all those for 8 in the string
1ðì‚ð:           # Do the same for [" 1","1 "] → " "
S                 # Convert the string to a list of characters (digits and space)
 ðT:              # Replace the spaces for 10

ステップ3:これらのインデックスを使用して、レーンのリストにインデックスを付けます。次に、これらのレーンのリストを正しい出力に変換します(整数入力のサイズに拡張/短縮するなど)。

è                 # Index the indices in the integer-list into the lanes-list
 ε                # Map over each lane
  I               #  Push the second integer-input
                 #  Extend/shorten each 10-sized lane to this input-size
                # After the map: zip/transpose; swapping rows/columns
   J              # Join inner list together to a single string
    »             # And then join each string by newlines
                  # (after which the result is output implicitly)

私のこの05AB1Eヒントを参照してください(セクションどのように圧縮文字列ではない辞書の一部?へどのように大きな整数を圧縮します?理由を理解することŽ5Eです1289.•o¤[‹‡•です"tlrpbhmu"ŽDýです10201•δ~¬]•です1119188999Ž\nÿです19889

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.