Last active
December 5, 2017 16:47
-
-
Save RoxasShadow/a68390e95b82e96043ce3e4d4f1373e0 to your computer and use it in GitHub Desktop.
http://adventofcode.com/2017/day/2 - Solidity - Demo: https://remix.ethereum.org/#version=soljson-v0.4.20-nightly.2017.12.4+commit.240c79e6.js
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