This commit is contained in:
Danny Hpy 2023-12-14 21:08:01 +01:00
parent 9ada5bb7cd
commit 04add7b4ff
9 changed files with 93 additions and 5 deletions

8
.editorconfig Normal file
View File

@ -0,0 +1,8 @@
root = true
[*]
charset = utf-8
[*.nim]
indent_style = space
indent_size = 2

View File

@ -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

1
.gitattributes vendored
View File

@ -1 +0,0 @@
session.key filter=git-crypt diff=git-crypt

6
.gitignore vendored
View File

@ -1,7 +1,13 @@
# binaries
**/first
**/first.exe
**/second
**/second.exe
**/both
**/both.exe
# input files
**/input.txt
# session.key
session.key

39
2023/01/both.nim Normal file
View File

@ -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}"

39
2023/02/first.nim Normal file
View File

@ -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}"

1
session.example.key Normal file
View File

@ -0,0 +1 @@
Rename this file to `session.key` and put your session key in it.

Binary file not shown.