44 lines
1.2 KiB
Nim
44 lines
1.2 KiB
Nim
import std/[strutils, sequtils, sugar]
|
|
|
|
type Assignment = object
|
|
first: int
|
|
last: int
|
|
|
|
proc assignment(sections: string): Assignment {.inline.} =
|
|
let edges = sections.split("-")
|
|
return Assignment(first: edges[0].parseInt, last: edges[1].parseInt)
|
|
|
|
proc contains(self: Assignment, other: Assignment): bool {.inline.} =
|
|
other.first >= self.first and other.last <= self.last
|
|
|
|
proc intersects(self: Assignment, other: Assignment): bool {.inline.} =
|
|
other.last >= self.first and other.first <= self.last
|
|
|
|
const input = readFile("day_04/input.txt")
|
|
|
|
iterator elfPairs(data: string): tuple[elf1: Assignment, elf2: Assignment] =
|
|
for pair in input.splitLines:
|
|
let assignments = pair.split(",")
|
|
yield (
|
|
elf1: assignment(assignments[0]),
|
|
elf2: assignment(assignments[1])
|
|
)
|
|
|
|
proc solvePart1() =
|
|
var count = 0
|
|
for pair in input.elfPairs:
|
|
if pair.elf1.contains(pair.elf2) or pair.elf2.contains(pair.elf1):
|
|
count += 1
|
|
echo "Fully incuded pairs: " & $count
|
|
|
|
proc solvePart2() =
|
|
var count = 0
|
|
for pair in input.elfPairs:
|
|
if pair.elf1.intersects(pair.elf2):
|
|
count += 1
|
|
echo "Intersecting pairs: " & $count
|
|
|
|
if isMainModule:
|
|
solvePart1()
|
|
solvePart2()
|