-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.nim
56 lines (45 loc) · 1.29 KB
/
day9.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
44
45
46
47
48
49
50
51
52
53
54
55
56
import algorithm
import strutils
import sugar
# Read file
let inputFile = "resources/day9_input.txt"
let numbers = collect(newSeq):
for line in lines inputFile:
parseInt(line)
# Find if two numbers in the sequence can be added to get a target number
proc findSum(sequence: seq[int], target: int): bool =
for i in 1 .. sequence.len - 1:
let matchingNumber = target - sequence[i - 1]
if matchingNumber in sequence[i .. ^1]:
return true
return false
# Find the first number without the property
var number = 0
var index = 0
for i in 25 .. numbers.len:
if not findSum(numbers[i - 25 .. i - 1], numbers[i]):
number = numbers[i]
index = i
break
# Print results
echo "--- Part 1 Report ---"
echo "Number = " & $number
## Part 2
# Find number set that adds to the target number
let reversedNumbers = reversed(numbers[0 .. index - 1])
var slice = (0, 0)
block outer:
for i in 0 .. index:
var sum = 0
for j in i .. index:
sum += reversedNumbers[j]
if sum > number:
break
if sum == number:
slice = (i, j)
break outer
var minNumber = min(reversedNumbers[slice[0] .. slice[1]])
var maxNumber = max(reversedNumbers[slice[0] .. slice[1]])
# Print results
echo "--- Part 2 Report ---"
echo "Min + Max = " & $(minNumber + maxNumber)