new structure for 2018, added license information

This commit is contained in:
Akumatic
2019-12-03 00:24:58 +01:00
parent e79d6aa6a6
commit 200d471145
60 changed files with 1257 additions and 73 deletions

82
2018/12/README.md Normal file
View File

@ -0,0 +1,82 @@
# 2018 Day 12: Subterranean Sustainability
Copyright (c) Eric Wastl
#### [Direct Link](https://adventofcode.com/2018/day/12)
## Part 1
The year 518 is significantly more underground than your history books implied. Either that, or you've arrived in a vast cavern network under the North Pole.
After exploring a little, you discover a long tunnel that contains a row of small pots as far as you can see to your left and right. A few of them contain plants - someone is trying to grow things in these geothermally-heated caves.
The pots are numbered, with `0` in front of you. To the left, the pots are numbered `-1`, `-2`, `-3`, and so on; to the right, `1`, `2`, `3`.... Your puzzle input contains a list of pots from 0 to the right and whether they do (`#`) or do not (`.`) currently contain a plant, the initial state. (No other pots currently contain plants.) For example, an **initial state** of `#..##....` indicates that pots `0`, `3`, and `4` currently contain plants.
Your puzzle input also contains some notes you find on a nearby table: someone has been trying to figure out how these plants **spread** to nearby pots. Based on the notes, for each generation of plants, a given pot has or does not have a plant based on whether that pot (and the two pots on either side of it) had a plant in the last generation. These are written as `LLCRR => N`, where `L` are pots to the left, `C` is the current pot being considered, `R` are the pots to the right, and `N` is whether the current pot will have a plant in the next generation. For example:
- A note like `..#.. => .` means that a pot that contains a plant but with no plants within two pots of it will not have a plant in it during the next generation.
- A note like `##.## => .` means that an empty pot with two plants on each side of it will remain empty in the next generation.
- A note like `.##.# => #` means that a pot has a plant in a given generation if, in the previous generation, there were plants in that pot, the one immediately to the left, and the one two pots to the right, but not in the ones immediately to the right and two to the left.
It's not clear what these plants are for, but you're sure it's important, so you'd like to make sure the current configuration of plants is sustainable by determining what will happen after **`20` generations**.
For example, given the following input:
```
initial state: #..#.#..##......###...###
...## => #
..#.. => #
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #
```
For brevity, in this example, only the combinations which do produce a plant are listed. (Your input includes all possible combinations.) Then, the next 20 generations will look like this:
```
1 2 3
0 0 0 0
0: ...#..#.#..##......###...###...........
1: ...#...#....#.....#..#..#..#...........
2: ...##..##...##....#..#..#..##..........
3: ..#.#...#..#.#....#..#..#...#..........
4: ...#.#..#...#.#...#..#..##..##.........
5: ....#...##...#.#..#..#...#...#.........
6: ....##.#.#....#...#..##..##..##........
7: ...#..###.#...##..#...#...#...#........
8: ...#....##.#.#.#..##..##..##..##.......
9: ...##..#..#####....#...#...#...#.......
10: ..#.#..#...#.##....##..##..##..##......
11: ...#...##...#.#...#.#...#...#...#......
12: ...##.#.#....#.#...#.#..##..##..##.....
13: ..#..###.#....#.#...#....#...#...#.....
14: ..#....##.#....#.#..##...##..##..##....
15: ..##..#..#.#....#....#..#.#...#...#....
16: .#.#..#...#.#...##...#...#.#..##..##...
17: ..#...##...#.#.#.#...##...#....#...#...
18: ..##.#.#....#####.#.#.#...##...##..##..
19: .#..###.#..#.#.#######.#.#.#..#.#...#..
20: .#....##....#####...#######....#.#..##.
```
The generation is shown along the left, where `0` is the initial state. The pot numbers are shown along the top, where `0` labels the center pot, negative-numbered pots extend to the left, and positive pots extend toward the right. Remember, the initial state begins at pot `0`, which is not the leftmost pot used in this example.
After one generation, only seven plants remain. The one in pot `0` matched the rule looking for `..#..`, the one in pot 4 matched the rule looking for `.#.#.`, pot 9 matched `.##..`, and so on.
In this example, after 20 generations, the pots shown as `#` contain plants, the furthest left of which is pot `-2`, and the furthest right of which is pot `34`. Adding up all the numbers of plant-containing pots after the 20th generation produces **`325`**.
**After `20` generations, what is the sum of the numbers of all pots which contain a plant?**
## Part 2
You realize that 20 generations aren't enough. After all, these plants will need to last another 1500 years to even reach your timeline, not to mention your future.
**After fifty billion (`50000000000`) generations, what is the sum of the numbers of all pots which contain a plant?**

119
2018/12/code.py Normal file
View File

@ -0,0 +1,119 @@
""" https://adventofcode.com/2018/day/12 """
def readFile():
with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f:
lines = [line[:-1] for line in f.readlines()]
state = list(lines[0][15:])
rules = [Rule(line) for line in lines[2:] if line[9] == "#"]
return (state, rules)
def getTest():
stateString = "initial state: ...#..#.#..##......###...###..........."
ruleStrings = ["...## => #", "..#.. => #", ".#... => #", ".#.#. => #",
".#.## => #", ".##.. => #", ".#### => #", "#.#.# => #", "#.### => #",
"##.#. => #", "##.## => #", "###.. => #", "###.# => #", "####. => #"]
state = list(stateString[15:])
rules = [Rule(rule) for rule in ruleStrings]
return (state, rules)
class Rule:
def __init__(self, string):
self.left = string[:2]
self.center = string[2]
self.right = string[3:5]
self.rule = list(self.left + self.center + self.right)
self.result = string[9]
def compare(self, state):
return self.rule == state
def part1(vals, generations, offset = 0, printGen = False):
generation = vals[0]
pad = ["."]
if generation[-1] == "#":
generation += [".", "."]
elif generation[-2] == "#":
generation.append(".")
if printGen:
print(f" 0: {''.join(generation)}")
for i in range(generations):
size = len(generation)
cur = ["." for j in range(size)]
for j in range(size):
if j < 2: # pad left side with empty pods
temp = pad*(2 - j) + generation[:(3 + j)]
elif j > size - 3: # pad right side with empty pods
diff = size - j
temp = generation[(size-2-diff):] + pad*(3-diff)
else:
temp = generation[j-2:j+3]
for rule in vals[1]:
if rule.compare(temp):
cur[j] = rule.result
break
if cur[-1] == "#":
cur += [".", "."]
elif cur[-2] == "#":
cur.append(".")
generation = cur
if printGen:
print(f"{i + 1 if i + 1 > 9 else f' {i + 1}'}: {''.join(generation)}")
return sum(i - offset for i in range(len(generation)) if generation[i] == "#")
def part2(vals, generations, offset = 0):
generation = vals[0]
pad = ["."]
genSum = sum(i - offset for i in range(len(generation)) if generation[i] == "#")
if generation[-1] == "#":
generation += [".", "."]
elif generation[-2] == "#":
generation.append(".")
i = 0
while True:
i += 1
size = len(generation)
cur = ["." for j in range(size)]
for j in range(size):
if j < 2: # pad left side with empty pods
temp = pad*(2 - j) + generation[:(3 + j)]
elif j > size - 3: # pad right side with empty pods
diff = size - j
temp = generation[(size-2-diff):] + pad*(3-diff)
else:
temp = generation[j-2:j+3]
for rule in vals[1]:
if rule.compare(temp):
cur[j] = rule.result
break
if cur[-1] == "#":
cur += [".", "."]
elif cur[-2] == "#":
cur.append(".")
generation = cur
curSum = sum(i - offset for i in range(len(cur)) if cur[i] == "#")
if curSum - genSum == 52:
break
genSum = curSum
return curSum + 52*(generations - i)
if __name__ == "__main__":
print(f"Test: {part1(getTest(), generations=20, offset=3, printGen=True)}")
vals = readFile()
print(f"Part 1: {part1(vals, generations=20)}")
print(f"Part 2: {part2(vals, generations=5*10**10)}")

34
2018/12/input.txt Normal file
View File

@ -0,0 +1,34 @@
initial state: ###.......##....#.#.#..###.##..##.....#....#.#.....##.###...###.#...###.###.#.###...#.####.##.#....#
..... => .
#..## => .
..### => #
..#.# => #
.#.#. => .
####. => .
##.## => #
#.... => .
#...# => .
...## => .
##..# => .
.###. => #
##### => #
#.#.. => #
.##.. => #
.#.## => .
...#. => #
#.##. => #
..#.. => #
##... => #
....# => .
###.# => #
#..#. => #
#.### => #
##.#. => .
###.. => #
.#### => .
.#... => #
..##. => .
.##.# => .
#.#.# => #
.#..# => .

2
2018/12/solution.txt Normal file
View File

@ -0,0 +1,2 @@
Part 1: 3221
Part 2: 2600000001872