blob: 8b15391c64fff7cd41bc21e9a1636465a285a89e (
plain) (
blame)
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
|
# Day Nine: Encoding Error
import os, strutils, sequtils
let input: string = paramStr(1)
let xmas: seq[int] = map(split(strip(readFile(input)), '\n'), parseInt)
const preamble: int = 25
var encryption, weakness, smallest: int
for i in preamble ..< len(xmas):
var valid: bool
for j in 0 .. preamble:
for k in 0 .. preamble:
if xmas[i - k] + xmas[i - j] == xmas[i]:
valid = true
if not valid:
encryption = xmas[i]
for largest in 0 ..< len(xmas):
var sum: int = 0
for val in smallest .. largest:
inc(sum, xmas[val])
while sum > encryption:
dec(sum, xmas[smallest])
inc(smallest)
if sum == encryption:
weakness = xmas[smallest] + xmas[largest]
break
echo encryption
echo weakness
|