solution 1

This commit is contained in:
Daylin Morgan 2024-12-01 12:39:48 -06:00
parent 2d16655c21
commit 5020fc18ce
Signed by: daylin
GPG key ID: 950D13E9719334AD
2 changed files with 38 additions and 0 deletions

View file

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

View file

@ -0,0 +1,32 @@
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