From 2acd44f3698db6e026d5ff56eac1c0365075f82a Mon Sep 17 00:00:00 2001 From: Danny Harpigny Date: Sat, 1 Jan 2022 13:56:01 +0100 Subject: [PATCH] --- 2021/2/both.nim | 32 +++++++++++++++++++++++++++ 2021/2/first.nim | 22 ------------------- 2021/2/second.nim | 24 -------------------- 2021/5/both.nim | 41 ++++++++++++++-------------------- 2021/6/both.nim | 56 +++++++++++++++++++++++++++++++++++++++++++++++ adventofcode.nim | 11 +++++----- license.txt | 21 ++++++++++++++++++ 7 files changed, 131 insertions(+), 76 deletions(-) create mode 100644 2021/2/both.nim delete mode 100644 2021/2/first.nim delete mode 100644 2021/2/second.nim create mode 100644 2021/6/both.nim create mode 100644 license.txt diff --git a/2021/2/both.nim b/2021/2/both.nim new file mode 100644 index 0000000..f135edd --- /dev/null +++ b/2021/2/both.nim @@ -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}" \ No newline at end of file diff --git a/2021/2/first.nim b/2021/2/first.nim deleted file mode 100644 index b0805c1..0000000 --- a/2021/2/first.nim +++ /dev/null @@ -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}" \ No newline at end of file diff --git a/2021/2/second.nim b/2021/2/second.nim deleted file mode 100644 index 52f6fca..0000000 --- a/2021/2/second.nim +++ /dev/null @@ -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}" \ No newline at end of file diff --git a/2021/5/both.nim b/2021/5/both.nim index 1d7cb69..4643824 100644 --- a/2021/5/both.nim +++ b/2021/5/both.nim @@ -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() diff --git a/2021/6/both.nim b/2021/6/both.nim new file mode 100644 index 0000000..76d6b6e --- /dev/null +++ b/2021/6/both.nim @@ -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()}" \ No newline at end of file diff --git a/adventofcode.nim b/adventofcode.nim index f057b72..fc4c67b 100644 --- a/adventofcode.nim +++ b/adventofcode.nim @@ -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}" }) diff --git a/license.txt b/license.txt new file mode 100644 index 0000000..763058b --- /dev/null +++ b/license.txt @@ -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. \ No newline at end of file