Skip to content

Commit

Permalink
Day 06
Browse files Browse the repository at this point in the history
  • Loading branch information
mcebular committed Dec 6, 2023
1 parent 81452a5 commit 74821ce
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
70 changes: 70 additions & 0 deletions day06.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import groovy.transform.TupleConstructor;
import groovy.transform.ToString;


def input = new File("input/day06.txt")
.readLines()

def times = input[0].split(/:\W+/)[1].split(/\W+/).collect { it.toLong() };
def distances = input[1].split(/:\W+/)[1].split(/\W+/).collect { it.toLong() };

@TupleConstructor
@ToString
class Race {
long time;
long distance;

long play(long holdingButtonTime) {
long speed = holdingButtonTime;
long timeRemaining = time - holdingButtonTime;
return timeRemaining * speed;
}
}

{
assert new Race(7, 9).play(0) == 0
assert new Race(7, 9).play(1) == 6
assert new Race(7, 9).play(2) == 10
assert new Race(7, 9).play(3) == 12
assert new Race(7, 9).play(4) == 12
assert new Race(7, 9).play(5) == 10
assert new Race(7, 9).play(6) == 6
assert new Race(7, 9).play(7) == 0
}

def races = (0..<times.size()).collect { i ->
new Race(times[i], distances[i]);
}


println races.collect { race ->
def results = (0..<(race.time)).collect { i ->
return [i, race.play(i)]
}.findAll { it[1] > race.distance }

return results.size();
}.inject(1) { acc, it -> acc * it };


def finalRace = new Race(
input[0].split(/:\W+/)[1].split(/\W+/).join("").toLong(),
input[1].split(/:\W+/)[1].split(/\W+/).join("").toLong()
);

def count = 0;
def prev = null;
for (int i = 0; i < finalRace.time; i++) {
def curr = finalRace.play(i);

if (curr > finalRace.distance) {
count += 1;
}

if (prev != null && curr < finalRace.distance && i > finalRace.time / 2) {
break;
}

prev = curr
}

println(count);
2 changes: 2 additions & 0 deletions input/day06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 51 92 68 90
Distance: 222 2031 1126 1225

0 comments on commit 74821ce

Please sign in to comment.