Skip to content

Commit

Permalink
Day 1 optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
e00dan committed Jan 17, 2024
1 parent c0b16d5 commit 3b45a67
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
4 changes: 4 additions & 0 deletions inputs/01_demo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
22 changes: 9 additions & 13 deletions src/Day01_01.sol
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
// 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;
bool firstDigitSet;
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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Day02_01.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/LibBytesUint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
17 changes: 17 additions & 0 deletions test/Day01.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 3b45a67

Please sign in to comment.