Skip to content

Commit 2a65a6b

Browse files
committed
day 12 part 1
1 parent ab01480 commit 2a65a6b

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

src/main/resources/day12/input.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
initial state: #.......##.###.#.#..##..##..#.#.###..###..##.#.#..##....#####..##.#.....########....#....##.#..##...
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+
##... => #

src/main/resources/day12/testData.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
initial state: #..#.#..##......###...###
2+
3+
...## => #
4+
..#.. => #
5+
.#... => #
6+
.#.#. => #
7+
.#.## => #
8+
.##.. => #
9+
.#### => #
10+
#.#.# => #
11+
#.### => #
12+
##.#. => #
13+
##.## => #
14+
###.. => #
15+
###.# => #
16+
####. => #
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.codefork.aoc2018
2+
3+
import scala.io.Source
4+
5+
object Day12Part1 extends Part {
6+
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+
18+
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
64+
}
65+
66+
}

0 commit comments

Comments
 (0)