Skip to content

Commit 99241d4

Browse files
feature(2018 day-12): day 12 part 1 solution
1 parent 9eef11d commit 99241d4

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

2018/day-12/helpers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
const { listToProps } = require('../day-10/helpers')
2+
const fs = require('fs')
3+
const path = require('path')
4+
const filePath = path.join(__dirname, 'input.txt')
5+
const { linesToArray } = require('../inputParser')
6+
7+
const loadInput = (callback) => {
8+
fs.readFile(filePath, { encoding: 'utf8' }, (err, data) => {
9+
if (err) throw err
10+
11+
const list = linesToArray(data).map(parseLine)
12+
if (typeof callback === 'function') {
13+
callback(list)
14+
}
15+
})
16+
}
217

318
/**
419
* Parses a line from the input into structured data
@@ -10,5 +25,6 @@ const parseLine = (input) => {
1025
}
1126

1227
module.exports = {
28+
loadInput,
1329
parseLine
1430
}

2018/day-12/input.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
...## => #
2+
..#.. => #
3+
.#... => #
4+
.#.#. => #
5+
.#.## => #
6+
.##.. => #
7+
.#### => #
8+
#.#.# => #
9+
#.### => #
10+
##.#. => #
11+
##.## => #
12+
###.. => #
13+
###.# => #
14+
####. => #

2018/day-12/solution.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
const {
2-
parseLine
2+
loadInput
33
} = require('./helpers')
44
const {
55
Plants
66
} = require('./plants')
77

8-
const initialState = '#..#.#..##......###...###'
9-
const rules = `...## => #
10-
..#.. => #
11-
.#... => #
12-
.#.#. => #
13-
.#.## => #
14-
.##.. => #
15-
.#### => #
16-
#.#.# => #
17-
#.### => #
18-
##.#. => #
19-
##.## => #
20-
###.. => #
21-
###.# => #
22-
####. => #`.split('\n').map(parseLine)
23-
const plantTracker = new Plants(initialState, rules)
24-
for (let gen = 1; gen <= 20; gen++) {
25-
plantTracker.advance()
8+
// Initial state from my puzzle input
9+
const initialState = '#...#...##..####..##.####.#...#...#.#.#.#......##....#....######.####.##..#..#..##.##..##....#######'
10+
11+
const init = (data) => {
12+
const rules = data
13+
const plantTracker = new Plants(initialState, rules)
14+
for (let gen = 1; gen <= 20; gen++) {
15+
plantTracker.advance()
16+
}
17+
console.log('Generating 20 generations from the input looks like this:')
18+
plantTracker.display()
19+
const answer = plantTracker.getCheckSum(20)
20+
const answer2 = ''
21+
22+
console.log(`-- Part 1 --`)
23+
console.log(`Answer: ${answer}`)
24+
console.log(`-- Part 2 --`)
25+
console.log(`Answer: ${answer2}`)
2626
}
27-
plantTracker.display()
27+
28+
loadInput(init)

0 commit comments

Comments
 (0)