-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.nim
35 lines (27 loc) · 875 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
import strutils
let input = readFile("./inputs/05.txt")
func reactions(polymer: string): string =
# Since lower and upper letters are 32 (2^5) characters apart, we can use
# `a xor b`, which is faster than `abs(a - b)`.
result = newString(12_000) # manually tweaked
var pos: int
for unit in polymer:
if pos > 0 and (ord(unit) xor ord(result[pos-1])) == 32:
dec pos
else:
result[pos] = unit
inc pos
result.setLen(pos)
func shortest(polymer: string): int =
result = int.high
var
improvedPolymer: string
polymerSize: int
for unit in 'a' .. 'z':
improvedPolymer = polymer.replace($unit).replace($unit.toUpperAscii)
polymerSize = improvedPolymer.reactions.len
if polymerSize < result:
result = polymerSize
let resultingPolymer = input.reactions
echo resultingPolymer.len
echo resultingPolymer.shortest