-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.nim
58 lines (46 loc) Β· 1.31 KB
/
part2.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
import strutils
proc hash(str: string): int =
var value = 0
for ch in str:
value = (value + int(ch)) * 17 mod 256
return value
type
Elem = object
key: string
lens: int
HashMap = array[256, seq[Elem]]
var map: HashMap
## Initialize map
for i in 0 .. map.len - 1:
map[i] = newSeq[Elem]()
proc mapFindInBox(map: var HashMap, box: int, key: string): int =
for i in 0 .. map[box].len - 1:
if key == map[box][i].key:
return i
return -1
proc mapPerformOpDash(map: var HashMap, key: string) =
var
box = hash(key)
pos = mapFindInBox(map, box, key)
if pos >= 0:
map[box].delete(pos)
proc mapPerformOpEquals(map: var HashMap, key: string, lens: int) =
var
box = hash(key)
pos = mapFindInBox(map, box, key)
if pos >= 0:
map[box][pos].lens = lens
else:
map[box].add(Elem(key: key, lens: lens))
for line in lines "input.txt":
for op in line.split(","):
if op[^1] == '-':
mapPerformOpDash(map, op[0..^2])
else:
var parts = op.split("=")
mapPerformOpEquals(map, parts[0], parts[1].parseInt())
var sum = 0
for box in 0 .. map.len - 1:
for pos in 0 .. map[box].len - 1:
sum += (box + 1) * (pos + 1) * map[box][pos].lens
echo sum