summaryrefslogtreecommitdiff
path: root/one/1.ha
blob: 72a1e1a87cd28929855dfc7e9b1221ced22c1489 (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
54
55
use fmt;
use bufio;
use os;
use shlex;
use strings;
use strconv;
use sort;
use sort::cmp;
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 l1: []int = [];
	let l2: []int = [];
	defer free(l1);
	defer free(l2);
	for (const line => bufio::scan_line(&sc)!) {
		const tok = strings::tokenize(line, " ");
		let isfirst = true;
		for (const t => strings::next_token(&tok)) {
			if (t == "") {
				continue;
			};
			const x = strconv::stoi(t)!;
			if (isfirst) {
				append(l1, x);
				isfirst = false;
			} else {
				append(l2, x);
			};
		};
	};
	sort::sort(l1, size(int), &cmp::ints);
	sort::sort(l2, size(int), &cmp::ints);
	let sum = 0;
	for (let i = 0z; i < len(l1); i += 1) {
		const l = l1[i];
		const j = l2[i];
		const diff = if (l > j) {
			yield (l, j, l - j);
		} else {
			yield (j, l, j - l);
		};
		fmt::printfln("{} - {} = {}", diff.0, diff.1, diff.2)!;
		sum += diff.2;
	};
	fmt::println(sum)!;
	return sum;
};