diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b7382b0 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +charset = utf-8 + +[*.nim] +indent_style = space +indent_size = 2 \ No newline at end of file diff --git a/.git-crypt/.gitattributes b/.git-crypt/.gitattributes deleted file mode 100644 index 665b10e..0000000 --- a/.git-crypt/.gitattributes +++ /dev/null @@ -1,4 +0,0 @@ -# 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 deleted file mode 100644 index 8f3bd3c..0000000 Binary files a/.git-crypt/keys/default/0/49291402D67B7CD30974E81B7139FBBEA22D1CEC.gpg and /dev/null differ diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 7ab9709..0000000 --- a/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -session.key filter=git-crypt diff=git-crypt diff --git a/.gitignore b/.gitignore index 39d3cbb..e853ab2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,13 @@ # binaries **/first +**/first.exe **/second +**/second.exe **/both +**/both.exe # input files **/input.txt + +# session.key +session.key \ No newline at end of file diff --git a/2023/01/both.nim b/2023/01/both.nim new file mode 100644 index 0000000..ed908d2 --- /dev/null +++ b/2023/01/both.nim @@ -0,0 +1,39 @@ +import + std/strformat, + std/strutils + +import + ../../adventofcode + +let input = adventofcode.getInput() +var sum = 0 + +for line in input.splitLines(): + var digitsInLine: seq[char] = @[] + for i in 0 .. line.high: + if line[i].isDigit(): + digitsInLine.add line[i] + when defined(second): + # We can't use multiReplace() as numbers can overlap + # e.g.: eighthree, twone, ... + proc matches(pattern: string): bool = + if i + pattern.high <= line.high: + return line[i .. i + pattern.high] == pattern + for (a, b) in [ + ("one", '1'), + ("two", '2'), + ("three", '3'), + ("four", '4'), + ("five", '5'), + ("six", '6'), + ("seven", '7'), + ("eight", '8'), + ("nine", '9'), + ]: + if matches a: + digitsInLine.add b + + # Take the first and the last digits of the current line + sum += parseInt(digitsInLine[0] & digitsInLine[^1]) + +echo fmt"Answer: {sum}" \ No newline at end of file diff --git a/2023/02/first.nim b/2023/02/first.nim new file mode 100644 index 0000000..b33ab5d --- /dev/null +++ b/2023/02/first.nim @@ -0,0 +1,39 @@ +import + std/strformat, + std/strutils + +import + ../../adventofcode + +const maxRedCubes = 12 +const maxGreenCubes = 13 +const maxBlueCubes = 14 + +let input = adventofcode.getInput() +var sum = 0 + +for line in input.splitLines(): + let colonIdx = line.find(':') + let gameID = parseInt line[len("Game ") ..< colonIdx] + let gameSets = line[colonIdx + 2 .. ^1].split("; ") + block verifyGameSet: + for gameSet in gameSets: + for cubeTypeAndCount in gameSet.split(", "): + let cubeCountAndTypeSplit = cubeTypeAndCount.split(' ') + let cubeCount = parseInt cubeCountAndTypeSplit[0] + let cubeType = cubeCountAndTypeSplit[1] + case cubeType: + of "red": + if cubeCount > maxRedCubes: + break verifyGameSet + of "green": + if cubeCount > maxGreenCubes: + break verifyGameSet + of "blue": + if cubeCount > maxBlueCubes: + break verifyGameSet + else: + discard + sum += gameID + +echo fmt"Answer: {sum}" \ No newline at end of file diff --git a/session.example.key b/session.example.key new file mode 100644 index 0000000..fa5a9c8 --- /dev/null +++ b/session.example.key @@ -0,0 +1 @@ +Rename this file to `session.key` and put your session key in it. \ No newline at end of file diff --git a/session.key b/session.key deleted file mode 100644 index 2c9eb74..0000000 Binary files a/session.key and /dev/null differ