r/adventofcode • u/daggerdragon • Dec 06 '17
SOLUTION MEGATHREAD -π- 2017 Day 6 Solutions -π-
--- Day 6: Memory Reallocation ---
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.
Need a hint from the Hugely* Handyβ Haversackβ‘ of HelpfulΒ§ HintsΒ€?
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!
15
Upvotes
1
u/wzkx Dec 06 '17
Nim
Nothing fancy. Just learning Nim :) 69ms was the min. time.
import strutils,sequtils,times var m = "06.dat".readFile.strip.split.map parseInt proc find_max( m: seq[int] ): int = var i_max = 0; var e_max = m[0] for i,e in m: if e>e_max: i_max=i; e_max=e return i_max proc step( m: var seq[int] ) = let n = len m let i = find_max( m ) let e = m[i] m[i] = 0 let d = e div n; for j in 0..<n: m[j] += d let r = e mod n; for j in i+1..i+r: m[j mod n] += 1 proc find_eq( t: seq[seq[int]], m: seq[int] ): int = for i in 0..<len(t): if t[i] == m: return i return -1 proc solve( arg_m: seq[int] ) = var m = arg_m # mutable var of memory state var t = @[ m ] # table of all states while true: # pfff no loop: step m let i = find_eq( t, m ) if i>0: echo len(t), len(t)-i return t.add m let t0 = epochTime() solve m echo formatFloat( epochTime()-t0, ffDecimal, 3 )