Skip to content

Instantly share code, notes, and snippets.

@fxn
Created December 4, 2022 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxn/d11b6b374a1989c5a3eaaec8a3143a88 to your computer and use it in GitHub Desktop.
Save fxn/d11b6b374a1989c5a3eaaec8a3143a88 to your computer and use it in GitHub Desktop.
import std/[os,sequtils,strutils]
type
Assingment = tuple[fromSection, toSection: uint]
func toAssingment(rangeStr: string): Assingment =
let pair = rangeStr.split('-')
result.fromSection = parseUInt(pair[0])
result.toSection = parseUInt(pair[1])
func `<=`(self: Assingment, other: Assingment): bool =
other.fromSection <= self.fromSection and self.toSection <= other.toSection
var count = 0
for line in paramStr(1).lines:
let assignments = line.split(',').mapIt(it.toAssingment)
if assignments[0] <= assignments[1] or assignments[1] <= assignments[0]:
inc count
echo count
import std/[os,sequtils,strutils]
type
Assingment = tuple[fromSection, toSection: uint]
func toAssingment(rangeStr: string): Assingment =
let pair = rangeStr.split('-')
result.fromSection = parseUInt(pair[0])
result.toSection = parseUInt(pair[1])
func sections(self: Assingment): Slice[uint] =
self.fromSection .. self.toSection
func `<=`(self: Assingment, other: Assingment): bool =
other.fromSection <= self.fromSection and self.toSection <= other.toSection
func overlap(a: Assingment, b: Assingment): bool =
b.fromSection in a.sections or b.toSection in a.sections or a <= b
var count = 0
for line in paramStr(1).lines:
let assignments = line.split(',').mapIt(it.toAssingment)
if overlap(assignments[0], assignments[1]):
inc count
echo count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment