From 7e06788d8dec0fa01607f5be38052452aa65722a Mon Sep 17 00:00:00 2001 From: Danny Harpigny Date: Wed, 25 Jan 2023 18:11:20 +0100 Subject: [PATCH] --- .git-crypt/.gitattributes | 4 + ...291402D67B7CD30974E81B7139FBBEA22D1CEC.gpg | Bin 0 -> 597 bytes .gitattributes | 1 + .gitignore | 7 +- 2022/01/both.nim | 38 ++++++++ 2022/02/first.nim | 43 +++++++++ 2022/02/second.nim | 60 +++++++++++++ 2022/03/both.nim | 50 +++++++++++ 2022/04/both.nim | 41 +++++++++ 2022/05/both.nim | 84 ++++++++++++++++++ 2022/06/both.nim | 35 ++++++++ adventofcode.nim | 57 +++++++++--- session.key | Bin 0 -> 151 bytes 13 files changed, 409 insertions(+), 11 deletions(-) create mode 100644 .git-crypt/.gitattributes create mode 100644 .git-crypt/keys/default/0/49291402D67B7CD30974E81B7139FBBEA22D1CEC.gpg create mode 100644 .gitattributes create mode 100644 2022/01/both.nim create mode 100644 2022/02/first.nim create mode 100644 2022/02/second.nim create mode 100644 2022/03/both.nim create mode 100644 2022/04/both.nim create mode 100644 2022/05/both.nim create mode 100644 2022/06/both.nim create mode 100644 session.key diff --git a/.git-crypt/.gitattributes b/.git-crypt/.gitattributes new file mode 100644 index 0000000..665b10e --- /dev/null +++ b/.git-crypt/.gitattributes @@ -0,0 +1,4 @@ +# Do not edit this file. To specify the files to encrypt, create your own +# .gitattributes file in the directory where your files are. +* !filter !diff +*.gpg binary diff --git a/.git-crypt/keys/default/0/49291402D67B7CD30974E81B7139FBBEA22D1CEC.gpg b/.git-crypt/keys/default/0/49291402D67B7CD30974E81B7139FBBEA22D1CEC.gpg new file mode 100644 index 0000000000000000000000000000000000000000..8f3bd3cafb838f15cf7f67a244ed0ab8f8399622 GIT binary patch literal 597 zcmV-b0;>Im0gMClu#MJzG@G zYEiAQu;EedLe$Q_cnIA&jKtjR^4+iGg*quNSQ%YeR)IV+4A1#iHnvr$y_rUCDdbPK zrQ-=Zk40miKi*9{0M^{P!(&?STwvCDt<7Hm_N>U5b%=;ApKF`wJ zs-|Pejn0fV$uEC6`he4jrb+*v#)N@z6*}z*e-L*XTeB1Cv^1O+TT11S=cF3%;fU=bC{UyCFevbJq!0MkJexm!Mn2R z{*nt@6oGrzeTu4YnerdO_Y!trZw(HU8<9(=TV6cp>zaNhypUGDJbx#u_|8H0u$4zL zTwd0h&<`Cl<-U`1+mN+L(MPSTmoajh@%xYx=bk3;nLYAIG= markerLen: + block check: + for j in 0 ..< len queue: + for k in 0 ..< len queue: + if j == k: continue + if queue[k] == queue[j]: + break check + + answer = i + 1 + break process + +echo fmt"Answer: {answer}" diff --git a/adventofcode.nim b/adventofcode.nim index 2a21ffe..0121ad3 100644 --- a/adventofcode.nim +++ b/adventofcode.nim @@ -1,26 +1,63 @@ +#[ + This file works by doing a lot of assumptions + about the project folder structure. + + For instance, it assumes the executable + directory to be something in a "YYYY/DD" + format. + + It also assumes the session key to be + available at the project root. + (in other words "../../session.key") +]# + import std/httpclient, std/os, std/strformat, std/strutils + +when isMainModule: + stderr.writeLine "adventofcode> This can not be ran directly." + quit 1 + +let sessionKeyPath = os.getAppDir() / ".." / ".." / "session.key" +if not fileExists sessionKeyPath: + stderr.writeLine "adventofcode> secret.key could not be found." +let sessionKey = readFile sessionKeyPath +if sessionKey[0 ..< 10] == "\x00GITCRYPT\x00": + stderr.writeLine "adventofcode> secret.key is encrypted." + quit 1 proc getInput*(year, day: int): string = - try: - let input = readFile(getCurrentDir() / "input.txt") + ## Automatically downloads the input from + ## AdventOfCode, unless there is a local copy + ## of the input as 'input.txt' in the app dir. + let inputPath = os.getAppDir() / "input.txt" + if fileExists inputPath: + echo "adventofcode> Proceeding with current input.txt file." + let input = readFile inputPath return input.strip(chars = {'\n'}) - except IOError: + else: echo "adventofcode> Missing input.txt. Downloading..." - let session = os.getEnv("SESSION") - if session == "": - echo "adventofcode> Unable to download without the SESSION environment variable." - quit 1 let client = newHttpClient() - client.headers = newHttpHeaders({ "Cookie": fmt"session={session}" }) + client.headers = newHttpHeaders({ "Cookie": fmt"session={sessionKey}" }) let input = client.getContent fmt"https://adventofcode.com/{year}/day/{day}/input" - writeFile(getCurrentDir() / "input.txt", input) + writeFile(inputPath, input) - return input.strip(chars = {'\n'}) \ No newline at end of file + return input.strip(chars = {'\n'}) + +proc getInput*(): string = + ## Tries to determine year and date of + ## day by looking at the app dir path. + let day = block: + let p = os.getAppDir().splitPath() + p.tail.parseInt() + let year = block: + let p = os.getAppDir().parentDir().splitPath() + p.tail.parseInt() + result = getInput(year, day) diff --git a/session.key b/session.key new file mode 100644 index 0000000000000000000000000000000000000000..dbd46647f2d69398f876c9cf15e673b0b41d21b4 GIT binary patch literal 151 zcmV;I0BHXJM@dveQdv+`08`~Oq@#m0%|9~`#^iwFzCYGZaeKti&eEI}_GZD3;?%Yo zCZqjE+<^A2DaZ6Dp%PKcud|ug{!my`kG(z^i>>Z~oILjJP`l8cofCR)g?dtYZ+@Q< zHM)$^7RGqsTW1f*Hg(qpl2$Zf-tJ9G!)M+&x|)@dw8`~T(oBu`Yyg+&o0J9)94YCu F*<