diff options
Diffstat (limited to 'two')
| -rw-r--r-- | two/+test.ha | 8 | ||||
| -rw-r--r-- | two/2.ha | 62 | ||||
| -rw-r--r-- | two/in | 6 |
3 files changed, 76 insertions, 0 deletions
diff --git a/two/+test.ha b/two/+test.ha new file mode 100644 index 0000000..8f61645 --- /dev/null +++ b/two/+test.ha @@ -0,0 +1,8 @@ +use os; +use io; + +@test fn testtwo() void = { + const f = os::open("two/in")!; + defer io::close(f)!; + assert(do(f) == 2); +}; diff --git a/two/2.ha b/two/2.ha new file mode 100644 index 0000000..aea9ea8 --- /dev/null +++ b/two/2.ha @@ -0,0 +1,62 @@ +use fmt; +use bufio; +use os; +use strings; +use strconv; +use io; + +export fn main() void = { + do(os::stdin); +}; + +fn do(h: io::handle) int = { + const sc = bufio::newscanner(h); + defer bufio::finish(&sc); + + let safereports = 0; + for (const line => bufio::scan_line(&sc)!) { + const spl = strings::split(line, " "); + defer free(spl); + let safe = true; + let inc: (bool | void) = void; + for (let i = 1z; i < len(spl); i += 1) { + const s1 = strconv::stoi(spl[i-1])!; + const s2 = strconv::stoi(spl[i])!; + const diff = if (s1 < s2) { + match (inc) { + case void => + inc = true; + case let inc: bool => + if (!inc) { + safe = false; + break; + }; + }; + yield s2 - s1; + } else { + match (inc) { + case void => + inc = false; + case let inc: bool => + if (inc) { + safe = false; + break; + }; + }; + yield s1 - s2; + }; + if (diff <= 0 || diff > 3) { + safe = false; + break; + }; + }; + if (safe) { + fmt::printfln("{}: Safe", line)!; + safereports += 1; + } else { + fmt::printfln("{}: Unsafe", line)!; + }; + }; + fmt::printfln("{} reports are safe", safereports)!; + return safereports; +}; @@ -0,0 +1,6 @@ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 |
