Created
December 5, 2017 16:47
-
-
Save RoxasShadow/0b1f5fd4d3385d8f588148e9b71780b8 to your computer and use it in GitHub Desktop.
http://adventofcode.com/2017/day/2 - Solidity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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