Skip to content

Commit 015b023

Browse files
committed
Day 18 and 21 clear code
Day 7 start
1 parent cd252a6 commit 015b023

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

day-18/Day18.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ object Day18 {
2525

2626
def nextState(state: Array[Array[Char]]): Array[Array[Char]] = {
2727
val newState = state.map(_.clone)
28-
for ((row, y) <- state.view.zipWithIndex;
29-
(light, x) <- row.view.zipWithIndex
28+
for ((row, y) <- state.zipWithIndex;
29+
(light, x) <- row.zipWithIndex
3030
if light == '#' || light == '.'
3131
if !edge(x, y)
3232
) {
@@ -53,7 +53,7 @@ object Day18 {
5353
val contents: Array[String] = Source.fromFile("input.data").getLines.toArray
5454
val grid = Array.ofDim[Char](contents.length, contents.length)
5555

56-
for ((l, i) <- contents.view.zipWithIndex) {
56+
for ((l, i) <- contents.zipWithIndex) {
5757
grid.update(i, l.toCharArray)
5858
}
5959

day-21/Day21.scala

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object Day21 {
2727
(80, 0, 3)
2828
)
2929

30-
def isWinner(p: Player): Boolean = simulate(p, boss, playerNow = true) match {
30+
def isWinner(p: Player): Boolean = simulate(p) match {
3131
case _: Player => true
3232
case _: Boss => false
3333
}
@@ -60,7 +60,6 @@ object Day21 {
6060
val twoRingedWinners = twoRinged.filter(isWinner)
6161
val oneRingedAndArmoredWinners = oneRingedAndArmored.filter(isWinner)
6262

63-
6463
val combined = weaponOnlyWinner ++ armoredWinners ++ oneRingedWinners ++ twoRingedWinners ++ oneRingedAndArmoredWinners
6564
println(s"Winner for part one is: ${combined.minBy(_.cost)}")
6665

@@ -73,15 +72,18 @@ object Day21 {
7372
}
7473

7574
// turn 0 = player; turn = 1
76-
def simulate(p: Player, b: Boss, playerNow: Boolean): Char = {
77-
if (p.hp <= 0) b
78-
else if (b.hp <= 0) p
79-
else {
80-
playerNow match {
81-
case true => simulate(p, new Boss(b.dmg, b.defence, b.hp - math.max(p.dmg - b.defence, 1)), playerNow = false)
82-
case false => simulate(new Player(p.dmg, p.defence, p.cost, p.rings, p.hp - math.max(b.dmg - p.defence, 1)), b, playerNow = true)
75+
def simulate(p: Player): Char = {
76+
def go(p: Player, b: Boss, playerNow: Boolean): Char = {
77+
if (p.hp <= 0) b
78+
else if (b.hp <= 0) p
79+
else {
80+
playerNow match {
81+
case true => go(p, new Boss(b.dmg, b.defence, b.hp - math.max(p.dmg - b.defence, 1)), playerNow = false)
82+
case false => go(new Player(p.dmg, p.defence, p.cost, p.rings, p.hp - math.max(b.dmg - p.defence, 1)), b, playerNow = true)
83+
}
8384
}
8485
}
86+
go(p, boss, playerNow = true)
8587
}
8688

8789
trait Char

day-7/Day7.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import scala.io.Source
2+
import scala.util.matching.Regex
3+
4+
/**
5+
* Created by paulius on 20/12/2015.
6+
*/
7+
8+
object Day7 {
9+
type op = (Option[Left], Option[String], Option[String], String)
10+
def main(args: Array[String]) {
11+
val lines: List[String] = Source.fromFile("test.data").getLines.toList
12+
val regex: Regex = """.*(AND|OR|NOT|LSHIFT|RSHIFT).*""".r
13+
14+
lines.map { l =>
15+
val op = l match {
16+
case regex(operator) => operator
17+
case _ => ""
18+
}
19+
20+
val split: Array[String] = l.split(" ")
21+
op match {
22+
case "AND" => (Some(split.head), op, Some(split(2)), split.last)
23+
case "OR" =>
24+
case "NOT" =>
25+
case "" =>
26+
}
27+
} foreach println
28+
}
29+
}
30+
31+
trait Left extends Any
32+
case class LString(v: String) extends Left
33+
case class LInt(v: Int) extends Left
34+
35+
//class Op(val out: String)
36+
//trait LR {
37+
// def left:
38+
//
39+
//}
40+
//class And(val left) extends Op with LR
41+
42+

0 commit comments

Comments
 (0)