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 /day4/solution.nim | |
| parent | Day 21 (part1) (diff) | |
| download | aoc22-main.tar.gz aoc22-main.zip | |
Diffstat (limited to 'day4/solution.nim')
| -rw-r--r-- | day4/solution.nim | 42 |
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 @@ | |||
| 1 | import std/strutils | ||
| 2 | import std/sequtils | ||
| 3 | import std/sugar | ||
| 4 | |||
| 5 | type | ||
| 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 | |||
| 10 | proc 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 | |||
| 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 | |||
| 37 | let | ||
| 38 | content = readFile("./input.txt").strip().splitLines() | ||
| 39 | entries = map(content, entryFromLine) | ||
| 40 | |||
| 41 | echo solve(entries) | ||
| 42 | echo solve(entries, true) | ||
