Instantly share code, notes, and snippets.

@kunitoo /Gemfile
Last active Jan 15, 2016

Embed
What would you like to do?
瞬き星 〜 第2回 ESM オフラインどう書く ( Ruby + Neo4j ) https://gist.github.com/mattsan/07674b095908fda117a0
source 'https://rubygems.org'
gem 'neo4j'
GEM
remote: https://rubygems.org/
specs:
active_attr (0.8.5)
activemodel (>= 3.0.2, < 5.0)
activesupport (>= 3.0.2, < 5.0)
activemodel (4.2.5)
activesupport (= 4.2.5)
builder (~> 3.1)
activesupport (4.2.5)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
builder (3.2.2)
colored (1.2)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.9.2)
faraday (>= 0.7.4, < 0.10)
faraday_middleware-multi_json (0.0.6)
faraday_middleware
multi_json
httparty (0.13.7)
json (~> 1.8)
multi_xml (>= 0.5.2)
httpclient (2.7.0.1)
i18n (0.7.0)
json (1.8.3)
minitest (5.8.3)
multi_json (1.11.2)
multi_xml (0.5.5)
multipart-post (2.0.0)
neo4j (6.0.4)
active_attr (~> 0.8)
activemodel (~> 4)
activesupport (~> 4)
neo4j-core (~> 6.0.0.rc.1)
orm_adapter (~> 0.5.0)
neo4j-core (6.0.4)
activesupport
faraday (~> 0.9.0)
faraday_middleware (~> 0.9.1)
faraday_middleware-multi_json
httparty
httpclient
json
multi_json
neo4j-rake_tasks (~> 0.3.0)
net-http-persistent
neo4j-rake_tasks (0.3.0)
colored
httparty
os
rake
rubyzip (~> 1.1.7)
net-http-persistent (2.9.4)
orm_adapter (0.5.0)
os (0.9.6)
rake (10.4.2)
rubyzip (1.1.7)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
neo4j
BUNDLED WITH
1.10.6
require 'bundler'
Bundler.require
@session = Neo4j::Session.open
def cleanup
@session.query('match n detach delete n')
end
def setup
cleanup
@session.query(<<~QUERY)
create
(a{name: 'A', color: 'W'}),
(b{name: 'B', color: 'W'}),
(c{name: 'C', color: 'W'}),
(d{name: 'D', color: 'W'}),
(e{name: 'E', color: 'W'}),
(f{name: 'F', color: 'R'}),
(g{name: 'G', color: 'R'}),
(h{name: 'H', color: 'R'}),
(i{name: 'I', color: 'R'}),
(j{name: 'J', color: 'R'}),
(a)-[:line{type: 'AC'}]->(j)-[:line{type: 'AC'}]->(i)-[:line{type: 'AC'}]->(c),
(a)-[:line{type: 'AD'}]->(f)-[:line{type: 'AD'}]->(g)-[:line{type: 'AD'}]->(d),
(b)-[:line{type: 'BE'}]->(j)-[:line{type: 'BE'}]->(f)-[:line{type: 'BE'}]->(e),
(b)-[:line{type: 'BD'}]->(i)-[:line{type: 'BD'}]->(h)-[:line{type: 'BD'}]->(d),
(e)-[:line{type: 'EC'}]->(g)-[:line{type: 'EC'}]->(h)-[:line{type: 'EC'}]->(c)
QUERY
end
def invert(name)
@session.query(<<~QUERY, name: name)
MATCH s
WHERE s.name = {name}
SET s.color = CASE s.color WHEN 'W' THEN 'R' ELSE 'W' END
QUERY
@session.query(<<~QUERY, name: name)
MATCH (s)-[r1]-(t)-[r2]-(tt)-[r3]-(e)
WHERE
r1.type = r2.type = r3.type
AND s.color = e.color <> t.color = tt.color
AND s.name = {name}
SET t.color = s.color
SET tt.color = s.color
QUERY
@session.query(<<~QUERY, name: name)
MATCH (s)-[r1]-(t)-[r2]-(e)
WHERE
r1.type = r2.type
AND s.color = e.color <> t.color
AND s.name = {name}
SET t.color = s.color
QUERY
end
def solve(input)
setup
input.each_char do |name|
invert(name)
end
add_label_by_color
@session.query('MATCH n RETURN n.color AS color ORDER BY n.name').map(&:color).join
end
def add_label_by_color
%w(R W).each do |color|
@session.query("MATCH n WHERE n.color = '#{color}' SET n :#{color}")
end
end
def test(input, expected)
result = solve(input)
if expected == result
print '.'
else
puts
puts "test(#{input}, #{expected}) #=> got #{result}"
end
end
test("A", "RWWWWRRRRR")
test("F", "WWWWWWWRRW")
test("J", "WWWWWWRRWW")
test("AA", "WWWWWWWRWW")
test("IC", "WWRWWRRRWW")
test("FC", "WWRWWWWRRW")
test("AE", "RWWWRRRRRR")
test("GJ", "WWWWWWWWWW")
test("CCB", "WRWWWRWWWR")
test("BEF", "WRWWRWWRRR")
test("JGD", "WWWRWWWWWW")
test("IHCC", "WWWWWRWWWW")
test("AIDD", "RWWWWRRWWR")
test("IJFA", "RWWWWWWWWW")
test("ABCDE", "RRRRRRRRRR")
test("ICEBA", "RRRWRRRRRR")
test("DAHHD", "RWWWWRWWWR")
test("GJIJC", "WWRWWWWWRR")
test("FGHIJ", "WWWWWWWWRR")
test("HJICGA", "RWRWWRRRRR")
test("IBCIGC", "WRWWWWWWWW")
test("BIJJJB", "WWWWWWRWWW")
test("DCBCHGD", "WRWWWWWRRW")
test("JEABDHD", "RRWWRRRWRR")
test("JHFADHE", "RWWRRRRRWW")
test("HDGGDBIB", "WWWWWWWWWW")
test("IIDIHCCG", "WWWRWRRWWW")
test("BBFBICIE", "WRRWRRRWWW")
test("HJHCFBJGG", "WRRWWWWRRW")
test("AJJIEAAII", "RWWWRWWWWR")
test("AIDHJFGAE", "WWWRRWWWWW")
test("FGBGHCBHJJ", "WWRWWWWRRW")
test("EFIGIGGHHJ", "WWWWRRRWWR")
test("HGAFDIFFFF", "RWWRWRRRRW")
test("AABBCCDDEE", "WWWWWWWWWW")
test("ABCDEFGHIJ", "RRRRRWWWWW")
test("FGHIJABCDE", "RRRRRRRRRR")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment