solution 1
This commit is contained in:
parent
2d16655c21
commit
5020fc18ce
2 changed files with 38 additions and 0 deletions
6
solutions/day01/example.txt
Normal file
6
solutions/day01/example.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
32
solutions/day01/solution.nim
Normal file
32
solutions/day01/solution.nim
Normal 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
|
Loading…
Reference in a new issue