aboutsummaryrefslogtreecommitdiff
path: root/2020/ten.nim
blob: 340d9f94515ddb1f8c6c00a4f7c55df03d0be282 (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
30
31
32
33
# Day Ten: Adapter Array
import os, strutils, sequtils, algorithm, math

let input: string = paramStr(1)
var adapters: seq[int] = input.readFile().strip().split('\n').map(parseInt)
adapters.add(0)
adapters = adapters.sorted()
adapters.add(adapters[^1] + 3)

var jolt, one, three: int = 0
for adapter in adapters:
  case adapter - jolt
  of 1:
    inc(one)
  of 3:
    inc(three)
  else:
    discard
  jolt = adapter

echo one * three

var paths: int = 0
proc recursive(adapters: seq[int]) =
  for i in 0 .. (len(adapters) - 4):
    if adapters[i+2] - adapters[i] <= 3:
      recursive(adapters[i+2..^1])
    if adapters[i+3] - adapters[i] <= 3:
      recursive(adapters[i+3..^1])
  inc(paths)

recursive(adapters)
echo paths