Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:18:37+00:00 2026-06-04T08:18:37+00:00

I have a fun little problem where I’m presented with a logic clause like

  • 0

I have a fun little problem where I’m presented with a logic clause like so:

Rule 1. A, B and C are unique, and are numbers from 1 to 3 (so every number is used).
Rule 2. B < 2
Rule 3. C > 2

Now (assuming this quick example I just came up with actually validates :P), it’s easy to see

A = 2
B = 1
C = 3

But that’s a very contrived example.

What about if you have 20 (or ten thousand) rules, and they overlapped. Assume there is valid single answer, and you have access to the rules somehow (be it a list of predicates say).

Is there a nice, general, intuitive solution? I know Prolog could solve it with certain libraries, but my attempts proved futile. I know I could just brute force permutate all numbers in the range, and then manually check they conform to the rules. But that seems pretty lame.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:18:39+00:00Added an answer on June 4, 2026 at 8:18 am

    Brute force solution

    First I create all possible inputs based on rule 1 which happens to be all permutations:

    val all = (1 to 3).permutations
    

    Then I define remaining rules:

    val rule2 = (a: Int, b: Int, c: Int) => b < 2
    val rule3 = (a: Int, b: Int, c: Int) => c > 2
    

    And I simply filter out solutions that do not match all the rules:

    val solutions = all.
      filter(i => rule2(i(0), i(1), i(2))).
      filter(i => rule3(i(0), i(1), i(2))).
      toList
    

    solutions.toList prints one and only valid solution:

    List(Vector(2, 1, 3))
    

    Note that because permutations are generated lazily, this implementation isn’t really that ineffective (e.g. rule3 is only applied on solutions that passed rule2).

    You can use foldLeft() to apply arbitrary number of predicates:

    val rules = List(rule2, rule3)
    
    rules.
      foldLeft(all)((solutionsSoFar, rule) => 
        solutionsSoFar.filter(s => 
          rule(s(0), s(1), s(2))
        )
      )
    

    Drools

    Have a look at Drools business rule engine. Just put all possible solutions as knowledge base and define rules using nice DSL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am moving from jQuery to MooTools (for the fun..) and I have this
I've got a wonderfully fun little SQL problem to solve today and thought I'd
I have a little problem: I want to solve this problem with OCaml, so
I'm developing a fun little project using Foursquare API. In this project, I have
I have a little problem with my code. I have a table containing car
i have little problem with boost::asio library. My app receive and process data asynchronously,
I started a little fun project - collecting a hello World-program for every letter
I have a fun issue where during application shutdown, try / catch blocks are
I have a fun application written in Visual Studio (C#), and I'm wondering -
I have found myself designing a language for fun that is a cross between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.