aboutsummaryrefslogtreecommitdiffstats
path: root/day3/solution.nim
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2024-06-08 13:50:47 +0300
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2024-06-08 13:50:47 +0300
commit7be570c4a6e86fb7060f0bc06910ca57003dfe90 (patch)
treea998f976596cb902cd8988d74f756b72660bc29c /day3/solution.nim
parentDay 21 (part1) (diff)
downloadaoc22-main.tar.gz
aoc22-main.zip
Update file names and init.shHEADmain
Diffstat (limited to 'day3/solution.nim')
-rw-r--r--day3/solution.nim47
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 @@
1import std/strutils
2import std/sequtils
3import std/sets
4
5proc 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
11proc 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
29proc 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
45let content = readFile("./input.txt").strip().split("\n")
46echo part1(content)
47echo part2(content)