Added initial attempt for "Day 12: Hot Springs", part 2 including test cases
This commit is contained in:
		
							parent
							
								
									c84d3e6a2d
								
							
						
					
					
						commit
						c0ee7894ae
					
				| @ -30,6 +30,7 @@ const | |||||||
|   CWildcardChar = '?'; |   CWildcardChar = '?'; | ||||||
|   COperationalPatternChars = [COperationalChar, CWildcardChar]; |   COperationalPatternChars = [COperationalChar, CWildcardChar]; | ||||||
|   CDamagedPatternChars = [CDamagedChar, CWildcardChar]; |   CDamagedPatternChars = [CDamagedChar, CWildcardChar]; | ||||||
|  |   CPart2Repetition = 4; | ||||||
| 
 | 
 | ||||||
| type | type | ||||||
| 
 | 
 | ||||||
| @ -40,7 +41,7 @@ type | |||||||
|     FValidation: specialize TList<Integer>; |     FValidation: specialize TList<Integer>; | ||||||
|     FSpringPattern: string; |     FSpringPattern: string; | ||||||
|     procedure ExtendArrangement(const AArrangement: string; const ARemainingFreeOperationalCount, ACurrentValidationIndex: |     procedure ExtendArrangement(const AArrangement: string; const ARemainingFreeOperationalCount, ACurrentValidationIndex: | ||||||
|       Integer); |       Integer; var AArrangementCount: Int64); | ||||||
|     function TryAppendOperationalChar(var AArrangement: string): Boolean; |     function TryAppendOperationalChar(var AArrangement: string): Boolean; | ||||||
|     function TryAppendValidationBlock(var AArrangement: string; const ALength: Integer): Boolean; |     function TryAppendValidationBlock(var AArrangement: string; const ALength: Integer): Boolean; | ||||||
|   public |   public | ||||||
| @ -57,19 +58,19 @@ implementation | |||||||
| { THotSprings } | { THotSprings } | ||||||
| 
 | 
 | ||||||
| procedure THotSprings.ExtendArrangement(const AArrangement: string; const ARemainingFreeOperationalCount, | procedure THotSprings.ExtendArrangement(const AArrangement: string; const ARemainingFreeOperationalCount, | ||||||
|   ACurrentValidationIndex: Integer); |   ACurrentValidationIndex: Integer; var AArrangementCount: Int64); | ||||||
| var | var | ||||||
|   match: Boolean; |   match: Boolean; | ||||||
|   temp: string; |   temp: string; | ||||||
| begin | begin | ||||||
|   if Length(AArrangement) = Length(FSpringPattern) then |   if Length(AArrangement) = Length(FSpringPattern) then | ||||||
|     Inc(FPart1) |     Inc(AArrangementCount) | ||||||
|   else begin |   else begin | ||||||
|     temp := AArrangement; |     temp := AArrangement; | ||||||
|     // Tries to append a dot (operational) to the current arrangement. |     // Tries to append a dot (operational) to the current arrangement. | ||||||
|     if (ARemainingFreeOperationalCount > 0) and TryAppendOperationalChar(temp) then |     if (ARemainingFreeOperationalCount > 0) and TryAppendOperationalChar(temp) then | ||||||
|     begin |     begin | ||||||
|       ExtendArrangement(temp, ARemainingFreeOperationalCount - 1, ACurrentValidationIndex); |       ExtendArrangement(temp, ARemainingFreeOperationalCount - 1, ACurrentValidationIndex, AArrangementCount); | ||||||
|     end; |     end; | ||||||
| 
 | 
 | ||||||
|     // Tries to append the current validation block (damaged) to the current arrangement. |     // Tries to append the current validation block (damaged) to the current arrangement. | ||||||
| @ -85,7 +86,7 @@ begin | |||||||
|         match := False; |         match := False; | ||||||
| 
 | 
 | ||||||
|       if match then |       if match then | ||||||
|         ExtendArrangement(temp, ARemainingFreeOperationalCount, ACurrentValidationIndex + 1); |         ExtendArrangement(temp, ARemainingFreeOperationalCount, ACurrentValidationIndex + 1, AArrangementCount); | ||||||
|     end; |     end; | ||||||
|   end; |   end; | ||||||
| end; | end; | ||||||
| @ -132,7 +133,7 @@ end; | |||||||
| procedure THotSprings.ProcessDataLine(const ALine: string); | procedure THotSprings.ProcessDataLine(const ALine: string); | ||||||
| var | var | ||||||
|   split: TStringArray; |   split: TStringArray; | ||||||
|   i, val, maxFreeOperationalCount: Integer; |   i, j, val, maxFreeOperationalCount: Integer; | ||||||
| begin | begin | ||||||
|   FValidation.Clear; |   FValidation.Clear; | ||||||
|   split := ALine.Split([' ', ',']); |   split := ALine.Split([' ', ',']); | ||||||
| @ -146,7 +147,19 @@ begin | |||||||
|     Dec(maxFreeOperationalCount, val); |     Dec(maxFreeOperationalCount, val); | ||||||
|   end; |   end; | ||||||
| 
 | 
 | ||||||
|   ExtendArrangement('', maxFreeOperationalCount, 0); |   ExtendArrangement('', maxFreeOperationalCount, 0, FPart1); | ||||||
|  |   WriteLn('Part 1: ', FPart1); | ||||||
|  | 
 | ||||||
|  |   for i := 1 to CPart2Repetition do | ||||||
|  |   begin | ||||||
|  |     FSpringPattern := FSpringPattern + CWildcardChar + split[0]; | ||||||
|  |     for j := 0 to Length(split) - 2 do | ||||||
|  |       FValidation.Add(FValidation[j]); | ||||||
|  |   end; | ||||||
|  |   maxFreeOperationalCount := (CPart2Repetition + 1) * maxFreeOperationalCount; | ||||||
|  | 
 | ||||||
|  |   ExtendArrangement('', maxFreeOperationalCount, 0, FPart2); | ||||||
|  |   WriteLn('Part 2: ', FPart2); | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| procedure THotSprings.Finish; | procedure THotSprings.Finish; | ||||||
|  | |||||||
| @ -26,6 +26,16 @@ uses | |||||||
| 
 | 
 | ||||||
| type | type | ||||||
| 
 | 
 | ||||||
|  |   { THotSpringsFullDataTestCase } | ||||||
|  | 
 | ||||||
|  |   THotSpringsFullDataTestCase = class(TEngineBaseTest) | ||||||
|  |   protected | ||||||
|  |     function CreateSolver: ISolver; override; | ||||||
|  |   published | ||||||
|  |     procedure TestPart1; | ||||||
|  |     procedure TestPart2; | ||||||
|  |   end; | ||||||
|  | 
 | ||||||
|   { THotSpringsExampleTestCase } |   { THotSpringsExampleTestCase } | ||||||
| 
 | 
 | ||||||
|   THotSpringsExampleTestCase = class(TExampleEngineBaseTest) |   THotSpringsExampleTestCase = class(TExampleEngineBaseTest) | ||||||
| @ -33,6 +43,7 @@ type | |||||||
|     function CreateSolver: ISolver; override; |     function CreateSolver: ISolver; override; | ||||||
|   published |   published | ||||||
|     procedure TestPart1; |     procedure TestPart1; | ||||||
|  |     procedure TestPart2; | ||||||
|   end; |   end; | ||||||
| 
 | 
 | ||||||
|   { THotSpringsTestCase } |   { THotSpringsTestCase } | ||||||
| @ -40,18 +51,29 @@ type | |||||||
|   THotSpringsTestCase = class(TSolverTestCase) |   THotSpringsTestCase = class(TSolverTestCase) | ||||||
|   protected |   protected | ||||||
|     function CreateSolver: ISolver; override; |     function CreateSolver: ISolver; override; | ||||||
|     procedure TestSingleLine(const ALine: string; const AValue: Integer); |     procedure TestSingleLine(const ALine: string); | ||||||
|   published |   published | ||||||
|     procedure TestExampleLine1; |     procedure TestExampleLine1Part1; | ||||||
|     procedure TestExampleLine2; |     procedure TestExampleLine2Part1; | ||||||
|     procedure TestExampleLine3; |     procedure TestExampleLine3Part1; | ||||||
|     procedure TestExampleLine4; |     procedure TestExampleLine4Part1; | ||||||
|     procedure TestExampleLine5; |     procedure TestExampleLine5Part1; | ||||||
|     procedure TestExampleLine6; |     procedure TestExampleLine6Part1; | ||||||
|  |     procedure TestExampleLine1Part2; | ||||||
|  |     procedure TestExampleLine2Part2; | ||||||
|  |     procedure TestExampleLine3Part2; | ||||||
|  |     procedure TestExampleLine4Part2; | ||||||
|  |     procedure TestExampleLine5Part2; | ||||||
|  |     procedure TestExampleLine6Part2; | ||||||
|   end; |   end; | ||||||
| 
 | 
 | ||||||
| implementation | implementation | ||||||
| 
 | 
 | ||||||
|  | procedure THotSpringsFullDataTestCase.TestPart2; | ||||||
|  | begin | ||||||
|  |   AssertEquals(-1, FSolver.GetResultPart2); | ||||||
|  | end; | ||||||
|  | 
 | ||||||
| { THotSpringsExampleTestCase } | { THotSpringsExampleTestCase } | ||||||
| 
 | 
 | ||||||
| function THotSpringsExampleTestCase.CreateSolver: ISolver; | function THotSpringsExampleTestCase.CreateSolver: ISolver; | ||||||
| @ -64,6 +86,11 @@ begin | |||||||
|   AssertEquals(21, FSolver.GetResultPart1); |   AssertEquals(21, FSolver.GetResultPart1); | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
|  | procedure THotSpringsExampleTestCase.TestPart2; | ||||||
|  | begin | ||||||
|  |   AssertEquals(525152, FSolver.GetResultPart2); | ||||||
|  | end; | ||||||
|  | 
 | ||||||
| { THotSpringsTestCase } | { THotSpringsTestCase } | ||||||
| 
 | 
 | ||||||
| function THotSpringsTestCase.CreateSolver: ISolver; | function THotSpringsTestCase.CreateSolver: ISolver; | ||||||
| @ -71,42 +98,83 @@ begin | |||||||
|   Result := THotSprings.Create; |   Result := THotSprings.Create; | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| procedure THotSpringsTestCase.TestSingleLine(const ALine: string; const AValue: Integer); | procedure THotSpringsTestCase.TestSingleLine(const ALine: string); | ||||||
| begin | begin | ||||||
|   FSolver.Init; |   FSolver.Init; | ||||||
|   FSolver.ProcessDataLine(ALine); |   FSolver.ProcessDataLine(ALine); | ||||||
|   FSolver.Finish; |   FSolver.Finish; | ||||||
|   AssertEquals(AValue, FSolver.GetResultPart1); |  | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| procedure THotSpringsTestCase.TestExampleLine1; | procedure THotSpringsTestCase.TestExampleLine1Part1; | ||||||
| begin | begin | ||||||
|   TestSingleLine('???.### 1,1,3', 1); |   TestSingleLine('???.### 1,1,3'); | ||||||
|  |   AssertEquals(1, FSolver.GetResultPart1); | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| procedure THotSpringsTestCase.TestExampleLine2; | procedure THotSpringsTestCase.TestExampleLine2Part1; | ||||||
| begin | begin | ||||||
|   TestSingleLine('.??..??...?##. 1,1,3', 4); |   TestSingleLine('.??..??...?##. 1,1,3'); | ||||||
|  |   AssertEquals(4, FSolver.GetResultPart1); | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| procedure THotSpringsTestCase.TestExampleLine3; | procedure THotSpringsTestCase.TestExampleLine3Part1; | ||||||
| begin | begin | ||||||
|   TestSingleLine('?#?#?#?#?#?#?#? 1,3,1,6', 1); |   TestSingleLine('?#?#?#?#?#?#?#? 1,3,1,6'); | ||||||
|  |   AssertEquals(1, FSolver.GetResultPart1); | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| procedure THotSpringsTestCase.TestExampleLine4; | procedure THotSpringsTestCase.TestExampleLine4Part1; | ||||||
| begin | begin | ||||||
|   TestSingleLine('????.#...#... 4,1,1', 1); |   TestSingleLine('????.#...#... 4,1,1'); | ||||||
|  |   AssertEquals(1, FSolver.GetResultPart1); | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| procedure THotSpringsTestCase.TestExampleLine5; | procedure THotSpringsTestCase.TestExampleLine5Part1; | ||||||
| begin | begin | ||||||
|   TestSingleLine('????.######..#####. 1,6,5', 4); |   TestSingleLine('????.######..#####. 1,6,5'); | ||||||
|  |   AssertEquals(4, FSolver.GetResultPart1); | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| procedure THotSpringsTestCase.TestExampleLine6; | procedure THotSpringsTestCase.TestExampleLine6Part1; | ||||||
| begin | begin | ||||||
|   TestSingleLine('?###???????? 3,2,1', 10); |   TestSingleLine('?###???????? 3,2,1'); | ||||||
|  |   AssertEquals(10, FSolver.GetResultPart1); | ||||||
|  | end; | ||||||
|  | 
 | ||||||
|  | procedure THotSpringsTestCase.TestExampleLine1Part2; | ||||||
|  | begin | ||||||
|  |   TestSingleLine('???.### 1,1,3'); | ||||||
|  |   AssertEquals(1, FSolver.GetResultPart2); | ||||||
|  | end; | ||||||
|  | 
 | ||||||
|  | procedure THotSpringsTestCase.TestExampleLine2Part2; | ||||||
|  | begin | ||||||
|  |   TestSingleLine('.??..??...?##. 1,1,3'); | ||||||
|  |   AssertEquals(16384, FSolver.GetResultPart2); | ||||||
|  | end; | ||||||
|  | 
 | ||||||
|  | procedure THotSpringsTestCase.TestExampleLine3Part2; | ||||||
|  | begin | ||||||
|  |   TestSingleLine('?#?#?#?#?#?#?#? 1,3,1,6'); | ||||||
|  |   AssertEquals(1, FSolver.GetResultPart2); | ||||||
|  | end; | ||||||
|  | 
 | ||||||
|  | procedure THotSpringsTestCase.TestExampleLine4Part2; | ||||||
|  | begin | ||||||
|  |   TestSingleLine('????.#...#... 4,1,1'); | ||||||
|  |   AssertEquals(16, FSolver.GetResultPart2); | ||||||
|  | end; | ||||||
|  | 
 | ||||||
|  | procedure THotSpringsTestCase.TestExampleLine5Part2; | ||||||
|  | begin | ||||||
|  |   TestSingleLine('????.######..#####. 1,6,5'); | ||||||
|  |   AssertEquals(2500, FSolver.GetResultPart2); | ||||||
|  | end; | ||||||
|  | 
 | ||||||
|  | procedure THotSpringsTestCase.TestExampleLine6Part2; | ||||||
|  | begin | ||||||
|  |   TestSingleLine('?###???????? 3,2,1'); | ||||||
|  |   AssertEquals(506250, FSolver.GetResultPart2); | ||||||
| end; | end; | ||||||
| 
 | 
 | ||||||
| initialization | initialization | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user