-
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
Showing
4 changed files
with
135 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time: 48 93 84 66 | ||
Distance: 261 1192 1019 1063 |
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,2 @@ | ||
Time: 7 15 30 | ||
Distance: 9 40 200 |
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,84 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.13; | ||
|
||
import {LibString} from "solady/src/utils/LibString.sol"; | ||
import {LibBytesUint} from "./LibBytesUint.sol"; | ||
|
||
struct Race { | ||
uint256 time; | ||
uint256 distance; | ||
} | ||
|
||
contract Day06_01 { | ||
Race[] public races; | ||
uint256 lineIndex; | ||
|
||
function splitWhitespace(string calldata subject) internal pure returns (string[] memory result) { | ||
uint256[] memory indices = LibString.indicesOf(subject, " "); | ||
|
||
result = new string[](10); | ||
uint256 nonConsecutiveIndicesStringsLength = 0; | ||
uint256 previousIndice = 9999; | ||
uint256 lastUsedIndice = 0; | ||
uint256 startingStreakIndice = 0; | ||
|
||
for (uint256 i = 0; i < indices.length; i++) { | ||
if (previousIndice + 1 != indices[i]) { | ||
uint256 startSliceIndex = startingStreakIndice == 0 ? 0 : startingStreakIndice + 1; | ||
|
||
result[nonConsecutiveIndicesStringsLength] = subject[startSliceIndex:indices[i]]; | ||
nonConsecutiveIndicesStringsLength++; | ||
|
||
lastUsedIndice = indices[i]; | ||
} else if (i + 1 == indices.length) { | ||
result[nonConsecutiveIndicesStringsLength] = subject[indices[i] + 1:bytes(subject).length]; | ||
nonConsecutiveIndicesStringsLength++; | ||
} else { | ||
startingStreakIndice = indices[i]; | ||
} | ||
|
||
previousIndice = indices[i]; | ||
} | ||
} | ||
|
||
function parseLine(string calldata line) public virtual returns (uint256 result) { | ||
string[] memory splitByWhitespace = splitWhitespace(line); | ||
|
||
for (uint256 i = 1; i < splitByWhitespace.length; i++) { | ||
if (bytes(splitByWhitespace[i]).length == 0) { | ||
break; | ||
} | ||
|
||
if (lineIndex == 0) { | ||
// time | ||
races.push(Race(LibBytesUint.bytesToUint(bytes(splitByWhitespace[i])), 0)); | ||
} else { | ||
// distance | ||
Race storage _race = races[i - 1]; | ||
_race.distance = LibBytesUint.bytesToUint(bytes(splitByWhitespace[i])); | ||
} | ||
} | ||
|
||
lineIndex++; | ||
|
||
if (lineIndex == 2) { | ||
result = 1; | ||
|
||
for (uint256 i = 0; i < races.length; i++) { | ||
uint256 waysToWin = 0; | ||
|
||
for (uint256 j = 0; j < races[i].time; j++) { | ||
uint256 distanceCurrent = (races[i].time - j) * j; | ||
|
||
if (distanceCurrent > races[i].distance) { | ||
waysToWin++; | ||
} | ||
} | ||
|
||
result *= waysToWin; | ||
} | ||
} else { | ||
return 0; | ||
} | ||
} | ||
} |
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,47 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.13; | ||
|
||
import {Test, console2} from "forge-std/Test.sol"; | ||
import {Day06_01} from "../src/Day06_01.sol"; | ||
|
||
contract Day04Test is Test { | ||
Day06_01 public day06_01; | ||
|
||
function setUp() public { | ||
day06_01 = new Day06_01(); | ||
} | ||
|
||
function test_day06_01_demo() public { | ||
uint256 result = 0; | ||
|
||
while (true) { | ||
string memory line = vm.readLine("./inputs/06_demo.txt"); | ||
if (bytes(line).length == 0) { | ||
break; | ||
} | ||
|
||
uint256 line_result = day06_01.parseLine(line); | ||
result += line_result; | ||
} | ||
|
||
console2.log("Day 6_1 (demo): ", result); | ||
assertEq(result, 288); | ||
} | ||
|
||
function test_day06_01() public { | ||
uint256 result = 0; | ||
|
||
while (true) { | ||
string memory line = vm.readLine("./inputs/06.txt"); | ||
if (bytes(line).length == 0) { | ||
break; | ||
} | ||
|
||
uint256 line_result = day06_01.parseLine(line); | ||
result += line_result; | ||
} | ||
|
||
console2.log("Day 6_1: ", result); | ||
assertEq(result, 1312850); | ||
} | ||
} |