advent of code 2020 day 10
so this is probably what was intended for part 2
but i did make it completely unreadable
https://github.com/undergroundmonorail/advent-of-code-2020/blob/main/day%2010/part2_alternate.py
unfortunately, as clever as the idea behind this solution is (i didn't come up with it lmao), i just threw a cache on the recursive function and it worked just as quickly
this one works without a cache though
advent of code 2020 day 11
advent of code 2020 day 12
extremely simple day. only thing that gave me trouble was just forgetting that the waypoint starts at (10, 1) for part 2
i didn't get to reuse any of the actual computation code though, just input parsing
https://github.com/undergroundmonorail/advent-of-code-2020/tree/main/day%2012
advent of code 2020 day 13
i want to say something like
maybe you only have to check "near" multiples of the LCM? for whatever "near" means. but that doesn't seem right
maybe something like subtracting out the offsets, finding the LCM, then adding the offsets back in somehow? no, that doesn't make sense either
advent of code 2020 day 13
no it's too hard to do it in my head, i'm going to do it here
the chinese remainder theorem lets you find x for a system like
x = n_1 % a_1
x = n_2 % a_2
x = n_3 % a_3
etc
let's say i have an input of something like `5,x,7`. that means i have to find a timestamp where timestamp % 5 = 0 and timestamp % 7 = 5 (i.e. -2)
but if timestamp % 7 = 5, that means (timestamp + 2) % 7 = 0
so in this case i'm looking for numbers that satisfy
0 = (x + 0) % 5
0 = (x + 2) % 7
which is *so close*
advent of code 2020 day 13
i want to just like
do it
like can i just...
0 = (x + n) % bus_n
-x = n % bus_n
...no that doesn't make sense. n % bus_n is just going to be n pretty much all the time, and -x means something completely different when it's congruent to something modulo whatever so when you resolve that they'll all be different again
advent of code 2020 day 14
yeah yeah i haven't finished day 13 yet, shut up i'll get to it
https://github.com/undergroundmonorail/advent-of-code-2020/tree/main/day%2014
easy day, i put in more effort than was actually required but it was fun. while solving part 2 i got an idea in my head like "could i write a recursive function that handles the floating bits at the same time as the conversion from binary to decimal" and sure enough, here we are. took a couple swings at it even after i got one working and i'm proud of how it came out
advent of code 2020 day 13 2: electric boogaloo
going back to this
after talking to emi it turns out i was extremely close to what i wanted
for a bunch of busses with offsets and ids, i had
0 = (x + bus_offset) % bus_id
and was struggling to get x on its own
the problem was that i was thinking of modular arithmetic from the way it's used as an operation in programming, not the way it's used in math: a fact about the entire congruence
so the way i get x on its own is just...
x = -bus_offset % bus_id
oops
advent of code 2020 day 13 2: electric boogaloo
okay i solved it
it involved an algorithm that i understand the broad strokes about why it works but don't really get the details of exactly, and it's a very naive implementation of that algorithm (i'm sure there are nicer ways to do it in python but... you'd need to understand the details exactly)
but it's done https://github.com/undergroundmonorail/advent-of-code-2020/blob/main/day%2013/part2.py
advent of code 2020 day 15
so this last thing was incorrect, i accidentally was measuring the runtime of an infinite loop, not a 30000000 iteration loop
here's the day 15 code https://github.com/undergroundmonorail/advent-of-code-2020/tree/main/day%2015
part 2 is a mess because i didn't feel like doing any actual thinking to get rid of the off-by-one errors, i just kind of massaged it until it spat out the right answers for the example data on 10 and 2020 and then gave it the real data on 3*10^7
advent of code 2020 day 16
oof
i figured i'd experiment with closures for this one and found some behaviour i actually really don't like
wrote a function like:
def parse_rules(field_rules):
for rule in field_rules:
a, b, c, d = map(int, re.findall(r'(\d+)-(\d+) or (\d+)-(\d+)', rule)[0])
yield lambda n, a, b, c, d: a <= n <= b or c <= n <= d
it extracts the four numbers out of all the rules and then yields a function with behaviour defined by those numbers
but
the closure doesn't remember the value of the numbers being returned, it only remembers what value it was pointing at. so as a, b, c and d were changing, it changed how all the lambda functions behaved. every single one was verifying that a number matched the last rule
and the way you fix that is: abuse default arguments. the lambas i'm yielding now look like this:
yield lambda n, a=a, b=b, c=c, d=d: a <= n <= b or c <= n <= d
advent of code 2020 day 16
so
i had a dictionary called rule_possibilities and my goal was to empty it out following some rules
i thought i was going to have to do an expensive operation to do that, but i could make it less expensive by removing the easy ones from the dictionary first, so i wrote the code to remove all the easy ones
and then it turned out that as i did that, they all became "easy", so when i was done with the easy ones there was nothing left
which is why my part 2 code has this block in it:
if rule_possibilities:
# i thought i was going to have to write code for this case but i guess i don't
pass
as for why the rest of the code sucks: i don't really have an excuse
https://github.com/undergroundmonorail/advent-of-code-2020/tree/main/day%2016
advent of code 2020 day 17
actually you know what
it worked
it takes 50 seconds and i could speed it up significantly but i don't fuckin want to. not an interesting challenge, just tedious
here is my extremely hacky code for extending my LIfeLikeCell class into new dimensions https://github.com/undergroundmonorail/advent-of-code-2020/tree/main/day%2017
re: advent of code 2020 day 17
@monorail oh dang
I just had a lot of nested loops
re: advent of code 2020 day 17
re: advent of code 2020 day 17
@monorail now I'm wondering how hard it'd be to make a solution for n-dimensional grids