diff --git a/2018/12/input.txt b/2018/12/input.txt new file mode 100644 index 0000000..beee9ee --- /dev/null +++ b/2018/12/input.txt @@ -0,0 +1,34 @@ +initial state: ##....#.#.#...#.#..#.#####.#.#.##.#.#.#######...#.##....#..##....#.#..##.####.#..........#..#...# + +..#.# => # +.#### => # +#.... => . +####. => # +...## => . +.#.#. => . +..#.. => . +##.#. => . +#.#.# => # +..... => . +#.#.. => . +....# => . +.#..# => . +###.# => # +#..#. => . +##### => . +...#. => # +#.##. => # +.#.## => # +#..## => # +.##.. => # +##.## => . +..### => . +###.. => . +##..# => # +.#... => # +.###. => # +#.### => . +.##.# => . +#...# => # +##... => . +..##. => . \ No newline at end of file diff --git a/2018/12/part1.js b/2018/12/part1.js new file mode 100644 index 0000000..95776da --- /dev/null +++ b/2018/12/part1.js @@ -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); \ No newline at end of file diff --git a/2018/12/part2.js b/2018/12/part2.js new file mode 100644 index 0000000..e3fc4da --- /dev/null +++ b/2018/12/part2.js @@ -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) \ No newline at end of file diff --git a/2018/12/test.txt b/2018/12/test.txt new file mode 100644 index 0000000..8335d88 --- /dev/null +++ b/2018/12/test.txt @@ -0,0 +1,16 @@ +initial state: #..#.#..##......###...### + +...## => # +..#.. => # +.#... => # +.#.#. => # +.#.## => # +.##.. => # +.#### => # +#.#.# => # +#.### => # +##.#. => # +##.## => # +###.. => # +###.# => # +####. => # \ No newline at end of file diff --git a/2018/README.md b/2018/README.md index 9028d99..8be6283 100644 --- a/2018/README.md +++ b/2018/README.md @@ -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: \ No newline at end of file +1 | :star: :star: +--: | :-- diff --git a/README.md b/README.md index 1dd6a4e..efad76f 100644 --- a/README.md +++ b/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