33 lines
720 B
Nim
33 lines
720 B
Nim
|
import std/[strutils, algorithm, sequtils, tables]
|
||
|
import aoc
|
||
|
|
||
|
proc parseInput(input: string): (seq[int], seq[int]) =
|
||
|
for line in input.splitLines():
|
||
|
let s = line.splitWhitespace()
|
||
|
assert s.len == 2
|
||
|
result[0].add parseInt(s[0])
|
||
|
result[1].add parseInt(s[1])
|
||
|
|
||
|
proc partOne*(input: string): int =
|
||
|
var ids = parseInput(input)
|
||
|
ids[0].sort
|
||
|
ids[1].sort
|
||
|
for (l, r) in zip(ids[0],ids[1]):
|
||
|
result += abs(l-r)
|
||
|
|
||
|
|
||
|
proc partTwo*(input: string): int =
|
||
|
var ids = parseInput(input)
|
||
|
let counts = toCountTable ids[1]
|
||
|
for id in ids[0]:
|
||
|
if id in counts:
|
||
|
result += id * counts[id]
|
||
|
|
||
|
solve:
|
||
|
"example.txt":
|
||
|
partOne: 11
|
||
|
partTwo: 31
|
||
|
"input.txt":
|
||
|
partOne: 1590491
|
||
|
partTwo: 22588371
|