Coder Social home page Coder Social logo

light-switch-problem-solver's Introduction

Light switch problem solver

I stumble upon this problem on a youtube video about battle field 4

There are certain number of lights in a room. There are also certain number of switches. Every switch opens some variaty of lights. If a light is already lit, switch closes the light. If it is not lit, it opens the light.

For instance in the diagram we have 2 lights A and B. We also have a switch S. Light A is lit and light B is not lit.

+----------+---------+
|##########|         |      +--+
|### A ####|    B    |      |S |
|##########|         |      +-++
+----+-----+----+----+        |
     |          |             |
     +----------+-------------+

If we press the switch S which is connected to Light A and B, Light A will be turned off and ligt B will be runed on.

+----------+---------+
|          |#########|      +--+
|    A     |### B ###|      |S#|
|          |#########|      +-++
+----+-----+----+----+        |
     |          |             |
     +----------+-------------+

Things get little more complicated if we have multiple switches connected to same light. Here are 3 ligts with 2 switches. Light B is connected to both switches So if we press one button light B will be turned on and if we press it again light will be turned off.

1 - Initial state

       +--------+-----------------+
       |        |                 |

+-------+---+---+----+--+ ++-+ | | | | | A | B | C | +--+ +--+ | | | | +---+---+---+---+-------+ ++-+ | | | +-------+--------------------+

2 - Turn on switch T

+-------+---+---+----+--+ ++-+ | #######| | A ## C ##| +--+ +--+ | #######| +---+---+---+---+-------+ ++-+ | | | +-------+--------------------+

3 - Turn on switch S

+-------+---+---+----+--+ ++-+ B +--+ +--+ +---+---+---+---+-------+ ++-+ | | | +-------+--------------------+

So problem is given the number of switches and lights and specification about how they are connected, find a switch combination that lits all the lights.

Here is the types for the solver.

module Solver where

type LightName = String
type SwitchName = String

data Switch = Switch SwitchName [LightName] deriving (Show, Eq)

solve :: [LightName] -> [Switch] -> Maybe [SwitchName]
solve _lns _ss = Nothing

Run unittest against you solution by

stack test

Lastly here here is a example fucntion call that finds the solution for 1 light and 1 switch

*Main> solve ["A"] [Switch {switchName="S", lights=["A"]}]
Just ["S"]

light-switch-problem-solver's People

Contributors

huseyinyilmaz avatar

Stargazers

Serdar Akarca avatar

Watchers

 avatar James Cloos avatar

Forkers

twanvl

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.