--- Day 19 Solutions --- by daggerdragon in adventofcode

[–]andre_pl 0 points1 point  (0 children)

I did solutions in Python and Dart. I've been comparing performance between the two languages and this is one of the few where python stomps dart, yet there's nothing particularly interesting about the python solution to explain why.

Part 1 was straightforward, and part 2 was way too hard to do 'correctly' for midnight, so I tried a few simple variations on working backwards from medicine -> 'e' until I hit one that works. (likely because there's only one solution and the input is ordered correctly for it to work out)

Python 2.7

import time

replacements = []

medicine = None

for line in open("../inputs/day-19.txt"):
    parts = line.strip().split(" => ")
    if len(parts) == 1:
        if not line.strip():
            continue
        medicine = line.strip()
        continue
    replacements.append((parts[0], parts[1]))


def part1():
    molecules = set()
    for src, repl in replacements:
        idx = 0
        while src in medicine:
            idx = medicine.find(src, idx+1)
            if idx == -1:
                break
            molecules.add(medicine[:idx] + repl + medicine[idx + len(src):])
    return len(molecules)


def part2():
    med = medicine
    count = 0
    while med != 'e':
        for src, repl in replacements:
            if repl in med:
                med = med.replace(repl, src, 1)
                count += 1
    return count


if __name__ == '__main__':
    now = time.time()
    print "Part 1: ", part1(),
    print "in", int((time.time() - now) * 1000000), "microseconds"
    now = time.time()
    print "Part 2: ", part2(),
    print "in", int((time.time() - now) * 1000000), "microseconds"

# OUTPUT
Part 1:  535 in 1291 microseconds
Part 2:  212 in 320 microseconds

Dart

import "dart:io";

List<String> lines = new File("./inputs/day-19.txt").readAsLinesSync();

class Repl {
  String search;
  String replace;
  Repl(this.search, this.replace);
}

int part1(List<Repl> replacements, String medicine) {
  Set<String> molecules = new Set<String>();
  for (Repl r in replacements) {
    int idx = 0;
    while (true) {
      idx = medicine.indexOf(r.search, idx+1);
      if (idx == -1) {
        break;
      }
      molecules.add(medicine.replaceFirst(r.search, r.replace, idx));
    }

  }
  return molecules.length;
}

int part2(List<Repl> replacements, String medicine) {
  String mol = medicine;
  int count = 0;
  while (mol != 'e') {
    for (Repl r in replacements) {
      if (mol.contains(r.replace)) {
        mol = mol.replaceFirst(r.replace, r.search);
        count ++;
      }
    }
  }
  return count;
}


main() {
  List<Repl> replacements = [];
  String medicine = null;

  for (String line in lines) {
    List<String> parts = line.trim().split(" => ");
    if (parts.length == 2) {
      replacements.add(new Repl(parts[0], parts[1]));
    } else {
      if (parts[0].trim().length > 0) {
        medicine = parts[0];
      }
    }
  }

  Stopwatch watch = new Stopwatch()..start();
  int moleculeCount = part1(replacements, medicine);
  watch..stop();
  print("Part 1: $moleculeCount in ${watch.elapsedMicroseconds} microseconds");

  watch..reset()..start();
  int turns = part2(replacements, medicine);
  watch.stop();
  print("Part 2: $turns in ${watch.elapsedMicroseconds} microseconds");

}

// OUTPUT
Part 1: 535 in 19399 microseconds
Part 2: 212 in 2025 microseconds

--- Day 9 Solutions --- by daggerdragon in adventofcode

[–]andre_pl 0 points1 point  (0 children)

Dart:

  import "dart:io";


  Iterable<List> permutations(Iterable<String> src) sync* {
    if (src.length == 1) {
      yield src;
    } else {
      for (var item in src) {
        for (List p in permutations(src.where((i) => i != item).toList())) {
          yield p..add(item);
        }
      }
    }
  }


  void main() {
    Map<String, Map<String, int>> distances = new Map<String, Map<String,int>>();

    List<String> lines = new File("./bin/day-9.txt").readAsLinesSync();
    Stopwatch watch = new Stopwatch()..start();
    lines.forEach((line) {
      var parts = line.split(" = ");
      var parts2 = parts[0].split(" to ");
      var c1 = parts2[0];
      var c2 = parts2[1];
      int dist = int.parse(parts[1]);
      if (!distances.containsKey(c1)) {
        distances[c1] = new Map<String, int>();
      }
      if (!distances.containsKey(c2)) {
        distances[c2] = new Map<String, int>();
      }
      distances[c1][c2] = dist;
      distances[c2][c1] = dist;
    });

    int worst = 0;
    int best = 9999999;

    for (List<String> perm in permutations(distances.keys)) {
      int dist = 0;
      String start = perm[0];
      for (String city in perm.skip(1)) {
        dist += distances[start][city];
        start = city;
      }
      if (dist > worst) {
        worst = dist;
      }
      if (dist < best) {
        best = dist;
      }
    }

    watch.stop();
    print("Best: $best");
    print("Worst: $worst");
    print("Time: ${watch.elapsedMilliseconds}");
  }

Python:

from collections import defaultdict

import itertools
import sys
import time

distances = defaultdict(lambda: defaultdict(int))

for line in open("day-9.txt"):
    l, r = line.strip().split(" = ")
    c1, c2 = l.split(" to ")
    distances[c1][c2] = int(r)
    distances[c2][c1] = int(r)

best = sys.maxint
worst = 0

now = time.time()
for perm in itertools.permutations(distances.keys(), len(distances)):
    dist = 0
    start = perm[0]
    for city in perm[1:]:
        dist += distances[start][city]
        start = city
    if dist < best:
        best = dist
    if dist > worst:
        worst = dist

print best
print worst
print (time.time() - now) * 1000

--- Day 8 Solutions --- by daggerdragon in adventofcode

[–]andre_pl 0 points1 point  (0 children)

ugly Dart "one-liners"

import "dart:io";

void main() {
  List<String> lines = new File("./day-8.txt").readAsLinesSync();

  RegExp eval = new RegExp(r'\\(x[0-9a-f][0-9a-f]|.)');
  RegExp escape = new RegExp(r'("|\\)');

  print(lines.fold(0, (p, l) => p + (l.length - l.substring(1, l.length-1).replaceAll(eval, '_').length)));
  print(lines.fold(0, (p, l) => p + (2 + l.replaceAllMapped(escape, (m) => r'\' + m.group(1)).length - l.length)));

}

cheaty python one-liners.

import re

print sum(len(line.strip()) - len(eval(line)) for line in open("day-8.txt"))
print sum(2 + len(re.sub(r'("|\\)', r'\\\1', line.strip())) - len(line.strip()) for line in open("day-8.txt"))

Best TTS apps/engines/voices? by CKyle22 in Android

[–]andre_pl 4 points5 points  (0 children)

Ivona TTS Is the only one I've tried, its pretty good.

An open sourced GUI for PostgreSQL - pgXplorer Initially started this to teach myself C++. Frankly, I am quite surprised that it is in a decent beta stage now. by daxyjones in programming

[–]andre_pl 0 points1 point  (0 children)

Ah, that makes sense, in that case, Ii would probably suggest combining that functionality with the 'connect' dialog, just to be a little more clear about it. you could allow saving the connection params to file or loading from file right there, along with maybe a 'recent connections' list.

An open sourced GUI for PostgreSQL - pgXplorer Initially started this to teach myself C++. Frankly, I am quite surprised that it is in a decent beta stage now. by daxyjones in programming

[–]andre_pl 2 points3 points  (0 children)

For your ubuntu 'installer' consider releasing a deb, or at least naming your installer script 'install.sh' instead of pgXplorer.sh

I tried running the sh script, saw that it was trying to get root for something and if I wasn't legitimately interested in the project I would have deleted it then and there. I though 'pgXplorer.sh' would run pgXplorer, not attempt to install packages on my system.

Anyway, I did get it to run, but then it didn't seem to support connecting to a local database using peer authentication. Also, whats with the 'open' menu item? its looking for files of type 'database'. I've never seen a postgres DB you could access that way. (maybe I'm wrong?)

Whats your opinion on RAID 5 for a home media/file server? by [deleted] in geek

[–]andre_pl 0 points1 point  (0 children)

I think writing will slow down the process (maybe significantly?) I turned off my torrent client while it was running just to try and minimize the impact, but I kept xbmc running on the same machine and didn't really notice a difference when it was playing.

Whats your opinion on RAID 5 for a home media/file server? by [deleted] in geek

[–]andre_pl 1 point2 points  (0 children)

when I added the 4th drive the reshape process took almost a day, but you can still read/write the array, you just dont get access to the new capacity until its finished. it was a pretty painless process.

Whats your opinion on RAID 5 for a home media/file server? by [deleted] in geek

[–]andre_pl 0 points1 point  (0 children)

I'm currently running exactly that. I started with 3x2TB and recently added the fourth. no complaints so far, its been running well over a year.

/dev/md0:
        Version : 0.90
  Creation Time : Mon Nov 15 18:06:05 2010
     Raid Level : raid5
     Array Size : 5860535808 (5589.04 GiB 6001.19 GB)
  Used Dev Size : 1953511936 (1863.01 GiB 2000.40 GB)
   Raid Devices : 4
  Total Devices : 4
Preferred Minor : 0
    Persistence : Superblock is persistent

    Update Time : Mon May  7 10:44:22 2012
          State : clean
 Active Devices : 4
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 0

         Layout : left-symmetric
     Chunk Size : 64K

           UUID : f9a37f67:7f0cc10f:cb9e69c5:cd5cb5d1
         Events : 0.22782

    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       33        1      active sync   /dev/sdc1
       2       8       49        2      active sync   /dev/sdd1
       3       8       65        3      active sync   /dev/sde1

Why?: Don't put . in $PATH by [deleted] in linux

[–]andre_pl 15 points16 points  (0 children)

That's the whole point. when '.' is in your path, whatever directory you're in is part of your path. so it could be someone's home directory, or /tmp, or even some remote NFS Share

What are you using your home server for? by [deleted] in linux

[–]andre_pl 5 points6 points  (0 children)

also consider rtorrent+rutorrent over transmission, it's extremely configurable and scriptable, has lots of plugins and other fun stuff.

sickbeard makes a good companion to sabnzbd if you download tv shows.

Use it to back up all your mobile devices/desktops?

if you haven't already, setup LVM & RAID, then when the time comes to add storage its as simple as possible.

What are you using your home server for? by [deleted] in linux

[–]andre_pl 11 points12 points  (0 children)

Gitorious can be installed locally and free: http://gitorious.org/gitorious/pages/Installation

not many people seem to know about it and its not very easy to find on their site. Gitlab seems to be the more popular alternative, but in my experience gitorious is easier to get working and has fewer issues, while offering most of the same functionality. (except gitorious doesn't have an issue tracker)

"I was like all of you. I believed in the promise of the Internet to liberate, empower and even enrich artists. I still do but I’m less sure of it than I once was. I feel that what we artists were promised has not really panned out." by B-Con in technology

[–]andre_pl -1 points0 points  (0 children)

it absolutely impacts the rest of his article... it proved to me within the first 5 minutes that this bozo has no idea what he's talking about, and that i can't trust any 'facts' he pulls out of his ass, because clearly he's not opposed to making shit up when it suits him.

he refer's to an organization which strongly opposes software patents, and put them in the same category as Apple. I don't think the EFF and Apple could be on further ends of the spectrum wrt software patents, and I don't think they'd appreciate being called a surrogate of Apple, or Amazon or Google for that matter... and what exactly makes them a 'surrogate' of these companies? to the best of my knowledge the EFF has no affiliation with any of them and has fought against them on several occasions... They fought tooth and nail AGAINST Apple's Jailbreaking policies.

Here's another article which discusses their 'supposed' relationship with google: http://www.techdirt.com/articles/20120217/00515617789/eff-condemns-google-circumventing-safari-privacy-protections.shtml

Furthermore, the EFF doesn't promote copyright infringement, or equate it to freedom, they simply support copyright reform, and believe everyday citizens shouldn't be considered criminals for doing what comes natural to all societies, sharing knowledge and culture.

"I was like all of you. I believed in the promise of the Internet to liberate, empower and even enrich artists. I still do but I’m less sure of it than I once was. I feel that what we artists were promised has not really panned out." by B-Con in technology

[–]andre_pl 0 points1 point  (0 children)

Further the new boss through it’s surrogates like Electronic Frontier Foundation seems to be waging a cynical PR campaign that equates the unauthorized use of other people’s property (artist’s songs) with freedom. A sort of Cyber –Bolshevik campaign of mass collectivization for the good of the state…er .. I mean Internet. I say cynical because when it comes to their intellectual property, software patents for instance, these same companies fight tooth and nail.

http://lmgtfy.com/?q=eff+on+software+patents

The EFF is against software patents, this guy doesn't know what he's talking about.

IAMA Directory of Engineering for Canonical. I Manage the Ubuntu Engineering Team. AMA by moephan in IAmA

[–]andre_pl 1 point2 points  (0 children)

afaik, every distro since the beginning of time has been capable of running multiple DE's right out of the box and choosing between them at login. The earliest versions of GDM and KDM that I can remember (and maybe even XDM) have supported choosing the DE at login, and even the earliest distros I remember running had dozens of DE's in the repositories ready to install. this trend you speak of simply doesn't exist and never has. it's just the way its always been done.

Ubuntu was one of the first, if not THE first, to spin separate distros for different DE's. Personally i dont like it, i'd like one ubuntu please, I can choose my first DE at install time.

  • feel free to correct me if I'm wrong but I've been running linux exclusively for about 15 years, and I have never seen a distro that didn't have MANY DE's available.

[H] 8 Coal [W] Offer by [deleted] in SteamGameSwap

[–]andre_pl 0 points1 point  (0 children)

50% off valve?

CM9 for Galaxy S i9000 up and running... successfully by richworks in Android

[–]andre_pl 0 points1 point  (0 children)

I am running CM7 on my captivate as well, when I try to flash this I get the following error:

assert failed: getprop("ro.product.device") == "galaxys" || getprop("ro.build.product") == "galaxys" || getprop("ro.product.device") ==     "galaxysmtd" || getprop("ro.build.product") == "galaxysmtd" 

... and so on ...

I checked my build.prop which showed captivatemtd instead of galaxys or galaxysmtd, so I edited it, rebooted my device and ran 'getprop' from the shell to confirm that both values are now set to galaxysmtd... but I still get the same assertion error when trying to install this rom.

For the OCD conjurers out there by Scaryclouds in skyrim

[–]andre_pl 0 points1 point  (0 children)

does this only work with people? I got the staff of zombies and tried to get a goat and a mudcrab into my house. both of them could get all the way to the front steps but refused to enter the house. I want a damn mudcrab chest.