<commit_before>
F=
->i{r=i.split'
'
node=Struct.new :incoming_connection_count, :next_node
n=Hash.new{|h,k|h[k]=node.new 0}
r.size.times{|y|x=0
  r[y].chars{|c|
    idxs = [
      [[y,x-4],[y,x+2]],
      [[y,x+4],[y,x-2]],
      [[y-3,x],[y+1,x]],
      [[y+3,x],[y-1,x]]
    ]
    x+=1

    i='><v^'.index c

    if i
      f,t=idxs[i]

      n[f].next_node=n[t]
      n[t].incoming_connection_count += 1
    end
  }
}

q=n.values
c=q.find{|n|n.incoming_connection_count<1}
s=q.find{|n|n.incoming_connection_count>1}

t=0
l=n.size
s ?((t+=1;c=c.next_node)while c!=s):t=l-=1
[t,l-t]
}



require 'minitest/autorun'

describe F do

  def test_simple_case
    assert_equal [2, 6], F[<<-DOC]
# --> # --> # --> #
^     ^           |
|     |           |
|     |           v
#     # <-- # <-- #
DOC
  end

  def test_harder_case
    assert_equal [3, 10], F[<<-DOC]
            # --> # --> #
            ^           |
            |           |
            |           v
      # --> # <-- #     # --> #
      ^           ^           |
      |           |           |
      |           |           v
# --> #           # <-- # <-- #
DOC
  end

  def test_no_loop
    assert_equal [6, 0], F[<<-DOC]
# --> # --> # --> #
                  |
                  |
                  v
        <-- # <-- #
DOC
  end

end<commit_msg>used String#lines enumerator instead of split+size+times to save a lot<commit_after>
F=
->i{node=Struct.new :incoming_connection_count, :next_node
n=Hash.new{|h,k|h[k]=node.new 0}
y=0
i.lines{|l|x=0
  l.chars{|c|
    idxs = [
      [[y,x-4],[y,x+2]],
      [[y,x+4],[y,x-2]],
      [[y-3,x],[y+1,x]],
      [[y+3,x],[y-1,x]]
    ]
    x+=1

    i='><v^'.index c

    if i
      f,t=idxs[i]

      n[f].next_node=n[t]
      n[t].incoming_connection_count += 1
    end
  }
  y+=1
}

q=n.values
c=q.find{|n|n.incoming_connection_count<1}
s=q.find{|n|n.incoming_connection_count>1}

t=0
l=n.size
s ?((t+=1;c=c.next_node)while c!=s):t=l-=1
[t,l-t]
}



require 'minitest/autorun'

describe F do

  def test_simple_case
    assert_equal [2, 6], F[<<-DOC]
# --> # --> # --> #
^     ^           |
|     |           |
|     |           v
#     # <-- # <-- #
DOC
  end

  def test_harder_case
    assert_equal [3, 10], F[<<-DOC]
            # --> # --> #
            ^           |
            |           |
            |           v
      # --> # <-- #     # --> #
      ^           ^           |
      |           |           |
      |           |           v
# --> #           # <-- # <-- #
DOC
  end

  def test_no_loop
    assert_equal [6, 0], F[<<-DOC]
# --> # --> # --> #
                  |
                  |
                  v
        <-- # <-- #
DOC
  end

end
{'fraction_non_alphanumeric': 0.2170339932760553, 'fraction_numerical': 0.01867762420620097, 'mean_word_length': 1.220563847429519, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1892379', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1110, 'n_words': 145}