Skip to content

Commit d632fc7

Browse files
committed
reorg day 12
1 parent 9c79fc6 commit d632fc7

File tree

2 files changed

+78
-58
lines changed

2 files changed

+78
-58
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.codefork.aoc2018
2+
3+
import scala.io.Source
4+
5+
object Day12 {
6+
7+
def getInput: Pots = {
8+
9+
val url = getClass.getResource("/day12/input.txt")
10+
11+
val lines = Source.fromURL(url).getLines().toSeq
12+
13+
lines.head
14+
.replace("initial state: ", "")
15+
.zipWithIndex
16+
.foldLeft(Set[Int]()) {
17+
case (acc, (char, index)) => {
18+
acc ++ (if (char == '#') Set(index) else Set.empty)
19+
}
20+
}
21+
22+
val initial = lines.head.replace("initial state: ", "")
23+
val rules = lines
24+
.drop(2)
25+
.map(line => line.substring(0, 5) -> line.last.toString)
26+
.toMap
27+
28+
Pots(initial, 0, rules)
29+
}
30+
31+
case class Pots(state: String,
32+
startingPotNumber: Int,
33+
rules: Map[String, String]) {
34+
35+
val padded = "...." + state + "...."
36+
37+
def trimTrailingPeriods(s: String) = s.reverse.dropWhile(_ == '.').reverse
38+
39+
def nextGenForPot(paddedPos: Int) = {
40+
val slice = padded.substring(paddedPos - 2, paddedPos + 3)
41+
val result =
42+
if (rules.contains(slice))
43+
rules(slice)
44+
else "."
45+
result
46+
}
47+
48+
def nextGen = {
49+
// result of next gen will be have 4 slots than padded
50+
val t1 = 2
51+
.to(padded.size - 3)
52+
.map(paddedPos => nextGenForPot(paddedPos))
53+
.mkString
54+
val t2 = t1.dropWhile(ch => ch == '.')
55+
val newStartingPotNumber = startingPotNumber - 2 + (t1.length - t2.length)
56+
val t3 = trimTrailingPeriods(t2)
57+
copy(state = t3, startingPotNumber = newStartingPotNumber)
58+
}
59+
60+
def sumPotNumbers =
61+
state
62+
.foldLeft((startingPotNumber, 0)) {
63+
case ((potNumber, acc), char) => {
64+
val newAcc = acc + (if (char == '#') potNumber else 0)
65+
(potNumber + 1, newAcc)
66+
}
67+
}
68+
._2
69+
70+
def sumPotNumbersAtGeneration(i: Int) = {
71+
val result = 1.to(i).foldLeft(this)((pots, i) => pots.nextGen)
72+
result.sumPotNumbers
73+
}
74+
75+
}
76+
77+
}
Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,9 @@
11
package org.codefork.aoc2018
22

3-
import scala.io.Source
4-
53
object Day12Part1 extends Part {
64

7-
def nextGen(state: String, pos: Int, rules: Map[String, String]) = {
8-
val slice = state.substring(pos - 2, pos + 3)
9-
val result =
10-
if (rules.contains(slice))
11-
rules(slice)
12-
else "."
13-
result
14-
}
15-
16-
def trimTrailingPeriods(s: String) = s.reverse.dropWhile(_ == '.').reverse
17-
185
override def answer: String = {
19-
val url = getClass.getResource("/day12/input.txt")
20-
21-
val lines = Source.fromURL(url).getLines().toSeq
22-
23-
lines.head
24-
.replace("initial state: ", "")
25-
.zipWithIndex
26-
.foldLeft(Set[Int]()) {
27-
case (acc, (char, index)) => {
28-
acc ++ (if (char == "#") Set(index) else Set.empty)
29-
}
30-
}
31-
32-
val initial = lines.head.replace("initial state: ", "")
33-
val rules = lines
34-
.drop(2)
35-
.map(line => line.substring(0, 5) -> line.last.toString)
36-
.toMap
37-
38-
val result = 1.to(20).foldLeft((0, initial)) { case ((startingPotNumberOfState, state), i) =>
39-
{
40-
val padded = "...." + state + "...."
41-
// result of next gen will be have 4 slots than padded
42-
val t1 = 2
43-
.to(padded.size - 3)
44-
.map(pos => {
45-
nextGen(padded, pos, rules)
46-
})
47-
.mkString
48-
val t2 = t1.dropWhile(ch => ch == '.')
49-
val newStartingPotNumberOfState = startingPotNumberOfState - 2 + (t1.length - t2.length)
50-
val t3 = trimTrailingPeriods(t2)
51-
//println(i, (newStartingPotNumberOfState, t3))
52-
(newStartingPotNumberOfState, t3)
53-
}
54-
}
55-
56-
val count = result._2.foldLeft((result._1, 0)) {
57-
case ((potNumber, acc), char) => {
58-
val newAcc = acc + (if (char == '#') potNumber else 0)
59-
(potNumber + 1, newAcc)
60-
}
61-
}
62-
63-
count._2.toString
6+
Day12.getInput.sumPotNumbersAtGeneration(20).toString
647
}
658

669
}

0 commit comments

Comments
 (0)