riddle

package
v0.0.0-...-cf66963 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAutocompleteFunc

func GetAutocompleteFunc(words []string) func(string) []string

func GetAutocompleteItemsFunc

func GetAutocompleteItemsFunc(items []Item) func(string) []string

Types

type Item

type Item string

Item represents a riddle item in ItemType:Item format (like color:red)

func NewItem

func NewItem(itemType, value string) Item

NewItem returns a new Item

func (Item) Split

func (item Item) Split() (string, string)

Split returns the item type and item as two separate strings

type Relation

type Relation int

Relation describes how items affect each other

const (
	RelAssociated Relation = iota
	RelDisassociated
)

Relation types

func (Relation) String

func (rel Relation) String() string

type Riddle

type Riddle struct {
	Setup           Setup
	Rules           []Rule
	PrimaryItemType string `json:",omitempty"`
}

Riddle contains the setup and rules of the riddle

func LoadRiddle

func LoadRiddle(data []byte) (*Riddle, error)

LoadRiddle loads the riddle from a byte slice in JSON format

func LoadRiddleFromFile

func LoadRiddleFromFile(file string) (*Riddle, error)

LoadRiddleFromFile loads the riddle from a JSON file

func NewEinsteinRiddle

func NewEinsteinRiddle() *Riddle

NewEinsteinRiddle returns Einstein's 5 house riddle

func NewJindoshRiddle

func NewJindoshRiddle() *Riddle

NewJindoshRiddle returns the Jindosh riddle

func NewRiddle

func NewRiddle() *Riddle

NewRiddle returns a new Riddle

func (*Riddle) Check

func (r *Riddle) Check() error

Check returns an error if the riddle is invalid

func (*Riddle) Save

func (r *Riddle) Save() ([]byte, error)

Save saves the riddle to a byte slice in JSON format

func (*Riddle) SaveToFile

func (r *Riddle) SaveToFile(file string) error

SaveToFile saves the riddle to a JSON file

func (*Riddle) Solve

func (r *Riddle) Solve() ([]SolverEntry, bool, error)

Solve solves the riddle and returns the entries

type Rule

type Rule struct {
	ItemA             Item
	ItemB             Item
	Relation          Relation
	Condition         string `json:",omitempty"`
	ConditionItemType string `json:",omitempty"`
	IsReversible      bool   `json:",omitempty"`
}

Rule is used by the riddle solver algorithm

func (*Rule) ApplyConditional

func (rule *Rule) ApplyConditional(entryA SolverEntry, others []SolverEntry) bool

ApplyConditional tries to apply a conditional rule to a SolverEntry

func (*Rule) ApplySimple

func (rule *Rule) ApplySimple(entry SolverEntry) bool

ApplySimple tries to apply a simple (non-conditional) rule to a SolverEntry

func (*Rule) Check

func (rule *Rule) Check(setup Setup) error

Check returns an error if the provided Rule is invalid

func (*Rule) HasCondition

func (rule *Rule) HasCondition() bool

HasCondition returns whether a rule has a condition

type Setup

type Setup map[string][]string

Setup is a map of riddle item types and their possible values

func (Setup) Check

func (setup Setup) Check() error

Check returns an error if the provided Setup is invalid

func (Setup) Contains

func (setup Setup) Contains(item Item) bool

Contains returns whether the setup contains the specified item

func (Setup) GetItemCountPerType

func (setup Setup) GetItemCountPerType() int

GetItemCountPerType returns the number of possible values under an item type

func (Setup) GetItemTypes

func (setup Setup) GetItemTypes() []string

GetItemTypes returns all item types in a slice

func (Setup) GetItems

func (setup Setup) GetItems() (items []Item)

GetItems returns all items in a slice in itemType:value format

type Solver

type Solver struct {
	Entries []SolverEntry
	// contains filtered or unexported fields
}

Solver can solve a riddle based on the setup and rules

func NewSolver

func NewSolver(setup Setup, rules []Rule) *Solver

NewSolver returns a new Solver

func (*Solver) FindAssociatedItems

func (solver *Solver) FindAssociatedItems(item Item) map[string][]string

FindAssociatedItems returns a map of items that possible belong to the provided item

func (*Solver) FindEntriesWithItem

func (solver *Solver) FindEntriesWithItem(item Item) (entries []SolverEntry)

FindEntriesWithItem returns the entries that contain the specified item

func (*Solver) GuessPrimaryItemType

func (solver *Solver) GuessPrimaryItemType() string

GuessPrimaryItemType tries to guess the primary item type as it's important for conditional rules

func (*Solver) IsSolved

func (solver *Solver) IsSolved() bool

IsSolved returns whether the solver managed to solve the riddle

func (*Solver) Solve

func (solver *Solver) Solve(primaryItemType string) (steps int, err error)

Solve tries to solve the riddle by applying all the rule to all entries over and over I like to call it brute-force deduction

type SolverEntry

type SolverEntry map[string][]string

SolverEntry is a unit of riddle items that belong together

func NewSolverEntry

func NewSolverEntry(setup Setup) SolverEntry

NewSolverEntry returns a new SolverEntry

func (SolverEntry) Contains

func (entry SolverEntry) Contains(item Item) bool

Contains returns whether the entry contains the specified item

func (SolverEntry) GetExcludedItems

func (entry SolverEntry) GetExcludedItems(setup Setup) (excluded []Item)

GetExcludedItems returns the excluded items compared to the setup

func (SolverEntry) GetValue

func (entry SolverEntry) GetValue(itemType string) (result interface{})

GetValue returns the value of an item type or nil if there are multiple choices

func (SolverEntry) IsSolved

func (entry SolverEntry) IsSolved() bool

IsSolved return true if the entry contains one value for each item type

func (SolverEntry) OnlyContains

func (entry SolverEntry) OnlyContains(item Item) bool

OnlyContains returns whether the entry only contains the specified item of that item type

func (SolverEntry) Set

func (entry SolverEntry) Set(item Item) bool

Set sets an item as the only possible value for that type

func (SolverEntry) Unset

func (entry SolverEntry) Unset(item Item) bool

Unset removes an item from the possible values for that type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址