blob: 9a30c91c0523e1bd04c51bffa63bf8d3eaa7ad92 (
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] = input.readFile().strip().split('\n').map(parseInt)
const preamble: int = 25
var encryption, weakness, smallest: int
for i in preamble ..< xmas.len:
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 ..< xmas.len:
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
|