-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
71 lines (59 loc) · 1.29 KB
/
solution.nim
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import std/[
algorithm,
bitops,
deques,
json,
math,
re,
sequtils,
sets,
strformat,
strutils,
tables,
sugar,
]
type
Range = (int, int)
proc parseLine(line: string): Range =
if line =~ re"(\d+)-(\d+)":
(matches[0].parseInt, matches[1].parseInt)
else:
raise newException(ValueError, "parse error: " & line)
when defined(test):
block:
doAssert parseLine("5-8") == (5, 8)
proc lowestAllowed(ranges: seq[Range]): int =
let ranges = ranges.sortedByIt(it[0])
var r = -1
for (a, b) in ranges:
if r + 1 < a: break
r = r.max b
r + 1
proc part1(input: string): int =
let ranges = input.split("\n").mapIt(it.parseLine)
lowestAllowed(ranges)
when defined(test):
let input = """
5-8
0-2
4-7
""".strip
block:
doAssert part1(input) == 3
proc countAllowed(ranges: seq[Range], hi: int): int =
let ranges = ranges.sortedByIt(it[0])
var r = -1
for (a, b) in ranges:
result += max(0, a - r - 1)
r = r.max b
result += max(0, hi - r)
proc part2(input: string, hi: int): int =
let ranges = input.split("\n").mapIt(it.parseLine)
countAllowed(ranges, hi)
when defined(test):
block:
doAssert part2(input, 9) == 2
when isMainModule and not defined(test):
let input = readAll(stdin).strip
echo part1(input)
echo part2(input, int32.high)