Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
David
advent-of-code-2019
Commits
b2832161
Commit
b2832161
authored
Dec 10, 2019
by
David
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Part 2 tests pass, but doesn't get the right answer :(
parent
ef4bc033
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
9 deletions
+82
-9
3/src/main/scala/is/kow/adventofcode/_2019/Main.scala
3/src/main/scala/is/kow/adventofcode/_2019/Main.scala
+2
-0
3/src/main/scala/is/kow/adventofcode/_2019/Puzzle3.scala
3/src/main/scala/is/kow/adventofcode/_2019/Puzzle3.scala
+50
-5
3/src/main/scala/is/kow/adventofcode/_2019/Wires.scala
3/src/main/scala/is/kow/adventofcode/_2019/Wires.scala
+16
-4
3/src/test/scala/is/kow/adventofcode/_2019/Puzzle3Spec.scala
3/src/test/scala/is/kow/adventofcode/_2019/Puzzle3Spec.scala
+14
-0
No files found.
3/src/main/scala/is/kow/adventofcode/_2019/Main.scala
View file @
b2832161
...
...
@@ -4,4 +4,6 @@ object Main extends App {
val
p3
=
new
Puzzle3
println
(
s
"DISTANCE: ${p3.distance("
input
.
txt
")}"
)
println
(
s
"Cheapest path: ${p3.cheapestRoute("
input
.
txt
")}"
)
}
3/src/main/scala/is/kow/adventofcode/_2019/Puzzle3.scala
View file @
b2832161
...
...
@@ -20,9 +20,6 @@ class Puzzle3 {
println
()
val
wire2Path
=
wirePath
(
wire2String
.
split
(
","
).
toList
).
reverse
println
(
"WIRE 1: "
+
wire1Path
)
println
(
"WIRE 2: "
+
wire2Path
)
//Find all the intersections
val
points
:
List
[
Point
]
=
wire1Path
.
iterator
.
flatMap
{
wire1
=>
//See if it intersects with any wire, and get that point
...
...
@@ -34,9 +31,8 @@ class Puzzle3 {
//Don't count 0,0
val
intersections
=
points
.
drop
(
1
)
println
(
"Intersecting points: "
+
intersections
)
val
distances
=
intersections
.
map
(
manhattanDistance
(
Point
(
0
,
0
),
_
))
;
val
distances
=
intersections
.
map
(
manhattanDistance
(
Point
(
0
,
0
),
_
))
val
smallest
=
distances
.
foldLeft
(
distances
.
head
)
{
(
acc
,
v
)
=>
if
(
v
<
acc
)
{
v
...
...
@@ -51,6 +47,55 @@ class Puzzle3 {
}
}
def
cheapestRoute
(
resource
:
String
)
:
Int
=
{
//load from resource
val
source
=
Source
.
fromResource
(
resource
)
try
{
//Two lines
val
lines
=
source
.
getLines
().
toList
val
wire1String
=
lines
.
head
val
wire2String
=
lines
.
tail
.
head
val
wire1Path
=
wirePath
(
wire1String
.
split
(
","
).
toList
).
reverse
val
wire2Path
=
wirePath
(
wire2String
.
split
(
","
).
toList
).
reverse
//Find all the intersections
val
points
:
List
[
Point
]
=
wire1Path
.
iterator
.
flatMap
{
wire1
=>
//See if it intersects with any wire, and get that point
wire2Path
.
map
{
wire2
=>
wire1
.
intersects2
(
wire2
)
}.
filter
(
_
.
isDefined
)
.
map
(
_
.
get
)
}.
toList
//Don't count 0,0
val
intersections
=
points
.
drop
(
1
)
val
distances
=
intersections
.
map
{
point
=>
pointPath
(
wire1Path
,
point
)
+
pointPath
(
wire2Path
,
point
)
}
val
smallest
=
distances
.
foldLeft
(
distances
.
head
)
{
(
acc
,
v
)
=>
if
(
v
<
acc
)
{
v
}
else
{
acc
}
}
smallest
}
finally
{
source
.
close
()
}
}
private
def
pointPath
(
wire
:
List
[
Wire
],
dest
:
Point
)
:
Int
=
{
//Get all points
val
path
=
wire
.
flatMap
(
_
.
points
).
distinct
.
map
(
Point
(
_
)).
takeWhile
(
_
!=
dest
)
path
.
length
}
private
def
manhattanDistance
(
p
:
Point
,
q
:
Point
)
:
Int
=
{
Math
.
abs
(
p
.
x
-
q
.
x
)
+
Math
.
abs
(
p
.
y
-
q
.
y
)
}
...
...
3/src/main/scala/is/kow/adventofcode/_2019/Wires.scala
View file @
b2832161
...
...
@@ -22,17 +22,29 @@ case class Wire(start: Point, end: Point) {
end
.
x
to
start
.
x
}
range
.
map
(
x
=>
val
path
=
range
.
map
(
x
=>
(
x
,
start
.
y
)
).
toList
)
val
items
=
if
(
start
.
x
<
end
.
x
)
{
path
}
else
{
path
.
reverse
}
items
.
toList
}
else
{
val
range
=
if
(
start
.
y
<
end
.
y
)
{
start
.
y
to
end
.
y
}
else
{
end
.
y
to
start
.
y
}
range
.
map
(
y
=>
(
start
.
x
,
y
)).
toList
val
pre
=
range
.
map
(
y
=>
(
start
.
x
,
y
))
val
finished
=
if
(
start
.
y
<
end
.
y
)
{
pre
}
else
{
pre
.
reverse
}
finished
.
toList
}
}
...
...
3/src/test/scala/is/kow/adventofcode/_2019/Puzzle3Spec.scala
View file @
b2832161
...
...
@@ -25,4 +25,18 @@ class Puzzle3Spec extends AnyFunSpec {
}
}
}
describe
(
"Day 3 part 2"
)
{
List
(
(
"test1.txt"
,
30
),
(
"test2.txt"
,
610
),
(
"test3.txt"
,
410
)
).
foreach
{
case
(
resource
,
expected
)
=>
{
it
(
s
"for input ${resource}, expect $expected path"
)
{
assert
(
p3
.
cheapestRoute
(
resource
)
==
expected
)
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment