ShareMyScreen/AdventOfCode/2023/06/WaitForIt/rdm
[day 6]
Day 6 was, in contrast with day 5, incredibly small and easy. We didn't need an input file.
Probably the best way to represent the day 6 data would have been in a two row matrix. But I went with two global variables defined in a function which takes no arguments:
1 sample=: {{
2 time=: 7 15 30
3 dist=: 9 40 200
4}}
5day6=: {{
6 time=: 48 98 90 83
7 dist=: 390 1103 1112 1360
8}}
9
10poss=: {{ (]*y-])i.1+y }}
11part1=: {{ */dist {{ +/x < poss y }}"0 time }}
12
13unkern=: {{ (<.10^>.10^.y) #. y }}
14part2=: {{ +/ (unkern dist) < poss unkern time }}
(That's it - that's my entire day 6 implementation, including my personal "input data".) And my session contained stuff like:
sample''
9 40 200
dist {{ +/x < poss y }} "0 time
4 8 9
part1''
288
unkern time
71530
unkern dist
940200
part2''
71503
In other words, I am literally trying every possible time-to-accelerate, calculating the distances reached for each, and counting the number which exceeded the corresponding threshold.
(If computation time had been an obstacle, the was another approach. I could have used J's support for polynomials to find the answer. Distance traveled as a function of total time and accelleration time forms a polynomial.
require'plot'
poss 100
0 99 196 291 384 475 564 651 736 819 900 979 1056 1131 1204 1275 1344 1411 1476 1539 1600 1659 1716 1771 1824 1875 1924 1971 2016 2059 2100 2139 2176 2211 2244 2275 2304 2331 2356 2379 2400 2419 2436 2451 2464 2475 2484 2491 2496 2499 2500 2499 2496 2491 2...
plot poss 100
Here, "time" is the x axis and "distance traveled" is the y axis, and it's 0 at x=0 and x=100 (because 100 was the total travel time we allowed in this example -- it's a stand in for a number that we would get from the time
variable). We could move th0 zeros to the points where we exceed the required dist
travel time by subtracting some dist
from this result. That would give us a polynomial with coefficients dist
, -time
and 1
, and the integer component of the x values where y is 0 for that polynomial would be the starting and ending acceleration times for our puzzle - so we want the difference between the two.
In other words:
p.dist,.(-time),.1
┌─┬───────────────┐
│1│5.30278 1.69722│
├─┼───────────────┤
│1│11.5311 3.46887│
├─┼───────────────┤
│1│20 10 │
└─┴───────────────┘
<.;@{:"1 p.dist,.(-time),.1
5 1
11 3
20 10
-/"1<.;@{:"1 p.dist,.(-time),.1
4 8 10
So.... I could have used this approach instead of the "brute force" approach I actually used. But... brute force worked just fine.