r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 6 Solutions -πŸŽ„-

--- Day 6: Chronal Coordinates ---


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

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

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


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 at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

1

u/danbala Dec 07 '18

This is CachΓ© Objectscript (i.e. fancy mumps). And I know, to be 'real' mumps I should use more than 1 letter for variable names ;)

Class User.Day6
{

ClassMethod m(a,b) {
	s ax=$ZSTRIP($P(a,",",1),"*W"),ay=$ZSTRIP($P(a,",",2),"*W")
	s bx=$ZSTRIP($P(b,",",1),"*W"),by=$ZSTRIP($P(b,",",2),"*W")
	return $ZABS(ax-bx)+$ZABS(ay-by)
}

ClassMethod closest(ByRef input,ByRef distancessum,x) {
 	//zw input
	s i=$O(input("")),min=1000
	k dist,disti
	s sum=0
	while i'="" {
		s val=..m(input(i),x),sum=sum+val
		s dist(i)=val
		if $D(disti(val)){s disti(val)="."} else {s disti(val)=i}
		s i=$O(input(i))
	}
	s distancessum(x)=sum,closest=$O(disti("")),closestid=$G(disti(closest))
	return closestid
}

ClassMethod run(){
	s file="Z:\work\proj\aoc2018\day6\day6input.txt"
	s borderx=400,bordery=400,fs=##clasS(%Stream.FileCharacter).%New()
	d fs.LinkToFile(file)
	s x = fs.ReadLine(),i=0
	while x'="" {s input(i)=x,x = fs.ReadLine(),i=i+1}
	k killlist
	s distancessum=""
	f cx=0:1:borderx {f cy=0:1:bordery{s val=..closest(.input,.distancessum,cx_","_cy),field(cx,cy)=val
			s count(val)=$I(count(val))
		}
	}
	//populate killist with nodes that are infinite, i.e. at the border of the area
	f t=0:1:borderx {	s killlist(field(t,0))="",killlist(field(0,t))="",killlist(field(borderx,t))="",killlist(field(t,borderx))=""	}
	//cleanup
	s c=$O(killlist(""))
	while c'="" {
		k count(c)
		s c=$O(killlist(c))
	}
	//find the max count
	s max=0,c=$O(count(""))
	while c'="" {i (count(c)>max) s max=count(c)  
	s c=$O(count(c))}
	w "part1:",max,!
	s distancesumcutoff=10000,count=0,c=$O(distancessum(""))
	while c'="" {
		if (distancessum(c)>=distancesumcutoff) {k distancessum(c)
		} else {s count=count+1}
		s c=$O(distancessum(c))
	}
	w "Part2:",count,!
}
}