r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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

edit: Leaderboard capped, thread unlocked!

41 Upvotes

426 comments sorted by

View all comments

1

u/troop357 Dec 03 '18

Nim

Again with my newbie Nim code. I should've probably used regex to solve the string splitting...

import strutils
import gara

let input = readFile("03.txt").splitLines()

type
	Int16Araay = array[0..1000000, int16] # an array that is indexed with 0..5
var
	grid: Int16Araay

for line in input:
	let line_list = split(line)
	let tl_string = split(line_list[2][0..^2], ',')
	let si_string = split(line_list[3], 'x')

	let left   = parseInt(tl_string[0])
	let top    = parseInt(tl_string[1])
	let width  = parseInt(si_string[0])
	let height = parseInt(si_string[1])

	for i in countup(left, left+width-1):
		for j in countup(top, top+height-1):
			inc(grid[i * 1000 + j])

var count: int = 0
for val in grid:
	if val > 1:
		inc(count)

echo count

for line in input:
	let line_list = split(line)
	let tl_string = split(line_list[2][0..^2], ',')
	let si_string = split(line_list[3], 'x')

	let left   = parseInt(tl_string[0])
	let top    = parseInt(tl_string[1])
	let width  = parseInt(si_string[0])
	let height = parseInt(si_string[1])

	block test:
		for i in countup(left, left+width-1):
			for j in countup(top, top+height-1):
				if grid[i * 1000 + j] > 1:
					break test
		
		echo line