This commit is contained in:
Danny Hpy 2022-01-02 10:50:00 +01:00
parent 2acd44f369
commit 6fdf3251f6
Signed by: dannyhpy
GPG Key ID: 7139FBBEA22D1CEC
14 changed files with 122 additions and 13 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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] = @[]

View File

@ -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] = @[]

View File

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

View File

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

27
2021/7/first.nim Normal file
View File

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

20
2021/7/second.nim Normal file
View File

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

24
2021/8/first.nim Normal file
View File

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

38
2021/9/first.nim Normal file
View File

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

View File

@ -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
return input.strip(chars = {'\n'})