r/adventofcode • • Dec 09 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-

--- Day 9: Marble Mania ---


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 9

Transcript:

Studies show that AoC programmers write better code after being exposed 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 at 00:29:13!

21 Upvotes

283 comments sorted by

View all comments

1

u/[deleted] Dec 09 '18

Julia

Pretty fast the first part using Arrays. The second part(with the same code) takes ~30'. I'd be interested to see a solution using LinkedLists.jl package. For fun (it's super slow: 11s for part 1 only), I also implemented the same solution using circular buffers through the circshift function.

using DelimitedFiles

function readInput(filename::String)
	f = readdlm(filename, ' ')
	no_players = f[1]
	final_pts = f[7]
	return no_players, final_pts
end

function addMarble(marbles::Array{Int64,1}, marble::Int64, current_idx::Int64, 
	scores::Array{Int64,1}, player::Int64)
	if marble%23 != 0
		if current_idx + 2 >= length(marbles)+2
			current_idx = 2
		else
			current_idx += 2
		end
		insert!(marbles, current_idx, marble)
	else
		scores[player] += marble
		if current_idx - 7 <= 0
			current_idx = length(marbles) + (current_idx - 7)
		else
			current_idx -= 7
		end
		scores[player] += marbles[current_idx]
		deleteat!(marbles, current_idx)
	end
	return marbles, current_idx, scores
end

function playGame(no_players::Int64, final_pts::Int64)
	scores = zeros(Int, no_players)
	marbles = [0]
	current_idx = 1
	player = 1
	for i = 1:final_pts
		marbles, current_idx, scores = addMarble(marbles, i, current_idx,
			scores, player)

		player += 1
		if player > no_players
			player = 1
		end
	end
	return maximum(scores)
end

function addMarbleCircular(marbles::Array{Int64,1}, marble::Int64, 
	scores::Array{Int64,1}, player::Int64)
	if marble%23 != 0
		marbles = circshift(marbles, -2)
		prepend!(marbles, marble)
	else
		scores[player] += marble
		marbles = circshift(marbles, 6)
		scores[player] += pop!(marbles)
	end
	return marbles, scores
end

function playGameCircular(no_players::Int64, final_pts::Int64)
	scores = zeros(Int, no_players)
	marbles = [0]
	current_idx = 1
	player = 1
	for i = 1:final_pts
		marbles, scores = addMarbleCircular(marbles, i,
			scores, player)

		player += 1
		if player > no_players
			player = 1
		end
	end
	return maximum(scores)
end

# part 1
no_players, final_pts = readInput("day9.input")
mscore = playGame(no_players, final_pts)
println("winning Elf's score: ", mscore)

# part 2
# mscore = playGame(no_players, final_pts*100)
println("Winning Elf's score with 100x marbles: ", mscore)