-
Notifications
You must be signed in to change notification settings - Fork 3
/
day10.nim
43 lines (33 loc) · 1.07 KB
/
day10.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
import strutils, sequtils
const res = toSeq(0..255)
func knotLogic(sizes: seq[int], iterations = 64): seq[int] =
result = res
const size = 255
var pos, skip, a, b: int
for i in 1 .. iterations:
for groupSize in sizes:
for p in 0 ..< (groupSize div 2):
a = (pos + p) and size
b = (pos + groupSize-1 - p) and size
swap(result[a], result[b])
pos = (pos + groupSize + skip) and size
inc skip
func knotHashing*(word: string, binOut = false): string =
let
asciiSizes = word.mapIt(ord it).concat(@[17, 31, 73, 47, 23])
numbers = knotLogic(asciiSizes)
for bl in 0 .. 15:
var hashed: int
for i in 0 .. 15:
hashed = hashed.xor(numbers[16*bl + i])
let hashOut = if binOut: hashed.toBin(8) else: hashed.toHex(2)
result.add(hashOut)
when isMainModule:
let
inputString = readFile("./inputs/10.txt")
intSizes = inputString.split(',').map(parseInt)
firstHash = knotLogic(intSizes, 1)
first = firstHash[0] * firstHash[1]
second = knotHashing(inputString).toLowerAscii()
echo first
echo second