From bfe634c168c6ec839ee010c28b381760175add8b Mon Sep 17 00:00:00 2001 From: An0nSaiko Date: Sat, 10 Dec 2022 05:50:17 +0200 Subject: Refactoring solutions --- day1/solution.nim | 13 ++++++------- day4/solution.nim | 46 ++++++++++++++++------------------------------ 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/day1/solution.nim b/day1/solution.nim index 189fc22..b17411a 100644 --- a/day1/solution.nim +++ b/day1/solution.nim @@ -1,12 +1,11 @@ import std/strutils import std/algorithm -import std/strformat -let content = readFile("./input.txt").split({'\n'}) +let content = readFile("./input.txt").splitLines() -var - sum : int - sums: seq[int] +var + sum = 0 + sums = newSeq[int]() for line in content: if line.isEmptyOrWhitespace(): @@ -17,5 +16,5 @@ for line in content: sort(sums, system.cmp[int], Descending) -echo fmt"Part 1: {sums[0]}" -echo fmt"Part 2: {sums[0]+sums[1]+sums[2]}" +echo sums[0] +echo sums[0]+sums[1]+sums[2] diff --git a/day4/solution.nim b/day4/solution.nim index 36e90fb..8085445 100644 --- a/day4/solution.nim +++ b/day4/solution.nim @@ -1,12 +1,13 @@ import std/strutils import std/sequtils +import std/sugar type # We use begin and length for Range representation to simplify calculations later Range = tuple[begin: int, length: int] Entry = tuple[first: Range, second: Range] -proc part1(entries: seq[Entry]): int = +proc solve(entries: seq[Entry], part2 = false): int = var score = 0 for e in entries: var r: Entry @@ -18,39 +19,24 @@ proc part1(entries: seq[Entry]): int = r.first = e.second r.second = e.first - if r.first.length >= r.second.length + r.second.begin - r.first.begin: - score += 1 - - return score - -proc part2(entries: seq[Entry]): int = - var score = 0 - for e in entries: - var r: Entry - # Let first become the leftmost range. In case both ranges start at the same index, we consider the longest one to be first - if e.first.begin < e.second.begin or (e.first.begin == e.second.begin and e.first.length >= e.second.length): - r.first = e.first - r.second = e.second + if not part2: + if r.first.length >= r.second.length + r.second.begin - r.first.begin: + score += 1 else: - r.first = e.second - r.second = e.first - - if r.first.begin + r.first.length - 1 >= r.second.begin: - score += 1 + if r.first.begin + r.first.length - 1 >= r.second.begin: + score += 1 return score +func entryFromLine(line: string): Entry = + # Get tokens and parse them as integers + let tokens = map(line.split({',', '-'}), token => token.parseInt()) + # Init Entry + ((tokens[0], tokens[1] - tokens[0]+1), (tokens[2], tokens[3] - tokens[2]+1)) + let content = readFile("./input.txt").strip().splitLines() - entries = map( - content, - proc(line: string): Entry = - let tokens = map(line.split({',', '-'}), proc(token: string): int = token.parseInt()) - ( - (tokens[0], tokens[1] - tokens[0]+1), - (tokens[2], tokens[3] - tokens[2]+1) - ) - ) + entries = map(content, entryFromLine) -echo part1(entries) -echo part2(entries) +echo solve(entries) +echo solve(entries, true) -- cgit v1.2.3