My Haskell solution for today was quite short, but performance was shit.
importqualifiedData.Map.StrictasMapsolve::Int->Map.MapIntInt->Int->Int->Intsolvelimitmemtprev|t==limit=prev|otherwise=caseMap.lookupprevmemofNothing->solvelimit(Map.insertprev(t-1)mem)(t+1)0Justt'->solvelimit(Map.insertprev(t-1)mem)(t+1)(t-t'-1)main=doletinput=[20,0,1,11,6,3]-- 436initMemory=Map.fromList$zip(initinput)[1..]print$solve2021initMemory(lengthinput+1)(lastinput)print$solve30000001initMemory(lengthinput+1)(lastinput)-- Runtime about 1:20m for part 2.
Technically simple but designed to catch out the users of, ahem, more academic languages! Rust should offer good performance of course. Optimised build runs both parts in about 5 seconds.
input=[1,17,0,10,18,11,6]spoken_numbers={}current_number=[]# n = 2020 # Part 1n=30000000n.timesdo|i|ifi<input.sizecurrent_number=input[i]elselast_number_spoken=current_numberifspoken_numbers[last_number_spoken]!=nillast_spoken=spoken_numbers[last_number_spoken].lastifspoken_numbers[last_number_spoken].size==1current_number=0elselast_spoken_before_that=spoken_numbers[last_number_spoken][-2]current_number=last_spoken-last_spoken_before_thatendendendspoken_numbers[current_number]||=[]spoken_numbers[current_number]<<iendpcurrent_number
Didn't really find this one interesting. Apparently it's a known series.
Either way, Ruby it was:
numbers=File.read('input.txt').chomp.split(',').map(&:to_i)turns=numbers.dupmemory=Hash.new{[]}numbers.each_with_indexdo|number,turn|memory[number]=[turn]puts"Turn #{turn+1}: #{number}"endgoal=30000000whileturns.length<goalturn=turns.lengthprevious=turns.lastbefore_last,last=memory[previous][-2..]speak=last.nil??0:last-before_last# You could store only the last two numbers, but with this set of data there# is really no pointmemory[speak]=memory[speak].push(turn)puts"Turn #{turn+1}, speaking #{speak} (#{(turn/goal.to_f*100).round}%)"if(turn+1)%(goal/100)==0turns.pushspeakendputsturns.last
I'm a Staff Software Engineer at Cisco. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
It's in Python, and it's not pretty, but I'm busy and it works and keeps me on track, so I'm counting that as a win. Just got to get through this week of finals and then I'll have more time :) Part two takes a while, but not like a "get up and get some coffee" while, so I'll take it.
"""Day 15: Rambunctions Recitation
Help the elves play a memory game where they remember which numbers
they said at which timestep.
It's not pretty, but it gets the job done, and I'm busy.
"""history=dict()starting=list(reversed([9,3,1,0,8,4]))foriinrange(1,30000001):ifstarting:current=starting.pop()eliffirst:current=0else:current=time_since_lastfirst=(currentnotinhistory)ifnotfirst:time_since_last=i-history[current]history[current]=iprint(current)
You can use a map to store the numbers you've seen. Map lookup is O(1), compared to looking through a list (O(n)). The keys can be the numbers you've seen, the values the list of positions.
We're a place where coders share, stay up-to-date and grow their careers.
I did a basic dynamic programming solution
My Haskell solution for today was quite short, but performance was shit.
Technically simple but designed to catch out the users of, ahem, more academic languages! Rust should offer good performance of course. Optimised build runs both parts in about 5 seconds.
Ruby solution. Same thing for Part 1 and 2.
Didn't really find this one interesting. Apparently it's a known series.
Either way, Ruby it was:
Here is my Haskell solution, please don't ask about performance for that big number :-)
COBOL as usual
It's in Python, and it's not pretty, but I'm busy and it works and keeps me on track, so I'm counting that as a win. Just got to get through this week of finals and then I'll have more time :) Part two takes a while, but not like a "get up and get some coffee" while, so I'll take it.
My JavaScript walkthrough:
Video:
You can use a map to store the numbers you've seen. Map lookup is O(1), compared to looking through a list (O(n)). The keys can be the numbers you've seen, the values the list of positions.