-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.nim
50 lines (41 loc) · 1.17 KB
/
day12.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import strutils, strscans
let input = "./inputs/12.txt"
const
length = 300
leftPad = 50
var
state = ".".repeat(length)
fruitful: seq[string]
for line in input.lines:
var s: string
if line.scanf("initial state:"):
let initial = line.split[^1]
for i in 0 ..< initial.len:
state[leftPad + i] = initial[i]
elif line.scanf("$+ => #", s):
fruitful.add s
type PlantCount = tuple[plants, total: int]
func countPlants(state: string): PlantCount =
for i, c in state:
if c == '#':
result.plants += 1
result.total += i - leftPad
proc live(state: string, generations: int64): int64 =
var
previous: PlantCount
state = state
newState: string
for generation in 1 .. generations:
newState = ".".repeat(length)
for i in 2 ..< length-2:
if state[i-2 .. i+2] in fruitful:
newState[i] = '#'
let (plants, total) = countPlants newState
if plants == previous.plants and total == previous.total + plants:
let remaining = generations - generation
return total + plants * remaining
previous = (plants, total)
state = newState
return previous.total
echo state.live(20)
echo state.live(50_000_000_000)