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()
|
||||
@@ -2,5 +2,6 @@
|
||||
|
||||
echo "Making 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
|
||||
|
||||
3
run_day.sh
Executable file
3
run_day.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
nim c -r day_$1/solve.nim
|
||||
@@ -1,6 +1,6 @@
|
||||
import std/[]
|
||||
|
||||
const input = readFile("input.txt")
|
||||
const input = readFile("day_DAYNR/input.txt")
|
||||
|
||||
proc solvePart1() =
|
||||
echo "TODO"
|
||||
|
||||
Reference in New Issue
Block a user