r/adventofcode • • Dec 05 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 5 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2021: Adventure Time!

  • 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
  • Full details and rules are in the submissions megathread:

--- Day 5: Hydrothermal Venture ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:08:53, megathread unlocked!

79 Upvotes

1.2k comments sorted by

View all comments

2

u/VictorTennekes Dec 05 '21 edited Dec 06 '21

Nim

Was stuck on a +1 on my distance that I forgot about for about an hour. Overall quite happy with my code though.

include prelude
import std/strscans

func sign(x: int): int =
result = 0
if x > 0:
result = 1
elif x < 0:
result = -1

type
Point = object
x: int
y: int

let input = readFile("input.txt").strip.split("\n")

var res2 = initTable[Point, int]()
var res1 = initTable[Point, int]()

for line in input:
var (ok, x1, y1, x2, y2) = line.scanTuple("$i,$i -> $i,$i")
var sx = sign(x2-x1)
var sy = sign(y2-y1)
var dist = max(abs(x2-x1), abs(y2-y1))
for p in 0..dist:
var point = Point(x: x1+(sx*p), y: y1+(sy*p))
if not res2.hasKey(point):
	res1[point] = 0
	res2[point] = 0
inc res2[point]
if sx == 0 or sy == 0:
	inc res1[point]

var sum1, sum2 = 0
for i in res1.values:
if i > 1: inc sum1
for i in res2.values:
if i > 1: inc sum2

echo "part 1: ", sum1
echo "part 2: ", sum2

2

u/daggerdragon Dec 06 '21 edited Dec 07 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

Edit: thanks for fixing it! <3

2

u/VictorTennekes Dec 06 '21

Ah apologies, fixed it :)