Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created October 29, 2015 16:16
Show Gist options
  • Save dtinth/b2d5d5c81cb34c46a5ce to your computer and use it in GitHub Desktop.
Save dtinth/b2d5d5c81cb34c46a5ce to your computer and use it in GitHub Desktop.
ใครคือโจร? (แก้ด้วยภาษา Ruby)
class TrueClass; def เป็นโจร; false end end
class FalseClass; def เป็นโจร; true end end
ชื่อ = [:ก, :ข, :ค, :ง]
ผู้ต้องสงสัย = [
-> ก, ข, ค, ง { ง.เป็นโจร },
-> ก, ข, ค, ง { ค.เป็นโจร },
-> ก, ข, ค, ง { ก.เป็นโจร && ข.เป็นโจร },
-> ก, ข, ค, ง { !ข.เป็นโจร }
]
คนบริสุทธิ์ = ชื่อ.combination(2).find { |คนดี|
ข้อสันนิษฐาน = ชื่อ.map { |n| คนดี.include?(n) }
ข้อสันนิษฐาน.zip(ผู้ต้องสงสัย).all? { |คำตอบที่ควรได้, ถามผู้ต้องสงสัย|
ถามผู้ต้องสงสัย[*ข้อสันนิษฐาน] == คำตอบที่ควรได้
}
}
p โจร = ชื่อ - คนบริสุทธิ์
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment