36 lines
738 B
Nim
36 lines
738 B
Nim
import std/[sets, strformat]
|
|
import lib/loader
|
|
|
|
|
|
let data = loadInts(1).toHashSet()
|
|
|
|
|
|
iterator sumPairs(sum: int): (int, int) =
|
|
for i in 0..(sum div 2):
|
|
yield (i, sum - i)
|
|
|
|
|
|
iterator sumTriples(sum: int): (int, int, int) =
|
|
for x in 0..(sum div 3):
|
|
for y, z in sumPairs(sum - x):
|
|
yield (x, y, z)
|
|
|
|
|
|
proc partOne() =
|
|
for x, y in sumPairs(2020):
|
|
if data.contains(x) and data.contains(y):
|
|
echo fmt"{x} * {y} = {x * y}"
|
|
break
|
|
|
|
|
|
proc partTwo() =
|
|
for x, y, z in sumTriples(2020):
|
|
if data.contains(x) and data.contains(y) and data.contains(z):
|
|
echo fmt"{x} * {y} * {z} = {x * y * z}"
|
|
break
|
|
|
|
|
|
when isMainModule:
|
|
partOne()
|
|
partTwo()
|