Day 4
This commit is contained in:
1000
day_04/input.txt
Normal file
1000
day_04/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
43
day_04/solve.nim
Normal file
43
day_04/solve.nim
Normal file
@@ -0,0 +1,43 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user