From 6fdf3251f6a68d880f2a2a220703089fe9eec29b Mon Sep 17 00:00:00 2001 From: Danny Harpigny Date: Sun, 2 Jan 2022 10:50:00 +0100 Subject: [PATCH] --- 2021/1/first.nim | 2 +- 2021/1/second.nim | 2 +- 2021/2/both.nim | 2 +- 2021/3/first.nim | 2 +- 2021/3/second.nim | 2 +- 2021/4/first.nim | 2 +- 2021/4/second.nim | 2 +- 2021/5/both.nim | 2 +- 2021/6/both.nim | 3 +-- 2021/7/first.nim | 27 +++++++++++++++++++++++++++ 2021/7/second.nim | 20 ++++++++++++++++++++ 2021/8/first.nim | 24 ++++++++++++++++++++++++ 2021/9/first.nim | 38 ++++++++++++++++++++++++++++++++++++++ adventofcode.nim | 7 ++++--- 14 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 2021/7/first.nim create mode 100644 2021/7/second.nim create mode 100644 2021/8/first.nim create mode 100644 2021/9/first.nim diff --git a/2021/1/first.nim b/2021/1/first.nim index f02b20b..c7c1a2c 100644 --- a/2021/1/first.nim +++ b/2021/1/first.nim @@ -5,7 +5,7 @@ import strutils let input = adventofcode.getInput(2021, 1) -let lines = input.splitLines()[0 .. ^2].mapIt(it.parseInt()) +let lines = input.splitLines().mapIt(it.parseInt()) var depths = 0 var prevResult = lines[0] diff --git a/2021/1/second.nim b/2021/1/second.nim index d864021..a126609 100644 --- a/2021/1/second.nim +++ b/2021/1/second.nim @@ -5,7 +5,7 @@ import strutils let input = adventofcode.getInput(2021, 1) -let lines = input.splitLines()[0 .. ^2].mapIt(it.parseInt()) +let lines = input.splitLines().mapIt(it.parseInt()) var depths = 0 var prevSum = lines[0] + lines[1] + lines[2] diff --git a/2021/2/both.nim b/2021/2/both.nim index f135edd..162cc59 100644 --- a/2021/2/both.nim +++ b/2021/2/both.nim @@ -4,7 +4,7 @@ import strformat let input = adventofcode.getInput(2021, 2) -let lines = input.splitLines()[0 .. ^2] +let lines = input.splitLines() var horizontalPosition = 0 var depth = 0 diff --git a/2021/3/first.nim b/2021/3/first.nim index 3a34fec..55a44ef 100644 --- a/2021/3/first.nim +++ b/2021/3/first.nim @@ -5,7 +5,7 @@ import strformat let input = adventofcode.getInput(2021, 3) -let lines = input.splitLines()[0 .. ^2] +let lines = input.splitLines() var gammaRateBinary = "" var epsilonRateBinary = "" diff --git a/2021/3/second.nim b/2021/3/second.nim index 8a9eee6..c7e6307 100644 --- a/2021/3/second.nim +++ b/2021/3/second.nim @@ -5,7 +5,7 @@ import strformat let input = adventofcode.getInput(2021, 3) -let lines = input.splitLines()[0 .. ^2] +let lines = input.splitLines() var oxygenGenRateValues = lines var co2ScrubberRateValues = lines diff --git a/2021/4/first.nim b/2021/4/first.nim index e8a374b..2fb0e57 100644 --- a/2021/4/first.nim +++ b/2021/4/first.nim @@ -9,7 +9,7 @@ import x/board let input = adventofcode.getInput(2021, 4) -let lines = input.splitLines()[0 .. ^2] +let lines = input.splitLines() let drawn = lines[0].split(",").mapIt(it.parseInt()) var currentlyDrawn: seq[int] = @[] diff --git a/2021/4/second.nim b/2021/4/second.nim index 5247cc1..bb62919 100644 --- a/2021/4/second.nim +++ b/2021/4/second.nim @@ -9,7 +9,7 @@ import x/board let input = adventofcode.getInput(2021, 4) -let lines = input.splitLines()[0 .. ^2] +let lines = input.splitLines() let drawn = lines[0].split(",").mapIt(it.parseInt()) var currentlyDrawn: seq[int] = @[] diff --git a/2021/5/both.nim b/2021/5/both.nim index 4643824..dcb27d1 100644 --- a/2021/5/both.nim +++ b/2021/5/both.nim @@ -61,7 +61,7 @@ proc getPoints(v: HydrothermalVent): seq[Point] = ) proc parseInput(input: string): seq[HydrothermalVent] = - for line in input.splitLines()[0 .. ^2]: + for line in input.splitLines(): let coords = line.split(" -> ").mapIt(it.split(",").mapIt(it.parseInt())) result.add HydrothermalVent( x1: coords[0][0], y1: coords[0][1], x2: coords[1][0], y2: coords[1][1] diff --git a/2021/6/both.nim b/2021/6/both.nim index 76d6b6e..9484426 100644 --- a/2021/6/both.nim +++ b/2021/6/both.nim @@ -10,7 +10,6 @@ type Cycle = int let input = adventofcode.getInput(2021, 6) -let inputLine = input.splitLines()[0] var days = 0 var fishes: Table[Cycle, int] @@ -18,7 +17,7 @@ for i in 0 .. 8: fishes[i] = 0 proc parseInput() = - let cycles = inputLine.split(",").mapIt(it.parseInt()) + let cycles = input.split(",").mapIt(it.parseInt()) for cycle in cycles: fishes[cycle] += 1 diff --git a/2021/7/first.nim b/2021/7/first.nim new file mode 100644 index 0000000..6c68e29 --- /dev/null +++ b/2021/7/first.nim @@ -0,0 +1,27 @@ +import + ../../adventofcode, + options, + sequtils, + strformat, + strutils + +let input = adventofcode.getInput(2021, 7)[0 .. ^2] +#let input = "16,1,2,0,4,2,7,1,2,14" # Example given +let crabs = input.split(",").mapIt(it.parseInt()) +let minPos = min(crabs) +let maxPos = max(crabs) + +var bestPosResult = none int +var bestFuelResult = none int + +for pos in minPos .. maxPos: + var fuel = 0 + for crab in crabs: + fuel += max(crab, pos) - min(crab, pos) + + if bestFuelResult.isNone() or fuel < bestFuelResult.get(): + bestPosResult = some pos + bestFuelResult = some fuel + +echo fmt"Position : {bestPosResult.get()}" +echo fmt"Fuel | Answer: {bestFuelResult.get()}" \ No newline at end of file diff --git a/2021/7/second.nim b/2021/7/second.nim new file mode 100644 index 0000000..fc73956 --- /dev/null +++ b/2021/7/second.nim @@ -0,0 +1,20 @@ +import + ../../adventofcode, + math, + sequtils, + strformat, + strutils + +let input = adventofcode.getInput(2021, 7) +#let input = "16,1,2,0,4,2,7,1,2,14" # Example given +let crabs = input.split(",").mapIt(it.parseInt()) + +let avg = toInt math.floor((math.sum crabs) / crabs.len()) + +var fuel = 0 +for crab in crabs: + let distance = max(crab, avg) - min(crab, avg) + fuel += sum toSeq 1 .. distance + +echo fmt"Position : {avg}" +echo fmt"Fuel | Answer: {fuel}" \ No newline at end of file diff --git a/2021/8/first.nim b/2021/8/first.nim new file mode 100644 index 0000000..52a9cf4 --- /dev/null +++ b/2021/8/first.nim @@ -0,0 +1,24 @@ +import + ../../adventofcode, + math, + sequtils, + strformat, + strutils, + tables + +let lines = adventofcode.getInput(2021, 8).splitLines() +var decodedDigits = newTable[int, int]() +for i in [2, 3, 4, 7]: + decodedDigits[i] = 0 + +for line in lines: + let lineParts = line.split(" | ") + + # We only consider output digits for now + let outputDigits = lineParts[1].split(" ") + + for outputDigit in outputDigits: + if decodedDigits.hasKey outputDigit.len(): + decodedDigits[outputDigit.len()] += 1 + +echo fmt"Answer: {math.sum toSeq decodedDigits.values()}" \ No newline at end of file diff --git a/2021/9/first.nim b/2021/9/first.nim new file mode 100644 index 0000000..28e4ad7 --- /dev/null +++ b/2021/9/first.nim @@ -0,0 +1,38 @@ +import + ../../adventofcode, + math, + options, + sequtils, + strformat, + strutils + +type + Grid = seq[seq[int]] + +proc at(g: Grid; x, y: int): Option[int] = + try: result = some g[y][x] + except IndexDefect: discard + +let input = adventofcode.getInput(2021, 9) +let grid = Grid input.splitLines().mapIt(toSeq(it).mapIt(($it).parseInt())) + +var lowPoints = newSeq[int]() + +for y in 0 .. grid.high(): + for x in 0 .. grid[y].high(): + let p = grid.at(x, y).get() + + let adjA = grid.at(x - 1, y) + if adjA.isSome() and p >= adjA.get(): continue + let adjB = grid.at(x, y - 1) + if adjB.isSome() and p >= adjB.get(): continue + let adjC = grid.at(x + 1, y) + if adjC.isSome() and p >= adjC.get(): continue + let adjD = grid.at(x, y + 1) + if adjD.isSome() and p >= adjD.get(): continue + + lowPoints.add p + +var riskLevels = lowPoints.mapIt(it + 1) + +echo fmt"Answer: {math.sum riskLevels}" \ No newline at end of file diff --git a/adventofcode.nim b/adventofcode.nim index fc4c67b..5e1f462 100644 --- a/adventofcode.nim +++ b/adventofcode.nim @@ -1,13 +1,14 @@ import httpclient, os, - strformat + strformat, + strutils proc getInput*(year, day: int): string = try: let input = readFile(getCurrentDir() / "input.txt") - return input + return input.strip(chars = {'\n'}) except IOError: echo "adventofcode> Missing input.txt. Downloading..." let session = os.getEnv("SESSION") @@ -22,4 +23,4 @@ proc getInput*(year, day: int): string = writeFile(getCurrentDir() / "input.txt", input) - return input \ No newline at end of file + return input.strip(chars = {'\n'}) \ No newline at end of file