[Blogging Intensifies]

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

  • About
  • Code Projects
  • Photo Gallery

Advent of Code 2022, Day 9

December 9, 2022

Things are getting tricky here now on Day 9, though not excessively hard. Just hard-ish. Today’s puzzle involves rope simulations. The data set consists of movement for the head of the rope and the end of the rope (and subsequent segments) follows rules that essentially keep the tail near the head. For example, if the head moves right 4 spaces, the tail follows, moving right 4 spaces. The tally was how many locations on a grid did the tail visit at least once.

The movement part was mostly simple, first create a function to move the head around. It wasn’t necessary, but I saved the head coordinates into a list. It was useful for verifying my code against the sample data. Moving the head was easy. Moving the tail was trickier. My movement function evolved quite a bit, it was originally quite a few messy if statements. But as i worked through the problem, it simplified down more and more until it was one movement function, called for the x or y if needed.

The diagonal movement was tricky, though, until I realized it was easiest to just, always align the opposing x or y coordinate. Let me try to clear up what I am saying. Straight Left, Right, Up, Down, movement is easy. When the head is opposite of the tail diagonally, by 1 each way, the tail won’t move. But if the head moves again, and now it’s 2 away along say, the Up direction, in addition to the tail moving up one, it also needs to move over left or right to align with the head.

To solve this, it just, does the alignment every time. In regular movement, this doesn’t do anything, since it’s already aligned, if it’s a diagonal movement, it doesn’t it’s job.

Each time the tail moves, it’s coordinates are also dropped into an array to track it’s movements. The tally needed only wants unique coordinates, so keeping track of where it’s been allows for easily checking for new coordinates and incrementing the counter.

Part 2 mixed things up a bit in a predictable way, multiple segments, before the tail. Same rules for movement. The code below could be simplified down to combine part 1 and 2, but I opted to just copy and paste for simplicity. The base loop is the same, though I removed the unneeded head tracking list. The main difference is that instead of a head and tail position, it gets fed a list of coordinates, for each segment, that it loops through every move of the head.

And it got close. Super close. Three away close!

the code worked for the sample given with an answer of 36. It worked for my original 1 segment rope, if i cut the starting array down to 2 coordinates. But not for the fill puzzle input.

I could see the problem, but not quite the solution. The issue was that sometimes, it was going to need to move diagonally. And it was fixed by adding a simple extra if statement.

if abs(hpos[0]-xpos) > 1 and abs(hpos[1]-ypos) > 1:
    xpos = move_pos(hpos[0], xpos)
    ypos = move_pos(hpos[1], ypos)

If BOTH coordinates are large, move them both, instead of one or the other. And the correct answer was revealed!

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

moves = data.split('\n')

positions = [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
head_pos = [0,0]
tail_pos = [0,0]

head_moves = [head_pos]
tail_moves = [tail_pos]
long_tail_moves = [positions[len(positions)-1]]

tail_counter = 1
long_tail_counter = 1

def move_pos(a,b):
    if a - b > 0:
        return b+1
    if a - b < 0:
        return b-1
    return b

def move_head(dir,hpos):
    if dir == "R":
        return [hpos[0]+1,hpos[1]]
    if dir == "L":
        return [hpos[0]-1,hpos[1]]
    if dir == "U":
        return [hpos[0],hpos[1]+1]
    if dir == "D":
        return [hpos[0],hpos[1]-1]
    return pos

def move_tail(hpos,tpos):
    xpos = tpos[0]
    ypos = tpos[1]
    if abs(hpos[0]-xpos) > 1 and abs(hpos[1]-ypos) > 1:
        xpos = move_pos(hpos[0], xpos)
        ypos = move_pos(hpos[1], ypos)
    if abs(hpos[0]-xpos) > 1:
        ypos = hpos[1]
        xpos = move_pos(hpos[0], xpos)
    if abs(hpos[1]-ypos) > 1:
        xpos = hpos[0]
        ypos = move_pos(hpos[1], ypos)
    return [xpos, ypos]

# Part 1
for move in moves:
    turn = move.split(" ")
    for i in range(int(turn[1])):
        head_pos = move_head(turn[0], head_pos)
        head_moves.append(head_pos)
        tail_pos = move_tail(head_pos, tail_pos)
        if tail_pos not in tail_moves:
            tail_counter += 1
        tail_moves.append(tail_pos)

# Part 2
for move in moves:
    turn = move.split(" ")
    for i in range(int(turn[1])):
        positions[0] = move_head(turn[0], positions[0])
        for i in range(1,len(positions)):
            positions[i] = move_tail(positions[i-1], positions[i])
        if positions[len(positions)-1] not in long_tail_moves:
            long_tail_counter += 1
        long_tail_moves.append(positions[len(positions)-1])



# print(head_moves)
# print(tail_moves)
print(tail_counter)
#5902

#print(long_tail_moves)
print(long_tail_counter)
# high 11271
# high 2448
# low 2395

## 2445

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Like this:

Like Loading...

Related

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

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] => 11 [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] => 622 [name] => Elite Dangerous [slug] => elite-dangerous [term_group] => 0 [term_taxonomy_id] => 622 [taxonomy] => category [description] => [parent] => 523 [count] => 8 [filter] => raw ) [8] => 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 ) [9] => 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 ) [11] => 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 ) [12] => 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 ) [13] => 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 ) [14] => WP_Term Object ( [term_id] => 524 [name] => Letterboxed [slug] => letterboxed [term_group] => 0 [term_taxonomy_id] => 524 [taxonomy] => category [description] => [parent] => 523 [count] => 276 [filter] => raw ) [15] => 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] => 29 [filter] => raw ) [16] => 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 ) [17] => 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 ) [19] => 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 ) [20] => WP_Term Object ( [term_id] => 437 [name] => Music [slug] => music [term_group] => 0 [term_taxonomy_id] => 437 [taxonomy] => category [description] => [parent] => 436 [count] => 17 [filter] => raw ) [21] => 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 ) [22] => 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 ) [23] => 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 ) [24] => 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 ) [25] => 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 ) [26] => 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 ) [28] => WP_Term Object ( [term_id] => 712 [name] => Programming Projects [slug] => projects [term_group] => 0 [term_taxonomy_id] => 712 [taxonomy] => category [description] => [parent] => 172 [count] => 7 [filter] => raw ) [29] => 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 ) [30] => WP_Term Object ( [term_id] => 166 [name] => Technology [slug] => technology [term_group] => 0 [term_taxonomy_id] => 166 [taxonomy] => category [description] => [parent] => 0 [count] => 9 [filter] => raw ) [31] => 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 ) [32] => 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 ) [33] => 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 ) [34] => 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 ) [35] => 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 (313)
    • Elite Dangerous (8)
    • Letterboxed (276)
    • Link List (29)
  • ▼Hobbies (29)
    • Books (4)
    • Language (1)
    • Music (17)
    • Organizing (7)
  • ▼Maker (98)
    • AI Art (5)
    • ▼Coding (59)
      • 100DaysOfCode (14)
      • Advent of Code (27)
      • Programming Projects (7)
    • IOT Projects (19)
    • My DIY Projects (7)
    • The Basement (6)
  • ►Micro Blog (55)
  • ►Opinion/Editorial/Life (18)
  • ▼Technology (37)
    • 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

%d bloggers like this: