Skip to content

Instantly share code, notes, and snippets.

@kunitoo
Last active December 9, 2017 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunitoo/c7eca957989c02f8c2f59c3dc655ed4a to your computer and use it in GitHub Desktop.
Save kunitoo/c7eca957989c02f8c2f59c3dc655ed4a to your computer and use it in GitHub Desktop.
オフラインリアルタイムどう書くE20 http://nabetani.sakura.ne.jp/hena/orde20maze/ をオンラインで Neo4j を使った回答
version: '2'
services:
neo4j:
image: neo4j:3.3
ports:
- "7474:7474"
- "7687:7687"
environment:
NEO4J_AUTH: "none"
require 'bundler'
Bundler.require
def solve(input)
Neo4j::Session.open(:server_db, 'http://localhost:7474')
conn = Neo4j::Session.current
conn.query('MATCH (n) DETACH DELETE n')
nodes = {}
(?0..?9).each {|n| nodes[n] = Neo4j::Node.create({name: n}, :Pos) }
(?A..?Z).each {|n| nodes[n] = Neo4j::Node.create({name: n}, :Pos) }
[
[?0, ?1], [?2, ?3], [?3, ?4], [?4, ?5],
[?7, ?8], [?8, ?9], [?9, ?A],
[?C, ?D], [?F, ?G],
[?J, ?K], [?K, ?L], [?L, ?M],
[?P, ?Q], [?Q, ?R], [?S, ?T],
[?U, ?V], [?V, ?W], [?Y, ?Z],
[?0, ?6], [?6, ?C], [?C, ?I], [?I, ?O],
[?1, ?7], [?J, ?P], [?P, ?V],
[?E, ?K],
[?3, ?9], [?9, ?F], [?R, ?X],
[?G, ?M], [?M, ?S], [?S, ?Y],
[?5, ?B], [?H, ?N], [?N, ?T]
].each do |l1, l2|
n1 = nodes[l1]
n2 = nodes[l2]
Neo4j::Relationship.create(:line, n1, n2)
end
s, g = input.chars
conn.query("MATCH (s:Pos{ name: '#{s}' }), (g:Pos{ name: '#{g}' }), p = shortestPath((s)-[:line*]-(g)) RETURN length(p) AS len").to_a.first.len.to_s
end
TEST_DATA = <<~EOS
/*0*/ test( "DE", "13" );
/*1*/ test( "EK", "1" );
/*2*/ test( "01", "1" );
/*3*/ test( "LG", "2" );
/*4*/ test( "A1", "4" );
/*5*/ test( "GJ", "4" );
/*6*/ test( "FK", "4" );
/*7*/ test( "LV", "4" );
/*8*/ test( "27", "4" );
/*9*/ test( "0O", "4" );
/*10*/ test( "G1", "5" );
/*11*/ test( "ZH", "5" );
/*12*/ test( "AB", "5" );
/*13*/ test( "KX", "5" );
/*14*/ test( "1G", "5" );
/*15*/ test( "WX", "5" );
/*16*/ test( "3L", "5" );
/*17*/ test( "9Y", "5" );
/*18*/ test( "EX", "6" );
/*19*/ test( "BG", "6" );
/*20*/ test( "7K", "7" );
/*21*/ test( "E3", "7" );
/*22*/ test( "SW", "7" );
/*23*/ test( "BM", "7" );
/*24*/ test( "3C", "7" );
/*25*/ test( "H9", "7" );
/*26*/ test( "J3", "7" );
/*27*/ test( "GX", "8" );
/*28*/ test( "2Z", "8" );
/*29*/ test( "8H", "8" );
/*30*/ test( "Z7", "8" );
/*31*/ test( "0B", "8" );
/*32*/ test( "U9", "9" );
/*33*/ test( "Z0", "10" );
/*34*/ test( "0N", "10" );
/*35*/ test( "U8", "10" );
/*36*/ test( "XZ", "10" );
/*37*/ test( "H0", "11" );
/*38*/ test( "CH", "13" );
/*39*/ test( "WB", "13" );
/*40*/ test( "0R", "13" );
/*41*/ test( "DZ", "13" );
/*42*/ test( "NI", "13" );
/*43*/ test( "QC", "14" );
/*44*/ test( "6U", "14" );
/*45*/ test( "PO", "15" );
/*46*/ test( "RI", "16" );
/*47*/ test( "UO", "17" );
/*48*/ test( "WO", "17" );
/*49*/ test( "OX", "18" );
EOS
Minitest::Reporters.use!(Minitest::Reporters::ProgressReporter.new)
describe 'Doukaku' do
def self.test_order; :sorted; end
TEST_DATA.each_line do |test|
number, input, expected = test.scan(/(\d+).*"(.*)", "(.*)"/)[0]
it "##{number}" do
assert_equal expected, solve(input)
end
end
end
source 'https://rubygems.org'
gem 'activesupport', require: 'active_support/all'
gem 'minitest', require: 'minitest/autorun'
gem 'minitest-reporters'
gem 'awesome_print'
gem 'tapp'
gem 'pry'
gem 'pry-rescue', require: 'pry-rescue/minitest'
gem 'pry-stack_explorer'
gem 'rake'
gem 'neo4j'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment