2018 day 12 complete
This commit is contained in:
parent
7bab2084eb
commit
f6e6bd69c7
34
2018/12/input.txt
Normal file
34
2018/12/input.txt
Normal file
|
@ -0,0 +1,34 @@
|
|||
initial state: ##....#.#.#...#.#..#.#####.#.#.##.#.#.#######...#.##....#..##....#.#..##.####.#..........#..#...#
|
||||
|
||||
..#.# => #
|
||||
.#### => #
|
||||
#.... => .
|
||||
####. => #
|
||||
...## => .
|
||||
.#.#. => .
|
||||
..#.. => .
|
||||
##.#. => .
|
||||
#.#.# => #
|
||||
..... => .
|
||||
#.#.. => .
|
||||
....# => .
|
||||
.#..# => .
|
||||
###.# => #
|
||||
#..#. => .
|
||||
##### => .
|
||||
...#. => #
|
||||
#.##. => #
|
||||
.#.## => #
|
||||
#..## => #
|
||||
.##.. => #
|
||||
##.## => .
|
||||
..### => .
|
||||
###.. => .
|
||||
##..# => #
|
||||
.#... => #
|
||||
.###. => #
|
||||
#.### => .
|
||||
.##.# => .
|
||||
#...# => #
|
||||
##... => .
|
||||
..##. => .
|
56
2018/12/part1.js
Normal file
56
2018/12/part1.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
const fs = require('fs'); // file system
|
||||
const input = fs.readFileSync(`${__dirname}/input.txt`, 'utf-8') // read the input file
|
||||
.split('\n'); // split on lines
|
||||
|
||||
// get the initial state from the input
|
||||
let state = input[0].slice('initial state: '.length);
|
||||
|
||||
let rules = {};
|
||||
|
||||
// extract all rules from input
|
||||
for (const line of input.splice(2)) {
|
||||
rules[line.slice(0, 5)] = line.slice(9);
|
||||
}
|
||||
|
||||
// to add extra pots as if there were endless pots
|
||||
function expand () {
|
||||
state = state.replace(/^(\.+)?/, '.....');
|
||||
state = state.replace(/(\.+)?$/, '.....');
|
||||
}
|
||||
|
||||
let zero = 5;
|
||||
// add empty pots
|
||||
expand();
|
||||
console.log(Array(20-zero).join(' ')+state);
|
||||
|
||||
for (let i = 0; i < 20; i++){
|
||||
|
||||
// create 'empty' array of '.'s
|
||||
const pots = Array(3).join('.').split('');
|
||||
|
||||
state.split('').forEach((_, n) => {
|
||||
// get the section to look at of prev gen
|
||||
const sect = state.slice(n-2, n+3);
|
||||
if (sect.length !== 5) { return }
|
||||
|
||||
// get the value of the matching rule
|
||||
let plant = rules[sect];
|
||||
if (!plant) { return }
|
||||
|
||||
// replace the value in the next gen
|
||||
pots.push(plant);
|
||||
});
|
||||
// move the '0th pot' marker
|
||||
zero -= pots.indexOf('#') - 5;
|
||||
state = pots.join('');
|
||||
|
||||
// add empty pots
|
||||
expand();
|
||||
console.log(Array(20-zero).join(' ')+state);
|
||||
}
|
||||
|
||||
const result = state.split('').reduce((val, pot, index) => {
|
||||
return (Number(val) || 0) + (pot === '#' ? index - zero : 0);
|
||||
})
|
||||
|
||||
console.log(result);
|
70
2018/12/part2.js
Normal file
70
2018/12/part2.js
Normal file
|
@ -0,0 +1,70 @@
|
|||
const fs = require('fs'); // file system
|
||||
const input = fs.readFileSync(`${__dirname}/input.txt`, 'utf-8') // read the input file
|
||||
.split('\n'); // split on lines
|
||||
|
||||
// get the initial state from the input
|
||||
let state = input[0].slice('initial state: '.length);
|
||||
|
||||
let rules = {};
|
||||
|
||||
// extract all rules from input
|
||||
for (const line of input.splice(2)) {
|
||||
rules[line.slice(0, 5)] = line.slice(9);
|
||||
}
|
||||
|
||||
// to add extra pots as if there were endless pots
|
||||
function expand (s) {
|
||||
let r = s.replace(/^(\.+)?/, '.....');
|
||||
r = r.replace(/(\.+)?$/, '.....');
|
||||
return r;
|
||||
}
|
||||
|
||||
let zero = 5;
|
||||
// add empty pots
|
||||
state = expand(state);
|
||||
|
||||
let done = false;
|
||||
for (var i = 0; i < 250; i++){
|
||||
|
||||
|
||||
if (done) {
|
||||
var gen = i;
|
||||
break;
|
||||
}
|
||||
|
||||
// create 'empty' array of '.'s
|
||||
const pots = Array(3).join('.').split('');
|
||||
|
||||
state.split('').forEach((_, n) => {
|
||||
// get the section to look at of prev gen
|
||||
const sect = state.slice(n-2, n+3);
|
||||
if (sect.length !== 5) { return }
|
||||
|
||||
// get the value of the matching rule
|
||||
let plant = rules[sect];
|
||||
if (!plant) { return }
|
||||
|
||||
// replace the value in the next gen
|
||||
pots.push(plant);
|
||||
});
|
||||
// move the '0th pot' marker
|
||||
zero -= pots.indexOf('#') - 5;
|
||||
|
||||
// if the pattern is repeating. process is done.
|
||||
done = expand(pots.join('')) === state;
|
||||
|
||||
// store the generation with extra pots.
|
||||
state = expand(pots.join(''));
|
||||
|
||||
|
||||
// console.log(Array(20-zero).join(' ')+state); // output with pot 0 aligned
|
||||
// console.log(state); // output misaligned
|
||||
|
||||
}
|
||||
|
||||
// calculate the value of last gen
|
||||
const result = state.split('').reduce((val, pot, index) => {
|
||||
return (Number(val) || 0) + (pot === '#' ? index - zero : 0);
|
||||
})
|
||||
|
||||
console.log(result + (50000000000 - gen) * state.match(/#/g).length)
|
16
2018/12/test.txt
Normal file
16
2018/12/test.txt
Normal file
|
@ -0,0 +1,16 @@
|
|||
initial state: #..#.#..##......###...###
|
||||
|
||||
...## => #
|
||||
..#.. => #
|
||||
.#... => #
|
||||
.#.#. => #
|
||||
.#.## => #
|
||||
.##.. => #
|
||||
.#### => #
|
||||
#.#.# => #
|
||||
#.### => #
|
||||
##.#. => #
|
||||
##.## => #
|
||||
###.. => #
|
||||
###.# => #
|
||||
####. => #
|
|
@ -1,34 +1,37 @@
|
|||
# [2018](https://adventofcode.com/2018 "2018 puzzle calendar")
|
||||
1. [:star: :star:](https://adventofcode.com/2015/day/1 "see puzzle")
|
||||
2. [:star: :star:](https://adventofcode.com/2015/day/2 "see puzzle")
|
||||
3. [:star: :star:](https://adventofcode.com/2015/day/3 "see puzzle")
|
||||
4. [:star: :star:](https://adventofcode.com/2015/day/4 "see puzzle")
|
||||
5. [:star: :star:](https://adventofcode.com/2015/day/5 "see puzzle")
|
||||
6. [:star: :star:](https://adventofcode.com/2015/day/6 "see puzzle")
|
||||
7. [:star: :star:](https://adventofcode.com/2015/day/7 "see puzzle")
|
||||
8. [:star: :star:](https://adventofcode.com/2015/day/8 "see puzzle")
|
||||
9. [:star: :star:](https://adventofcode.com/2015/day/9 "see puzzle")
|
||||
10. [:star: :star:](https://adventofcode.com/2015/day/9 "see puzzle")
|
||||
11. [:star: :star:](https://adventofcode.com/2015/day/10 "see puzzle")
|
||||
1 | [:star: :star:](https://adventofcode.com/2015/day/1 "see puzzle")
|
||||
--: | :--
|
||||
2 | [:star: :star:](https://adventofcode.com/2015/day/2 "see puzzle")
|
||||
3 | [:star: :star:](https://adventofcode.com/2015/day/3 "see puzzle")
|
||||
4 | [:star: :star:](https://adventofcode.com/2015/day/4 "see puzzle")
|
||||
5 | [:star: :star:](https://adventofcode.com/2015/day/5 "see puzzle")
|
||||
6 | [:star: :star:](https://adventofcode.com/2015/day/6 "see puzzle")
|
||||
7 | [:star: :star:](https://adventofcode.com/2015/day/7 "see puzzle")
|
||||
8 | [:star: :star:](https://adventofcode.com/2015/day/8 "see puzzle")
|
||||
9 | [:star: :star:](https://adventofcode.com/2015/day/9 "see puzzle")
|
||||
10 | [:star: :star:](https://adventofcode.com/2015/day/10 "see puzzle")
|
||||
11 | [:star: :star:](https://adventofcode.com/2015/day/11 "see puzzle")
|
||||
12 | [:star: :star:](https://adventofcode.com/2015/day/12 "see puzzle")
|
||||
|
||||
# Languages Used
|
||||
### Python
|
||||
2. :star: :star:
|
||||
|
||||
|
||||
11. :star: :star:
|
||||
2 | :star: :star:
|
||||
--: | :--
|
||||
11 | :star: :star:
|
||||
|
||||
### Node JS
|
||||
1. :star: :star:
|
||||
|
||||
|
||||
3. :star: :star:
|
||||
4. :star: :star:
|
||||
5. :star: :star:
|
||||
6. :star: :star:
|
||||
7. :star: :star:
|
||||
8. :star: :star:
|
||||
9. :star: :star:
|
||||
1 | :star: :star:
|
||||
--: | :--
|
||||
3 | :star: :star:
|
||||
4 | :star: :star:
|
||||
5 | :star: :star:
|
||||
6 | :star: :star:
|
||||
7 | :star: :star:
|
||||
8 | :star: :star:
|
||||
9 | :star: :star:
|
||||
12 | :star: :star:
|
||||
|
||||
### Java (Processing)
|
||||
10. :star: :star:
|
||||
1 | :star: :star:
|
||||
--: | :--
|
||||
|
|
33
README.md
33
README.md
|
@ -1,6 +1,6 @@
|
|||
# Advent of Code Solutions
|
||||
|
||||
It's a fun challenge. [2018](#2018) is currently in progress.
|
||||
It's a fun challenge! You should check it out if you like coding and are interested in algorithms.
|
||||
|
||||
```js
|
||||
if (advent) {
|
||||
|
@ -11,24 +11,25 @@ if (advent) {
|
|||
# Completed Challenges
|
||||
#### [2015](https://adventofcode.com/2015 "2015 puzzle calendar")
|
||||
1. [:star: :star:](https://adventofcode.com/2015/day/1 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2015/day/2 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2015/day/3 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2015/day/4 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2015/day/5 "see puzzle")
|
||||
1. [:star:](https://adventofcode.com/2015/day/6 "see puzzle")
|
||||
2. [:star: :star:](https://adventofcode.com/2015/day/2 "see puzzle")
|
||||
3. [:star: :star:](https://adventofcode.com/2015/day/3 "see puzzle")
|
||||
4. [:star: :star:](https://adventofcode.com/2015/day/4 "see puzzle")
|
||||
5. [:star: :star:](https://adventofcode.com/2015/day/5 "see puzzle")
|
||||
6. [:star:](https://adventofcode.com/2015/day/6 "see puzzle")
|
||||
|
||||
#### [2018](https://adventofcode.com/2018 "2018 puzzle calendar")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/1 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/2 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/3 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/4 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/5 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/6 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/7 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/8 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/9 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/10 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/11 "see puzzle")
|
||||
2. [:star: :star:](https://adventofcode.com/2018/day/2 "see puzzle")
|
||||
3. [:star: :star:](https://adventofcode.com/2018/day/3 "see puzzle")
|
||||
4. [:star: :star:](https://adventofcode.com/2018/day/4 "see puzzle")
|
||||
5. [:star: :star:](https://adventofcode.com/2018/day/5 "see puzzle")
|
||||
6. [:star: :star:](https://adventofcode.com/2018/day/6 "see puzzle")
|
||||
7. [:star: :star:](https://adventofcode.com/2018/day/7 "see puzzle")
|
||||
8. [:star: :star:](https://adventofcode.com/2018/day/8 "see puzzle")
|
||||
9. [:star: :star:](https://adventofcode.com/2018/day/9 "see puzzle")
|
||||
10. [:star: :star:](https://adventofcode.com/2018/day/10 "see puzzle")
|
||||
11. [:star: :star:](https://adventofcode.com/2018/day/11 "see puzzle")
|
||||
12. [:star: :star:](https://adventofcode.com/2018/day/12 "see puzzle")
|
||||
|
||||
## Languages Used
|
||||
* Python
|
||||
|
|
Loading…
Reference in a new issue