Author Topic: Advent of Code  (Read 116352 times)

Re: Advent of Code
« Reply #1000 on: 15 December, 2020, 10:33:48 am »
Day 14, part 2
(click to show/hide)

Day 15, part 2
(click to show/hide)

tonycollinet

  • No Longer a western province of Númenor
Re: Advent of Code
« Reply #1001 on: 15 December, 2020, 11:43:58 am »
Rewrote it in C to see how quick I could get it:-

Code: [Select]
$ gcc -ansi -pedantic -Wall -O4 15.c -o 15
$ time ./15 2 3 1
2020: 78
30000000: 6895259

real    0m0.813s
user    0m0.761s
sys     0m0.052s

(click to show/hide)

I tried the same in python, and it increased  execution time from 13s to nearly 15s

EDIT
(click to show/hide)

Re: Advent of Code
« Reply #1002 on: 15 December, 2020, 01:41:38 pm »
My Day 15 Part 2 is forecast to complete in around 4 hours :jurek:
Clever enough to know I'm not clever enough.

Davef

Advent of Code
« Reply #1003 on: 15 December, 2020, 02:46:38 pm »
My whopping array takes a few milliseconds

(click to show/hide)

Re: Advent of Code
« Reply #1004 on: 15 December, 2020, 03:27:16 pm »
My whopping array takes a few milliseconds

You can shave a tiny bit off if you remove the memset(). Linux should automatically give you zeroed memory.
"Yes please" said Squirrel "biscuits are our favourite things."

Re: Advent of Code
« Reply #1005 on: 15 December, 2020, 03:34:01 pm »
I finally wheeled out a programming language and decided to see quite how slow a naive C implementation would actually be. It's taken 45 minutes to get to 2.8 million and progress has fallen well below 1000 numbers per second.

Meanwhile I tried to use Applescript. The closest thing it has to a hashtable has keys fixed at compile time, so no good.

So in leiu of a spreadsheet or a programming language, some PHP, which does 30 million iterations in about 7 seconds:
(click to show/hide)

Davef, do you want to talk us through your code a little bit? My head indirectly hurts.

Re: Advent of Code
« Reply #1006 on: 15 December, 2020, 03:50:33 pm »
I finally wheeled out a programming language and decided to see quite how slow a naive C implementation would actually be. It's taken 45 minutes to get to 2.8 million and progress has fallen well below 1000 numbers per second.

Meanwhile I tried to use Applescript. The closest thing it has to a hashtable has keys fixed at compile time, so no good.

So in leiu of a spreadsheet or a programming language, some PHP, which does 30 million iterations in about 7 seconds:
(click to show/hide)

Davef, do you want to talk us through your code a little bit? My head indirectly hurts.

Here's my fast C code (it's slightly faster, and significantly more readable, than Davef's if I run both on my machine):-

(click to show/hide)
"Yes please" said Squirrel "biscuits are our favourite things."

Davef

Re: Advent of Code
« Reply #1007 on: 15 December, 2020, 03:52:25 pm »
I finally wheeled out a programming language and decided to see quite how slow a naive C implementation would actually be. It's taken 45 minutes to get to 2.8 million and progress has fallen well below 1000 numbers per second.

Meanwhile I tried to use Applescript. The closest thing it has to a hashtable has keys fixed at compile time, so no good.

So in leiu of a spreadsheet or a programming language, some PHP, which does 30 million iterations in about 7 seconds:
(click to show/hide)

Davef, do you want to talk us through your code a little bit? My head indirectly hurts.

Here's my fast C code (it's slightly faster, and significantly more readable, than Davef's if I run both on my machine):-

(click to show/hide)
Mine was specifically designed to be unreadable.

Davef

Advent of Code
« Reply #1008 on: 15 December, 2020, 03:59:36 pm »
I finally wheeled out a programming language and decided to see quite how slow a naive C implementation would actually be. It's taken 45 minutes to get to 2.8 million and progress has fallen well below 1000 numbers per second.

Meanwhile I tried to use Applescript. The closest thing it has to a hashtable has keys fixed at compile time, so no good.

So in leiu of a spreadsheet or a programming language, some PHP, which does 30 million iterations in about 7 seconds:
(click to show/hide)

Davef, do you want to talk us through your code a little bit? My head indirectly hurts.
Basically the same as yours but a big array instead of a dictionary And then I have pissed around to use as few characters as possible and make it as unreadable as possible!

e.g.
Int a[3000000]
a[19] = 1

How I actually wrote it first time with more characters  ...
(click to show/hide)

tonycollinet

  • No Longer a western province of Númenor
Re: Advent of Code
« Reply #1009 on: 15 December, 2020, 04:43:15 pm »
I finally wheeled out a programming language and decided to see quite how slow a naive C implementation would actually be. It's taken 45 minutes to get to 2.8 million and progress has fallen well below 1000 numbers per second.

Meanwhile I tried to use Applescript. The closest thing it has to a hashtable has keys fixed at compile time, so no good.

So in leiu of a spreadsheet or a programming language, some PHP, which does 30 million iterations in about 7 seconds:
(click to show/hide)

Davef, do you want to talk us through your code a little bit? My head indirectly hurts.

Here's my fast C code (it's slightly faster, and significantly more readable, than Davef's if I run both on my machine):-

(click to show/hide)

Where I am struggling with a lot of these C implementations - I can't see where your input arrives in the code - the first 5 (or whatever) numbers.

Davef

Advent of Code
« Reply #1010 on: 15 December, 2020, 04:50:21 pm »
I finally wheeled out a programming language and decided to see quite how slow a naive C implementation would actually be. It's taken 45 minutes to get to 2.8 million and progress has fallen well below 1000 numbers per second.

Meanwhile I tried to use Applescript. The closest thing it has to a hashtable has keys fixed at compile time, so no good.

So in leiu of a spreadsheet or a programming language, some PHP, which does 30 million iterations in about 7 seconds:
(click to show/hide)

Davef, do you want to talk us through your code a little bit? My head indirectly hurts.

Here's my fast C code (it's slightly faster, and significantly more readable, than Davef's if I run both on my machine):-

(click to show/hide)

Where I am struggling with a lot of these C implementations - I can't see where your input arrives in the code - the first 5 (or whatever) numbers.
In my obscure version at post 1004 above there is a ‘for’ loop after the comment that says parse input

It reads the input string
const char *s = "0,14,6,20,1,4";
 one ascii character at a time and converts to numbers and puts the numbers in the array. Normally I would use a library function for that but instead I convert the asciii to decimal by subtracting ‘0’ (and repeatedly multiplying by 10 if more than one char)

Edit: looking at greenbanks, his input is coming in on the command line. C programs main() function are invoked with an array of arguments in char *argv[]

Re: Advent of Code
« Reply #1011 on: 15 December, 2020, 07:42:11 pm »
On day 15 part 1, I had a list of numbers, and for 2020 numbers it didn't take long.

Once I set the same code to run to 30,000,000 numbers it was heading for all day with the computer fan running flat out, and it was slowing down.

(click to show/hide)
Quote from: Kim
Paging Diver300.  Diver300 to the GSM Trimphone, please...

tonycollinet

  • No Longer a western province of Númenor
Re: Advent of Code
« Reply #1012 on: 15 December, 2020, 08:37:03 pm »

Edit: looking at greenbanks, his input is coming in on the command line. C programs main() function are invoked with an array of arguments in char *argv[]

That'll be it - thanks.

Re: Advent of Code
« Reply #1013 on: 15 December, 2020, 11:43:01 pm »
Once I set the same code to run to 30,000,000 numbers it was heading for all day with the computer fan running flat out, and it was slowing down.

So I've worked out that (for my data), to get to 30 million by brute force you'd need to do 71 trillion comparisons. Whereas for 1 million it only needs to do 92 billion. Which is 771, and computer science tells us that's the same as 900.

So to finish in under an hour it needs to get to a million in under 4 seconds, or 23 billion comparisons per second. It's not implausible you could get there with SIMD and/or running it on a GPU.

ETA: I've got the inner loop down to three x86 assembly instructions (load+compare, decrement pointer, conditional jump) and it still takes 45 seconds to get to a million, suggesting a 10 hour completion time. Bah.

Davef

Re: Advent of Code
« Reply #1014 on: 16 December, 2020, 09:05:36 am »
Once I set the same code to run to 30,000,000 numbers it was heading for all day with the computer fan running flat out, and it was slowing down.

So I've worked out that (for my data), to get to 30 million by brute force you'd need to do 71 trillion comparisons. Whereas for 1 million it only needs to do 92 billion. Which is 771, and computer science tells us that's the same as 900.

So to finish in under an hour it needs to get to a million in under 4 seconds, or 23 billion comparisons per second. It's not implausible you could get there with SIMD and/or running it on a GPU.

ETA: I've got the inner loop down to three x86 assembly instructions (load+compare, decrement pointer, conditional jump) and it still takes 45 seconds to get to a million, suggesting a 10 hour completion time. Bah.
I think you have the problem the wrong way round. It should only take 30,000,000 loops

I have moved on to trying to squeeze my day 16 down in size. I feel it is going to spread to a couple of punch cards.

Re: Advent of Code
« Reply #1015 on: 16 December, 2020, 01:12:57 pm »
I think you have the problem the wrong way round. It should only take 30,000,000 loops

I have the problem the right way round, which is O(n^2), and that's how I like it. I'm trying to figure out if computers are fast enough yet to JFDI, at least for n = 30 million.



Davef

Advent of Code
« Reply #1016 on: 16 December, 2020, 02:01:05 pm »
I think you have the problem the wrong way round. It should only take 30,000,000 loops

I have the problem the right way round, which is O(n^2), and that's how I like it. I'm trying to figure out if computers are fast enough yet to JFDI, at least for n = 30 million.
Are you doing it with no storage then ?

With no storage it is O(n^2). With n integers of storage it is O(n)

Re: Advent of Code
« Reply #1017 on: 16 December, 2020, 02:06:58 pm »
I'm storing the raw list of numbers and looking backwards until I find a matching number (or, worse, don't).

Davef

Advent of Code
« Reply #1018 on: 16 December, 2020, 03:23:57 pm »
Day 16 2
(click to show/hide)
Better result processing using bitmask

Davef

Advent of Code
« Reply #1019 on: 16 December, 2020, 03:26:35 pm »
I'm storing the raw list of numbers and looking backwards until I find a matching number (or, worse, don't).
Why ? The beauty of arrays is that they provide fast access via an integer key

(click to show/hide)

Re: Advent of Code
« Reply #1020 on: 16 December, 2020, 06:19:18 pm »
Why ? The beauty of arrays is that they provide fast access via an integer key

Idle curiosity.

Clearly the question was set with the expectation that many people would create a naive O(n^2) algorithm for Part 1 and then have to replace it with something at least O(n log n) to calculate Part 2 in reasonable time. But I reckon it's possible to run an O(n^2) solution for n = 30 million in reasonable time.

Day 16:
(click to show/hide)

tonycollinet

  • No Longer a western province of Númenor
Re: Advent of Code
« Reply #1021 on: 16 December, 2020, 06:34:55 pm »
Day 16 done.

But I really don't enjoy the ones that result in a mess of data manipulation (how to code it) as distinct from puzzle solving (what do I need to code). It was more like work than fun.

(click to show/hide)

Re: Advent of Code
« Reply #1022 on: 16 December, 2020, 08:00:29 pm »
Why ? The beauty of arrays is that they provide fast access via an integer key

Idle curiosity.

Clearly the question was set with the expectation that many people would create a naive O(n^2) algorithm for Part 1 and then have to replace it with something at least O(n log n) to calculate Part 2 in reasonable time. But I reckon it's possible to run an O(n^2) solution for n = 30 million in reasonable time.

Have implemented what I think is the fastest O(n^2) algorithm for day 15 part 2. So far:-

Code: [Select]
$ time ./15slow 2 1 3
t=4 turn=300000
t=17 turn=600000
t=39 turn=900000
t=68 turn=1200000
t=106 turn=1500000
t=153 turn=1800000
t=208 turn=2100000
t=272 turn=2400000
t=347 turn=2700000

Each one of those lines represents 1% of the number of turns with the t value being the time in seconds it has been running.

Extrapolating I'd be happy if it's done in <24 hours.

[EDIT] The rate of change of the rate of change is about 10 seconds per 1%.

So extrapolating that gives 10 * 100 * 99 / 2 = 49500 seconds =~ 14h
"Yes please" said Squirrel "biscuits are our favourite things."

Re: Advent of Code
« Reply #1023 on: 16 December, 2020, 09:41:35 pm »
Day 16 done:
https://docs.google.com/spreadsheets/d/1t3jC4tIqWEm3IPSIk0FSWWqPeY4BrNc9HMUSfa0R2fg/edit?usp=sharing

Reasonably straightforward but a lot of steps and therefore a lot of annoying debugging cell reference typos.

Re: Advent of Code
« Reply #1024 on: 17 December, 2020, 08:13:41 am »
Have implemented what I think is the fastest O(n^2) algorithm for day 15 part 2. So far:-

Code: [Select]
$ time ./15slow 2 1 3
t=4 turn=300000
t=17 turn=600000
t=39 turn=900000
t=68 turn=1200000
t=106 turn=1500000
t=153 turn=1800000
t=208 turn=2100000
t=272 turn=2400000
t=347 turn=2700000

Each one of those lines represents 1% of the number of turns with the t value being the time in seconds it has been running.

Extrapolating I'd be happy if it's done in <24 hours.

[EDIT] The rate of change of the rate of change is about 10 seconds per 1%.

So extrapolating that gives 10 * 100 * 99 / 2 = 49500 seconds =~ 14h

It's exciting (and that updated prediction is looking quite accurate). Should be done inside an hour.

Code: [Select]
...
t=41498 turn=28800000
t=42355 turn=29100000
t=43223 turn=29400000
t=44095 turn=29700000

(The question is whether it will give the right answer! [Which should be 3544142 for the input I gave it.])
"Yes please" said Squirrel "biscuits are our favourite things."