From ab13b4515e960324f480961ff4534bc2c9f17cbb Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sun, 5 Dec 2021 11:00:58 -0600 Subject: [PATCH] WIP day 5 --- 2021/five.nim | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2021/five.nim diff --git a/2021/five.nim b/2021/five.nim new file mode 100644 index 0000000..fab6ab4 --- /dev/null +++ b/2021/five.nim @@ -0,0 +1,42 @@ +import std/[strutils, sequtils, options, math, parseutils, sets, strformat, algorithm, tables] +import ./common + +proc toPointPair(s: string): (int, int, int, int) = + let nums = s.split(" -> ").mapIt(it.split(',').mapIt(it.parseInt())).concat() + (nums[0], nums[1], nums[2], nums[3]) + +proc part1(inputLines: seq[string]): int = + var points: CountTable[(int, int)] = initCountTable[(int, int)](1000) + for l in inputLines: + let (x1, y1, x2, y2) = l.toPointPair() + if x1 == x2 or y1 == y2: + echo &"{x1} {y1} -> {x2} {y2}" + for x in x1..x2: + for y in y1..y2: + points.inc((x, y)) + echo &"{x} {y}" + for p in points.values(): + if p >= 2: + echo p + inc result + +proc part2(inputLines: seq[string]): int = + return 0 + +let input = 5.loadInput() +# time("day 5 part 1"): echo input.part1() +# time("day 5 part 2"): echo input.part2() + +when not defined(release): + let testInput = """0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,5 -> 8,2""".split('\n') + doAssert testInput.part1() == 5 + doAssert testInput.part2() == 0