diff options
author | Nat Lasseter <user@4574.co.uk> | 2019-12-12 12:13:56 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2019-12-12 12:13:56 +0000 |
commit | 2bb84ae6c6c71d51b445bf0b232c0eb6ca38e89b (patch) | |
tree | cf18f9c666a257d8c23699a5a152fb185aeeccd5 /day12/part2 | |
parent | 87f64bc00dbe5778bfc273754a3e561c1f25ac57 (diff) |
Day 12 part 2, working, using the potentially bad assumption that the cycle contains the initial state
Diffstat (limited to 'day12/part2')
-rwxr-xr-x | day12/part2 | 38 |
1 files changed, 13 insertions, 25 deletions
diff --git a/day12/part2 b/day12/part2 index 237432f..f5b0f96 100755 --- a/day12/part2 +++ b/day12/part2 @@ -38,43 +38,31 @@ moonsz = input.map do |line| [m[2].to_i, 0] end -xs = [moonsx.dup] +initx = moonsx.map { |m| [m[0], m[1]] } +xlen = 0 loop do moonsx = apply_gravity(moonsx) moonsx = update(moonsx) - if xs.include?(moonsx) then - break - else - xs << moonsx.dup - end + xlen += 1 + break if initx == moonsx end -xintro = xs.index(moonsx) -xlen = xs.count - xintro -ys = [moonsy.dup] +inity = moonsy.map { |m| [m[0], m[1]] } +ylen = 0 loop do moonsy = apply_gravity(moonsy) moonsy = update(moonsy) - if ys.include?(moonsy) then - break - else - ys << moonsy.dup - end + ylen += 1 + break if inity == moonsy end -yintro = ys.index(moonsy) -ylen = ys.count - yintro -zs = [moonsz.dup] +initz = moonsz.map { |m| [m[0], m[1]] } +zlen = 0 loop do moonsz = apply_gravity(moonsz) moonsz = update(moonsz) - if zs.include?(moonsz) then - break - else - zs << moonsz.dup - end + zlen += 1 + break if initz == moonsz end -zintro = zs.index(moonsz) -zlen = zs.count - zintro -puts [xintro, yintro, zintro].max + [xlen, ylen, zlen].reduce(1, :lcm) +puts [xlen, ylen, zlen].reduce(1, :lcm) |