/aocg/ - Advent of Code 2017 General #20

Previous Thread: adventofcode.com/
>Advent of Code is a series of small programming puzzles for a variety of skill levels. They are self-contained and are just as appropriate for an expert who wants to stay sharp as they are for a beginner who is just learning to code. Each puzzle calls upon different skills and has two parts that build on a theme.
You need to register with an account but you can appear as anonymous.

The new leaderboard with empty space is
join-code: 43046-941b8011

The old (full) leaderboard is
join-code: 194764-c554ff91

Other urls found in this thread:

pastebin.com/cy0Pcw6e
redblobgames.com/grids/hexagons/
twitter.com/NSFWRedditImage

Anyone else feeling like this years puzzles are way to easy?

Maybe you just got better this year?

Just gonna put in Yotsuba as filler for day 17

fuck it I can't fucking figure this out. I'm stuck on part 1 my code is not giving me the right answer I have to wait 5 min before I can resubmit.
fucking hell why is this piece of shit not working

maybe your code is wrong

Read carefully the instructions, you may have misunderstood something

Sometimes the best thing to do is start over from the beginning

you'll have fun with part 2.

I've deleted everything

>can't do part 2 of 7 because brainlet
>breeze through day 8 like it's nothing
what the fuck

Complexity of task never was strictly linear in AoCs.

modularize your code and unit test the smaller parts. its okay to have a separate function for everything, you can optimize and polish later on

Today's part 2 is pretty hard, but I quit 2015 after day 11 so it's probably easier this time.

Day 7 is still the only problem so far that requires an actual data structure
(And day 12 but day 12 is even easier)

Just started (late). Day 1 took me literally < 2 minutes in Java. Is the rest going to be this easy?

t. retard

don't @ me before you keep up

I've kinda given up right now, but actually building the inverted tower with a linked data structure I've already done at least once in C, but I have no idea how to do in python, especially the links

basically something like this
class Node:
nextNode = None

n1 = Node()
n2 = Node()
n3 = Node()
n1.nextNode = n2
n2.nextNode = n3

It would have to be more like this:
class Node:
def __init__(self, weight):
self.upNode = None
self.downNodes = []
self.weight = weight


And then build the tree by pointing the upNode to the program it sits on and appending nodes to the downNode list
The Node whose upNode is None at the end is the root

yup, listen to this guy

Ok someone give me your input and solution for d18 part 1.
I've rewritten everything and still get the same fucking answer

set i 31
set a 1
mul p 17
jgz p p
mul a 2
add i -1
jgz i -2
add a -1
set i 127
set p 622
mul p 8505
mod p a
mul p 129749
add p 12345
mod p a
set b p
mod b 10000
snd b
add i -1
jgz i -9
jgz a 3
rcv b
jgz b -1
set f 0
set i 126
rcv a
rcv b
set p a
mul p -1
add p b
jgz p 4
snd a
set a b
jgz 1 3
snd b
set f 1
add i -1
jgz i -11
snd a
jgz f -16
jgz a -19


Solution for part 1: 9423.

Puzzle input: pastebin.com/cy0Pcw6e
Part 1 answer: 3423

I approached part 1 differently
import time
start_time = time.time()
inputstring = the whole fucking input
inputarr = list(inputstring.split("/"))
discs = list(filter(lambda x: "-" in x, inputarr))
temp = []
final = []
for i in discs:
temp.append(list(i[i.index(">")+1:].split(",")))
for j in temp:
for k in j:
final.append(k)
for n in final:
try:
for k in discs:
if k.startswith(n+"("):
discs.remove(k)
except ValueError:
print("wow fuck you")
print(discs)
print("%s seconds" % (time.time() - start_time))
But yeah, thanks, I'll try that as soon as I'm done with day 9

set i 31
set a 1
mul p 17
jgz p p
mul a 2
add i -1
jgz i -2
add a -1
set i 127
set p 618
mul p 8505
mod p a
mul p 129749
add p 12345
mod p a
set b p
mod b 10000
snd b
add i -1
jgz i -9
jgz a 3
rcv b
jgz b -1
set f 0
set i 126
rcv a
rcv b
set p a
mul p -1
add p b
jgz p 4
snd a
set a b
jgz 1 3
snd b
set f 1
add i -1
jgz i -11
snd a
jgz f -16
jgz a -19


part1: 3423
part2: 7493

Thanks

But wait, see pic related. I don't get it. Explain in a retard friendly way pls.

hurrrr

Did we really not have an image for day 17? Man these threads have been slowing down.

Or maybe the user who kept making the images decided to succumb to brainletness and drop out

At the time of the first rcv instruction, what was the most recent value passed to snd prior to executing the rcv?

I think like 3 different people have been making the images but nobody had an idea for day 17
Make a variable named `sound` and every time you encounter a `snd x` instruction, store the value of `x` (which could be a number or a register) into the variable `sound`. Then the first time you encounter a `rcv x` instruction where `x` (which could be a number or a register) is non-zero, return the current value of `sound`.

p2: shit don't werk, does about 50k insns per prog and returns a 7xxx result.
Mos probs I'm doing something retarded, but I gotta cut it short today. Any obvious brainfarts that jump out at anyone in pic related?

nigga i'm gettan 7493 on MY input, got the "yuz a cheatin nigga" msg. fun times

set i 31
set a 1
mul p 17
jgz p p
mul a 2
add i -1
jgz i -2
add a -1
set i 127
set p 316
mul p 8505
mod p a
mul p 129749
add p 12345
mod p a
set b p
mod b 10000
snd b
add i -1
jgz i -9
jgz a 3
rcv b
jgz b -1
set f 0
set i 126
rcv a
rcv b
set p a
mul p -1
add p b
jgz p 4
snd a
set a b
jgz 1 3
snd b
set f 1
add i -1
jgz i -11
snd a
jgz f -16
jgz a -19

p1 2951 (correct)
p2 7493 at the moment (wrong)

Nevermind I finally got it. Oh god I'm so happy holy shit what a retard I am

Actually the `x` in `rcv x` is always a register but that's a story for part 2

you have to keep track or whatever value is after a instruction 'snd'. Only the last one that executes count and it has to be different to 0 to count.
for example let's say you have:
1. set a 0
2. snd a
3. rcv a
4. add a 1
5. snd a
6. rcv a

1. you set a to 0
2. you "play" a, however since it's value 0 it doesn't count and your internal last_sound variable should stay intact
3. tries to return a but since the value of last sound is 0 / undefined it does nothing
4. set a to 1
5. "plays" a, last_sound = a, therefore last_sound = 1
6. return the value of last_sound, and since last_sound > 0, then it returns 1

day 17 could be a fidget spinner with angery eyes

holly fuck, just ran your input in my code and also got 7493 for part2.. I guess it was a lucky guess for my part..
checked with the code with someone else and got the actual answer for your input.
It's a value between 7300 and 7400.

R8 my day 7 part 1 solution
ex = /([a-z]+) \((\d+)\)(?: -> (.+))?/
weight_hash = {} of String => Int32
tree_hash = {} of String => Array(String)
have_parent = [] of String
File.read("day7.txt").chomp.split("\n").each do |it|
match = ex.match(it)
if ! match.nil?
name = match[1]
weight = match.[2]
children = match.[3]?
child_array = children.nil? ? ([] of String) : children.split(", ")
weight_hash[name] = weight.to_i
tree_hash[name] = child_array
have_parent.concat child_array
end
end
puts "#{(tree_hash.keys - have_parent)[0]}"

Timing it:
time ./day7_1
vgzejbd
./day7_1 0.01s user 0.00s system 51% cpu 0.022 total

cheers. so I take it your code has the same issue, eh?
might go medium brainlet and give myself a hint 2moro - compare the vcpu state stream from execution of the same input by a correct implementation and my buggy one

>code for day 7 part 1
I solved it in 10 seconds by opening the input in notepad and control+f'ing till I found the bottom

shitty image coming up, to get day17 started

needs more spins

No it doesn't require a special datastructure, unless you count a map (associative collection) a specific data structure. The tree is representable by two hashmaps. One is name -> weight and one is name -> children names. That's completely sufficient. You just run a recursive algorithm on those two hashmaps.

>Find image related to puzzle i.e. tornado or some shit
>Add spinning around it comments from the thread (i.e. FUCK) and references
I'll do it in a second desu

Part 1 is trivial. Just find the name that's never named as a child to another node. THat's the root. I just took a set of all the names in the first column minus the set of all the names after all the ->. You should get a set of one name.

Two hashmaps is a datastructure my friend

THAT FUCKING FEELING WHEN YOU FINALLY FINISH THIS FUCKING DAY

As I said, strictly speaking it is, but people here have been cooking their own TreeNode implementations. Which aren't really needed.

>tfw brainlet

How the fuck do i print these numbers: 10, 10, 20, 30, 50, 80, 130, 210, 340, 550 without printing them like "print(10, 10, 20, 30, 50, 80, 130, 210, 340, 550)?

should i give up?

tfw gave up

everybody in this thread is literally smarter than me

for (auto i : { 10, 10, 20, 30, 50, 80, 130, 210, 340, 550 })
printf("%d\n", i);

started programming when I was 10-13
I'm 28
prolly got a bit of a head start on ya

I started when I was 29. Fuck, you must be good.

i know i have to print the first number, hold it somewhere, and add it up, and then repeat, but im trying, i'm not getting those numbers right.

here you go
for (int i = 0, j = 10, sum = 10; sum < 600; sum = i + j, i = j, j = sum)
printf("%d\n", sum);

worthwhile doing the previous years of this as someone whos been coding for a year properly in java?

thanks senpai, but im still a brainlet for sure.

I'm a decent programmer but I'm not amazing at this competition
it's the first one I've done
I think in future ones I'll be more prepared
and I'll use a language better suited to shitting out solutions in under 2 minutes

Kept getting part 2 wrong because I still thought it had to skip rcv when reg was 0.
Also look at this beauty. It shows that (the literary version of) APL was first used to describe a computer architecture, even if it's now embellished by the much needed structured programming constructs.
out ← exec code
regs ← 2 26⍴0
regs[;15] ← ⍳2
mail ← ⍬ ⍬
mcnt ← 0 0
pc ← 0 0
wait ← 0 0
thrd ← 0
rval ← {↑⍵∊⎕a:regs[thrd;⎕a⍳⍵]⋄⍎⍵}
→ begin
sched:
wait[thrd] ← 1
thrd ← wait⍳0
begin:
→ (thrd ≥ 2) / end
:repeat
→ ((pc[thrd] < 0) ∨ pc[thrd] ≥ ≢code) / sched
line ← pc[thrd] ⊃ code
:select ↑line
:case 'set' ⋄ regs[thrd;⎕a⍳1⊃line] ← rval 2 ⊃ line
:case 'add' ⋄ regs[thrd;⎕a⍳1⊃line] +← rval 2 ⊃ line
:case 'mul' ⋄ regs[thrd;⎕a⍳1⊃line] ×← rval 2 ⊃ line
:case 'mod' ⋄ regs[thrd;⎕a⍳1⊃line] |⍨← rval 2 ⊃ line
:case 'jgz' ⋄ pc[thrd] +← (1 -⍨ rval 2 ⊃ line) ∧ 0 < rval 1 ⊃ line
:case 'snd'
mcnt[thrd] +← 1
mail[∼thrd] ,← rval 1 ⊃ line
wait[∼thrd] ← 0
:case 'rcv'
:if 0 ≠ ≢thrd ⊃ mail
regs[thrd;⎕a⍳1⊃line] ← ↑ thrd ⊃ mail
mail[thrd] ↓⍨← 1
:else
→ sched
:endif
:else
⎕fmt line
:endselect
pc[thrd] +← 1
:endrepeat
end: out ← mcnt pc regs

And this is the input processing:
⎕io ← 0
data ← ⎕nread'I:\2017d18.txt'⎕ntie 0
data ← (≠∘' '⊂⊢)¨ (≠∘(⎕ucs 10)⊂⊢) data

>mfw off by one
debugging this will be fun

Hello, its me the late python fag.
I'm on question 2 but encountering a bit of a problem. Python 3 doesn't seem to be too kind to the idea of taking in the raw input of numbers with spaces between them, so its throwing a hissy-fit over syntax errors. How do i force python to take the input despite it being improper formatting?

write a function that takes a bunch of text and splits it on newline
then split each of those lines on space

Try taking each line of input and and using .split().

for line in sys.stdin:
numbers = [int(n) for n in line.split()]

Or
with open("2.txt") as file:
for line in file:
numbers = [int(n) for n in line.split()]

the splitting part it easy, the input part is the problem.
To illustrate my point:
def checksum(seq):
#splitting and suchforth happen here

>>>checksum(123 456 789)
"syntax error"

I need to force it to take the raw input and ignore formatting or syntax. splitting and suchforth is easy but i can't do that if it won't take the input to begin with.
I could try hacking code togeterh by going:
def checksum(seqA,seqB,seqC,seqD,seqE,seqF,seqG,seqH,seqI,seqJ,seqK,seqL,seqM...)
and then putting in the seperate numbers one piece at a time.

But that's what satan would do, so i would rather not.

good idea. I will just copy-pasta into a local csv or txt file and then use python's inbuilt file parsing functions to get the data.

with open("2.txt") as file:
for line in file:
numbers = [int(n) for n in line.split()]

only problem with that is that day 2's question is line intensive, so i need to seperate by newlines.
I think python can do that though, so it shouldn't be a problem.

put quotes around it

What if i just define the input variable as an object?

>checksum(123 456 789)
That's not valid Python...
>I need to force it to take the raw input and ignore formatting or syntax.
Input from where? input()?

for line in file automatically separates on newlines if `file` is a file object, which it is

...

i was actually given a similar problem on an exam, i was supposed to take any number of inputs seperated by commas and then calculate their sum.

Not the same user btw

in that case this is the best route to take.

i need it to take the input (123 456 789) directly, not give me shit about syntax.

123 456 789 is a syntax error, if you're trying to pass a string to checksum it should be checksum('123 456 789')

"Directly" is very vague, stdin? As part of code?
If you want to make Python "understand" 123 456 789 as the latter, there's no way.

>there's no way
you could write your own """""Python""""" interpreter

>If you want to make Python "understand" 123 456 789 as the latter, there's no way.
wth? what kind of bullshit is that?
I demand my program accept any and all input, regardless of validity. Remove the training wheels, oh michael!

>spent 4-5 hours on part 2, can't for the life of me figure out what I fucked up
>turns out I forgot to check for alphabetical first arguments with registers and jgz accepting integers (I was having integer strings as register names as a result)

Look on my streets, ye mighty, and poop in them.

>mfw day 11

Yes it only gets easier from there

Writing all my solutions again in Crystal. How's my day 8 part 2?

th-this is just a game right? Am I really going to miss Christmas if I don't get 50 stars?

> have all the problems solved
> get bored
> don't want to quit because I'm not a pussy

when will it end

So, I'm at 39711235 p1 sends and the code is still running, it looks like that's too many?

hmm, I guess the challenge is in writing a "clever" solution or a highly optimized solution.

if you dont get all the stars ill bring the christmas to you user

>Don't have to deal with people wanting presents
>Don't have to deal with people sending me shit I don't need
>Don't have to deal with people

I'd work my ass off for those 50 stars if it meant no one ever mentioned Christmas to me ever again.

LONDON
O
N
D
O
N

yeah London, ON

welp that was easy...
I refuse to believe anyone made that puzzle without googling.

I didn't google for it, but I also didn't implement proper hex coords.

Yeah, any correct solution will finish in seconds

Yeah, I was assigning the line number of the old process to the new process when I switched them.

This. There were loads of "solutions" that day using half-assed implementations of hex coords (mine included), which happened to work on the AOC input but failed spectacularly on lots of others.

I'm not sure what proper hex coords are, I just called the starting hex (0, 0), north and south incremented or decremented y by two and the others incremented or decremented x and y by one.

Yeah, that works, and actually I did the same thing. The half-assed part on mine was really the distance computation. Using that particular coordinate system means there are two cases to worry about, and I only implemented one, which happened to be the one that the AOC input fell into.

A lot of people had even more fucked-up coordinate systems, though. One that I remember is exactly like yours, except north/south steps by 1 instead of 2. This is flat-out wrong on all sorts of inputs, but happened to work for the AOC one. Another one is tracking distance along two of the three axes, such as north and northwest. This can work, but most people who tried it fucked up the distance function, though again, it tended to work on the AOC input.

The right way to do it is to use one of the options from the Red Blob article: redblobgames.com/grids/hexagons/
Preferably the 3D "cube" coordinate system, which has the most elegant math behind it.

Maybe what you want is
def checksum(*seq):
...

>>>checksum(123, 456, 789)

?

You still need commas, but the splat operator sounds like what you want

how long you been programming for?

#Node Class
class Node:
def __init__(self, data, pointer = None):
self.data = data
self.pointer = pointer

def __str__(self):
return self.data

#Your data-structure of choice
class [name](Node):
#define functions as needed.


Hope that helps