-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.worksheet.sc
84 lines (75 loc) · 2.82 KB
/
Day18.worksheet.sc
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import aoc.*
val input = io.Source.fromResource("2023/day-18.txt").getLines.toList
def parse(instruction: String): (Dir, Int) =
instruction match
case s"$rawDir $n (#$rawHex)" =>
val distance = n.toInt
val dir = rawDir match
case "U" => Dir.N
case "D" => Dir.S
case "R" => Dir.E
case "L" => Dir.W
dir -> distance
def parse2(instruction: String): (Dir, Int) =
instruction match
case s"$rawDir $n (#$rawHex)" =>
val distance = Integer.parseInt(rawHex.take(5), 16)
val dir = rawHex(5) match
case '0' => Dir.E
case '1' => Dir.S
case '2' => Dir.W
case '3' => Dir.N
dir -> distance
val parsed = input.map(parse2)
val maxRight = Integer.parseInt("FFFFF", 16) * input.size / 4
val (p, nsArea) = parsed.foldLeft((Point.origin, 0L)):
case ((pos, area), (newDir, distance)) =>
val newPos = pos.move(newDir, distance)
val line = Line(pos, newPos)
newDir match
case Dir.E | Dir.W => newPos -> area
case Dir.N =>
val a = Area(line.xRange.min to maxRight, line.yRange)
newPos -> (area + a.size[Long])
case Dir.S =>
val a = Area(line.xRange.min + 1 to maxRight, line.yRange)
newPos -> (area - a.size[Long])
val ewPrevNext =
val prev = (parsed.last :: parsed)
val ew = parsed
val next = (parsed.tail :+ parsed.head)
prev.zip(ew).zip(next)
val (pew, ewArea) = ewPrevNext.foldLeft(Point.origin -> 0L):
case ((pos, area), (((prevDir, _), (newDir, distance)), (nextDir, _))) =>
val newPos = pos.move(newDir, distance)
val line = Line(pos, newPos)
println(s"$pos: $prevDir $newDir $distance $nextDir")
newDir match
case Dir.N | Dir.S => newPos -> area
case Dir.E => (prevDir, nextDir) match
case (Dir.N, Dir.N) =>
val a = Area(newPos.x to maxRight, pos.y to pos.y)
newPos -> (area - a.size[Long])
case (Dir.N, Dir.S) =>
newPos -> area
case (Dir.S, Dir.S) =>
val a = Area(pos.e.x to maxRight, pos.y to pos.y)
newPos -> (area + a.size[Long])
case (Dir.S, Dir.N) =>
val a = Area(pos.e.x to newPos.w.x, pos.y to pos.y)
newPos -> (area + a.size[Long])
case Dir.W => (prevDir, nextDir) match
case (Dir.N, Dir.N) =>
val a = Area(pos.x to maxRight, pos.y to pos.y)
println(s"subtracting $a -${a.size[Long]}")
newPos -> (area - a.size[Long])
case (Dir.N, Dir.S) =>
val a = Area(newPos.e.x to pos.w.x, pos.y to pos.y)
println(s"adding $a +${a.size[Long]}")
newPos -> (area + a.size[Long])
case (Dir.S, Dir.S) =>
val a1 = Area(newPos.e.x to maxRight, pos.y to pos.y)
newPos -> (area + a1.size[Long])
case (Dir.S, Dir.N) =>
newPos -> area
val ans2 = nsArea + ewArea