r/adventofcode • u/anoi • Dec 06 '19
Upping the Ante [2019 Day 6] [Git] Version control is important
git is great for working with trees, so why not?
Here's how I generated a git repo from my sample data. The code's been slightly cleaned up since I ran it, and it takes a long time, so I haven't tested it again. Hopefully it still works.
The generated repo is here for my input. There's only one commit on master, but there's a whole bunch of tags/releases.
Part 1:
echo $(($(git log --all --pretty=oneline | cut -d' ' -f1 | \
xargs -n 1 git log --pretty=oneline | wc -l) - \
$(git log --all --pretty=oneline | wc -l)))Broken down: git log --all gets a list of every commit in the repo. cut pulls out the commit hash. xargs -n 1 will take each of those, and pass it back to git log. wc -l counts how many lines of output are generated. Basically, "for each commit, get its commit log, and add them all up". Finally, we subtract the total number of commits, as nodes do not orbit themselves.
Part 2: echo $(($(git log YOU...SAN --pretty=oneline | wc -l) - 2))
Broken down: YOU...SAN in git is "the set of commits that are reachable from either one of YOU or SAN but not from both." That is to say, it finds the most recent common ancestor, and only shows you stuff after that point (on either side of the fork). We have to subtract two because this will output both YOU and SAN.
Part 1 takes quite a long time to run, but part 2 surprised me by only taking about 7 seconds.
EDIT: u/CCC_037 gave me another idea
15
u/CCC_037 Dec 06 '19
Now I'm thinking that you could probably do something with a directory tree to represent this data...
8
u/amalloy Dec 06 '19
Instead of
git log --all --pretty=oneline | cut -d' ' -f1, why notgit rev-list --all? It's better to use plumbing commands from a script than to parse the output of porcelain commands.42
u/1vader Dec 06 '19
I thought this post was just some story about how git is important because you would have lost some changes or something but this is awesome!
13
u/miran1 Dec 06 '19
ASKALSKIANOI NO!13
u/M124367 Dec 06 '19
Now this is next level version control.
Using Git to track version history: <insert small brain>
Using Git to track planet orbits and calculate distance between two orbiting celestial objects in terms of hops through the most recent ancestor: <insert BIG BRAIN>
6
u/theriskster Dec 06 '19
r/madlads
2
u/miauw62 Dec 06 '19
i thought about this while i was solving the challenge, but i just dismissed it as ridiculous. i guess not!
2
u/e_blake Dec 07 '19
Part 2 can be golfed:
That is, by starting your search from the ancestor, you don't have to perform subtraction.
1
u/bugz945123 Dec 06 '19
Excellent! I love it!
1
u/mr_robb Dec 06 '19
Respect
1
u/littlesnorrboy Dec 06 '19
Damn
1
u/vypxl Dec 06 '19
Wow ok. Now I have a new view of git.
1
u/e_blake Dec 07 '19
Hmm. My input file includes lines such as:
CCD)W8T
176)S7S
B41)G6R
Note that those lines include valid 3-digit hex values. Fortunately, git requires at least 4 hex characters before it will start complaining that 'warning: refname 'CCD1' is ambiguous'; at only 3 characters, git finds only the tag, and not any other abbreviated object whose sha1 prefix starts with those 3 hex chars. If the puzzle had used 4-byte names, you'd have to take care of adding a disambiguation character (perhaps a 'g' prefix) to make all your tag names unambiguously non-hex strings and therefore no risk of a potential collision with the sha1 numbers computed while constructing your repository.