This commit is contained in:
2022-12-17 20:09:45 +01:00
parent 949178dd0c
commit cdc79643be
7 changed files with 1049 additions and 2 deletions

1000
day_04/input.txt Normal file

File diff suppressed because it is too large Load Diff

43
day_04/solve.nim Normal file
View 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()

View File

@@ -2,5 +2,6 @@
echo "Making day_$1" echo "Making day_$1"
mkdir day_$1 mkdir day_$1
cp template.nim day_$1 cp template.nim day_$1/solve.nim
sed -i "s/DAYNR/$1/g" day_$1/solve.nim
touch day_$1/input.txt touch day_$1/input.txt

3
run_day.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
nim c -r day_$1/solve.nim

View File

@@ -1,6 +1,6 @@
import std/[] import std/[]
const input = readFile("input.txt") const input = readFile("day_DAYNR/input.txt")
proc solvePart1() = proc solvePart1() =
echo "TODO" echo "TODO"