diff options
| author | An0nSaiko <porfeas12@gmail.com> | 2022-12-10 05:50:17 +0200 |
|---|---|---|
| committer | An0nSaiko <porfeas12@gmail.com> | 2022-12-10 05:50:17 +0200 |
| commit | bfe634c168c6ec839ee010c28b381760175add8b (patch) | |
| tree | 7fe9187cf43c2790d655d6351702a68e72a6e55d | |
| parent | Day 8 (diff) | |
| download | aoc22-bfe634c168c6ec839ee010c28b381760175add8b.tar.gz aoc22-bfe634c168c6ec839ee010c28b381760175add8b.zip | |
Refactoring solutions
| -rw-r--r-- | day1/solution.nim | 13 | ||||
| -rw-r--r-- | 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 @@ | |||
| 1 | import std/strutils | 1 | import std/strutils |
| 2 | import std/algorithm | 2 | import std/algorithm |
| 3 | import std/strformat | ||
| 4 | 3 | ||
| 5 | let content = readFile("./input.txt").split({'\n'}) | 4 | let content = readFile("./input.txt").splitLines() |
| 6 | 5 | ||
| 7 | var | 6 | var |
| 8 | sum : int | 7 | sum = 0 |
| 9 | sums: seq[int] | 8 | sums = newSeq[int]() |
| 10 | 9 | ||
| 11 | for line in content: | 10 | for line in content: |
| 12 | if line.isEmptyOrWhitespace(): | 11 | if line.isEmptyOrWhitespace(): |
| @@ -17,5 +16,5 @@ for line in content: | |||
| 17 | 16 | ||
| 18 | sort(sums, system.cmp[int], Descending) | 17 | sort(sums, system.cmp[int], Descending) |
| 19 | 18 | ||
| 20 | echo fmt"Part 1: {sums[0]}" | 19 | echo sums[0] |
| 21 | echo fmt"Part 2: {sums[0]+sums[1]+sums[2]}" | 20 | 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 @@ | |||
| 1 | import std/strutils | 1 | import std/strutils |
| 2 | import std/sequtils | 2 | import std/sequtils |
| 3 | import std/sugar | ||
| 3 | 4 | ||
| 4 | type | 5 | type |
| 5 | # We use begin and length for Range representation to simplify calculations later | 6 | # We use begin and length for Range representation to simplify calculations later |
| 6 | Range = tuple[begin: int, length: int] | 7 | Range = tuple[begin: int, length: int] |
| 7 | Entry = tuple[first: Range, second: Range] | 8 | Entry = tuple[first: Range, second: Range] |
| 8 | 9 | ||
| 9 | proc part1(entries: seq[Entry]): int = | 10 | proc solve(entries: seq[Entry], part2 = false): int = |
| 10 | var score = 0 | 11 | var score = 0 |
| 11 | for e in entries: | 12 | for e in entries: |
| 12 | var r: Entry | 13 | var r: Entry |
| @@ -18,39 +19,24 @@ proc part1(entries: seq[Entry]): int = | |||
| 18 | r.first = e.second | 19 | r.first = e.second |
| 19 | r.second = e.first | 20 | r.second = e.first |
| 20 | 21 | ||
| 21 | if r.first.length >= r.second.length + r.second.begin - r.first.begin: | 22 | if not part2: |
| 22 | score += 1 | 23 | if r.first.length >= r.second.length + r.second.begin - r.first.begin: |
| 23 | 24 | score += 1 | |
| 24 | return score | ||
| 25 | |||
| 26 | proc part2(entries: seq[Entry]): int = | ||
| 27 | var score = 0 | ||
| 28 | for e in entries: | ||
| 29 | var r: Entry | ||
| 30 | # Let first become the leftmost range. In case both ranges start at the same index, we consider the longest one to be first | ||
| 31 | if e.first.begin < e.second.begin or (e.first.begin == e.second.begin and e.first.length >= e.second.length): | ||
| 32 | r.first = e.first | ||
| 33 | r.second = e.second | ||
| 34 | else: | 25 | else: |
| 35 | r.first = e.second | 26 | if r.first.begin + r.first.length - 1 >= r.second.begin: |
| 36 | r.second = e.first | 27 | score += 1 |
| 37 | |||
| 38 | if r.first.begin + r.first.length - 1 >= r.second.begin: | ||
| 39 | score += 1 | ||
| 40 | 28 | ||
| 41 | return score | 29 | return score |
| 42 | 30 | ||
| 31 | func entryFromLine(line: string): Entry = | ||
| 32 | # Get tokens and parse them as integers | ||
| 33 | let tokens = map(line.split({',', '-'}), token => token.parseInt()) | ||
| 34 | # Init Entry | ||
| 35 | ((tokens[0], tokens[1] - tokens[0]+1), (tokens[2], tokens[3] - tokens[2]+1)) | ||
| 36 | |||
| 43 | let | 37 | let |
| 44 | content = readFile("./input.txt").strip().splitLines() | 38 | content = readFile("./input.txt").strip().splitLines() |
| 45 | entries = map( | 39 | entries = map(content, entryFromLine) |
| 46 | content, | ||
| 47 | proc(line: string): Entry = | ||
| 48 | let tokens = map(line.split({',', '-'}), proc(token: string): int = token.parseInt()) | ||
| 49 | ( | ||
| 50 | (tokens[0], tokens[1] - tokens[0]+1), | ||
| 51 | (tokens[2], tokens[3] - tokens[2]+1) | ||
| 52 | ) | ||
| 53 | ) | ||
| 54 | 40 | ||
| 55 | echo part1(entries) | 41 | echo solve(entries) |
| 56 | echo part2(entries) | 42 | echo solve(entries, true) |
