diff --git a/2021/1.test.ts b/2021/deno/1.test.ts similarity index 100% rename from 2021/1.test.ts rename to 2021/deno/1.test.ts diff --git a/2021/1.ts b/2021/deno/1.ts similarity index 100% rename from 2021/1.ts rename to 2021/deno/1.ts diff --git a/2021/2.ts b/2021/deno/2.ts similarity index 100% rename from 2021/2.ts rename to 2021/deno/2.ts diff --git a/2021/3.test.ts b/2021/deno/3.test.ts similarity index 100% rename from 2021/3.test.ts rename to 2021/deno/3.test.ts diff --git a/2021/3.ts b/2021/deno/3.ts similarity index 100% rename from 2021/3.ts rename to 2021/deno/3.ts diff --git a/2021/common.ts b/2021/deno/common.ts similarity index 100% rename from 2021/common.ts rename to 2021/deno/common.ts diff --git a/2021/deps.ts b/2021/deno/deps.ts similarity index 100% rename from 2021/deps.ts rename to 2021/deno/deps.ts diff --git a/2021/common.nim b/2021/nim/common.nim similarity index 100% rename from 2021/common.nim rename to 2021/nim/common.nim diff --git a/2021/one.nim b/2021/nim/day1.nim similarity index 100% rename from 2021/one.nim rename to 2021/nim/day1.nim diff --git a/2021/ten.nim b/2021/nim/day10.nim similarity index 100% rename from 2021/ten.nim rename to 2021/nim/day10.nim diff --git a/2021/two.nim b/2021/nim/day2.nim similarity index 100% rename from 2021/two.nim rename to 2021/nim/day2.nim diff --git a/2021/three.nim b/2021/nim/day3.nim similarity index 100% rename from 2021/three.nim rename to 2021/nim/day3.nim diff --git a/2021/four.nim b/2021/nim/day4.nim similarity index 100% rename from 2021/four.nim rename to 2021/nim/day4.nim diff --git a/2021/five.nim b/2021/nim/day5.nim similarity index 100% rename from 2021/five.nim rename to 2021/nim/day5.nim diff --git a/2021/six.nim b/2021/nim/day6.nim similarity index 100% rename from 2021/six.nim rename to 2021/nim/day6.nim diff --git a/2021/seven.nim b/2021/nim/day7.nim similarity index 100% rename from 2021/seven.nim rename to 2021/nim/day7.nim diff --git a/2021/eight.nim b/2021/nim/day8.nim similarity index 100% rename from 2021/eight.nim rename to 2021/nim/day8.nim diff --git a/2021/nine.nim b/2021/nim/day9.nim similarity index 100% rename from 2021/nine.nim rename to 2021/nim/day9.nim diff --git a/2021/nim/eleven.nim b/2021/nim/eleven.nim new file mode 100644 index 0000000..a98ea9b --- /dev/null +++ b/2021/nim/eleven.nim @@ -0,0 +1,31 @@ +import ./common, std/[strutils, sequtils, algorithm, sugar, tables] + +const pairs = toTable {'(': ')', '[': ']', '{': '}', '<': '>'} +const corruptPoints = toTable {')': 3, ']': 57, '}': 1197, '>': 25137} + +proc parse(input: seq[string]): (int, int) = + var cmps = newSeq[int]() + for line in input: + var stack: seq[char] = @[] + for ch in line: + if pairs.hasKey ch: stack.add pairs[ch] + elif stack.pop != ch: + result[0] += corruptPoints[ch] + stack.setLen 0 + break + if stack.len > 0: cmps.add stack.mapIt(" )]}>".find it).foldr 5 * b + a + result[1] = cmps.sorted[cmps.len div 2] + +doDay 10, n => n.loadInput(), s => s.parse[0], s => s.parse[1], + @[ + "[({(<(())[]>[[{[]{<()<>>", + "[(()[<>])]({[<{<<[]>>(", + "{([(<{}[<>[]}>{[]{[(<()>", + "(((({<>}<{<{<>}{[]{[]{}", + "[[<[([]))<([[{}[[()]]]", + "[{[{({}]{}}([{[{{{}}([]", + "{<[[]]>}<{[{[{[]{()[[[]", + "[<(<(<(<{}))><([]([]()", + "<{([([[(<>()){}]>(<<{{", + "<{([{{}}[<[[[<>{}]]]>[]]", + ], 26397, 288957 diff --git a/2021/readme.md b/2021/readme.md index d8df5df..4f8d6bd 100644 --- a/2021/readme.md +++ b/2021/readme.md @@ -6,6 +6,7 @@ solutions using it. Specifically, here's my `deno --version` output: + $ deno --version deno 1.16.3 (release, x86_64-unknown-linux-gnu) v8 9.7.106.5 typescript 4.4.2 @@ -14,7 +15,13 @@ Enjoy! **EDIT**: Since performance is not what I would like, it looks like I'm also doing some of these in Nim. -**EDIT 2**: I sprinkled some zig in there. I've been meaning to mess with it for a while. + $ nim --version + Nim Compiler Version 1.6.0 [Linux: amd64] + Compiled at 2021-10-19 + Copyright (c) 2006-2021 by Andreas Rumpf + + git hash: 727c6378d2464090564dbcd9bc8b9ac648467e38 + active boot switches: -d:release ## Usage @@ -24,12 +31,7 @@ Run these solutions like so: And the Nim ones like so: - nim c -d:release -d:ssl --run $DAYMODULE.nim - -And the Zig ones like so: - - zig build-exe -O ReleaseFast 6.zig -femit-bin=build/zig-$DAY - ./build/zig-$DAY + nim c -d:release -d:ssl --run day$DAY.nim And if you want to measure memory usage with Deno programs: @@ -40,14 +42,8 @@ And if you want to measure memory usage with Deno programs: Or for Nim programs: mkdir -p build - nim c -d:release -d:ssl --outdir:build $DAYMODULE.nim - /usr/bin/time -v ./$DAYMODULE - -Or for Zig programs: - - mkdir -p build - zig build-exe -O ReleaseFast 6.zig -femit-bin=build/zig-$DAY - /usr/bin/time -v ./build/zig-$DAY + nim c -d:release -d:ssl --outdir:build day$DAY.nim + /usr/bin/time -v ./day$DAY # Days diff --git a/2021/6.zig b/2021/zig/6.zig similarity index 100% rename from 2021/6.zig rename to 2021/zig/6.zig diff --git a/2021/common.zig b/2021/zig/common.zig similarity index 100% rename from 2021/common.zig rename to 2021/zig/common.zig