generated from techmmunity/base-project-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fa285b
commit 24a1049
Showing
20 changed files
with
501 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export enum JugEnum { | ||
SMALLER = "SMALLER", | ||
BIGGER = "BIGGER", | ||
LARGER = "LARGER", | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
|
||
import { getStepIndex } from "../get-step-index"; | ||
|
||
import type { ActionsState } from "./types"; | ||
|
||
import { JugEnum } from "../../../enums/jug"; | ||
import { StepActionEnum } from "../../../enums/step-action"; | ||
import type { SolutionStep } from "../../../types/solution"; | ||
|
||
export interface EmptyJugInput { | ||
previousStep: SolutionStep; | ||
target: JugEnum; | ||
} | ||
|
||
export type EmptyJugType = (p: EmptyJugInput) => SolutionStep; | ||
|
||
export const makeEmptyJugAction = | ||
(_state: ActionsState) => | ||
({ previousStep, target }: EmptyJugInput): SolutionStep => { | ||
const { smallerJugContent, largerJugContent } = previousStep; | ||
|
||
return { | ||
smallerJugContent: target === JugEnum.SMALLER ? 0 : smallerJugContent, | ||
largerJugContent: target === JugEnum.LARGER ? 0 : largerJugContent, | ||
index: getStepIndex({ smallerJugContent, largerJugContent }), | ||
action: { | ||
type: StepActionEnum.EMPTY, | ||
jug: target, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
|
||
import { getStepIndex } from "../get-step-index"; | ||
|
||
import type { ActionsState } from "./types"; | ||
|
||
import { JugEnum } from "../../../enums/jug"; | ||
import { StepActionEnum } from "../../../enums/step-action"; | ||
import type { SolutionStep } from "../../../types/solution"; | ||
|
||
export interface FillJugInput { | ||
previousStep: SolutionStep; | ||
target: JugEnum; | ||
} | ||
|
||
export type FillJugType = (p: FillJugInput) => SolutionStep; | ||
|
||
export const makeFillJugAction = | ||
({ smallerJugCapacity, largerJugCapacity }: ActionsState) => | ||
({ previousStep, target }: FillJugInput): SolutionStep => { | ||
const { smallerJugContent, largerJugContent } = previousStep; | ||
|
||
const jugCapacity = | ||
target === JugEnum.SMALLER ? smallerJugCapacity : largerJugCapacity; | ||
|
||
const newSmallerJugContent = | ||
target === JugEnum.SMALLER ? jugCapacity : smallerJugContent; | ||
const newLargerJugContent = | ||
target === JugEnum.LARGER ? jugCapacity : largerJugContent; | ||
|
||
const index = getStepIndex({ | ||
smallerJugContent: newSmallerJugContent, | ||
largerJugContent: newLargerJugContent, | ||
}); | ||
|
||
return { | ||
smallerJugContent: newSmallerJugContent, | ||
largerJugContent: newLargerJugContent, | ||
index, | ||
action: { | ||
type: StepActionEnum.FILL, | ||
jug: target, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { makeEmptyJugAction } from "./empty-jug"; | ||
import { makeFillJugAction } from "./fill-jug"; | ||
import { makeTransferJugAction } from "./transfer-jug"; | ||
import type { ActionsState } from "./types"; | ||
|
||
export const makeActions = (actionsState: ActionsState) => { | ||
const emptyJug = makeEmptyJugAction(actionsState); | ||
const fillJug = makeFillJugAction(actionsState); | ||
const transferJug = makeTransferJugAction(actionsState); | ||
|
||
return { | ||
emptyJug, | ||
fillJug, | ||
transferJug, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
|
||
import { getStepIndex } from "../get-step-index"; | ||
|
||
import type { ActionsState } from "./types"; | ||
|
||
import { JugEnum } from "../../../enums/jug"; | ||
import { StepActionEnum } from "../../../enums/step-action"; | ||
import type { SolutionStep } from "../../../types/solution"; | ||
|
||
export interface TransferJugInput { | ||
previousStep: SolutionStep; | ||
target: JugEnum; | ||
} | ||
|
||
export type TransferJugType = (p: TransferJugInput) => SolutionStep; | ||
|
||
export const makeTransferJugAction = | ||
({ smallerJugCapacity, largerJugCapacity }: ActionsState) => | ||
({ previousStep, target }: TransferJugInput): SolutionStep => { | ||
const { smallerJugContent, largerJugContent } = previousStep; | ||
|
||
const targetJugCapacity = | ||
target === JugEnum.SMALLER ? smallerJugCapacity : largerJugCapacity; | ||
|
||
const originContent = | ||
target === JugEnum.SMALLER ? smallerJugContent : largerJugContent; | ||
const destinationContent = | ||
target === JugEnum.SMALLER ? largerJugContent : smallerJugContent; | ||
|
||
const toAvailability = targetJugCapacity - originContent; | ||
|
||
const newDestinationContent = Math.min( | ||
originContent + destinationContent, | ||
targetJugCapacity, | ||
); | ||
|
||
const newOriginContent = Math.max(originContent - toAvailability, 0); | ||
|
||
const newSmallerJugContent = | ||
target === JugEnum.SMALLER ? newOriginContent : newDestinationContent; | ||
const newLargerJugContent = | ||
target === JugEnum.SMALLER ? newDestinationContent : newOriginContent; | ||
|
||
const index = getStepIndex({ | ||
smallerJugContent: newSmallerJugContent, | ||
largerJugContent: newLargerJugContent, | ||
}); | ||
|
||
return { | ||
smallerJugContent: newSmallerJugContent, | ||
largerJugContent: newLargerJugContent, | ||
index, | ||
action: { | ||
type: StepActionEnum.TRANSFER, | ||
originJug: target === JugEnum.SMALLER ? JugEnum.LARGER : target, | ||
destinationJug: target === JugEnum.SMALLER ? target : JugEnum.LARGER, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface ActionsState { | ||
smallerJugCapacity: number; | ||
largerJugCapacity: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
import type { SolutionStep } from "../../types/solution"; | ||
|
||
interface AddStepToPossibleSolutions { | ||
previousStep: SolutionStep; | ||
step: SolutionStep; | ||
possibleSolutions: Array<Array<SolutionStep>>; | ||
} | ||
|
||
export const addStepToPossibleSolutions = ({ | ||
previousStep, | ||
step, | ||
possibleSolutions, | ||
}: AddStepToPossibleSolutions) => { | ||
const possibleSolutionsClone = JSON.parse( | ||
JSON.stringify(possibleSolutions), | ||
) as Array<Array<SolutionStep>>; | ||
|
||
const found = possibleSolutionsClone.find(states => { | ||
const lastIndex = states.at(-1)!; | ||
|
||
return lastIndex.index === previousStep.index; | ||
}); | ||
|
||
if (found) { | ||
found.push(step); | ||
|
||
return { | ||
possibleSolutions: possibleSolutionsClone, | ||
solutionDeep: found.length, | ||
}; | ||
} | ||
|
||
const foundSecondFromLast = possibleSolutionsClone.find(states => { | ||
const secondFromLast = states.at(-2)!; | ||
|
||
return secondFromLast.index === previousStep.index; | ||
}); | ||
|
||
if (foundSecondFromLast) { | ||
possibleSolutionsClone.push([...foundSecondFromLast.slice(0, -1), step]); | ||
|
||
return { | ||
possibleSolutions: possibleSolutionsClone, | ||
solutionDeep: foundSecondFromLast.length, | ||
}; | ||
} | ||
|
||
possibleSolutionsClone.push([previousStep, step]); | ||
|
||
return { | ||
possibleSolutions: possibleSolutionsClone, | ||
solutionDeep: 2, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export interface GetJugsInput { | ||
firstJugCapacity: number; | ||
secondJugCapacity: number; | ||
} | ||
|
||
export const getJugs = ({ | ||
firstJugCapacity, | ||
secondJugCapacity, | ||
}: GetJugsInput) => { | ||
const [smaller, larger] = [firstJugCapacity, secondJugCapacity].sort( | ||
(a, b) => a - b, | ||
); | ||
|
||
return { | ||
smallerJugCapacity: smaller, | ||
largerJugCapacity: larger, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { EmptyJugType } from "./actions/empty-jug"; | ||
import type { FillJugType } from "./actions/fill-jug"; | ||
import type { TransferJugType } from "./actions/transfer-jug"; | ||
|
||
import { JugEnum } from "../../enums/jug"; | ||
import type { SolutionStep } from "../../types/solution"; | ||
|
||
export interface GetStates { | ||
actions: { | ||
emptyJug: EmptyJugType; | ||
fillJug: FillJugType; | ||
transferJug: TransferJugType; | ||
}; | ||
previousStep: SolutionStep; | ||
} | ||
|
||
export const getStates = ({ actions, previousStep }: GetStates) => { | ||
const { emptyJug, fillJug, transferJug } = actions; | ||
|
||
return [ | ||
fillJug({ | ||
previousStep, | ||
target: JugEnum.SMALLER, | ||
}), | ||
fillJug({ | ||
previousStep, | ||
target: JugEnum.LARGER, | ||
}), | ||
emptyJug({ | ||
previousStep, | ||
target: JugEnum.SMALLER, | ||
}), | ||
emptyJug({ | ||
previousStep, | ||
target: JugEnum.LARGER, | ||
}), | ||
transferJug({ | ||
previousStep, | ||
target: JugEnum.SMALLER, | ||
}), | ||
transferJug({ | ||
previousStep, | ||
target: JugEnum.LARGER, | ||
}), | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { SolutionStep } from "../../types/solution"; | ||
|
||
export const getStepIndex = ({ | ||
smallerJugContent, | ||
largerJugContent, | ||
}: Pick<SolutionStep, "largerJugContent" | "smallerJugContent">) => | ||
`${smallerJugContent},${largerJugContent}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
|
||
export const isEven = (nbr: number) => nbr % 2 === 0; | ||
|
||
export const isOdd = (nbr: number) => !isEven(nbr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
interface StepHasGoal { | ||
smallerJugContent: number; | ||
largerJugContent: number; | ||
desiredAmount: number; | ||
} | ||
|
||
export const stepHasGoal = ({ | ||
smallerJugContent, | ||
largerJugContent, | ||
desiredAmount, | ||
}: StepHasGoal): boolean => | ||
[smallerJugContent, largerJugContent].includes(desiredAmount) || | ||
smallerJugContent + largerJugContent === desiredAmount; |
Oops, something went wrong.