Skip to content

Commit

Permalink
finished day 12 2018
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingAP committed Dec 3, 2024
1 parent a974a9a commit 2e7459a
Show file tree
Hide file tree
Showing 24 changed files with 4,274 additions and 4,223 deletions.
Binary file modified aoc/aoc_data.db
Binary file not shown.
406 changes: 203 additions & 203 deletions index.html

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions puzzles/2018/day12/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Advent of Code 2018 - Day 12: [Subterranean Sustainability](https://adventofcode.com/2018/day/12)

## [Write Up](https://codingap.github.io/advent-of-code/writeups/2018/day12)

## Results

| | **Part 1** | **Part 2** |
| :--------------: | :--------: | :--------: |
| **Results** | 3472 | 2600000000919 |
| **Time (in ms)** | 1.14 | 14.05 |
81 changes: 61 additions & 20 deletions puzzles/2018/day12/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,80 @@ const part1 = (input: string) => {
return obj;
}, {});

for (let gen = 0; gen < 1; gen++) {
const min = Math.min(...Object.keys(state).map(num => parseInt(num)));
const max = Math.max(...Object.keys(state).map(num => parseInt(num)));
// go for 20 generations
for (let gen = 0; gen < 20; gen++) {
// find first and last plant
const min = Math.min(...Object.keys(state).filter(key => state[parseInt(key)]).map(num => parseInt(num)));
const max = Math.max(...Object.keys(state).filter(key => state[parseInt(key)]).map(num => parseInt(num)));
const newState: { [key: number]: boolean } = {};

let debug = '';
for (let i = min; i <= max; i++) {
// let current = '';
// for (let j = -2; j <= 2; j++) {
// if (state[i + j] === undefined) {
// state[i + j] = false;
// newState[i + j] = false;
// }
// current += state[i + j] ? '.' : '#';
// }

// newState[i] = rules[current] === '#';
// debug += rules[current] ? '#' : '.';
debug += state[i] ? '#' : '.';
// go through all plants and find new plants from them
for (let i = min - 4; i <= max + 4; i++) {
let current = '';
for (let j = -2; j <= 2; j++) {
if (state[i + j] === undefined) state[i + j] = false;
current += state[i + j] ? '#' : '.';
}

newState[i] = rules[current] === '#';
}

state = newState;
console.log(debug);
}

return Object.values(state).map((plant, i) => plant ? i : 0).reduce((sum, num) => sum + num, 0);
return Object.entries(state).map(([i, plant]) => plant ? parseInt(i) : 0).reduce((sum, num) => sum + num, 0);
};

/**
* the code of part 2 of the puzzle
*/
const part2 = (input: string) => {
return 0;
const [initial, transforms] = input.trim().split('\n\n');

// intitialize state
let state: { [key: number]: boolean } = {};
initial.split(': ')[1].split('').forEach((char, i) => {
state[i] = char === '#';
});

// parse rules
const rules = transforms.split('\n').reduce<{ [key: string]: string }>((obj, line) => {
const [left, right] = line.split(' => ');
obj[left] = right;
return obj;
}, {});

// a pattern emerges after a certain time where the plants gain a specific amount each time
// find the amount and extrapolate
const differences: { [key: number]: number } = {};
let current = 0, generation = 0;
while (true) {
// find first and last plant
const min = Math.min(...Object.keys(state).filter(key => state[parseInt(key)]).map(num => parseInt(num)));
const max = Math.max(...Object.keys(state).filter(key => state[parseInt(key)]).map(num => parseInt(num)));
const newState: { [key: number]: boolean } = {};

// go through all plants and find new plants from them
for (let i = min - 4; i <= max + 4; i++) {
let current = '';
for (let j = -2; j <= 2; j++) {
if (state[i + j] === undefined) state[i + j] = false;
current += state[i + j] ? '#' : '.';
}

newState[i] = rules[current] === '#';
}

const newValue = Object.entries(state).map(([i, plant]) => plant ? parseInt(i) : 0).reduce((sum, num) => sum + num, 0);
const difference = newValue - current;
differences[difference] = (differences[difference] || 0) + 1;

if (differences[difference] >= 100) return newValue + (50000000000 - generation) * difference;

generation++;
current = newValue;
state = newState;
}
};

export { part1, part2 };
400 changes: 200 additions & 200 deletions writeups/2015/day01/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day01/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day02/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day03/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day04/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day05/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day06/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day07/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day08/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day09/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day10/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day11/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day12/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day13/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day16/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day18/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2023/day19/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2024/day01/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2024/day02/index.html

Large diffs are not rendered by default.

400 changes: 200 additions & 200 deletions writeups/2024/day03/index.html

Large diffs are not rendered by default.

0 comments on commit 2e7459a

Please sign in to comment.