Commit 74a03ce1 authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

[day24] solution

parent bd27d6ea
sesenwnenenewseeswwswswwnenewsewsw
neeenesenwnwwswnenewnwwsewnenwseswesw
seswneswswsenwwnwse
nwnwneseeswswnenewneswwnewseswneseene
swweswneswnenwsewnwneneseenw
eesenwseswswnenwswnwnwsewwnwsene
sewnenenenesenwsewnenwwwse
wenwwweseeeweswwwnwwe
wsweesenenewnwwnwsenewsenwwsesesenwne
neeswseenwwswnwswswnw
nenwswwsewswnenenewsenwsenwnesesenew
enewnwewneswsewnwswenweswnenwsenwsw
sweneswneswneneenwnewenewwneswswnese
swwesenesewenwneswnwwneseswwne
enesenwswwswneneswsenwnewswseenwsese
wnwnesenesenenwwnenwsewesewsesesew
nenewswnwewswnenesenwnesewesw
eneswnwswnwsenenwnwnwwseeswneewsenese
neswnwewnwnwseenwseesewsenwsweewe
wseweeenwnesenwwwswnew
sweswneswswswswwswswswseneswswnwswwne
swwswswswnwswwnwswswswwswswseenwsesw
eeeeseeeneeenesweweeeswee
swswswwneseneswswwnwswneswswwenewne
seesesesenewsenwseseesesesesesenwesee
nwsenwnwnewnewnwenenewnenenwneenenwne
seseswsesenwnwswsenwneswswsesenwseswswsesw
enwnwneswnewneswnwnenenwnesewsenenenwne
swswnwsweseseswswswseweswswswnwseswsesw
wwwweneswswwswwswswwwwswnewnww
wwwnewwswswseswewwsewswwwswwnwne
wswswwnwswwswwseewwweswwnesww
wnwnwsenenwnwnwsenwnw
nwwwsesenwnewsenenweneneeeswwsese
senenwnwwnwsewwewswnwnwewnwnwwnwnw
seswsesenwseswseseeseeseseewswneseww
ewswseneswseswswswnewnwwswswswnwewnw
nwnwnwnenwsesesenwnwwewnwnwwewwe
swswswswswswnwseneswwswsww
swseneweswswseswseeseswswswseswwnww
swseswswswneswswswewwwswwsw
wswwnwnewnwnwwnwswwnesenwnwwwnww
wwnewnewnwswwwwwsewwwswwwswese
wswsesewswswwwwenewwwwwwnwswsw
seeeeneweeesweeeeneeeeeew
swseswneneseeswweswneswsewnwswsewese
eseseeneswsenweswesewenwseneenww
neswnwnwnenwnwenenwwswnwswnenwnenwenw
swewswswswswswwenwswswwswswnewswnesw
eswwnwnwnewsenwwnewwwsweesesene
swneneswnwneneswnwwnenwneneneneeseswwsene
swnwnenenenwnewenwnenweeneswneswnenwswsw
seneewesesesesenwseswsesesesewseseesese
wseswsenwwnesenwnwenwneesewnwwnwwnw
nwnenenwnenwnwnenenwnwneeneesesweneswnwsw
nesenewneneneneneneenenenenenenewe
seneseseseswswseesenwsee
swsweswenwswneswwswswwswswewsweswsw
ewneswewwwnwwsewwswwwwwwwww
wwwneswnwnweseseewnenwewwswww
swneenwnwnwnwwwwnwsenwwwenwswnwnwww
seenwswseseesenwsenwseseseseseseswsese
seeesweeseeneesee
nwnwwsenewnwwnwnwwsenwnewsesenwsenwnw
wswswseswseneeswswswswseswswseswswnw
neeseesesewsesewseeseseeeeeenwse
swnwnwenewnwnwnwnwnwnwnwsenwnwnwnwnwnwe
wwenweswnweenwnwsewswneenewsenenw
nwseeweswseeswnwneseweenesenwenee
nwnwnwnwnwnwnwnwnwnwswsenewesenwnwnwnw
nwwneseneeneneneneneesenwnenene
neneeneeseneneneswneswnwnenenenenewnene
neneeswenwseneene
nesenenweswswswsesenwnesesewswswwsww
wwwsewnwnwwwnwnwneewse
wwwnwnwneswsenwewswswewnewswnwnw
neewwewneneeeseseneeneenewseneee
wswnweswnewswwswsewnenewwe
neneesesenwnwsweene
swnwenenwnwnwseswswneewnwnenwnesewesesw
seseswseswnwswswweseswseweswsesenwsee
swsesesenwwnwenwnwneeswseswseswesesenw
nwnwnwnwnwnwnwnwnwnenwsenwnw
senweeewewsewewnwesenewneswenwsw
nenwnenwwweswsenwseseswswswwnenewswenw
eseeesweenweeesesesewseeeenwe
neswnenenenweeswseeseswewswnwneneene
sewnwwswwwnwnwnenenwswenwwwnwnwnw
wseneseeeseseseseseseee
seeeeeseweseseseseewwsesesesesew
swseswnwswnwnesewsweswswsw
neseneneeneenenewseeenesweenwene
swseswsenewswseseseswseseseswse
nwnwnwwswwswwwnene
nwnwewnweneneenenwwwsenwswneswee
wnwswnwneswnwwnwwnewwenwwwsenwww
swneswneswswneenenenenwenweneneeew
wwswwswswswwswwseswswswswnwswne
swnwseesesesesesesenweseeseseseesewe
eenenwneneneswnenwneeneneeswnenenwneswne
seeseseeswseewnweenwsenwswesesene
wswswswswswseswsewwnwswswnww
eeeweneeswenenewweeeenenesenee
nwwwewswnwwwwewnwsesew
wswsenweenwnwwnwswewswnene
seseesenweswswseseswsesweswwswnwswse
eeneswenenenwswnwnenenesw
seseneswwnwneswswsweswnenwnwsenewswenesw
neneneneneswneneswnenenenwneswnenenenee
neeswnenenenenenenenwnewe
swswswnwseswseswsenwswswswseswsesw
nenwswswswswswswwsenwseswswwswswswswswesw
wseesenwwnwswseseenwseseseswseeswsenese
sewwnenewwwwwwswwwwwnww
wsewseseseneeeseeweeesew
nwnwnwnwenwnwnwnwnwnwnwenwnwnwswsenwswnw
neswwseswseswseswswswseseswnewseswswne
wswnwwnwenwwnwwnwwewwwww
nwnwnwswwwwnwewnewwwnwnw
eweeeseseeeeseee
wswwnwnwnwnwenwnwnwswnwnwwwnenw
nenenwnenenenenenenenesenenenenw
wneneswneseeneenenenenweneeeneene
wewnwenwwenwnwnwnesenwnwwnwnwew
seseswswneseseseseseseseneswsesese
neneneneneenwsewnesenenenenenwnwnewnene
wwwwwnewswseswwsewnwwwwneww
swwswwwwwwesewswwswwwswnene
nwenwnenwneneswnwnenesewnwnenenwnwnwne
seenwswswseseeswswnwenwenwnweesene
senwsewnenwneswwnwnwenwswenwwnwwnenw
newsenwseweesenewnwnwnwwwswnwnwnwsw
eseseseeewnwesewswseenwenwewne
wswneswswswswswswsw
sewswsesesesesesewswwseneseenesw
enenwnwsenwnwwsenwnwnwwwnwnwnwsewnw
senwewswsenenwwseesweweneeseswnew
senenenwnenenwnenwnwnenwenenwnwnwswwe
neeseseeseseeewseeseseee
wswwwnwsewnewswswnewwwse
nwnwnenwwwwswwsewwwewsewswswswesw
esweneeeneeeeeeenwne
swwwnewswnwwswwsenweseeewswswswnw
nwnwnwsesenwnwwwenenesewnwswwnwnwnwsw
nwswnenenenwnwnwenenenwswnweewnwnwne
nwnenwwnwnwewnwnwnwsenwnwsewnwnwsene
eswnwswswswsweswwswwsweswswswswwnwsw
enewnenenenenewneneeswnenwnenwneew
swswseswswneswewswwseswnwwnwneswneswsesw
nwewswseeswsenwwweswneesewenenwe
eswswseswseseswswnewse
nwneneneseneneneseneneneneneswnenenwnene
sesesenwenweeenwnwswwsweeneneenene
nwwnwnwsesenwnwnwnwnwnenenwwneswesenwne
seewnenwwnwnewwsenwswwwwwnwnew
eswneneeneenwneswneneneneswnewsenee
neneswnenwnenwnwnenene
eesweseeeeneeweeeeeenwenw
seseseseneseseseseswswnewnwsesewswswse
nwnwsenwswwnwnesenwenenenwneeswnwnwne
wneswwwwnwnewnwwswswwnwwnwwsene
eswneneswnweneneneneneeneene
nwnwwwsenwswswnwswenenwnew
swswswswwwnwwenwwswswswneswswseswe
ewwnwseseswnewswwneswwswnwswswee
eesewwneeeseneeseseweseeseee
eeweweeeeesenweeeeeeeesw
enwnenwnwnenwnwnwnenwswneswnwnwnwenwnw
nwnwwwswsewwnwnweswnwwnewswnewnwnw
eeneneewseneneeenenweeeweeseese
nwnwswsenewwwsewnwwwneww
newnenenwnwwnweseswsenenesenwnenew
eseseneseeeseseswwnwseeeenwseesese
seswswswnewnwnwewswsenweswseenwswse
swsenenwseswseseswwseneswsewswsenenew
wnenenwnenwnwewnwnenwseneenwnwnewnw
neneneenwwseeenwswseeewseneeewse
swewsesenwneseneeswenwsese
eswseesweeenenenweenenenewnenwne
newnenenenewnenesweneneneneenwnwnenesw
wnwwnwwnwwnwnwsewwnwnwnww
sewwwsewwwwwwnenwenenwwww
eseewnesenwseseseseneswsenwsesesenenww
neswswnewseswseeswswswsewswseseswswswe
seswwnwsweweswnwnesenwswseeenwsesw
nenenenenenenweneewwenesweswenenw
seseneseswsesewnwwseesenwnwsw
nwswnwweswnenwnwnenenenwnwnwswnenwnwnwnw
nwswnwewswswswwewwwswswswwswnewsw
seneseneeeeswswsenwnewnwwenenwwswne
seeseseseenwswsewseswwneswsenewnwswne
wwwwnwwewewswswwwwwwewnw
neeswewwwwwwswwwseenwnewwseew
neneswnwwnenwnwwswwnwnwesese
eeneneeneneneneneneewee
nwwswseneeeenewneeeeneenewsweesw
nweswsweswneswenwsweeneswswwswwnw
swneneneseneeeswnwswnweneneneenwneee
seenwswnwnwswswswswseswswesweswseswsene
swswswneswswnwenwsenesewenwnwsenwswswswe
eeneweeseseweeseeesesenesesese
nwnesenenwnwnenwnenwnewswneenenenenene
wnewnenwswwnwwsenwseseeswnenewewwsw
eeeseneweenenwwweewsweneeee
neneswenwswnwenesweneneeeneneeneeene
eneswweeswesenwnenenenee
nwswweswneesesenenwswswswe
neswseswseneswwneneneeswnwnenenewnenene
swswesesenwsenewseseseseswseeswswswswse
eswnwswswswseswswswswswsw
nwnwnwewnwnwneneenwwsenwnwnwnwwnene
enewswwseseswnenwneenenwnwsesesewnenene
eenwnwwswenwnwnwwenesenwewneswne
seseseseswseneseseneewswsesewsese
neneneneneneswswnewneenwneneewnenenee
ewswwswswwnwnwwwswswswseswwwwse
wnwneswwswswnwwnwweenenwnwewsenwne
sewneseenwseseseneenwswseseesew
enweeneseswswneeneweeswsesweenw
nwnwnwsenwnwnwnenwnwnwwnwnwnwnwsew
nwnenwwnwnwswnwnwwnwnwwwnwenwenww
nwnenenwneswswnwnwnenwnesenwsenenenenenenw
sesenesewnwseseeseew
swnwwnwsenwneswnwneenenwsenesene
seeseswwnewwnwewwwswesenwnewsenw
swsweswswweswwswewswswnwswwneswswsw
seeseswnwweseenesesesenesesenwwseew
swseswswswswsweswswsewswswenwswnesww
nwnwwwnwwnwswnwnwseenwenwsenwnwnenw
nwsewsewnwsesesenweswseseseseeesese
seeneeenwneneneeeenenenee
swnwwnwnwnwnwnwnenwnwnwnwnw
swseeswswseneeseswseeswsesenwwswswswnw
wnewwswsewwwwww
nwseeswnwnweewweswnewswnwweee
swseswsesenesesenwwseswseeswseseseenwse
swswnwwseswneeswwswneseswnew
seseeseneseseweeseesesenenwseswww
nesenwwewsesweesewseswswwswnwne
swnwwneeenwnwnwswswenwnwnenwenwwswnw
senwnwsenwwsenwnwne
swswswnwswwseneeneswwswnewneswnesewswse
wsenenweewwewnwwnwnenwnwnwesenw
nwnwnwnwwnwnwnwnwnwsenwswnwnwnwnwnwsene
neenenwneneseneesweneswwswne
eweeeesenweeeenwseweweeee
wswwweswsewwnewseswwwnewwwnesw
wnwnenwswnwnwnwnwwwnwwnwnw
nwnwewnwswneenenwnenwnwnwnenw
eneeweseeswswesenwnwseeswenwseene
eswnwnwsweeeswwswnwswwswwseenww
weswsesewnwwseeseweseseeeneenenew
nwnwnwnwnwwewnwnwnwwnwnwsenwnw
nwnwnwswnwnwnwnenewnwenwnwne
nwseneewneseneeseseseswsewwsesewsew
swsenweseseeeseswseeeseseewesenwne
wswnwnwwwewswnwwnewsenwnwweww
seesewswnwesenwswswsenwswswseswesw
neesewnwsenwsewnwsweswsenenewneewse
nwnwwwnwnwsewnewwwnwnwwnwnwswenw
neneswswswswsesenwswswswswwswseseswswsw
nweeswswneneeeneneewswneenenenwswne
wseeeweseeeeseneeeeenweeew
nenenewneneneneswneneseneeneeenenewne
nwnwnwenwnwnwnwnwnenwswnw
sewsesesenenesewseseseseneeesesewe
eeneseswseweeeeeeweseeeenwe
nwnwnwnwewnwwswnwnwnwwenwenwnwenw
nwnwnwsewseeseenww
nwenwnenenwswnwenenwneswneswnwnwnwnwnwne
wwswseneswseneweswenwseseswswsweswsw
swwwnweswswwwnesewsw
eneeeeeeswwneneeeee
nwnewwwwwseewswnewwwwwwnww
senwnwnwnwnwnwnwnwwsenenwnwnwnwnwsenwnwnw
seswnwsesewseswseeseseswewnwesenwswse
newneswneneeneenenenenenewne
swwwswswswweeswnwnewseseneseneenesew
wwewwnewswnwwwwnwnwswwsewwse
nenenesenesenewsenenwnwnesenewnwnenenenw
seeeenwesweweeeneneseeneseswsw
nwnwswnwsenewnenwnwnwnwsenwnenwnwnwnwnenw
swneneneneneenesewneeeseswewswnenee
sweewenwneenesenwsweneneeneneswne
sesweseseenwseseseswsenwseneseseseseese
nwsewwenenwseewnwewwsweswewnwse
seswswseseneesewwenwswwswneseeneswswse
sweswsweseswswswsweswswswswswnwswswwnw
newnwseeenwnwenesewwneswswne
nenwnenwwwneenenwnenwnenwswnwneseese
eseenwesesesenwseweseeeeswswnwe
nwnwwnwnwwenwnwnwwnwnwwsw
swwwseswwnwwswswenewwswswwwnwww
wnenwnenenwnwwnenwsenwnwsenwnwnesewsene
swnwwsewswnewwswwwewewwnwwsw
wsweswswneneswseneswswnwswswenwswse
swnwseseenewseswsenw
eesenesweeneesenwswseeseewnweee
eneeeeneeeneneewnesene
seeseneseswwneweseneeeswenwsesenesw
wswseswwswswwwneswneseswneeswwwwwsw
nwesesweewswnwenwnweswnwswsw
nwnwnwnenwnwsenwnesenwnwenwwnwnwwnwnw
swneswenwnwnweneenwnwswnwnenwnwnwnenenwse
nesweswnwswnwneneeneewnwneseneeswnenese
neeenwseneewneswnenwseeeneenenenenew
swnwnwenwnwnweswnwnwnwnwnwnwnenwnwnwnese
nesenewswwneneswnweneenwnenee
swswswswswneswswswswswswswswsw
neneneneeneneewswenenenwewneswenene
weneneseeneneeenenenweeeeneewswe
wwwsenwwsewnwenenenwneswwnewseseesw
newnwnesenesenwneesewnwwnwwnwenenw
seenwnwneeeneeeweenesweeesese
nwnenewnesenwwnwnenwnwnwnwwnwseenwwnwse
ewnenwswneseneeese
wneewseeswneneswsweeeenwnesenwne
swswseswnwsewsewnwswswwswnwswswnesesw
seseseseseeseesesesewsese
nwwswswwswsewwwswsww
swneswseeneswsewswswwswsw
neswwswswnewewnesewwesenesenesww
newwnenwneneseesenenenenenenenweswnw
weswsweswswswwswwneswswswswswswnwwsw
swswswswswneswenwswweswswseswseseswnw
wsewnwsewnwwwsweneww
seeeeeenweesweeseee
sesesesesesesesesewesenwswneneseseswnw
nwnenwnwnwsenwnwnwwnwnweneswwe
wnenwnwsewnwwnewnwsesenwnwsenewnw
seeseneswesewnwe
swwnenwnwenenwseswneswwnesesweeswnwnw
swwswneswswwwswwneseeswwswswswsenwsw
eewwwneeseneeewwewenenewne
swswswwseneeneswwswseswswnewswnewnwsw
newnenenewswenesenenwneneesewneenesw
neeseswnweswswsenwwsenwweesesesesesw
seeseeswenwnweeeeeeeenw
seswswswseswwseenwseseswseseseesesw
wswswswnenwswswseswswseswswseswswnesesww
esesenweeswseeeseenesenwese
eseseenwseseeseseweesewseseesesw
enwnwnwsenwnwswnwnwnwnwnwnwnenwne
esesewneneeseswseewwnweeseeswnwse
wsewwenwswnwwsewwnwwwwswwwew
nwswswswseneswswswneseseeswwwnesenesesw
nwsesenwseeseesesweseseseeseeseeew
enewwwswwewwewwnenwsewwwwse
nwnweneeswewwnwnenwenwwwenenwnw
wwwwwneswswswwwwwewwwswnew
eswseseswnwswsewsw
wsweswwwenwwwsesewweswnwswwnwsw
nwsewwwnwnwsenwnweewwnwwnenewnw
package aoc2020
import aoc2020.lib._
import cats._, cats.implicits.given
import Vectors._
object day24 extends (List[String] => String) {
def apply(input: List[String]): String =
val directions = input.map(parseLine)
val flipped = directions.map(dirsToCoord)
.groupBy(identity)
.filter(_._2.size % 2 == 1)
.map(_._1).toSet
val after100days = (1 to 100).foldLeft(flipped)((tiles, _) => gameOfLife(tiles))
s"""Black tiles on first day: ${flipped.size}
|Black tiles on first day: ${after100days.size}
|""".stripMargin
val dirPattern = raw"(se|sw|nw|ne|e|w)".r
def parseLine(in: String): List[String] =
dirPattern.findAllMatchIn(in).map(_.matched).toList
/* A great article on hexagonal coordinate systems:
* https://www.redblobgames.com/grids/hexagons/
* I use an axial coordinate system here */
val mapVecs = Map(
"se" -> Vec2D( 0, 1),
"sw" -> Vec2D(-1, 1),
"ne" -> Vec2D( 1,-1),
"nw" -> Vec2D( 0,-1),
"e" -> Vec2D( 1, 0),
"w" -> Vec2D(-1, 0),
)
def dirsToCoord(dirs: List[String]): Vec2D[Int] =
dirs.foldLeft(Vec2D(0,0)){(pos, dir) => pos + mapVecs(dir)}
def gameOfLife(blackTiles: Set[Vec2D[Int]], aliveRule: Set[Int] = Set(1,2)): Set[Vec2D[Int]] =
val neighbours = blackTiles.map(tile => tile -> mapVecs.values.map(_ + tile))
val stayingBlack = neighbours.filter(aliveRule contains _._2.count(blackTiles)).map(_._1)
val newBlack = neighbours.flatMap(_._2).filterNot(blackTiles)
.filter(whiteTile => mapVecs.values.map(_ + whiteTile).count(blackTiles) == 2)
stayingBlack ++ newBlack
}
......@@ -26,6 +26,7 @@ import aoc2020.lib._
case 21 => input()(day21)
case 22 => input()(day22)
case 23 => flatInputF(_.map(_.toInt - '0'))(Vector)(day23)
case 24 => input()(day24)
case _ => "No such day implemented"
}
if (sample.nonEmpty) println("SAMPLE VALUES!")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment