From e5a898873c0cd571af23834a1456fc621f944686 Mon Sep 17 00:00:00 2001 From: Daniel Kmak <4950658+Kuzirashi@users.noreply.github.com> Date: Thu, 1 Feb 2024 18:53:24 +0100 Subject: [PATCH] Day 6 part 1 --- inputs/06.txt | 2 ++ inputs/06_demo.txt | 2 ++ src/Day06_01.sol | 84 ++++++++++++++++++++++++++++++++++++++++++++++ test/Day06.sol | 47 ++++++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 inputs/06.txt create mode 100644 inputs/06_demo.txt create mode 100644 src/Day06_01.sol create mode 100644 test/Day06.sol diff --git a/inputs/06.txt b/inputs/06.txt new file mode 100644 index 0000000..2636b6c --- /dev/null +++ b/inputs/06.txt @@ -0,0 +1,2 @@ +Time: 48 93 84 66 +Distance: 261 1192 1019 1063 \ No newline at end of file diff --git a/inputs/06_demo.txt b/inputs/06_demo.txt new file mode 100644 index 0000000..b39f49d --- /dev/null +++ b/inputs/06_demo.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/src/Day06_01.sol b/src/Day06_01.sol new file mode 100644 index 0000000..e09af53 --- /dev/null +++ b/src/Day06_01.sol @@ -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; + } + } +} diff --git a/test/Day06.sol b/test/Day06.sol new file mode 100644 index 0000000..2bc9d1b --- /dev/null +++ b/test/Day06.sol @@ -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); + } +}