From 82be8473d21c15bc22c46cc4ba5463e3d2213c7d Mon Sep 17 00:00:00 2001 From: j-james Date: Mon, 5 Dec 2022 00:01:10 -0800 Subject: Day Four --- 2022/nim/day04/src/main.nim | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2022/nim/day04/src/main.nim (limited to '2022/nim') diff --git a/2022/nim/day04/src/main.nim b/2022/nim/day04/src/main.nim new file mode 100644 index 0000000..bb024fb --- /dev/null +++ b/2022/nim/day04/src/main.nim @@ -0,0 +1,23 @@ +# Day 4: Camp Cleanup +import std/[os, strutils, sequtils, sugar] + +let input = paramStr(1).readFile().strip().split("\n") + +let assign = input.mapIt(it.split(",")) + .mapIt(it.mapIt(it.split("-").map(parseInt))) + +func contains(a, b: seq[int]): bool = + return a[0] in b[0] .. b[1] and + a[1] in b[0] .. b[1] or + b[0] in a[0] .. a[1] and + b[1] in a[0] .. a[1] + +echo assign.filterIt(contains(it[0], it[1])).len + +func overlaps(a, b: seq[int]): bool = + return a[0] in b[0] .. b[1] or + a[1] in b[0] .. b[1] or + b[0] in a[0] .. a[1] or + b[1] in a[0] .. a[1] + +echo assign.filterIt(overlaps(it[0], it[1])).len -- cgit v1.2.3-70-g09d2