Skip to content

Commit

Permalink
Day 6 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
e00dan committed Feb 1, 2024
1 parent d2811a8 commit e5a8988
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inputs/06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 48 93 84 66
Distance: 261 1192 1019 1063
2 changes: 2 additions & 0 deletions inputs/06_demo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200
84 changes: 84 additions & 0 deletions src/Day06_01.sol
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;
}
}
}
47 changes: 47 additions & 0 deletions test/Day06.sol
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);
}
}

0 comments on commit e5a8988

Please sign in to comment.