[Blogging Intensifies]

Technology, Projects, Linux, Coding, Internet of Things, Music, Books, Life...

  • About
  • Code Projects Portfolio
  • Friends
  • Photo Gallery

Advent of Code

Advent of Code 2022, Day 12

December 12, 2022

So, day 12, you get an elevation map, and you get to run a path-finding algorithm on it to find the shortest path up the hill.

That sounds kind of complicated.

So I did it with the best computer I have available, my mind. And Microsoft Word, and some color coded path markers. I did do a bit of black outs on obvious dead ends so my final map looks like some sort of government document but I got the answer. It probably took me less time than it would have to figure out and write code to do it too. I started after finishing breakfast, put on a podcast on the headphones and a half hour later, I had the map.

I did have a brain fart that screwed me up, For some reason I was using the Upper Left corner as “Start”. Start was actually the big fat “S” in the middle of the first column.

Anyway, no code, but here is an image of my finished map.

Posted in: Advent of Code Tagged: AdventOfCode2022, Coding, Coding?

Advent of Code 2022, Day 11

December 11, 2022

So, now that you’re foraging through the jungle alone, you have discovered that Monkeys have stolen your items and are tossing them around. Use MATH to build a shotgun out of bamboo reeds and vines and stop the Monkeys!

No wait, that’s not friendly. Instead maybe calculate the rounds your items spend being tossed around based on some Mysterious Monkey Criteria and try to get your items back. There is probably some easy library to process the input, but that’s not how I’m rolling on these puzzles, so like 90% of this problem was processing the “Almost a dictionary input” into a format that json.loads would accept and transform into a dictionary. Then massaging that data just a bit more because my loop was adding an empty item each iteration and breaking things, but I can’t just lop that item off because the initial data doesn’t have an empty item. That’s easily fixed by adding an empty item to the initial data.

Part 1 does this loop for 20 rounds.

Part 2 does it for 10,000 rounds. (with slight modification to the code)

This is where some fancy library would probably be better, but instead I just, left my laptop running and made lunch…. and played some Fortnite… and probably made supper too, I’m not sure I am not there yet, it’s still lunch time while I am writing this. I am feeling confident it will work though. It hasn’t creashed yet at least. But maybe It will run out of memory before it can finish…. Or the heat death of the universe….

Or I can use the “Modulo Trick” which is to reduce the numbers down each round by a common multiple. Which literally finished in seconds, and was correct the first try.

Also, I can’t spell “Monkeys” but fuck it, I am leaving it.

import math
import json

with open("Day11Input.txt") as file:
    data = file.read()

split = data.split("\n\n")

monkies = {}

for each in split:
    each = each.replace('Monkey ', '{"')
    each = each.replace(': ', '": "')
    each = each.replace('\n  ', '", "')
    each = each.replace(':", "Starting ', '": {"')
    each = each.replace('new = old ', '')
    each = each.replace('divisible by ', '')
    each = each.replace('  If t', 'T')
    each = each.replace('  If f', 'F')
    each = each.replace('throw to monkey ', '')
    each = each + '"}}'
    monkies.update(json.loads(each))

monkey_list = monkies.keys()
num_monks = len(monkey_list)

# Part1
# rounds = 20
# Part2
rounds = 10000

common_multiple = 1
# print(monkies['3'])
for monk in range(0, num_monks):
    monkies[str(monk)]["item_count"] = 0
    monkies[str(monk)]["items"] = ", "+monkies[str(monk)]["items"]
    common_multiple *= int(monkies[str(monk)]["Test"])

for i in range(rounds):
    for monk in range(0, num_monks):
        # print(monk)
        items = monkies[str(monk)]["items"].split(", ")
        # print(items)
        if len(items) > 0:
            for item in items[1:]:
                monkies[str(monk)]["item_count"] += 1
                operations = monkies[str(monk)]["Operation"].split(" ")
                if operations[1] == "old":
                    # Part 1
                    # test_case = math.floor((int(item) * int(int(item)))/3)
                    # Part 2
                    test_case = math.floor((int(item) * int(int(item))))
                elif operations[0] == "*":
                    # Part 1
                    # test_case = math.floor((int(item) * int(operations[1]))/3)
                    # Part 2
                    test_case = math.floor((int(item) * int(operations[1])))
                else:
                    # Part 1
                    # test_case = math.floor((int(item) + int(operations[1]))/3)
                    # Part 2
                    test_case = math.floor((int(item) + int(operations[1])))
                if test_case % int(monkies[str(monk)]["Test"]) == 0:
                    throw_to = monkies[str(monk)]["True"]
                else:
                    throw_to = monkies[str(monk)]["False"]
                monkies[throw_to]["items"] = monkies[throw_to]["items"] + ", "+str(test_case%common_multiple)
        monkies[str(monk)]["items"] = ""

# print(monkies)

item_counts = []
for monk in range(0, num_monks):
    item_counts.append(monkies[str(monk)]["item_count"])

item_counts.sort(reverse=True)
print(item_counts[0]*item_counts[1])
# 25738411485
Posted in: Advent of Code Tagged: AdventOfCode2022, Coding, Python

Advent of Code 2022, Day 10

December 10, 2022

Wheeeeee another Easy Mode/Hard Mode day. Ok, the hard mode is, debatable-ish. It was pretty tedious to get working, though the basic idea was pretty obvious.

Today’s puzzle, the communicator from a previous puzzle is broken, and the screen needs fixed. So you get an input of data off the broken communicator. For Part 1, you find some totals of the fluctuating signal level and add them up for a solution. Pretty simple honestly.

Then part two. Whoo boy, Part 2.

For Part 2, the data needs decoded into a series of letters. The biggest hassle, was figuring out exactly what the instructions were asking. There was a stepped through example, but it ended a bit too early to really get an idea of what was happening. This visualization someone posted to Reddit, helps a LOT. It made it much more clear how to process the data.

I still wasn’t getting a proper answer. I wasn’t even getting the proper result for the sample data set, but, oddly, I was ALMOST getting the sample data set result. Like, the first line was fine, halfway through the second line, it just went off and became spotty. I solved that, but more on that in a bit, because it was the end.

I also feel like I overly complicated things by using a 2×2 matrix of data, instead of just a long string then splitting the result to print it. But the 2×2 Matrix seemed more fun. It would almost be fun to connect this code up to an LED Matrix screen for the result.

Anyway, after lots and lots of tweaking to my original loop for Part 1, which is probably non existent now, I manage to get the solution. I had to change how I was handling the signals, because one type used 2 cycles, and the other took one. I had to add several variables to account for the position in the matrix. Though tracking that wasn’t too hard using the mod (remainder) operator and some basic division.

I’m a little disappointed in my result, because I used an import on “math” so I could round the division result down. One thing I usually try to do with these is just use, Pure Python. A lot of replies on Mastodon to previous posts suggest different modules, most often Numpy, but I really like the extra little challenge of “No Imports, pure Python”. The annoying part is, I could modify the code a bit to make the math work without using “floor” from “math”, but I’ve been working on this off and on all day, and it works, so I don’t really care to bother.

I also realized I needed to account for looping the pixel placer around. Something I would not have needed had I just used long string instead of a 2×2 matrix.

Anyway, After sons of modifications, and maybe I need a +1 here or a -1 here, I managed to solve it, which I said I would get to. I had enclosed the Pixel Placing algorithm inside the signal generating commands only. I had stupidly assumed, “noop” (No Operation) meant “Do nothing”. It just means, the signal doesn’t change. So I rearranged things a bit so the loop always ran, just sometimes once, sometimes twice, and poof, there it was, the answer! Mostly, there was a pixel off at the end, probably side effect of my looping, but I had the result and it worked.

import math

with open("Day10Input.txt") as file:
    data = file.read()

moves = data.split('\n')

def make_display(pixels,lines):
    display = []
    display_line = []
    for n in range(pixels):
        display_line.append(" ")
    for i in range(lines):
        display.append(display_line.copy())
    return display

def show_display(display):
    for each in display:
        print("".join(each))
    print("\n")

signal_strengths = [0, 1]
cycles = 0
signal = 1 # X In the instructions
line = 0

display = make_display(40, 6)

for move in moves:
    command = move.split(" ")
    signal_strengths.append(signal)
    if command[0] == "noop":
        rangeend = 2
    else:
        rangeend = 3
    for p in range(1, rangeend):
        cycles += 1
        pixel = (cycles)%40
        line = math.floor((cycles)/40)

        if p == 2:
            signal += int(command[1])
            signal_strengths.append(signal)
        first = signal - 1
        last = signal + 1
        if first < 0:
            first = 39
            line -= 1
        if last > 39:
            last = 0
            line += 1
        if pixel in [first, signal, last]:
            display[line][pixel] = "#"

check_values = [20, 60, 100, 140, 180, 220]
total = 0
for each in check_values:
    # print(f"{signal_strengths[each-1]} | {signal_strengths[each]} | {signal_strengths[each]}")
    total += each * signal_strengths[each]
print(total)
# 14060

show_display(display)
## PAPKFKEJ
Posted in: Advent of Code Tagged: AdventOfCode2022, Coding, Python
« Previous 1 2 3 … 9 Next »

Categories

  • collapsCat options: Array ( [title] => Categories [showPostCount] => 1 [inExclude] => exclude [inExcludeCats] => Photos, Uncategorized, mastodon-feed, goodreads [showPosts] => 0 [showPages] => 0 [linkToCat] => 1 [olderThan] => 0 [excludeAll] => 0 [catSortOrder] => ASC [catSort] => catName [postSortOrder] => ASC [postSort] => postTitle [expand] => 0 [defaultExpand] => Technology, Maker, Coding, Hobbies [debug] => 1 [postTitleLength] => 0 [catfeed] => none [taxonomy] => category [post_type] => post [postDateAppend] => after [postDateFormat] => m/d [showPostDate] => 1 [useCookies] => 1 [postsBeforeCats] => 1 [expandCatPost] => 1 [showEmptyCat] => 1 [showTopLevel] => 1 [useAjax] => 0 [customExpand] => [customCollapse] => [style] => kubrick [accordion] => 1 [title_link] => [addMisc] => 1 [addMiscTitle] => [number] => 2 [includeCatArray] => Array ( ) [expandSym] => ► [collapseSym] => ▼ ) postsToExclude: Array ( ) CATEGORY QUERY RESULTS Array ( [0] => WP_Term Object ( [term_id] => 641 [name] => 100DaysOfCode [slug] => 100daysofcode [term_group] => 0 [term_taxonomy_id] => 641 [taxonomy] => category [description] => [parent] => 172 [count] => 14 [filter] => raw ) [1] => WP_Term Object ( [term_id] => 486 [name] => Advent of Code [slug] => advent-of-code [term_group] => 0 [term_taxonomy_id] => 486 [taxonomy] => category [description] => [parent] => 172 [count] => 27 [filter] => raw ) [2] => WP_Term Object ( [term_id] => 666 [name] => AI Art [slug] => ai-art [term_group] => 0 [term_taxonomy_id] => 666 [taxonomy] => category [description] => [parent] => 153 [count] => 5 [filter] => raw ) [3] => WP_Term Object ( [term_id] => 438 [name] => Books [slug] => books [term_group] => 0 [term_taxonomy_id] => 438 [taxonomy] => category [description] => [parent] => 436 [count] => 4 [filter] => raw ) [4] => WP_Term Object ( [term_id] => 172 [name] => Coding [slug] => programming [term_group] => 0 [term_taxonomy_id] => 172 [taxonomy] => category [description] => [parent] => 153 [count] => 12 [filter] => raw ) [5] => WP_Term Object ( [term_id] => 541 [name] => Concerts [slug] => concertphotos [term_group] => 0 [term_taxonomy_id] => 541 [taxonomy] => category [description] => [parent] => 527 [count] => 7 [filter] => raw ) [6] => WP_Term Object ( [term_id] => 155 [name] => Devices (Phones and Tablets) [slug] => devices [term_group] => 0 [term_taxonomy_id] => 155 [taxonomy] => category [description] => [parent] => 166 [count] => 9 [filter] => raw ) [7] => WP_Term Object ( [term_id] => 606 [name] => Fairs [slug] => fairs [term_group] => 0 [term_taxonomy_id] => 606 [taxonomy] => category [description] => [parent] => 527 [count] => 8 [filter] => raw ) [8] => WP_Term Object ( [term_id] => 523 [name] => Feeds [slug] => feeds [term_group] => 0 [term_taxonomy_id] => 523 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [10] => WP_Term Object ( [term_id] => 436 [name] => Hobbies [slug] => hobbies [term_group] => 0 [term_taxonomy_id] => 436 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [11] => WP_Term Object ( [term_id] => 656 [name] => IOT Projects [slug] => iot [term_group] => 0 [term_taxonomy_id] => 656 [taxonomy] => category [description] => [parent] => 153 [count] => 19 [filter] => raw ) [12] => WP_Term Object ( [term_id] => 446 [name] => Language [slug] => language [term_group] => 0 [term_taxonomy_id] => 446 [taxonomy] => category [description] => [parent] => 436 [count] => 1 [filter] => raw ) [13] => WP_Term Object ( [term_id] => 524 [name] => Letterboxed [slug] => letterboxed [term_group] => 0 [term_taxonomy_id] => 524 [taxonomy] => category [description] => [parent] => 523 [count] => 284 [filter] => raw ) [14] => WP_Term Object ( [term_id] => 653 [name] => Link List [slug] => link-list [term_group] => 0 [term_taxonomy_id] => 653 [taxonomy] => category [description] => [parent] => 523 [count] => 63 [filter] => raw ) [15] => WP_Term Object ( [term_id] => 224 [name] => Linux & Open Source [slug] => linux [term_group] => 0 [term_taxonomy_id] => 224 [taxonomy] => category [description] => [parent] => 166 [count] => 6 [filter] => raw ) [16] => WP_Term Object ( [term_id] => 153 [name] => Maker [slug] => maker [term_group] => 0 [term_taxonomy_id] => 153 [taxonomy] => category [description] => [parent] => 0 [count] => 2 [filter] => raw ) [18] => WP_Term Object ( [term_id] => 530 [name] => Micro Blog [slug] => microblog [term_group] => 0 [term_taxonomy_id] => 530 [taxonomy] => category [description] => [parent] => 0 [count] => 55 [filter] => raw ) [19] => WP_Term Object ( [term_id] => 437 [name] => Music [slug] => music [term_group] => 0 [term_taxonomy_id] => 437 [taxonomy] => category [description] => [parent] => 436 [count] => 18 [filter] => raw ) [20] => WP_Term Object ( [term_id] => 395 [name] => My DIY Projects [slug] => my-diy-projects [term_group] => 0 [term_taxonomy_id] => 395 [taxonomy] => category [description] => [parent] => 153 [count] => 7 [filter] => raw ) [21] => WP_Term Object ( [term_id] => 154 [name] => Opinion/Editorial/Life [slug] => articles [term_group] => 0 [term_taxonomy_id] => 154 [taxonomy] => category [description] => [parent] => 0 [count] => 18 [filter] => raw ) [22] => WP_Term Object ( [term_id] => 491 [name] => Organizing [slug] => organizing [term_group] => 0 [term_taxonomy_id] => 491 [taxonomy] => category [description] => [parent] => 436 [count] => 7 [filter] => raw ) [23] => WP_Term Object ( [term_id] => 534 [name] => Other Photos [slug] => otherphotos [term_group] => 0 [term_taxonomy_id] => 534 [taxonomy] => category [description] => [parent] => 527 [count] => 12 [filter] => raw ) [24] => WP_Term Object ( [term_id] => 617 [name] => Outdoor and Nature [slug] => outdoor [term_group] => 0 [term_taxonomy_id] => 617 [taxonomy] => category [description] => [parent] => 527 [count] => 4 [filter] => raw ) [25] => WP_Term Object ( [term_id] => 242 [name] => PC Hardware [slug] => pcs [term_group] => 0 [term_taxonomy_id] => 242 [taxonomy] => category [description] => [parent] => 166 [count] => 6 [filter] => raw ) [27] => WP_Term Object ( [term_id] => 712 [name] => Programming Projects [slug] => projects [term_group] => 0 [term_taxonomy_id] => 712 [taxonomy] => category [description] => [parent] => 172 [count] => 11 [filter] => raw ) [28] => WP_Term Object ( [term_id] => 241 [name] => Synology NAS [slug] => synology-nas [term_group] => 0 [term_taxonomy_id] => 241 [taxonomy] => category [description] => [parent] => 166 [count] => 5 [filter] => raw ) [29] => WP_Term Object ( [term_id] => 166 [name] => Technology [slug] => technology [term_group] => 0 [term_taxonomy_id] => 166 [taxonomy] => category [description] => [parent] => 0 [count] => 10 [filter] => raw ) [30] => WP_Term Object ( [term_id] => 424 [name] => The Basement [slug] => the-basement [term_group] => 0 [term_taxonomy_id] => 424 [taxonomy] => category [description] => [parent] => 153 [count] => 6 [filter] => raw ) [31] => WP_Term Object ( [term_id] => 557 [name] => Toy Photos [slug] => toyphotos [term_group] => 0 [term_taxonomy_id] => 557 [taxonomy] => category [description] => [parent] => 527 [count] => 0 [filter] => raw ) [32] => WP_Term Object ( [term_id] => 1 [name] => Uncategorized [slug] => uncategorized [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [33] => WP_Term Object ( [term_id] => 280 [name] => Windows [slug] => windows [term_group] => 0 [term_taxonomy_id] => 280 [taxonomy] => category [description] => [parent] => 166 [count] => 2 [filter] => raw ) [34] => WP_Term Object ( [term_id] => 538 [name] => Zoos [slug] => zoophotos [term_group] => 0 [term_taxonomy_id] => 538 [taxonomy] => category [description] => [parent] => 527 [count] => 12 [filter] => raw ) ) POST QUERY: POST QUERY RESULTS
  • ►Feeds (347)
    • Letterboxed (284)
    • Link List (63)
  • ▼Hobbies (30)
    • Books (4)
    • Language (1)
    • Music (18)
    • Organizing (7)
  • ▼Maker (103)
    • AI Art (5)
    • ▼Coding (64)
      • 100DaysOfCode (14)
      • Advent of Code (27)
      • Programming Projects (11)
    • IOT Projects (19)
    • My DIY Projects (7)
    • The Basement (6)
  • ►Micro Blog (55)
  • ►Opinion/Editorial/Life (18)
  • ▼Technology (38)
    • Devices (Phones and Tablets) (9)
    • Linux & Open Source (6)
    • PC Hardware (6)
    • Synology NAS (5)
    • Windows (2)
  • ►Uncategorized (0)

MastodonLinkedIn

emailInstagramInstagram

GitHubLetterboxdDuolongo
GoodreadsLast.fmElite Dangerous INARA
Lameazoid Logo


Copyright © 2023 [Blogging Intensifies].

Me WordPress Theme by themehall.com