This commit is contained in:
Danny Hpy 2022-01-01 13:56:01 +01:00
parent 5e5192c9a3
commit 2acd44f369
Signed by: dannyhpy
GPG Key ID: 7139FBBEA22D1CEC
7 changed files with 131 additions and 76 deletions

32
2021/2/both.nim Normal file
View File

@ -0,0 +1,32 @@
import
../../adventofcode,
strutils,
strformat
let input = adventofcode.getInput(2021, 2)
let lines = input.splitLines()[0 .. ^2]
var horizontalPosition = 0
var depth = 0
when defined(second):
var aim = 0
for line in lines:
var command = line.split(" ")
case command[0]:
of "forward":
horizontalPosition += command[1].parseInt()
when defined(second):
depth += aim * command[1].parseInt()
of "up":
when defined(second):
aim -= command[1].parseInt()
else:
depth -= command[1].parseInt()
of "down":
when defined(second):
aim += command[1].parseInt()
else:
depth += command[1].parseInt()
echo fmt"Answer: {horizontalPosition * depth}"

View File

@ -1,22 +0,0 @@
import
../../adventofcode,
strutils,
strformat
let input = adventofcode.getInput(2021, 2)
let lines = input.splitLines()[0 .. ^2]
var horizontalPosition = 0
var depth = 0
for line in lines:
var command = line.split(" ")
case command[0]:
of "forward":
horizontalPosition += command[1].parseInt()
of "up":
depth -= command[1].parseInt()
of "down":
depth += command[1].parseInt()
echo fmt"Answer: {horizontalPosition * depth}"

View File

@ -1,24 +0,0 @@
import
../../adventofcode,
strutils,
strformat
let input = adventofcode.getInput(2021, 2)
let lines = input.splitLines()[0 .. ^2]
var horizontalPosition = 0
var depth = 0
var aim = 0
for line in lines:
var command = line.split(" ")
case command[0]:
of "forward":
horizontalPosition += command[1].parseInt()
depth += aim * command[1].parseInt()
of "up":
aim -= command[1].parseInt()
of "down":
aim += command[1].parseInt()
echo fmt"Answer: {horizontalPosition * depth}"

View File

@ -10,7 +10,7 @@ type
Grid = TableRef[Point, int]
HydrothermalVent = object
x1, x2, y1, y2: int
x1, y1, x2, y2: int
Point = object
x, y: int
@ -33,11 +33,10 @@ proc hash(p: Point): Hash =
else: A + B * B
proc getPoints(v: HydrothermalVent): seq[Point] =
let xd = max(v.x1, v.x2) - min(v.x1, v.x2)
let yd = max(v.y1, v.y2) - min(v.y1, v.y2)
when defined(second):
let xoffset = v.x2 - v.x1
let yoffset = v.y2 - v.y1
let xOffset = v.x2 - v.x1
let yOffset = v.y2 - v.y1
let xd = xOffset.abs()
let yd = yOffset.abs()
if xd != 0 and v.y1 == v.y2:
for i in 0 .. xd:
@ -49,20 +48,17 @@ proc getPoints(v: HydrothermalVent): seq[Point] =
else:
when defined(second):
# TODO:
if xoffset < 0:
for i in 0 .. xd:
result.add Point(
x: min(v.x1, v.x2) + i,
y: max(v.y1, v.y2) - i
)
else:
for i in 0 .. yd:
result.add Point(
x: max(v.x1, v.x2) + i,
y: min(v.y1, v.y2) + i
)
assert xd == yd
for i in 0 .. xd:
result.add Point(
x:
if xOffset > 0: min(v.x1, v.x2) + i
else: max(v.x1, v.x2) - i,
y:
if yOffset > 0: min(v.y1, v.y2) + i
else: max(v.y1, v.y2) - i
)
proc parseInput(input: string): seq[HydrothermalVent] =
for line in input.splitLines()[0 .. ^2]:
@ -71,11 +67,6 @@ proc parseInput(input: string): seq[HydrothermalVent] =
x1: coords[0][0], y1: coords[0][1], x2: coords[1][0], y2: coords[1][1]
)
static:
let dummyVent = HydrothermalVent(x1: 9, y1: 7, x2: 7, y2: 9)
for point in dummyVent.getPoints():
echo $point
let input = adventofcode.getInput(2021, 5)
let vents = input.parseInput()
let grid = Grid()

56
2021/6/both.nim Normal file
View File

@ -0,0 +1,56 @@
import
../../adventofcode,
math,
sequtils,
strutils,
strformat,
tables
type
Cycle = int
let input = adventofcode.getInput(2021, 6)
let inputLine = input.splitLines()[0]
var days = 0
var fishes: Table[Cycle, int]
for i in 0 .. 8:
fishes[i] = 0
proc parseInput() =
let cycles = inputLine.split(",").mapIt(it.parseInt())
for cycle in cycles:
fishes[cycle] += 1
proc newDay() =
days += 1
var temporaryBox: Table[Cycle, int]
for i in 0 .. 8:
temporaryBox[i] = 0
for cycle in [8, 7, 6, 5, 4, 3, 2, 1]:
let count = fishes[cycle]
fishes[cycle] -= count
temporaryBox[cycle - 1] += count
block cycle0:
let count = fishes[0]
fishes[0] -= count
temporaryBox[6] += count
temporaryBox[8] += count
for cycle in temporaryBox.keys():
fishes[cycle] += temporaryBox[cycle]
when isMainModule:
const simulateDays =
when defined(second): 256
else: 80
parseInput()
for i in 1 .. simulateDays:
newDay()
echo fmt"{days} days passed."
echo fmt"Answer: {math.sum toSeq fishes.values()}"

View File

@ -3,17 +3,18 @@ import
os,
strformat
let session = os.getEnv("SESSION")
if session == "":
echo "Missing SESSION environment variable."
quit 1
proc getInput*(year, day: int): string =
try:
let input = readFile(getCurrentDir() / "input.txt")
return input
except IOError:
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}" })

21
license.txt Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Danny Harpigny
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.