aboutsummaryrefslogtreecommitdiffstats
path: root/day4/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 /day4/solution.nim
parentDay 21 (part1) (diff)
downloadaoc22-7be570c4a6e86fb7060f0bc06910ca57003dfe90.tar.gz
aoc22-7be570c4a6e86fb7060f0bc06910ca57003dfe90.zip
Update file names and init.shHEADmain
Diffstat (limited to 'day4/solution.nim')
-rw-r--r--day4/solution.nim42
1 files changed, 0 insertions, 42 deletions
diff --git a/day4/solution.nim b/day4/solution.nim
deleted file mode 100644
index 8085445..0000000
--- a/day4/solution.nim
+++ /dev/null
@@ -1,42 +0,0 @@
1import std/strutils
2import std/sequtils
3import std/sugar
4
5type
6 # We use begin and length for Range representation to simplify calculations later
7 Range = tuple[begin: int, length: int]
8 Entry = tuple[first: Range, second: Range]
9
10proc solve(entries: seq[Entry], part2 = false): int =
11 var score = 0
12 for e in entries:
13 var r: Entry
14 # Let first become the leftmost range. In case both ranges start at the same index, we consider the longest one to be first
15 if e.first.begin < e.second.begin or (e.first.begin == e.second.begin and e.first.length >= e.second.length):
16 r.first = e.first
17 r.second = e.second
18 else:
19 r.first = e.second
20 r.second = e.first
21
22 if not part2:
23 if r.first.length >= r.second.length + r.second.begin - r.first.begin:
24 score += 1
25 else:
26 if r.first.begin + r.first.length - 1 >= r.second.begin:
27 score += 1
28
29 return score
30
31func 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
37let
38 content = readFile("./input.txt").strip().splitLines()
39 entries = map(content, entryFromLine)
40
41echo solve(entries)
42echo solve(entries, true)