Skip to content

Commit c1579d9

Browse files
committed
2016.08
1 parent 0b1409a commit c1579d9

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed

src/main/resources/y2016/day08.txt

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
rect 1x1
2+
rotate row y=0 by 10
3+
rect 1x1
4+
rotate row y=0 by 10
5+
rect 1x1
6+
rotate row y=0 by 5
7+
rect 1x1
8+
rotate row y=0 by 3
9+
rect 2x1
10+
rotate row y=0 by 4
11+
rect 1x1
12+
rotate row y=0 by 3
13+
rect 1x1
14+
rotate row y=0 by 2
15+
rect 1x1
16+
rotate row y=0 by 3
17+
rect 2x1
18+
rotate row y=0 by 2
19+
rect 1x1
20+
rotate row y=0 by 3
21+
rect 2x1
22+
rotate row y=0 by 5
23+
rotate column x=0 by 1
24+
rect 4x1
25+
rotate row y=1 by 12
26+
rotate row y=0 by 10
27+
rotate column x=0 by 1
28+
rect 9x1
29+
rotate column x=7 by 1
30+
rotate row y=1 by 3
31+
rotate row y=0 by 2
32+
rect 1x2
33+
rotate row y=1 by 3
34+
rotate row y=0 by 1
35+
rect 1x3
36+
rotate column x=35 by 1
37+
rotate column x=5 by 2
38+
rotate row y=2 by 5
39+
rotate row y=1 by 5
40+
rotate row y=0 by 2
41+
rect 1x3
42+
rotate row y=2 by 8
43+
rotate row y=1 by 10
44+
rotate row y=0 by 5
45+
rotate column x=5 by 1
46+
rotate column x=0 by 1
47+
rect 6x1
48+
rotate row y=2 by 7
49+
rotate row y=0 by 5
50+
rotate column x=0 by 1
51+
rect 4x1
52+
rotate column x=40 by 2
53+
rotate row y=2 by 10
54+
rotate row y=0 by 12
55+
rotate column x=5 by 1
56+
rotate column x=0 by 1
57+
rect 9x1
58+
rotate column x=43 by 1
59+
rotate column x=40 by 2
60+
rotate column x=38 by 1
61+
rotate column x=15 by 1
62+
rotate row y=3 by 35
63+
rotate row y=2 by 35
64+
rotate row y=1 by 32
65+
rotate row y=0 by 40
66+
rotate column x=32 by 1
67+
rotate column x=29 by 1
68+
rotate column x=27 by 1
69+
rotate column x=25 by 1
70+
rotate column x=23 by 2
71+
rotate column x=22 by 1
72+
rotate column x=21 by 3
73+
rotate column x=20 by 1
74+
rotate column x=18 by 3
75+
rotate column x=17 by 1
76+
rotate column x=15 by 1
77+
rotate column x=14 by 1
78+
rotate column x=12 by 1
79+
rotate column x=11 by 3
80+
rotate column x=10 by 1
81+
rotate column x=9 by 1
82+
rotate column x=8 by 2
83+
rotate column x=7 by 1
84+
rotate column x=4 by 1
85+
rotate column x=3 by 1
86+
rotate column x=2 by 1
87+
rotate column x=0 by 1
88+
rect 34x1
89+
rotate column x=44 by 1
90+
rotate column x=24 by 1
91+
rotate column x=19 by 1
92+
rotate row y=1 by 8
93+
rotate row y=0 by 10
94+
rotate column x=8 by 1
95+
rotate column x=7 by 1
96+
rotate column x=6 by 1
97+
rotate column x=5 by 2
98+
rotate column x=3 by 1
99+
rotate column x=2 by 1
100+
rotate column x=1 by 1
101+
rotate column x=0 by 1
102+
rect 9x1
103+
rotate row y=0 by 40
104+
rotate column x=43 by 1
105+
rotate row y=4 by 10
106+
rotate row y=3 by 10
107+
rotate row y=2 by 5
108+
rotate row y=1 by 10
109+
rotate row y=0 by 15
110+
rotate column x=7 by 2
111+
rotate column x=6 by 3
112+
rotate column x=5 by 2
113+
rotate column x=3 by 2
114+
rotate column x=2 by 4
115+
rotate column x=0 by 2
116+
rect 9x2
117+
rotate row y=3 by 47
118+
rotate row y=0 by 10
119+
rotate column x=42 by 3
120+
rotate column x=39 by 4
121+
rotate column x=34 by 3
122+
rotate column x=32 by 3
123+
rotate column x=29 by 3
124+
rotate column x=22 by 3
125+
rotate column x=19 by 3
126+
rotate column x=14 by 4
127+
rotate column x=4 by 3
128+
rotate row y=4 by 3
129+
rotate row y=3 by 8
130+
rotate row y=1 by 5
131+
rotate column x=2 by 3
132+
rotate column x=1 by 3
133+
rotate column x=0 by 2
134+
rect 3x2
135+
rotate row y=4 by 8
136+
rotate column x=45 by 1
137+
rotate column x=40 by 5
138+
rotate column x=26 by 3
139+
rotate column x=25 by 5
140+
rotate column x=15 by 5
141+
rotate column x=10 by 5
142+
rotate column x=7 by 5
143+
rotate row y=5 by 35
144+
rotate row y=4 by 42
145+
rotate row y=2 by 5
146+
rotate row y=1 by 20
147+
rotate row y=0 by 45
148+
rotate column x=48 by 5
149+
rotate column x=47 by 5
150+
rotate column x=46 by 5
151+
rotate column x=43 by 5
152+
rotate column x=41 by 5
153+
rotate column x=38 by 5
154+
rotate column x=37 by 5
155+
rotate column x=36 by 5
156+
rotate column x=33 by 1
157+
rotate column x=32 by 5
158+
rotate column x=31 by 5
159+
rotate column x=30 by 1
160+
rotate column x=28 by 5
161+
rotate column x=27 by 5
162+
rotate column x=26 by 5
163+
rotate column x=23 by 1
164+
rotate column x=22 by 5
165+
rotate column x=21 by 5
166+
rotate column x=20 by 1
167+
rotate column x=17 by 5
168+
rotate column x=16 by 5
169+
rotate column x=13 by 1
170+
rotate column x=12 by 3
171+
rotate column x=7 by 5
172+
rotate column x=6 by 5
173+
rotate column x=3 by 1
174+
rotate column x=2 by 3

src/main/scala/y2016/Day08.scala

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package y2016
2+
3+
import scala.annotation.tailrec
4+
5+
trait Day08 {
6+
7+
object Display {
8+
val Rect = """rect (\d+)x(\d+)""".r
9+
val Column = """rotate column x=(\d+) by (\d+)""".r
10+
val Row = """rotate row y=(\d+) by (\d+)""".r
11+
12+
def apply(width: Int = 50, height: Int = 6) =
13+
new Display(Array.ofDim[Boolean](height, width).map(_.toIndexedSeq).toIndexedSeq)
14+
}
15+
16+
case class Display(pixels: IndexedSeq[IndexedSeq[Boolean]]) {
17+
def rect(x: Int, y: Int): Display = {
18+
@tailrec def recurse(display: Display, x1: Int = 0, y1: Int = 0): Display = {
19+
if (y1 == y) display
20+
else if (x1 == x) recurse(display, 0, y1 + 1)
21+
else recurse(display.copy(pixels = display.pixels.updated(y1, display.pixels(y1).updated(x1, true))), x1 + 1, y1)
22+
}
23+
24+
recurse(this, 0, 0)
25+
}
26+
27+
def rotateColumn(x: Int, n: Int): Display = {
28+
@tailrec def recurse(display: Display, y: Int = 0): Display =
29+
if (y == pixels.size) display
30+
else recurse(display.copy(
31+
pixels = display.pixels.updated(y,
32+
display.pixels(y).updated(x, pixels((y + pixels.size - n) % pixels.size)(x)))), y + 1)
33+
34+
recurse(this)
35+
}
36+
37+
def rotateRow(y: Int, n: Int): Display = {
38+
@tailrec def recurse(display: Display, x: Int = 0): Display =
39+
if (x == pixels.head.size) display
40+
else recurse(display.copy(
41+
pixels = display.pixels.updated(y,
42+
display.pixels(y).updated(x, pixels(y)((x + pixels.head.size - n) % pixels.head.size)))), x + 1)
43+
44+
recurse(this)
45+
}
46+
47+
def execute(instruction: String): Display = {
48+
import Display._
49+
instruction match {
50+
case Rect(x, y) => rect(x.toInt, y.toInt)
51+
case Row(y, n) => rotateRow(y.toInt, n.toInt)
52+
case Column(x, n) => rotateColumn(x.toInt, n.toInt)
53+
}
54+
}
55+
56+
def lit: Int = pixels.map(row => row.count(_ == true)).sum
57+
58+
override def toString: String = pixels.map(row => row.map(p => if (p) 'X' else ' ').mkString("|")).mkString("\n")
59+
}
60+
61+
def solve(input: Seq[String]): Display = input.foldLeft(Display()) {
62+
case (display, input) => display.execute(input)
63+
}
64+
}

src/test/scala/y2016/Day08Spec.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package y2016
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should._
5+
6+
class Day08Spec extends AnyFlatSpec with Matchers with Day08 {
7+
private lazy val input = util.Loader(this, "day08.txt").toSeq
8+
it should "pass part 1" in {
9+
val display = solve(input)
10+
(display.lit, display.toString) shouldBe(121,
11+
"X|X|X| | |X| | |X| |X|X|X| | |X| | |X| | |X|X| | |X|X|X|X| | |X|X| | |X|X|X|X| | |X|X|X| |X| | | | \n" +
12+
"X| | |X| |X| | |X| |X| | |X| |X| | |X| |X| | |X| |X| | | | |X| | |X| |X| | | | | | |X| | |X| | | | \n" +
13+
"X| | |X| |X| | |X| |X| | |X| |X| | |X| |X| | | | |X|X|X| | |X| | |X| |X|X|X| | | | |X| | |X| | | | \n" +
14+
"X|X|X| | |X| | |X| |X|X|X| | |X| | |X| |X| | | | |X| | | | |X| | |X| |X| | | | | | |X| | |X| | | | \n" +
15+
"X| |X| | |X| | |X| |X| |X| | |X| | |X| |X| | |X| |X| | | | |X| | |X| |X| | | | | | |X| | |X| | | | \n" +
16+
"X| | |X| | |X|X| | |X| | |X| | |X|X| | | |X|X| | |X|X|X|X| | |X|X| | |X|X|X|X| | |X|X|X| |X|X|X|X| ")
17+
}
18+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy