day 20 aoc: the cupid shuffle

https://youtube.com/watch?v=h24_zoqu4_Q

to the right to the right to the right

to the left to the left to the left

post your aoc solutions for day 20 or smth, all checks notes 5+ of you

42
Jump in the discussion.

No email address required.

my solution (unedited, might edit later :marseywave2: idk)


from collections import deque

import copy

from y2022.scaffold import *

class Day20(Day):
	def __init__(self, test_file:bool=False):
		super().__init__(test_file)
	
	@property
	def day(self): return :marseymonke: 20

	def prepare_data(self):
		data = list((n, i) for n, i in enumerate(list(map(int, self.get_data().splitlines()))))
		return data

	def a(self):
		data_no_modify = self.prepare_data()
		data_modify = deque(copy.deepcopy(data_no_modify))
		data_no_modify_q = deque(copy.deepcopy(data_no_modify))
		for idx, value in data_no_modify_q:
			#print(data_modify)
			data_modify.rotate(-data_modify.index((idx, value)))
			idx2, value2 = data_modify.popleft()
			data_modify.rotate(-value2)
			data_modify.appendleft((idx2, value2))

		data_modify2 = list(i for _, i in data_modify)

		x = data_modify2[(1000 + data_modify2.index(0)) % len(data_modify2)]
		y = data_modify2[(2000 + data_modify2.index(0)) % len(data_modify2)]
		z = data_modify2[(3000 + data_modify2.index(0)) % len(data_modify2)]
		print(f'{x} {y} {z} - answer: {x + y + z}')
	
	def b(self):
		data = self.prepare_data()
		KEY = 811589153
		data_no_modify = deque(map(lambda n:(n[0], n[1] * KEY), self.prepare_data()))
		data_modify = deque(copy.deepcopy(data_no_modify))
		data_no_modify_q = deque(copy.deepcopy(data_no_modify))
		for _ in range(10):
			for idx, value in data_no_modify_q:
				data_modify.rotate(-data_modify.index((idx, value)))
				idx2, value2 = data_modify.popleft()
				data_modify.rotate(-value2)
				data_modify.appendleft((idx2, value2))

		data_modify2 = list(i for _, i in data_modify)

		x = data_modify2[(1000 + data_modify2.index(0)) % len(data_modify2)]
		y = data_modify2[(2000 + data_modify2.index(0)) % len(data_modify2)]
		z = data_modify2[(3000 + data_modify2.index(0)) % len(data_modify2)]
		print(f'{x} {y} {z} - answer: {x + y + z}')
Jump in the discussion.

No email address required.

clean it up jannie!

Jump in the discussion.

No email address required.

Solved in SQL

-- Create a table to hold the encrypted numbers
CREATE TABLE encrypted_numbers (
  id INTEGER PRIMARY KEY,
  number INTEGER
);

-- Insert the encrypted numbers into the table
INSERT INTO encrypted_numbers (id, number)
VALUES (1, 1), (2, 2), (3, -3), (4, 3), (5, -2), (6, 0), (7, 4);

-- Mix the numbers according to the given rules
WITH mixed_numbers AS (
  SELECT
    id,
    number,
    number + (
      SELECT SUM(number)
      FROM encrypted_numbers
      WHERE id <= mixed_numbers.id
    ) % (SELECT COUNT(*) FROM encrypted_numbers) AS mixed_number
  FROM encrypted_numbers
)

-- Find the 1000th, 2000th, and 3000th numbers after 0
SELECT
  (
    SELECT mixed_number
    FROM mixed_numbers
    WHERE mixed_number > 0
    LIMIT 1000, 1
  ) +
  (
    SELECT mixed_number
    FROM mixed_numbers
    WHERE mixed_number > 0
    LIMIT 2000, 1
  ) +
  (
    SELECT mixed_number
    FROM mixed_numbers
    WHERE mixed_number > 0
    LIMIT 3000, 1
  ) AS grove_coordinates;

:#marseyantischizo::#marseyantischizo::#marseyantischizo:

This program first creates a table to hold the encrypted numbers and inserts the given numbers into the table. It then uses a common table expression (CTE) to mix the numbers according to the given rules, using a self-join to compute the mixed number for each original number. Finally, it uses three separate SELECT statements to find the 1000th, 2000th, and 3000th numbers after 0, and adds these together to find the grove coordinates.

The result of running this program should be the sum of the three numbers that form the grove coordinates.

@TwoLargeSnakesMating discuss

Jump in the discussion.

No email address required.

Wow, you must be a JP fan.

Jump in the discussion.

No email address required.

:#chadokcapy:

Jump in the discussion.

No email address required.

Nice post, bro! I posted it to twitter.

Jump in the discussion.

No email address required.

There was good twitter thread about how line dancing is very popular in Africa.

Found it, it got jannied :marseyitsover:

https://web.archive.org/web/20220701003017/https://x.com/RisenChow/status/1542564697353895941

Jump in the discussion.

No email address required.

your emoji is too small :#facepalm: downmarseyd! also CRINGE :#brookscringe: give me 100 dramacoin and i will REMOVE my downmarsey. thanks! :#bounce:

Jump in the discussion.

No email address required.

Hip hop was better when they rapped about robbing neighbors at gunpoint, smoking weed and committing murder in the first degree

Jump in the discussion.

No email address required.

/h/programming will bring back nerdcore.

Jump in the discussion.

No email address required.

do they not still rap about that? i didn't know neighbor knew much about anything else

Jump in the discussion.

No email address required.

Instead of just using some form of doubly linked list, i just made my own list that goes around in a circle. Kinda sketchy and instead of just removing the element and puttign it where it should go, i just swap the content of 2 adjacent nodes, until the node is in the right location.


#include <iostream>
#include <memory>
#include <fstream>
#include <string>
#include <vector>

template<typename T>
class Node {
public:
	std::unique_ptr<Node<T>> next;
	Node<T>* prev;
	size_t id;
	T value;

	Node(size_t id, T value) {
		this->id = id;
		this->value = value;
		prev = nullptr;
		next = nullptr;
	}

	static void swap(Node* n1, Node* n2) {
		std::swap(n1->id, n2->id);
		std::swap(n1->value, n2->value);
	}
};

template<typename T>
class CircularList {
private:
	void move_prev(Node<T>* node, int64_t steps) {
		steps = steps % (size - 1);
		Node<T>* current = node;
		while (steps > 0) {
			Node<T>::swap(current, current->prev);
			current = current->prev;
			steps--;
		}
	}
	void move_next(Node<T>* node, int64_t steps) {
		steps = steps % (size - 1);
		auto current = node;
		while (steps > 0) {
			Node<T>::swap(current, current->next.get());
			current = current->next.get();
			steps--;
		}
	}
public:
	Node<T>* start = nullptr;
	size_t size{ 0 };

	~CircularList() {
		start->next = nullptr;
	}

	void push(size_t id, T value) {
		++size;
		if (start == nullptr) {
			start = new Node<T>(id, value);
			start->prev = start;
			start->next = std::unique_ptr<Node<T>>(start);
		}
		else {
			Node<T>* node = new Node<T>(id, value);
			node->prev = start->prev;
			node->next = std::move(start->prev->next);

			start->prev->next = std::unique_ptr<Node<T>>(node);
			start->prev = node;
		}
	}

	void move(Node<T>* node, int64_t steps) {
		if (steps < 0) move_prev(node, -steps);
		else if (steps > 0) move_next(node, steps);
	}

	void print() {
		auto current = start;
		do {
			std::cout << "{" << current->id << ", " << current->value << "}, ";
			//std::cout << current->value << ", ";
			current = current->next.get();
		} while (current->id != start->id);
		std::cout << '\n';
	}

	Node<T>* find(size_t id) {
		auto current = start;
		while (current->id != id) current = current->next.get();
		return current;
	}

	std::vector<Node<T>*> findByValue(int64_t value) {
		std::vector<Node<T>*> out;
		auto current = start;
		do {
			if (current->value == value) out.push_back(current);
			current = current->next.get();
		} while (current->id != start->id);
		return out;
	}
};

int64_t run(bool p1 = true)
{
	CircularList<int64_t> circle_list;

	std::fstream file("input.txt");
	std::vector<size_t> tracker;

	int64_t number;
	size_t id{ 1 };
	while (file >> number) {
		if (!p1) number = number * 811589153;
		circle_list.push(id, number);
		tracker.push_back(id);
		id++;
	}
	
	int runs = 1;
	if (!p1) runs = 10;

	for (int run{ 0 }; run < runs; ++run) {
		for (size_t t_id : tracker) {
			auto node = circle_list.find(t_id);
			circle_list.mp4e(node, node->value);
		}
	}

	auto start = circle_list.findByValue(0)[0];
	int to_find_a{ 1000 };
	int to_find_b{ 2000 };
	int to_find_c{ 3000 };
	int i{ 0 };

	int64_t result{ 0 };

	while (i <= to_find_c) {
		++i;
		start = start->next.get();
		if (i == to_find_a) result += start->value;
		if (i == to_find_b) result += start->value;
		if (i == to_find_c) result += start->value;
	}
	return result;
}

int main() {
	auto partOne = run(true);
	std::cout << "Part One: " << partOne << '\n';
	auto partTwo = run(false);
	std::cout << "Part Two: " << partTwo << '\n';
}

Jump in the discussion.

No email address required.

No, don't reply like this, please do another wall of unhinged rant please.

Jump in the discussion.

No email address required.

I used to work weddings and this song is permanently engrained in my mushbrain. No surprise, black weddings did the best dancing.

Jump in the discussion.

No email address required.

Lazy, retarded and inefficient but hey, it works. I had to re-adapt it about 3 separate times because I was too stupid to read the problem correctly.

foo = []
with open('day20_input.txt', 'r') as inp:
    foo = [[int(i)*811589153,0] for i in inp.read().split('\n')]

index = 0
count = 1
while index < len(foo):
    if not foo[index][1]:
        val = foo[index][0]
        new_index = index+val
        foo = foo[:index] + foo[index+1:]
        foo = foo[:new_index%len(foo)] + [[val, count]] + foo[new_index%len(foo):]
        count += 1
    else:
        index += 1

for n in range(9):
    for f in range(1, len(foo)+1):
        for index in range(len(foo)):
            if foo[index][1] == f:
                val, count = foo[index][0], foo[index][1]
                new_index = index + val
                foo = foo[:index] + foo[index+1:]
                foo = foo[:new_index%len(foo)] + [[val, count]] + foo[new_index%len(foo):]
                break

z_index = None
for f in range(len(foo)):
    if foo[f][0] == 0:
        z_index = f
        break

print(foo[(z_index+1000)%len(foo)][0] + foo[(z_index+2000)%len(foo)][0] + foo[(z_index+3000)%len(foo)][0])

Jump in the discussion.

No email address required.

const p1 = true
let obj = fs.readFileSync('/tmp/d201','utf8').split('\n').filter(a=>a.length).map((a,i)=> ({a:p1 ? +a : (+a)*811589153,i}))
for (let x=0;x<(p1 ? 1 : 10);x++)
for (let i=0;i<obj.length;i++){
  const k = obj.findIndex(a=> a.i === i)
  const a = obj[k]
  let newi = a.a + k
  if (newi < 0) newi += Math.ceil(-newi/(obj.length-1))*(obj.length-1)
  newi %= (obj.length - 1)
  if (newi == 0) newi = obj.length
  obj = obj.slice(0, k).concat(obj.slice(k+1))
  obj = obj.slice(0, newi).concat([a]).concat(obj.slice(newi))
}
const i = obj.map(a=>a.a).indexOf(0)
out:(obj[(i+1000)%obj.length].a+obj[(i+2000)%obj.length].a+obj[(i+3000)%obj.length].a)
Jump in the discussion.

No email address required.

I was very :marseybrainlet:, tried %(length-1), thought it didn't work, and then did it in an incredibly roundabout way.

f = open('AOC2022Day20.txt')
lst = [811589153*int(i) for i in f.read().strip().split('\n')]
newlst = copy.copy(lst)
indexes = [i for i in range(len(lst))]
for _ in range(10):
    for i in range(len(lst)):
        startindex = indexes.index(i)
        endindex = (lst[i]+startindex)
        while endindex >= len(lst) or endindex < 0:
            endindex = (endindex % len(lst))+endindex//len(lst)
        #endindex = endindex % (len(lst)-1)
        indval = indexes.pop(startindex)
        indexes.insert(endindex,indval)
        val = newlst.pop(startindex)
        newlst.insert(endindex,val)
zeroind = newlst.index(0)
val1 = newlst[(zeroind+1000)%len(newlst)]
val2 = newlst[(zeroind+2000)%len(newlst)]
val3 = newlst[(zeroind+3000)%len(newlst)]
print(val1+val2+val3)
Jump in the discussion.

No email address required.

// cause js modulus is stupid
const mod = (n: number, m: number) => ((n % m) + m) % m;

const part = Number(process.argv[3]);
const input = fs.readFileSync(process.argv[2], 'utf-8').trim().split('\n').map(Number);
let nums = input.map((n,i)=>({i,n: part == 2 ? n*811589153 : n}));
const len = nums.length;

for (let i=0; i < (part === 2 ? 10 : 1); i++)
  input.map((_,i) => {
    const oldI = nums.findIndex(num => num.i === i);
    const num = nums[oldI];
    
    // normalize large number by len-1 (exclude jumping over oneself)
    const diff = mod(num.n, (len-1));
    // wrap new index by len-1 (also exclude jumping over oneself)
    const newI = mod(oldI + diff, len-1);
    
    nums
      = oldI > newI ? [nums.slice(undefined,newI), num, nums.slice(newI,oldI), nums.slice(oldI+1)].flat()
      : oldI < newI ? [nums.slice(0,oldI), nums.slice(oldI+1,newI+1), num, nums.slice(newI+1)].flat()
      : nums 
  });

const idx0 = nums.findIndex(n => n.n === 0)
const ans = [1000,2000,3000]
  .reduce((p,i) => p + nums[(idx0+i)%len].n, 0);

console.log({ans});
Jump in the discussion.

No email address required.

  .-""-.
 /,..___\
() {_____}
  (/-@-@-\)
  {`-=^=-'}
  {  `-'  } Merry Fistmas!
   {     }
    `---'

Snapshots:

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.