-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday05.nim
44 lines (37 loc) · 977 Bytes
/
day05.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
import strutils
const
instructions = readFile("./inputs/05.txt").strip.splitLines
vowels = {'a', 'e', 'i', 'o', 'u'}
naughty = ["ab", "cd", "pq", "xy"]
func firstPart(): int =
for line in instructions:
var
nrVowels: int
nrDoubles: int
block inner:
if line[0] in vowels: inc nrVowels
for i in 1 .. line.high:
if line[i] in vowels:
inc nrVowels
if line[i-1] == line[i]:
inc nrDoubles
if line[i-1 .. i] in naughty:
break inner
if nrVowels > 2 and nrDoubles > 0:
inc result
func secondPart(): int =
for line in instructions:
var
pair: bool
repeated: bool
for i in 0 .. line.high-2:
if line[i+2 .. line.high].contains(line[i..i+1]):
pair = true
break
for i in 0 .. line.high-2:
if line[i] == line[i+2]:
repeated = true
break
if pair and repeated: inc result
echo firstPart()
echo secondPart()