forked from narimiran/AdventOfCode2017
-
Notifications
You must be signed in to change notification settings - Fork 1
/
day02.nim
105 lines (92 loc) · 1.86 KB
/
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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import tables, strutils
var counter = initCountTable[char]()
var test = "abc"
echo counter
for c in test:
echo c
counter.inc(c)
echo counter
echo counter
proc countChars(text : string) : CountTable[char] =
echo result
for c in text:
echo c
result.inc(c)
echo result
# this throws an error and I cannot understand why!
# echo countChars(test)
#[ output:
{:}
a
{'a': 1}
b
{'a': 1, 'b': 1}
c
{'a': 1, 'b': 1, 'c': 1}
{'a': 1, 'b': 1, 'c': 1}
{:}
a
day02.nim(22) day02
day02.nim(19) countChars
tables.nim(1004) inc
system.nim(414) rawGet
system.nim(2830) sysFatal
Error: unhandled exception: index out of bounds [IndexError]
Error: execution of an external program failed: 'C:\Users\ppeterlongo\Documents\nim\AdventOfCode2018\nim\day02.exe '
]#
const ids = readFile("./inputs/02.txt").strip().splitLines()
echo ids[0]
# echo countChars(ids[0])
var twos = 0
var threes = 0
var hasTwo : bool
var hasThree : bool
echo len ids
for id in ids:
clear(counter)
# echo id
for c in id:
counter.inc(c)
# echo counter
hasTwo = false
hasThree = false
for k, v in counter:
if v == 2:
hasTwo = true
elif v == 3:
hasThree = true
if hasTwo:
twos += 1
if hasThree:
threes += 1
echo twos
echo threes
echo twos*threes
# second part
# distance bewteen ids
var id1 : string
var id2 : string
var dist : int
id1 = "abcde"
id2 = "axcye"
dist = 0
for i in 0..<id1.len:
if id1[i] != id2[i]:
dist += 1
echo dist
proc distance(a, b: string): int =
for i in 0..<a.len:
if a[i] != b[i]:
inc result
echo distance("abcde", "axcye")
# cycle over all pairs of ids
for i in 0..<(ids.len - 1):
for j in (i+1)..<ids.len:
dist = distance(ids[i], ids[j])
if dist == 1:
id1 = ids[i]
id2 = ids[j]
break
if dist == 1: break
echo id1
echo id2