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.
Private leaderboard join code: 368748-e33dcae3
People who are inactive for 5 days will be kicked, but other leaderboards can be made if desired.
Alternative leaderboard for those waiting for inactives to be kicked from the first: 208834-4a1a9383
I guess this isn't good gif material. I made the velocities too large
Jaxon Clark
Reminder that if you take longer than about two and a half hours to solve this, you and the elves would literally be better off waiting for the lights in the sky to align.
I've been looking at my older graphics programs, and fuck, I forgot that you can just use 1d Arrays for this shit and it's way less inconvenient than 2d arrays.
Joshua Flores
But what if you all blinked and missed it, user?
Easton Brooks
now we need actual bigboi input. with lots of noise, different scales and intersection points.
Chase Sullivan
make this the calendar image, without screencaps of posts
Today was gross import re, itertools, matplotlib.pyplot as plt with open('input.txt') as f: parse = lambda x: tuple(map(int, re.findall(r'-?\d+', x))) points = [*map(parse, f.read().splitlines())] def yield_all(points): for i in itertools.count(): yield (i, [(a + c * i, b + d * i) for (a,b,c,d) in points]) def lowest_bounds(points): generator = yield_all(points) (_, state) = next(generator) for j, trans in generator: current = sum(map(area, zip(*trans))) last = sum(map(area, zip(*state))) if current > last: return (state, j-1) state = trans bounds, area = lambda xs: (min(xs), max(xs)+1), lambda xs: max(xs) - min(xs) data, index = lowest_bounds(points); xs, ys = map(bounds, zip(*data)) index, plt.imshow([ [1 if (x,y) in data else 0 for x in range(*xs)] for y in range(*ys)], cmap="cool")
Agreed. Too many caps make the image noisy. It's hard to tell at a glance what the day was about.
Chase Morris
It's only gross if you make it gross. data=''' position=< 21456, 53293> velocity= '''.strip().splitlines()
from itertools import count import re
def p1(data): data = [((a,b),(c,d)) for x in data for a,b,c,d in [map(int,re.findall(r"-?\d+",x))]] for i in count(): minx,miny,maxx,maxy = (m(z[xy] for z,_ in data) for m in (min,max) for xy in (0,1)) if maxy-miny < 10: clist = [z for z,_ in data] for y in range(miny, maxy+1): print(''.join(('#' if (x,y) in clist else '.') for x in range(minx,maxx+1))) return i data = [((x+a,y+b),(a,b)) for (x,y),(a,b) in data]
> better how? this is the same old. we need a more creative bigboi
Connor Martinez
i think the poster meant the output user
Jaxon Murphy
>actually spent 30 minutes on input parsing before resorting to getting a regex from last thread Fuck. This is so ugly. Initially, I wasn't resetting the output but making a new grid each loop, which was obviously molasses-slow output. Then I guessed too high/low and missed the output a couple times, as the boundary is quite wide for the input and will print all over. And my print boundaries don't work correctly but at this point I don't care.
Screencaps shows what the day was about - anons can't read, anons break hardrives etc. If by "what day was about" you mean what was the puzzle then just go to fucking site and read it.
Ugly and felt like trial and error. Anyone got a solution that doesn't depend on the output being the smallest box, or on knowing the height of the text? Some user said he counted how many points were adjacent to each other, that's the only other thing I could think of.
>Some user said he counted how many points were adjacent to each other, that's the only other thing I could think of I thought about this but that seemed like crazy amount of work. You could probably look for vertical lines and if one is longer than like, 8 points, it's probably a letter
Nicholas Lopez
Okay, good point. Screencaps are important. I just like the images that are more clip art/GIMP than caps. Honestly, as long as it's not just screencaps, I'm fine with it. It's not like I'm the one putting in effort to make the images anyway.
Daniel Gonzalez
from collections import defaultdict from pprint import pprint
with open('day4/aoc-original.txt', 'r') as f: shifts = f.readlines()
highest = 0 for guard in d: total_sleep = sum(d[guard]) if total_sleep > highest: highest = total_sleep best_guard = guard
schedule = [['-']*59 for n in range(int(len(c[best_guard])/2))]
counter = 0 elements = 0 for time in c[best_guard]: if counter%2==0: sleep = time else: wake = time counter+=1 if counter>=2 and counter%2==0: for seconds in range(59): if seconds < sleep: schedule[elements][seconds]='-' elif seconds >= wake: schedule[elements][seconds]='-' else: schedule[elements][seconds]='X' elements+=1
correct_minute_count = 0 best_minute = 0 time_list = [0] * 60 highest_count = 0 for date in schedule: correct_date_count = 0 for index, time in enumerate(date): if time == 'X': time_list[correct_date_count] += 1 if time_list[correct_date_count] > highest_count: highest_count = time_list[correct_date_count] best_minute = index correct_date_count += 1
print(best_guard) print(best_minute) Why is day 4 part 1 so tedious? Or am I just a retard? pic related
>counted how many points were adjacent to each other, Isn't that identically correct to finding the first smallest box? Both are assuming all points converge once initially and that this convergence is the output.
David Roberts
user... import re
def day_4(lines: sorted): lines = iter(lines) guards = {} minutes = [0]*60 + [0] for s in lines: match = re.search(r'Guard #(\d+) begins', s) if match: id = int(match.group(1)) if id not in guards: guards[id] = minutes[:] elif 'falls' in s: m = int(s[15:17]) # minutes s = next(lines) # assume 'wakes' is next m2 = int(s[15:17]) guards[id][-1] += m2 - m for i in range(m, m2): guards[id][i] += 1
id = max(guards.keys(), key=lambda x: guards[x][-1]) part1 = id * max(range(60), key=lambda x: guards[id][x])
id = max(guards.keys(), key=lambda x: max(guards[x][:60])) part2 = id * max(range(60), key=lambda x: guards[id][x])
Fuck lmao, somehow I didn't internalise that the velocities are constant.
Zachary Kelly
If by "figure something like this out" you meant understand the solution, it's not that hard. Columns D and E are the intial positions of the stars. G and H are the velocities. J and K are equal to the positions + velocities * J3, where J3 is the number of seconds.To solve this, the person just graphed the final positions of the stars in a scatter plot and then fiddled with J3 until the graph showed the answer. If you meant "come up with this on your own," I think this solution is by the user who does everything in spreadsheets anyways. Today's problem was also perfect for spreadsheets because it involved simple math on arrays of data and required some sort of graphical feedback to find the answer.
Josiah Parker
wrote yours in haskell {-#LANGUAGE QuasiQuotes#-} import Text.RE.TDFA.String
main :: IO() main = solve.map regex.lines readFile "input.txt" >>= \(j, stars) -> do mapM_ putStrLn $format stars; putStrLn $"^- Spent "++show j++" Seconds -^" where regex = map read.matches.(*=~ [re|@{%int}|])
solve :: [[Int]] -> (Int, [(Int, Int)]) solve xss = head $dropWhile unbound [(i, [(x+u*i, y+v*i) | [x,y,u,v] [String] format xvs = [[if (x,y) `elem` xvs then '@' else ' ' | x
what all tools do you use for your usual haskell development, i've tried getting into haskell (on windows) but the IDEs i've used have all had serious problems, all i want is some kind of intellisense so i dont have to look at documentation to know whats in a library
Zachary Campbell
I'm new to python, how can I improve on my code? What am I doing incorrectly?
Bentley Cook
At first I was trying to treat each point as a line and then look for intersections.. then realised I was over thinking it and just did the smallest area
Hudson Turner
Kek. nice one user
Sebastian Morris
I rate it by how many points have an X coordinate in common with at least one other point. it worked but I think it would fail if the message contained too many letters like A, O or W
def starone(listf): count = 0 while 1: top, bot, rig, lef = max(i.x for i in listf), min(i.x for i in listf), max(i.y for i in listf), min(i.y for i in listf) while top > 1000: for i in listf: i.x += i.vx*40 i.y += i.vy*40 count += 40 print(count) top = max(i.x for i in listf) for i in listf: i.x += i.vx i.y += i.vy count += 1 print(count) image = np.zeros((1000,1000)) for i in listf: try: image[i.x][i.y] = 255 except IndexError: pass cv2.imshow("niggers", image) cv2.waitKey()
if __name__ == '__main__': import re import numpy as np import cv2 file = open("Z:\input.txt", 'r') inputstring = file.read() pattern = re.compile("(?:position=< *(-?\d*), *(-?\d*)> velocity=< *(-?\d*), *(-?\d*)>)") inputlist = re.findall(pattern,inputstring) lightlist = [] for i in inputlist: lightlist.append(lightpoint(int(i[0]),int(i[1]),int(i[2]),int(i[3]))) starone(lightlist.copy()) the most pajeet it's ever been the code doesn't even read correctly
Eli Gonzalez
Oh, right it's image[i.y][i.x]
Caleb Garcia
That's seriously convoluted. I thought my code was pretty pajeet and cramped:
#!/usr/bin/env python from __future__ import print_function import re
class Point(object): def __init__(self, x, y, vx, vy): self.x = x self.y = y self.vx = vx self.vy = vy
def print_grid(grid): coords = [None] * 4 for x, y in grid: if coords[0] is None or x < coords[0]: coords[0] = x if coords[1] is None or x > coords[1]: coords[1] = x if coords[2] is None or y < coords[2]: coords[2] = y if coords[3] is None or y > coords[3]: coords[3] = y
for y in range(coords[2], coords[3] + 1): for x in range(coords[0], coords[1] + 1): if (x, y) in prev_grid: print('#', end='') else: print('.', end='') print()
data = open('input').read() points = [] for p, v in re.findall(r'position=]*)> velocity=]*)>', data): x, y = [int(m) for m in re.findall(r'-?\d+', p)] vx, vy = [int(m) for m in re.findall(r'-?\d+', v)] points.append(Point(x, y, vx, vy))
def starone(listf): count = 0 top, bot = max(i.x for i in listf), min(i.x for i in listf) for i in listf: i.update(1) count += 1 ntop, nbot = max(i.x for i in listf), min(i.x for i in listf) while top > ntop and bot < nbot: top, bot = ntop, nbot for i in listf: i.update(1) count += 1 ntop, nbot = max(i.x for i in listf), min(i.x for i in listf) count -= 1 for i in listf: i.update(-1) image = np.zeros((1000,1000)) for i in listf: try: image[i.y][i.x] = 255 except IndexError: pass print(count) cv2.imshow("niggers", image) cv2.waitKey()
if __name__ == '__main__': import re import numpy as np import cv2 file = open("Z:\input.txt", 'r') inputstring = file.read() pattern = re.compile("(?:position=< *(-?\d*), *(-?\d*)> velocity=< *(-?\d*), *(-?\d*)>)") inputlist = re.findall(pattern,inputstring) lightlist = [] for i in inputlist: lightlist.append(lightpoint(int(i[0]),int(i[1]),int(i[2]),int(i[3]))) starone(lightlist.copy()) same thing as before, but way slower
Michael Parker
pajeet'd in ipython until I got the output. I can't believe I haven't been /filtered/ yet
What the hell was part 2 today? I thought i misread it somehow, it was so easy, literally just had to add one print statement to output a counter variable which i already had. Who were they trying to challenge there?
kinda disappointed in part 2s >what if... elf.... marble... TIMES HUNDRED!!! >ok... but how long... DOES LOOP RUN?!
Jaxon Johnson
That's a shame I enjoyed part 1 I wondered if perhaps it was just the specific way I did it that made part2 easy for me but i cant really think of how else it could be done
Evaluation took: 111.481 seconds of real time 111.437037 seconds of total run time (111.437035 user, 0.000002 system) [ Run times consist of 0.009 seconds GC time, and 111.429 seconds non-GC time. ] 99.96% CPU 378,328,869,426 processor cycles 276,593,184 bytes consed
10000000
Eh, not so bad for using lists of pairs of for both positions and velocities. BIG BOI also exposed a bug, I was using MOST-POSITIVE-FIXNUM as an initial area value which was less than the BIG BOI's area.
Luke Perry
Either my output is I I I I I I I I or I fucked something up
def starone(listf): count = 1 top, bot, ntop, nbot = max(i.x for i in listf), min(i.x for i in listf), max(i.nx for i in listf), min(i.nx for i in listf) step = max(abs((i.x)//(i.vx))//10*10 for i in listf) while abs(top-bot) > abs(ntop-nbot): top, bot = ntop, nbot for i in listf: i.update(step) count += step ntop, nbot = max(i.nx for i in listf), min(i.nx for i in listf) if abs(top-bot) < abs(ntop-nbot): for i in listf: i.update(-(step)) count -= step step = step//2 ntop, nbot = max(i.nx for i in listf), min(i.nx for i in listf) image = np.zeros((1000,1000)) for i in listf: try: image[i.y][i.x] = 255 except IndexError: pass print(count-1) cv2.imshow("niggers", image) cv2.waitKey()
if __name__ == '__main__': import re import numpy as np import cv2 file = open("Z:\input.txt", 'r') inputstring = file.read() pattern = re.compile("(?:position=< *(-?\d*), *(-?\d*)> velocity=< *(-?\d*), *(-?\d*)>)") inputlist = re.findall(pattern,inputstring) lightlist = [] for i in inputlist: lightlist.append(lightpoint(int(i[0]),int(i[1]),int(i[2]),int(i[3]))) starone(lightlist.copy())
10000000 7793 function calls (7736 primitive calls) in 0.054 seconds
I pretty much rewrote my JavaScript solution in Crystal It should be working
Cooper Thomas
Checklist of things that are in every AoC:
x sum and modulo problem to make brainlets happy x character counting problem x infinite matrix problem x graph theory problem - assembly interpreter - exhaustive enumeration problem - dijkstra x algorithmic optimization problem
Gavin Garcia
BIIIIIIIIGGG BOY INPUT IS READY! pastebin.com/L6AJwKmZ since the problem is quite restrictive when it comes to bigboi input, I MODIFIED the rules a little. there are now multiple times(t>0) when a part of the stars forms a letter. the task is to find those letters in the order (by time) they occur and concatenate them. HAVE FUN!
def starone(listf): count, step = 1, max(abs((i.x)//(i.vx))//10 for i in listf) odd = step%2 while 1: top, bot, ntop, nbot = max(i.x for i in listf), min(i.x for i in listf), max(i.nx for i in listf), min(i.nx for i in listf) while abs(top-bot) > abs(ntop-nbot): top, bot = ntop, nbot for i in listf: i.update(step) count += step ntop, nbot = max(i.nx for i in listf), min(i.nx for i in listf) if abs(top-bot) < abs(ntop-nbot): top, bot = ntop, nbot for i in listf: i.update(-step) count -= step step = step//2 if step == 1: break image = np.zeros((1000,1000)) if not odd: for i in listf: try: image[i.ny][i.nx] = 255 except IndexError: pass else: for i in listf: try: image[i.y][i.x] = 255 except IndexError: pass print(count-odd) cv2.imshow("niggers", image) profile.print_stats() cv2.waitKey()
if __name__ == '__main__': import re import numpy as np import cv2 import cProfile profile = cProfile.Profile() profile.enable() file = open("Z:\input.txt", 'r') inputstring = file.read() pattern = re.compile("(?:position=< *(-?\d*), *(-?\d*)> velocity=< *(-?\d*), *(-?\d*)>)") inputlist = re.findall(pattern,inputstring) lightlist = [] for i in inputlist: lightlist.append(lightpoint(int(i[0]),int(i[1]),int(i[2]),int(i[3]))) starone(lightlist.copy())
actually fixed it to work for all steps
Ethan Richardson
>having to compute distance density and turning a perfectly fine O(log N) solution into a O(N^3) monster fuck right off desu senpai
Jackson Allen
my shitty laptop would have never been able to handle O(N^3)
Owen Sanders
Took a break before trying to figure it out again. The issue was how I initialized my 2D Array in Crystal on which I print the map. Now I just gotta find out why its 3 times slower than JavaScript
good job, I was busy making a similar bigboy puzzle time to get-a working
Owen Harris
go back
Aiden Hill
>O(log N) how'd you do that?
Eli Taylor
$ g++ -Ofast -march=native c10.cpp -std=gnu++1y && time ./a.out #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.##.. #...............................................#..# #.#.....#.............#............#....#.......##.. ##.......#.............#..........#......#......##.. ##.......##.............#.........#.......#.....##.. ##........#.............#........##.......#.....##.. #.#.......#.#.......#..###...####.##.......#....##.. #..#......##...#####..........######.#.....#....#..# #...#......####....................####....#....##.. #....##.....#........###################...#....##.. #......#.....#########.#.###.#.######..#...#....##.. #.......#...#..#.####..######.########..#..#....##.. #.......##.#...#.######......#.#######..###.....##.. #......#.###...#######.......#..#####...#..#....##.. #.....#...#...################..##.##.#.....#...##.. #.....#....#..###...#############.####.......#..##.. #....#.#....######...#####...###.............#..##.. #....#..##..........####.......##..........##.#.##.. #...#..............#....#.....#..#............#.##.. #...#.............#....#.......#..#...........#.##.. #...#..........#.#....#.........#..#...........###.. #...#.........#.#......#########....#..........###.. #..#...........#........#....#.......#.........###.. #..#..........#.........#....#.......#.........###.. #.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#... 10000000 ./a.out 0.06s user 0.00s system 96% cpu 0.067 total
Robert Ramirez
I just rewrote day 10 since I wrote it in the morning with the usual method with which I was not pleased (iterate over each second and look for a point in time when the min-max height of the dataset was lowest), this time with golden selection search and it works like a charm now
Kayden Lopez
>-march=native can't find anything online about this flag
have you tried g++ -Ofast -ffast-math c10.cpp ....
?
Christian Hall
for any non-believers. I randomized the input and it is actually solvable.I have a "quick" (O(N**2)) algorithm. about 5 min runtime in python.
I tried it, didn't help much. the reason it was so slow was how I decided whether or not the picture was found. I counted the number of stars that are on the same X coordinate as one other star. When I change it to just look for the smallest area it finishes in 2ms
Owen Butler
if i search "-march=native" doesnt show anything for me. had to remove the first "-" to see anything
Ethan Morales
on most seach engines, if you search -something that means exclude something from results
Jayden Moore
How can I find the correct steps number without fucking everything up with the output?
by checking the smaller grid size? by checking if some # are aligned? help pls
Adrian Collins
when you don't know, try them all, starting with the simplest
Noah Ward
!!! I figured it out: check when the Y axis is
Dominic Morgan
fuck, how do i print unicode chars in c++? i triew std::wcout
widechars aren't unicode. print regular utf8, any half decent terminal will display it correctly (u8")
Eli Baker
that's not really proofing, the bounding box can get smaller than the bounding box of the message's state, the edge case being all points converging to a single coordinate, in which the message's bounding box isn't the smallest bounding box the points will ever occupy it's not a P-complete problem