Skip to content

Instantly share code, notes, and snippets.

@RoxasShadow
Last active December 5, 2017 16:47
Show Gist options
  • Save RoxasShadow/a68390e95b82e96043ce3e4d4f1373e0 to your computer and use it in GitHub Desktop.
Save RoxasShadow/a68390e95b82e96043ce3e4d4f1373e0 to your computer and use it in GitHub Desktop.
contract Day2 {
uint constant SPACE = 32;
uint constant NEW_LINE = 10;
bytes public b;
uint[] public stack;
function Day2() public {
string memory puzzle = "5 1 9 5\n7 5 3\n2 4 6 8";
b = bytes(puzzle);
}
function getAnswer() public view returns (uint answer) {
answer = 0;
uint min = 0;
uint max = 0;
for (uint i = 0; i < b.length; i++) {
uint v = uint(b[i]);
if (v == SPACE) { continue; }
if (v == NEW_LINE) {
answer += (max - min);
min = 0;
max = 0;
continue;
}
v -= 48;
if (min == 0 || v <= min) { min = v; }
if (max == 0 || v >= max) { max = v; }
}
return answer + (max - min);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment