diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-06-08 13:50:47 +0300 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-06-08 13:50:47 +0300 |
| commit | 7be570c4a6e86fb7060f0bc06910ca57003dfe90 (patch) | |
| tree | a998f976596cb902cd8988d74f756b72660bc29c /day3/solution.nim | |
| parent | Day 21 (part1) (diff) | |
| download | aoc22-main.tar.gz aoc22-main.zip | |
Diffstat (limited to 'day3/solution.nim')
| -rw-r--r-- | day3/solution.nim | 47 |
1 files changed, 0 insertions, 47 deletions
diff --git a/day3/solution.nim b/day3/solution.nim deleted file mode 100644 index 6df88f2..0000000 --- a/day3/solution.nim +++ /dev/null | |||
| @@ -1,47 +0,0 @@ | |||
| 1 | import std/strutils | ||
| 2 | import std/sequtils | ||
| 3 | import std/sets | ||
| 4 | |||
| 5 | proc Points(c: char): int = | ||
| 6 | if c.isLowerAscii(): | ||
| 7 | return ord(c) - ord('a') + 1 | ||
| 8 | else: | ||
| 9 | return ord(c) - ord('A') + 27 | ||
| 10 | |||
| 11 | proc part1(content: seq[string]): int = | ||
| 12 | let ruckshacks = map( content, | ||
| 13 | proc(line: string): tuple[first: string, second: string] = | ||
| 14 | let pivot = int(line.len()/2) | ||
| 15 | (line[0 .. pivot-1], line[pivot .. ^1]) | ||
| 16 | ) | ||
| 17 | |||
| 18 | var score = 0 | ||
| 19 | for ruckshack in ruckshacks: | ||
| 20 | let | ||
| 21 | uniqFirst = toHashSet(ruckshack.first) | ||
| 22 | uniqSecond = toHashSet(ruckshack.second) | ||
| 23 | |||
| 24 | var c = toSeq(uniqFirst * uniqSecond)[0] | ||
| 25 | score += Points(c) | ||
| 26 | |||
| 27 | return score | ||
| 28 | |||
| 29 | proc part2(content: seq[string]): int = | ||
| 30 | assert(content.len() mod 3 == 0) | ||
| 31 | |||
| 32 | let ruckshacks = content | ||
| 33 | var score = 0 | ||
| 34 | for i in countup(0, ruckshacks.len()-1, 3): | ||
| 35 | let | ||
| 36 | uniqFirst = toHashSet(ruckshacks[i]) | ||
| 37 | uniqSecond = toHashSet(ruckshacks[i+1]) | ||
| 38 | uniqThird = toHashSet(ruckshacks[i+2]) | ||
| 39 | |||
| 40 | var c = toSeq(uniqFirst * uniqSecond * uniqThird)[0] | ||
| 41 | score += Points(c) | ||
| 42 | |||
| 43 | return score | ||
| 44 | |||
| 45 | let content = readFile("./input.txt").strip().split("\n") | ||
| 46 | echo part1(content) | ||
| 47 | echo part2(content) | ||
