blob: d05ff253ffbaf7b1b63f6fddf76d99e9cf6e0264 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
use fmt;
use io;
use math;
use os;
use strconv::{stou8};
use strings::{fromutf8_unsafe, split, sub};
fn joltage(bank: str) u64 = {
let digits: [12]u8 = [0...];
let indexes: [12]size = [0...];
let total: u64 = 0;
for (let i: size = 0; i < 12; i += 1) {
let next_index: size = 0;
if (i > 0)
next_index = indexes[i - 1] + 1;
if (i > next_index)
next_index = i;
for (let j: size = next_index; j < len(bank); j += 1) {
let val = stou8(sub(bank, j, j + 1))!;
if (val > digits[i] && j < len(bank) - (11 - i)) {
digits[i] = val;
indexes[i] = j;
};
};
total += (digits[i]: u64 * math::powi64(10, 11 - i: uint): u64);
};
return total;
};
export fn main() void = {
let handle = os::open("input.txt")!;
defer io::close(handle)!;
let buf = io::drain(handle)!;
defer free(buf);
let lines = split(fromutf8_unsafe(buf), "\n")!;
let output: u64 = 0;
for (let line .. lines)
if (line != "")
output += joltage(line);
fmt::printfln("Answer: {}", output)!;
};
|