From 3b45a67fc205dd95556cf2e7faee70cee1c0f8ae Mon Sep 17 00:00:00 2001 From: Daniel Kmak <4950658+Kuzirashi@users.noreply.github.com> Date: Wed, 17 Jan 2024 19:01:29 +0100 Subject: [PATCH] Day 1 optimizations --- inputs/01_demo.txt | 4 ++++ src/Day01_01.sol | 22 +++++++++------------- src/Day02_01.sol | 2 +- src/LibBytesUint.sol | 10 ++++++++++ test/Day01.sol | 17 +++++++++++++++++ 5 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 inputs/01_demo.txt diff --git a/inputs/01_demo.txt b/inputs/01_demo.txt new file mode 100644 index 0000000..1ba8437 --- /dev/null +++ b/inputs/01_demo.txt @@ -0,0 +1,4 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet \ No newline at end of file diff --git a/src/Day01_01.sol b/src/Day01_01.sol index 93033d1..1605949 100644 --- a/src/Day01_01.sol +++ b/src/Day01_01.sol @@ -1,10 +1,9 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; -contract Day01_01 { - bytes constant DIGITS = "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39"; - uint8 constant DIGITS_LENGTH = 10; +import {LibBytesUint} from "./LibBytesUint.sol"; +contract Day01_01 { function parseLine(string memory line) public view virtual returns (uint256) { bytes memory lineBytes = bytes(line); uint8 firstDigit; @@ -12,17 +11,14 @@ contract Day01_01 { uint8 lastDigit; for (uint256 index; index < lineBytes.length; index++) { - for (uint256 index_digits; index_digits < DIGITS_LENGTH; index_digits++) { - if (DIGITS[index_digits] == lineBytes[index]) { - uint8 digit = uint8(bytes1(lineBytes[index])) - 48; - if (!firstDigitSet) { - firstDigit = digit; - firstDigitSet = true; - } - - lastDigit = digit; - break; + if (LibBytesUint.isDigit(lineBytes[index])) { + uint8 digit = uint8(bytes1(lineBytes[index])) - 48; + if (!firstDigitSet) { + firstDigit = digit; + firstDigitSet = true; } + + lastDigit = digit; } } diff --git a/src/Day02_01.sol b/src/Day02_01.sol index 19cab22..6f10b7c 100644 --- a/src/Day02_01.sol +++ b/src/Day02_01.sol @@ -10,7 +10,7 @@ contract Day02_01 { uint8 constant MAXIMUM_ALLOWED_BLUE = 14; function parseLine(string calldata line) public view virtual returns (uint256) { - uint256 indexOfColon = LibString.indexOf(line, ":", 0); + uint256 indexOfColon = LibString.indexOf(line, ":", 6); string[] memory gameDataSplit = LibString.split(string(bytes(line[indexOfColon:])), ";"); uint256 maxRed; diff --git a/src/LibBytesUint.sol b/src/LibBytesUint.sol index bbc3bd3..505eac8 100644 --- a/src/LibBytesUint.sol +++ b/src/LibBytesUint.sol @@ -12,4 +12,14 @@ library LibBytesUint { } return result; } + + function isDigit(bytes1 singleByte) public pure returns (bool) { + for (uint256 i = 48; i < 58; i++) { + if (uint256(uint8(singleByte)) == i) { + return true; + } + } + + return false; + } } diff --git a/test/Day01.sol b/test/Day01.sol index 9d40b70..b80fee1 100644 --- a/test/Day01.sol +++ b/test/Day01.sol @@ -14,6 +14,23 @@ contract Day01Test is Test { day01_02 = new Day01_02(); } + function test_day01_01_demo() public { + uint256 total_calibration_value = 0; + + while (true) { + string memory line = vm.readLine("./inputs/01_demo.txt"); + if (bytes(line).length == 0) { + break; + } + + uint256 line_calibration_value = day01_01.parseLine(line); + total_calibration_value += line_calibration_value; + } + + console2.log("Day 1_1 (demo): ", total_calibration_value); + assertEq(total_calibration_value, 142); + } + function test_day01_01() public { uint256 total_calibration_value = 0;