So thanks to Luna_blade, we know this started recently... anyone else interested.... ? http://adventofcode.com
[spoiler=My answer (in Python code) to the first part of the Day 1 problem (So don't look until you solve. But the main point is to show that it's short.)]myInput = "R4, R3, L3, L2, L1, R1, L1, R2, R3, L5, L5, R4, L4, R2, R4, L3, R3, L3, R3, R4, R2, L1, R2, L3, L2, L1, R3, R5, L1, L4, R2, L4, R3, R1, R2, L5, R2, L189, R5, L5, R52, R3, L1, R4, R5, R1, R4, L1, L3, R2, L2, L3, R4, R3, L2, L5, R4, R5, L2, R2, L1, L3, R3, L4, R4, R5, L1, L1, R3, L5, L2, R76, R2, R2, L1, L3, R189, L3, L4, L1, L3, R5, R4, L1, R1, L1, L1, R2, L4, R2, L5, L5, L5, R2, L4, L5, R4, R4, R5, L5, R3, L1, L3, L1, L1, L3, L4, R5, L3, R5, R3, R3, L5, L5, R3, R4, L3, R3, R1, R3, R2, R2, L1, R1, L3, L3, L3, L1, R2, L1, R4, R4, L1, L1, R3, R3, R4, R1, L5, L2, R2, R3, R2, L3, R4, L5, R1, R4, R5, R4, L4, R1, L3, R1, R3, L2, L3, R1, L2, R3, L3, L1, L3, R4, L4, L5, R3, R5, R4, R1, L2, R3, R5, L5, L4, L1, L1"
c = [ 0, 0, 0, 0]
d = 0 #Direction facing
for b in myInput.split(", "):
d += {"L": 1, "R": -1}[b[0]]
c[d&3] += int(b[1:])
print c #c[0], c[1], c[2], c[3]
print c[0] - c[2] + c[1] - c[3] #Remove the sign.
#Extra:
print d #Difference between L and R turns.[/spoiler]
Edit: Replaced if block with dictionary check, and swapped regex with the more simplified.
Do you write super short Python, or is it naturely this short?
Um? Both? Well, it depends on the coder... ( For example, someone who divides things up might code like https://github.com/jldork/Advent_04/blob/master/room.py , but that's a spoiler for today's AOC. ... And yeah, I looked on reddit to see what everyone's talking about. :) )
Someone else also tried Day 1, and their code was a bit longer than mine... They had written out if blocks for the caps, where-as I just &3.
My Part 2 for the Day 1 challenge was a bit longer than Part 1 as well... I didn't look into much optimizations.
[spoiler]import re
a = "R4, R3, L3, L2, L1, R1, L1, R2, R3, L5, L5, R4, L4, R2, R4, L3, R3, L3, R3, R4, R2, L1, R2, L3, L2, L1, R3, R5, L1, L4, R2, L4, R3, R1, R2, L5, R2, L189, R5, L5, R52, R3, L1, R4, R5, R1, R4, L1, L3, R2, L2, L3, R4, R3, L2, L5, R4, R5, L2, R2, L1, L3, R3, L4, R4, R5, L1, L1, R3, L5, L2, R76, R2, R2, L1, L3, R189, L3, L4, L1, L3, R5, R4, L1, R1, L1, L1, R2, L4, R2, L5, L5, L5, R2, L4, L5, R4, R4, R5, L5, R3, L1, L3, L1, L1, L3, L4, R5, L3, R5, R3, R3, L5, L5, R3, R4, L3, R3, R1, R3, R2, R2, L1, R1, L3, L3, L3, L1, R2, L1, R4, R4, L1, L1, R3, R3, R4, R1, L5, L2, R2, R3, R2, L3, R4, L5, R1, R4, R5, R4, L4, R1, L3, R1, R3, L2, L3, R1, L2, R3, L3, L1, L3, R4, L4, L5, R3, R5, R4, R1, L2, R3, R5, L5, L4, L1, L1"
wordList = re.sub("[^\w]", " ", a).split()
#Part 2
d = 0
mxy =
- * 1000000 #Random-ish number. But array-size.
x = 0
y = 0
for b in wordList:
if b[0] == 'R':
d -= 1
if b[0] == 'L':
d += 1
d = d & 3
i = int(b[1:])
while i > 0:
if mxy[y*1000+x] == 1:
print x, y
break
mxy[y*1000+x] = 1
if d == 0:
y += 1
elif d == 1:
x += 1
elif d == 2:
y -= 1
elif d == 3:
x -= 1
i -= 1[/spoiler]I think the forum BBCode stuff is messing up something. :/ Don't mess with my - , forum!
I don't know all the Python tricks yet, but it is possible it could be even shorter than that.
Like the L and R checks:
- Not sure if Python has a ? : operator.
- Could probably use a dictionary type.