-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.nim
37 lines (31 loc) · 897 Bytes
/
day02.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
import strutils
let input = readFile("./inputs/02.txt").splitLines
func first(input: seq[string]): int =
var twice, thrice: int
for line in input:
var hasPairs, hasTriplets: bool
for c in line:
case line.count c
of 2: hasPairs = true
of 3: hasTriplets = true
else: discard
if hasPairs: inc twice
if hasTriplets: inc thrice
result = twice * thrice
func sameLetters(line1, line2: string): string =
for c in 0 ..< line1.len:
if line1[c] == line2[c]:
result.add line1[c]
func second(input: seq[string]): string =
let l = input[0].len
var found: bool
for i, line1 in input:
for line2 in input[i+1 .. input.high]:
for c in 0 ..< l:
if line1[c] != line2[c]:
found = not found
if not found: break
if found:
return sameLetters(line1, line2)
echo first(input)
echo second(input)