mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Compare commits
8 Commits
fd561a3e9d
...
2021-day23
| Author | SHA1 | Date | |
|---|---|---|---|
| 81f244bde9 | |||
| 09b590e927 | |||
| 9dacb4c1ae | |||
| 07e03c1630 | |||
| 3accf9845d | |||
| fd26f58e25 | |||
| b2f9898714 | |||
| d757c389f0 |
@@ -7,7 +7,7 @@ use criterion::criterion_main;
|
||||
use criterion::BenchmarkId;
|
||||
use criterion::Criterion;
|
||||
|
||||
const DAYS_IMPLEMENTED: usize = 21;
|
||||
const DAYS_IMPLEMENTED: usize = 25;
|
||||
|
||||
fn read_input(day: usize) -> Vec<u8> {
|
||||
let input_path = format!("inputs/{:02}.txt", day);
|
||||
@@ -26,15 +26,18 @@ pub fn benchmark_days(c: &mut Criterion) {
|
||||
let input = read_input(day);
|
||||
|
||||
let part1 = get_implementation(day, false);
|
||||
let part2 = get_implementation(day, true);
|
||||
|
||||
c.bench_with_input(BenchmarkId::new("part1", day), &input, |b, i| {
|
||||
b.iter(|| part1(&mut &i[..]));
|
||||
});
|
||||
|
||||
c.bench_with_input(BenchmarkId::new("part2", day), &input, |b, i| {
|
||||
b.iter(|| part2(&mut &i[..]));
|
||||
});
|
||||
if day < 25 {
|
||||
let part2 = get_implementation(day, true);
|
||||
|
||||
c.bench_with_input(BenchmarkId::new("part2", day), &input, |b, i| {
|
||||
b.iter(|| part2(&mut &i[..]));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
5
2021/inputs/23.txt
Normal file
5
2021/inputs/23.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
#############
|
||||
#...........#
|
||||
###D#B#A#C###
|
||||
#B#D#A#C#
|
||||
#########
|
||||
252
2021/inputs/24.txt
Normal file
252
2021/inputs/24.txt
Normal file
@@ -0,0 +1,252 @@
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 1
|
||||
add x 14
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 1
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 1
|
||||
add x 15
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 7
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 1
|
||||
add x 15
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 13
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 26
|
||||
add x -6
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 10
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 1
|
||||
add x 14
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 0
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 26
|
||||
add x -4
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 13
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 1
|
||||
add x 15
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 11
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 1
|
||||
add x 15
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 6
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 1
|
||||
add x 11
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 1
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 26
|
||||
add x 0
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 7
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 26
|
||||
add x 0
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 11
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 26
|
||||
add x -3
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 14
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 26
|
||||
add x -9
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 4
|
||||
mul y x
|
||||
add z y
|
||||
inp w
|
||||
mul x 0
|
||||
add x z
|
||||
mod x 26
|
||||
div z 26
|
||||
add x -9
|
||||
eql x w
|
||||
eql x 0
|
||||
mul y 0
|
||||
add y 25
|
||||
mul y x
|
||||
add y 1
|
||||
mul z y
|
||||
mul y 0
|
||||
add y w
|
||||
add y 10
|
||||
mul y x
|
||||
add z y
|
||||
137
2021/inputs/25.txt
Normal file
137
2021/inputs/25.txt
Normal file
@@ -0,0 +1,137 @@
|
||||
>.v>>....v...v...v>...>>>...>v.v>.>.v>...>.>.v.>>>>.>.>.v>>v.v.>.>>vvv....>v.>v....v>vv.v.vvvv>>...>v>v...vvv>>>>..>v>...v>v..vv.>.>..v.v>.
|
||||
v.v.>.vv..v.v.>.......>.v.>>......>.v..>..v..>.vvvvv>v.>>vv>...vvvv.>>v>v>>v...v...v>.vv.v>.>.>..>>....v..>..vvv>.>vv>v....>>>v....>.vv...v
|
||||
..v...>vvv>>......>>..v.v..>.v.v>....v...>v>v..>>v>......>..v.>.>.>.v.>v>v..>.vv>.>...v.vv....>>>..>.v.v>..v...v.>v.>..>v>.v..>>v>v..>>>.v.
|
||||
.>vvv.>.>>>.v..v.>.v..vv>>>..v...v...v>v.>........>>>.>.v..vv>...v.v.vv>v>>>..>>>.v.vvv..>v.vv.v>..v>vv..>.>....>v>vv....v>...>v>v.>>>v>.>>
|
||||
v...v.v.v>.vv>>...vv.v.>v>.>..>>>>...>>vv>.v.>.>>vv.v.>>.>v.>vvvv>v.vv>>.>>.v>v..v.>.>...>>>>v.v>..>.>..v..>vv...v>vv...vvv>>.vv.v.v>.v...v
|
||||
>>v...v.v.>v>.>>>....>.>>v.>....>>>>v.....v>..>v.vv>.....>..>..v..v...>v..>v>..vvv>.>..v.vvv...>vvv>.vv>..>.v...v....v.v.v>.v.>>.v.v..>v.>>
|
||||
>v....v>>>........>....>..>.v>>....v..>...vv>v...v>.>>..v...v>>.......v>..>>.v......vv>>v>>>v.>v>.....>..vv..>v.>>..v.v..v..v>.vv>>>v>>v>vv
|
||||
v.>v.......>...vv..v.v>>v.v>....vv.v>..v..>..>v..>v..v..vv.vv.....>>>....>>v>.>....v>.>.v>...v.>v..v.v..>..>.vv...>.>v.....v.vvvvv..>.>v.>>
|
||||
>.>>..>.vvvv.>.vvv.vv.v>v>>vvv.vv......>...v.>.>vv>>..vv>v>..v>v>>..>.v..v.>>...>.v>>v.v.>.v.v>v>....>>...vv.v>..>v>.>>.>........>.....v>>.
|
||||
>.>v.>v..vv..v>v.vv.>v....>>.v>.>.vv...v>v.>.v>>vv.v>>..>>vvvv..vv>>.vv>.>>...>.>>..vvvv>.>>>.v.v.>>>v..>>..>.>>....vvvv.>..v>v.>...v>...vv
|
||||
>..v.>.>...>v>..>.......>vv.v.>>>.v>..>>>.>.>v...v.v>>>>>v>...>v>..>..>.v.>.>.vv>>v..>v>>.vv......vvv....>>>>v>v.....v.>v>.....>>>..>...>..
|
||||
.v..>>v>.>.>...vv.v.>vv...>.v...>>>.>..v...v...>>v>.>...>.>.v>v.v.>..>.>...v.>>.v...v..>.....vv..vvv.v.>..>v..>v....v>v...v.....vv..>..v.v.
|
||||
..>.>v..v.....>.>>v.>.v>.v>>>.>..>vvv.>.>....>v..v.....v>..>..>>..>v>v.>.v..>.vv.>...>.....v.....vv>......>..vv.>.vvv.v.v>v>v.>>.>>>>...v.>
|
||||
v..vv.v>v>>>..>...>...>vv.>.v.>vv.....>v.>>.>...v.>>...>vvv>v.v>v>v.v.>.......vv>.v>>.vv>.>.v...>..vv>v>..v>>>.....>>...vvv..>vv........v>.
|
||||
..v....v.>>>vv>.>.>>...>.>.v.v..vv......v.....>>.....vv>.vv.v>v..>....v.vv...>....v............>>.>>v...v.>.>>v>..>>>.....v.v..>vv>..v>>...
|
||||
.>v.vv.v.vv.>.>..v>.v...v....>>v...>v.....v.....>>.vv>v.vvv.v..vvv.vv>v.>..>>>v..>.vv...v..v>.v>>>..>>vv..>.v>.vv...v.>>......>..>...vvv.vv
|
||||
..v....>v.>>>v..vv>v..>....v.>.>v..>v..v.v>v.v>...>>>.v...vv>v>>>.v....>.vv.>v...v.v.>v.>vv.v.vvv.>v...v...>.v.>..v.v>>.....vv>>.v>v.......
|
||||
....>>vvv........>v.>.v..>.vv.....>..v..>..vv.>>...>.v...>v.>..>.>....>>>>.....v.v.....>v>.vvvv>.>v..>.vv..>vv>...v..v>>...v..>..v.>.v.....
|
||||
>v>v>>.v....>.v>.v>>..>..v>>v...>.>>>v...>>>vv....>..>.v.v.v..v.>...>.v.v.v.v>.>v...v>.v..vv....v.>...>v.v..>v.v..>...........>>>..>.v.vv..
|
||||
..v...>.>>.v>>.v.v>>.>..>>.>.......v>.v>vv..v.>.v...>v>..v...vv.>...v.>.vv.>..>.v>v.v.>..v>..>..vv>v.v.v>...v>v..>v.v.v.vvv..>v>vv>...vv.>>
|
||||
>.v>.v.>.vvv....v.v....v..vv.v.>>...v...>.v.>.v>v.>v.>.>>>..>v.vvv.v>.>v..>>...>v>..vv.....v.v..>..v.v>.v>v.....v.>vvvv.vv.v.>vv.v..v.>.>.v
|
||||
.>v>v>.v>>v..v>>v.v>..>v..>>.v.v..>..>v.vv>..>v>v.>.vv.>>.v....vvv>...v..v>.v.>.v.v..v>.v>v..>.>..v.v..>...vvv.>.vvv...v>v.....>....vv>....
|
||||
.v>.v>.v.vv>..>....>v......vv..>.>v.....>v>>.v>.......>.v.vvvv..>v....>.v..vv.>v.v..>.>.v>v.>...>.>..vv>.>>.....v.>>v....>...>>>v..v>v>.>..
|
||||
v>.v>>vv>>vv........>v.>..>>v..v.........v.v.vvv.>v>vvv>.......>>>....>>>v..>>..>>>.>......>.v>v..v.vv.>...v>>v..>>>..>.>vv...v>.>...vv>.v.
|
||||
.>...v....>.v>..v.>.vv>.>>v...vv.>..>>v...v.>..>>>vv..>.v.>.v.>.vv>.>>>>.vv>.>v..v>...>..v.v.v.v...v...v>>..v>.....v.>..v.v>.v.vv..vv.v...>
|
||||
.>..v>>...>v>.v>.>>..>>.vvvvv.v>v....v>v>....>v..v.>>>..>>vv..v>.v..>v>..>.>.v.>>>v...v...v>.v..v.v.....v>v..>.........>.>vvvvv>>vvvv..>...
|
||||
>v>......v..v..>v.>>.....>>.v.>..>.>>v.v.>..>.>.>.>v.>..>...v>>.....v.>>......>.>.vvv.>..vvvv..>.>....v..>>..v.>.>>..>.v...>..v>v>>.>....>>
|
||||
>v..>>.>v.>>.v>.>.>.>v.vvvv.v...>.....v....>>v.>vv>.>.>..v...>v>v.v.vv.>>.....>>...v.v>>v.v>.v..>..v>v..>..>.v.v.v.>.vv.>>>.v.v..v.>vv.v.>.
|
||||
v.....v....>>v.v.>..>..>>v>vv>.>v>>.>.vv..v..v>.>.v>.>..v>...>.>v>...v..>>vv...>.>..>v.v>...vvv.v>.>...v>v.vvv>v.>...v....>>>v>vvv>>>.v>...
|
||||
>.>>.vv>.v.v>>>.v>>.v....>v>......v...>>..>.v.>..v.>>.v>..v....>.>.>.v....>>>..>..v>.vv>..>v...v..vv.v...vv>.>..v>..>..vvv.....>>....v.>.vv
|
||||
...>.>.v>....>>v....v.v.>>v>v.v..>v.>>>v..>..>..>v.>.....v.v.v.>>...>.vv..>..v.>vvv...v>....>.v>v>>v>v..v..>.v..vv>..>>>.vv>vv.v>>.v.>>...v
|
||||
.>vv.v...>.>.>..>v.v.v>v...v....v>vv>>.>>.....v>>..v.v>.vv>.v>>vv..>v...v..>v>..>.v..>vv>..>>.v.>.vv>vv.........v>.v........v>>v..v>vv>v.>>
|
||||
.....>...vv.>.v.v..>vv.>.>.>>>..v>...>v...>.>>>.v..>>.>...v>>.v.v.v>>v..>.>>v.>vvv.vv>.>v..vv.>>....v...>v.......v.v.>.>.v.>>.....>v.....>.
|
||||
v>.>v.v>>.v.>vv.vv..v.>v.v........>vv.v>.....>v.>.v....v>.>>..v>.vvv...>.vv.>......>v.>.>..>v>.>..v..>..>..>.>..>>>v.>v.v.>..v.v>>>vv.>>>v>
|
||||
v>v....>...>..>v..v>.vv..vvv>>v.>.>>..>..v....vv..>v>v...>>>.......v.......v.v>....v>>>..>v.vv..v>.>>.vv...vvv.>.>>>>.>.>.>.>>....>>.>>..>.
|
||||
.v.>..vv..>.....>v.>v.>>>v..v>.v>....v>>...>..>>>v>.>>>.........>.>.v.>..v>..v>..vv>.>>v.vv....v>>.v>..v.>.v.>>...>...vv.v.v..vv.>>..v>v..v
|
||||
v>v>.>.v..>v>>.>>.>.>.v>>....v>vv>vv>..v.........>.>.>....v>vv>vv>..v.vv.......vv.vv>>v.v.v..v........v...>v>.>.v.vv..>.>>>>>....v.v...>>>.
|
||||
....vv..>..v>>..v.....v.v>v.>...v>>..>.>.v..v>.>>vv...>>..v.v>.>...>..v.....v>>vv>v..>.>..>.>....vv>..>.v...>.v....>...>.>..>.....vv>v.>v.v
|
||||
.>.v>v..>>...v.v...>...v>.v...v>>.>>....v..v.>.....>>...v.vvvv...vvv.>.>v>v.>>vv..>..>>..v>v.v>.>.v.>>..vv.>.>vv>vv.>.>.>..v..>..v.vv......
|
||||
vv.v.>v.v.>v>>.>....>v>>>vvvv..v.>........>vvv..>..v.....>...>.vv>..v.>v>..v.vv.>>>.>v...>.v>.....>.>v..>...>>>.>v.v..>.v>..vv..v>..vvv>..v
|
||||
.vv>>v>......>.>....vv>..>>.>.>v>..v...>v.>vv..>.>>v>...v>..>..>>>..v>v.v>.v..>v..vv>.>.>..>>...vv>..>v>v.>...vv..v...vvvv..vv>.v..>>.vv...
|
||||
.v.v>.vv....>...>..>>..>v>.>...>.>>..>.v..v..v>.>v...>>>.vv.>....v.v.v.vv>>v>v...>..>.v.v.v....vv.>>.>vvv>..>...vv....v>v>..>>>.v.....v.>>.
|
||||
v.>..vv>v>v>>>....>>....>>v..>..v.>.v.>vv.v.>.v>..>.>>>.v..v>>>.....>vv.v.>>vv....>>>.v>...>v.v.v>v.vv.>....v>>v>...>.v>...>v.v.>>>.vv>.>v>
|
||||
v.>...>>.>v.>>.>..v.v>..vvv.v....>>>vv.....v.v.>>v..v..>..vv..v>...>>.v>v>.>..v.>..>v..>.....>>..v>>v>>>v..>v.v>.>...v..>vv>v>....v..>>>>>v
|
||||
....>.>v>.v.v.>.>.vv>v.v>>>.>...v>v.....vv....v..v.v..vv>>>.v.vvv.v.>..v.vv.v...vvv>..v.>.>.v...vv>.v>.v.>..>vv....v>.>..>v.>>>v.>.>....>.v
|
||||
>>.>>>.>.>vv>..v.>>...v.>....v...vv..v>v>v...v>>v...v.v.>.....>.>>>.>v.v.>vv...v.>......>..>v.>vv.v>v.>...v>>..v.>v>.>.v>v..v.v>.vv.>....v>
|
||||
v..>...v.v....>.>.vvv>.......v>.>v.....v>.>>vvvvvv.v..v..>vv..v.>v..v...>.vv.>>.v...v.vv>..v>..>...>vv>>>...>>.v>..vv..>.....v>v>...>vv.v>.
|
||||
.vv.>v.vv.v.>.>...>v>..>>..>v.v.>>...>v.>>vv>vv..v..>>..>>.v.>>vv>v.>.vvvvv.v..>>>.vv..>>>>>..vv>...vvv....v>vv.>v..vv.....v..>>...>>>>..>.
|
||||
...v>.v..v.>.>.>.v>>.....v.v>.v.>v.v.>.>>>.v>.v>>.vv.>..v.v...vv.>...>.>v..>v....v.v>.>.v..vvv.v.....v.vv..>..v.>..>..v..v>.vvv>>.>..>.>>>.
|
||||
...v.>v.....>vv>v.v>...>vv>>..>.v.>..v>.v.v....v.>.v>.vv>.>..>..>>.v>..>v..>.v>vv.vvv>......>...>.>v>>>....>.v.v.v.v..>.v.>>v>.>v>.>..>v..v
|
||||
v>.v.vvv.v..>.....vv.v>..>>.....v..>.>...>>..v.v>>.>..v....>v>.>v..>.>....>>...>...vv>v.>>vv..>....>v>>v..v>...v.....>>.....v..vv.v.>>...>v
|
||||
v.vvv.>>.vv....>..>v...>>.v>....>v...>.vvv.v......vvv.>vv..vvv...>..v.>.v..vv.v>....v....>.v.v..v>.>...v>v>>>.vvvv..v.vv..>vv>.>v...v.v>..>
|
||||
vv.>....v...vv>.v...vvv.>>v.v>.....>...v>>.>>vv..v.....>vv.vvv>>.v.>>.v..v>>..v>.v.>v>.>.v.v.v..v>>>vv.......>>...v>vvvv>v>.>>v.v>.>>v.>..v
|
||||
v...>.vv..>v..>...>....vv....>.v.>>v>.......v...>>.....v....vv..>v.>.>..>...>>...v.v>v.>.>.....>..>.>.v......>.v.v.vv.>.vv.vv..vvv>vvvvvv>v
|
||||
vv.v>.>v>v..v.v...v.vvv..>v>.v>.v..v>v>>v>...>v.vv..v....v..>..v...vv>>.v..>>.>>v>..>..v.v..>v.v...>>.v>>.v>..v.....v.v.>>>.vvv.>...v.v...>
|
||||
v...v.>v.....>..>v>..>.>>>.v..>.>..>>>>>>v>vvv.>>vv.>>.>..>>v.>.v......v.>.v.v>v...vvvvv>...>.>.vvvv.....v>.>.>.>vv...>..vv....>v>>..>.v>..
|
||||
vv>..v...>.>...v..v.v....>..v>>...>.v>v.>...vv....vv.>...>.>..>>...v>..v>...>...>v..>>...vv.vv.>v...v.>>......>.>.>v>v...v.>...>.v.>.v.>>>.
|
||||
>...>.>.>>..v.....vv.v...>.>>..vvv.v.>>vv>...v.>...>.>.>v...v>.....>v>v>v....v..>>....vv..>v>v...>>v.>vv.>v.>..>....>...v..>.v....v.v..>v>.
|
||||
vv...>v.v>v.v..>>.>>>vv....v.>...>v.v.>>>v...vv..v.v..v.....>v..>>.>..>.v>.>>v..>vv..vvv>v.vvv..vv.v>....>>>>>.v>....>>>..v.>.>.v>......>>v
|
||||
>>.vv>>.>>...vvv.>.>.>>.v>.vv>..>..v.v>.v........v.v>vv.....v.v>v.v.v.v..>>..>v.>.vvv..>>..vv..>...v.v>>.v>....>>>>v.>v>vvv........v.....>.
|
||||
..>..v..v>>vv.>.....v..v.v..vvv.vv..>vv>..vv.v.>v>.>>>v>.v...v.v.v.v..>.v...>.v.vv>.vvv.vv>.v....>....>..>.vv>>v..v.v.>>....v.v>.v..v..v>>.
|
||||
.v>.v>>.....v.>.>v>.>..>>.vvv....>..>>>..v.v.>.>...>v.v>.vvv.>>.vv>v.>.v.v.>...v>vvv.>.v..>v..v>.>>.>>....>v...>..v>.>..v>..v...>v..v.>v>>v
|
||||
v.>.vv.v>...>v..v.>.vvvvv.v..>...>...vv>>..>>>vv.>>v>>>.>.>....vvv.>vv>..vv..>.>>.>>v>.vv..>>.....>.>>..>..>v..>..>>.v>>...vv>>.>..vvv...v.
|
||||
>>..>.v......v....>>.>...>>.>>..>vv.v>>.>.vv.>v.>v.>v..v>..v.v>.v.>v.>>>...>.v.>v..v..>>.v.v.vv>..>>v.v.v>...v>>v>>..>...v.>.....v>v>.>v.>>
|
||||
.v.v>>>>>vv>>.>.>>>.vv..v.>...v.>..>>v.v.vvv...>.>..v.>>.>>.vv..>>>>.vv...>.v.vv.v..v>v>>>.v>vv.v>..vv..>v..>v.>>..>v..v>.>..>.>...v>.v..vv
|
||||
.>.v...vvvv..v...>..>....v.>...v>>v.>v..v>v>..>>>v>...>..>vvv.v.>.v...>v>.v>>>>v...v.>v>....v.v>vv>.vv>.>>v..>...>vv...>.>v.>.vv.>...>v...v
|
||||
>.v.>.>...v>.v..>.v>>.>...v.>>..v.v>.v>.>vvv......>..>.>v>....>>.>v.v...v...>>>>.>..>...>.>v>.v..>...v..>>.v..v>.v>v...>v>>..>..v.>.>.v.v..
|
||||
.v.>>..>.>.v.>v..vv>>..>.>.v.>.>.v..vv..>...v..v.v>...>v..vv>.>vv>.vv>>.>>...v....>..>vv>>>.>.v>..v..vv>v.....>..>>......>.>>v>>.>.>v...vv.
|
||||
....v.>.>>>v.>....>>>v..v.v..vvv>.>v.>v..v..v.>>.....v.v..vv...>v.v...>>.>.v..>.v....v.>v.......vv....>.>v>vv>>..>v.....v>...>..>>>..v....v
|
||||
..>>>.....v>.v>v.vv>>.vv...>>.....v.>..v>v.>v>>.v..vv......>v.v.v.vv>>..v>v.v>..v.>.vvv.>.>>>...>....vv.>v>.....>.v.>..v..vv.>.>...>v>v.>>>
|
||||
...vvv......v.....>>>.....>.>..>.v.>.......>.>.v>v...>.vv>.>...v>.>>.>.>.>>.>vvv...v.v>>>>..v>..>>v..v.v.v..>.vv..v>v..v.v.v......>..vv.v>.
|
||||
v.v.v.v..v..vvv......>vv>>v..>>>>>v>v>.>.......>.>.>..>>>...>vv>.>.>...>..v>>>>.>.>v.>vvv...>.>..v.>.v...>.v>>vvv>vvv....v>>.v>.v>.v>....>.
|
||||
>>vvv.>>..>v.v...v.>v....v...v>>.vv>.vv..vv.v.v>vv.v.........>..v.vv>v>>>v....>...>...>vv.>.>.v>v..>>..>v>.vv..>v.>>>>>...v>...>vv>.v>.v..>
|
||||
>....>.v..>.>.>..>..vv...>>v>vv.>>.v>>>.v....>>>.v.>.v>.>>.vvv..v>>v.....>..v.v..>.vv.>....>>>v..>.v.>>v.....v..>>...v>.>>...>.v...>.>.v>>.
|
||||
v>.v>..>v....v.>>>v.v.v.....v>....vv...>v..vvv..>..>vv>vvv...v>>.vvv..vv>>v>.v...>.v......v>>...v.v....>..v..v>.v>...v.>.>...v.v.vvv..>>..>
|
||||
>v.>.......v>....v.v.>vv>..v>>.v.v.vv.....v........>.v...>.>.>>>v>....>vv>...>v.vv..>.>>v.>.v.>..>..v..>......>>>v>vv....v....>.v>.v.vv.>v>
|
||||
.v>..>..>.>...v.>.v>v..>.>.>.v.>>vvv..>.....v>.vv>v.v.v..>.v..........>v...>.>>.v>>vv.>...>v>..v>v>.>>>.>v.>.>...v.v.>v..>..v.v>..v.>>.vv.>
|
||||
..>.>v.vvv..v.v......>>>>>...>>v>....>v.>vv.........>>vv.vv.>v>...v>.>...vv...>v.>.>vv>....v...>>v>>..v>.>v.>vvv.>>.>v>..v.v>>....vv.v>>v>>
|
||||
>..>>v...v..>v.>..vv.>...v.>>.v.vvv.v..>..>>.v.>....>.>...vv.v..>>.v.>v....v...>..>.v.v>vv...vvvv>>v..>......>..v...>..v..>>........v...v..
|
||||
>v..v.>>......v.vvv.........>>..vv>>vv.>..>v....v.....>..vv..>.v.vv..>>.vv..>>.v.v..>..vvv>>....>...v.v..>v.>>.v.>>>>......v..>....>.v>>v.v
|
||||
.v.>>..>v>>.>...>>..>v.v.v>>v.>..>v..v>>.....>.vvvv>.>....>>.vv..>.>v.>.>>.>>>>..>.>vv>.v>..>.>vv>vvv..v>...v>.v>>.>..>>..>vv>v>.....v>.>v.
|
||||
>v.v..vv.v......vv..v>>....>>>.v..v>v.v>...v..v.v>.>.>..>v>>v>>>..>>v.>...v>>...>.vv.v.v..>..>.v>...>..v.>v...>.v.v>.>>vv>.vv..>v..>>v.vv..
|
||||
...>v..v.vvvv>..>v..v>.vv....v..v..v>vv.>.>v>.v..>.vv>>vv.v.....v.....>.vvv.vv>.>...>v...v>>.vvvvvv.v>......vv.......v..v...>.vvv.vv..>>.vv
|
||||
.vv.>v...v>>.v...v>v.v.vvv.>>v.v.v.....v..v>>.vvv...>v....>vv..>..>.>vv..v..vv.>vvvv.>>>v.v.vv>..>...v>>.v.v>.>>...>...>v>>vv...>v.>.vv>...
|
||||
>>v..v>>>v>v>.v.....>...>v.>.v.v.v..>.>.>...>.vv..>..>>...>v>.v.v.>.....>>>v..v.v..>v.v.v>v...v.>...>v>>.>...v>...>.>.>...v.v.v.....vv.>>>v
|
||||
.v.......v.>v>>.>>v..v.v..v>..>.>>..v....>>.v>.>..vv>>v..>v..v...>...>.....vv.v>...>.>v..>.v.>...vv....v...v..v.vvv.....v...vv...>>>.>v>.>.
|
||||
>>v.....vv.v>.v>.v.>>.vv>.v...v>.v>>v..>>v.>.>>v....>..>v>>>.>..v.v>..>>v>>.....v>.>v....vv.v.v.v.vv.>>>...v.vv>.v>...v>vv>vv..v.>..v....v.
|
||||
..>>...>......>....>v.v.v>.>...>>vv.v>>>.v.v>.>>>vv.v.>..>...>...v.....v....>..vv>>...vv..v..v>.v>.v.v..v...vvv..........>.>v.>.v.>v>v.>v>>
|
||||
..v..>>>>...vvv.v....>..>>...v>.>v.>v...v.>.>.v.v>.v.......>.>v.>.v..vv.....v.v.>.v..v>>...vvvvvvv...v....v..vv..>>..>>>..v.>vv>..v.>>>vv.v
|
||||
vv>.>.vv.v.v.....vv>>v..>v..vv.>>.>.>.v..v>v...>...v.v>>..>.v......v.>>.v.....v......>......>v.>v.v>>>v..>.v>..>.>v.v>..>v.v..v.>.>.>vvv..>
|
||||
>.v.......v>..>.vv>v....>v..>v..vv.v>>v>..>.>>..>..>..v.>v.....>.>>v....v>..v>...>v..>vvv.....vv>v>.v>vv.>v>>.vv>>.v.>.>...vv.v>vv..>.v...>
|
||||
vvv.>.>.>...v.v.vvvvv>.vv>.>...v.>.v..>.>...>v..v...>>vvvv.v>.v.>.vvv...vv.vv.>>.>.>.....vv..vv..vv>>>>.v>>v.>v.v.>>.v..>>v.v>..>>..>v>....
|
||||
v..>...vv...v>......v>...>.....>.>.v>.>vv>v>......>>.v..>.v.>..>>..vv.>v>.v..vv.v..v.>..>v....>..>.>.v.>>vvv..>v.v>v>v..>>.v.>v.v.>>.>>>v..
|
||||
v.........>>v>vv..>.>..>>>.v>...>v...>>.vv.v>...vv...v>>..v..>>>>.>v...vv.>vv>..v.v>...>v.....>.v>>..v.>v>.vvv.>.>v..v.>.v....>>>>.v...v.v>
|
||||
>>.>.>>.vv.>v.v.v.v>v....vvvvv..v.v>>>..>>.>..v>vvv>v.>v>>v>>..>>>>.v.v>.>.>.>>v.v.>v..v.v>>..vv....>....>>.>.vvv>.vv.....v>v.>>vv>v.v.v..>
|
||||
....vv....>....v>v>>...vv>>>.>..>.vv>v>.>>.v>vv..v...>>v..vv..>.vv.v>>>.>.v.v>.>.v>>...v....>.........>.....v>...>.>....vv..v>.v.>v...v....
|
||||
.>v.>.v>>v...>...>.....>.v>..vv>v>.vv>..>.v>.>...v.v.v.v.>v..vv...>...>vv.....>...v>v.>.>..>..v..v.v>.v..v>v.>v..>>...v....v.v>..vv.v>...>v
|
||||
>vvv.v>..vvv>.v>..v>.vv.v>>>vv......vvvvv>>.>v>..>vvv.v.vv>vvv....vvvv.v.>>.v>>..v>..v>v>.>vv.>...v.>>>v.>.>..>..v>>..>..>.v>>.>..>..v.v...
|
||||
.>v>v....>.>.v.....>v.v>.v..v>vv.vvv>.>..>vv..>..>v.>>...>.v>.v..>v..>.>...>..>...vv...>.>>v.>.vv>>....v.>.>.>vv>...v..>.v>v>v>...>v>....>v
|
||||
.>.>...vv.>>>...vv>.v..>>>..>.vv>v..>v.v....>>v>.v.vvv...v>...v..v>.>.>>>>>vv..v.>..v>..>vv>.v>>...v>>.>v.>.vv>.v..v..v...v>>>.v.>.>..>v>.>
|
||||
.>>v.vv.>>>>.>vvvv>>.>vv.>.vvv..>....v.>..>.>.v............>>...>>>>>.>.>>>...v..v.>.>.>..v..>.v>>.>v.>..v.>v.>.....>..>>>>v.v..>v>.>.>..>.
|
||||
.>v>.v..vv.>..v.v.>vvvv..>vv.v.>.v>vv>....v.v...>>..v>.>.>..v..v>.>>>v.>>>v>>...v..vv.vv.vvv...>..>..v..>>...>v.....v>vv....vv....vvv..>>..
|
||||
>v.v.vvvv.v.v....v.>>v....>>.>..>...>.>v>.....>v>v...>>v>...>.>..v.>v>>>..v..>.vv.>>>vv.vv.>v>.....>v.>>.v>.v>vvv.v...v....v...v....>v...v.
|
||||
v>>>v>>>.vv>vvv...v>v..>v>.v>..>.v..v>v....>.>>.v.....v.>>....vv.....v>v.>.>v...>>..>>>.>v.v.vv.>>v..v>vv.vv..>..>v.....v>>v.>.>>...vv.v...
|
||||
>vv.>.v>.>>....>v.>.>v>.>..>.v.vv.>>>>..v>v>vv>>v....>v>>.vv..>..vv>>v....v.v.>>>>v.....vv.v>>.v>....v..>..>>>v..>.>.>>v>.vv....vv>v>.....v
|
||||
..v>.v..>>vv>......>>.>...>..vv..v.v>>.>..>>..v>.....v>..v>>v.v.v.>>v..>v.>>.>vv....v>v>...vv>>>.v>v.v>.>..>v..v>.....v.>>.>v..>v>vv>>>..vv
|
||||
...>>>>>.v.v>..v.....vvv..>.>...vv..v...>.....>v.v..v..v.>v.vvvvv>v..vv.....v>.v.>.....>...>..>...v.>>v>..>.>vv.>>v.v.vv.v.v>..>.....>v>>v.
|
||||
vvv.>...>v>>>v...v.v.v..v...v.>>..vv.....>vv.>..>...>>....>>....>vv.v.>v...vv.vv>.>v.>v.>.>>...v>.....vv..vv.v>vv..>v>>>.>..>>>.vvv...>....
|
||||
..v>.v..>..>....v...>....v>v>v.vv..>>>.v.>.v..>..>>>..vv>>>.v>v.v>...>.v.>>.>.>...>.......v.v>>>.>...vv.v.>>.vv>v>v>..v.>v..v.>..>v.>..>v.>
|
||||
..>v.v...v>...>..v>...>>v>>>>>>.v.>.>.....>.v..v>v...vv.....>v.v.vv..>.v>v.>.>>>>>v.>>.v.v...v>v>>.v>..>vv>.>.vvv>v..>.v.>v>v>>..v>........
|
||||
...v..v>..vvv>...v>...vv....>..>...>...>v.>>vv.>v.>v...>vv>>v.>v......>...>.>..v..v.>v>>.>v>>>...vvv.vv.>..>..v..>>>vv..>...vv.vv>>...>..>.
|
||||
>.v...>>>>>>vvv..vvvvvv.vv..>.v>.v.v..vvv>v..>v>.>vvvvv>>..>.vv...>.v....>>>...v.vv..>......v..>..v.v....v....v.....>.v...v.vv>>..>..v..v.v
|
||||
...v>v..>.>..v...>v>v....>vvv>>>.v>>...>....vv>v...>v..>.v.>....v..>v..>.>v>.v.>..vv..v.....v..v..>....>.>.>v.>vv.vv.vv.>>v...vv.....v.>>v>
|
||||
v..vv..>...>.vv..v.v.....vv..>v......>v...v>v..v>.v>...>v>vvv>...v>v...>v.>>v......v>.>.........vv>....>vv>>v..v.v>..>vvv..>vvv..>.v.vv...v
|
||||
v.>v.>..vv>..>.....>>>....v.vv.v...vv.v.vvv..>....>>v>v>.>.v>>>.>.>.>...v.v...v.>v..v.>v>.vv.>>>.v.vv..>v>.v>..>vv>>.v>.>v..v..v>v>.>.>vvvv
|
||||
..>..>>......>.v>>>>.>v>..>.v....>...>>.v.v.vv....>.v>.>v.>>.>>v>.>..>vvvvv.v.>.v.v>.v>v.v>..>v..v.v.>v>>.>v>>.>.v.v..>.v.v>..>v...>.v>>.>>
|
||||
v....>.v>..v.>..v...>...>>v>.....v.....>..vv>v...v..vvv>.v..vv>v>v..>.>....>....v..v>>......v.>.vv>..>v.>>..v.....>.v>>.v...v>..>.>.v.v.>>v
|
||||
..v>.v....>v.v.>..>v.>.v....>.>v..v>...>v.v.....>..vv...v>....v.v..v>v.>.v>>>v>..>v.>...>.>...v..>>>v>v>....v>>v..v..>>....v>>.vv>.vv.>.v..
|
||||
>v..>...>..>>.vv.>v.>.....v>v.vv>vvv.v>>.>..v.>>..v..>.....>>.>vv.v.>>>v.>vv...v.>v>..>..>..v.vv...v...v>>>vv>.v.v..>>>..>.>v>...vv>.v..v>.
|
||||
....>vv.>vv.v>.v....>>v..v>.v.>>v>vv>..v.>>.>v>v.v..vv.v.>.v..>.......>.>v>.v>vvv>v.>.>..>>v>..vv>.vvv>.>>..v>vv.>>..>v>v>.v.>>........>>v.
|
||||
>.>vvv.>>.>..vvv.v...vvv..>>v.>>.v>.v..>v.v>..v.>...>..vv>v........>v..v>.v...v.v..vv.v...v.v>..>v.....vv...v.vvvv.>.v...>>......>>.>>...>v
|
||||
...>>...>>>v..vv...v>.>.v....v>v...>.>>>v.>>.............vv.>..v>.>v.>v.>..>.vv..>>.v..>>>>....v..>>v...v.v.>.>v>>>..>......>vvv>vvv>>.>>vv
|
||||
...v...v.v>>.>.>>>..v.v.>..>>.>.>.v>....>v...>>.v>>vv.....>>.>...v......v...>v.vv>>vv>....>.>>vv>v.>>>.>>vv..>.v..v>v...v.v..v...v..>v...>>
|
||||
..v..v>vvvv..>>..v>>.>>....v>>v...>>>..v.....v....>.>v.>>v>v...vv.....>>.>v..v.>..>v...>>.>.>>...>>...>>.>>...v>..v...>v...>v>>>..v.v.vvv..
|
||||
...v..v>>>v..>>>v..v>.>vv>.v.>.v...>..>vv>.v>>v>..v>>......>.v>>...v>v..>v.>...>......v.vv..v.>v...vvv>.>..v..v..v>v..>..>........>>...v...
|
||||
.>.v>...vv..v.v>>>v..>v>..v..>>v....v..v>...>v..>.v.>....>v>v.>>v.>.v....v.>.v.>>v>>.v>>..v.v..>v.>..vv>.>>>>>v.>.v>v.>...v.vvv...v.>v..>>>
|
||||
.v>.>>..>v.v>>..>>...>v..v..>..v.>>>.>.vv...v....v...>vv>v>..v..vv>......v>v.>>>.v...v.>>.>.>v..>....vv.vvv..>v..vvv>>.v.v>....vvv>vv.v.v..
|
||||
>..>v.>.>..>v.v.vv.>>.>....v.>v..>v.>>.vv..>.>>.>.v.>>..vvv>>.v...v.>.v.v>...>>...>...vvv>>vvvv.>v.>v..v..v.v..v..v...>.>...>>v.....>v..>v.
|
||||
..>....>...>>.>....vv.v.v>.......>v...v..>..>>......>>.v...>.>.>...>>.v.v.v..v>.>>>v...>>>.v..>....v..>v.>.vv......v..v..>.>>.>..vv.>.v..>.
|
||||
v>..v.v>.>...vv.v.>>..v>.vv.>v.>.>..v>>vv>>.v.>.>>.vv.>.>..v....v>>v....>.>.v.>.>v>v.>.v>v.>..>....v...v.v>v.>.>v.....>>>>v.v.>..v......v>.
|
||||
..v...>v>.>...>.....vv.vv>>.>..>.v.v>...v....>..v.>v.....>......v>....vv.v.v.>...v..v.v..v.>v.v.>vvv..v....>..>..v.>v.v.vvv.v.>>>....>v....
|
||||
>>..vv...>>.vv>vv>.>>...>...>.>v>>..vv.>.>.v.v.>>...>..>>v>v>...>.v>....>.>.....v..>>vv..>.>...v>>v......v.>v......>>>..>>.>.>.v>>v...>v...
|
||||
>>>..vv>v....v>..>......v..>vv.>>v>..>.>..vv.vv.v.>>..v...>.v.v..>>.>..v..vv.v.>>..>>.......>.>>v.v>v..>v>v>>>...vv..>v>..vvvv.>v>v.v>.>>v.
|
||||
.>....v.>>.v.v..>vv.>v..>v...>.vv.v.>vv.v...v...>v..v..v..v...>...v.>>>.>>.....>.>....>v>vvv..>.vv.vv.v...>v.>.v.>.>...>v..vv>>>.>...vvv>.>
|
||||
v.....>...v.>....>v..>v.>.vv.v>>v..>....>vvv.>>.>v.v.v.v..v...>v.>v...v>v...v.v>v...v.>v..v.vv..v>v..>..v.>...>..>....>..v.vv>>.v...v>.vv.v
|
||||
....>>.>.vv>>..v>>vvv.......>v...>>.vv>..>.vv>v>.v>v..>v...vvv..>>v>.>.v.v..>...vv...v..>..vv..vv...>..v..>v>...>.v>.v....v..vvv.>v.>v>vv..
|
||||
>v..v.....vv>v>..>>..v...>v>..>...v>>>>....v..vvvv>.........>..v.v>.v...v.v....v.>v.v>.>v..v.vvv>.v>.>..>>..>>>..v.>>>v.>..>.>v..>vvv.>v>..
|
||||
@@ -79,7 +79,7 @@ pub fn part2(input: &mut dyn Read) -> String {
|
||||
let mut grid = read_grid(input, &mut buffer);
|
||||
let mut todo = Vec::new();
|
||||
|
||||
let target = grid.iter().map(|line| line.len()).sum();
|
||||
let target: usize = grid.iter().map(|line| line.len()).sum();
|
||||
|
||||
(1..)
|
||||
.find(|_| advance(&mut grid, &mut todo) == target)
|
||||
|
||||
@@ -5,23 +5,19 @@ use crate::common::LineIter;
|
||||
fn read_input(input: &mut dyn Read) -> (i32, i32) {
|
||||
let mut reader = LineIter::new(input);
|
||||
|
||||
let a = reader
|
||||
.next()
|
||||
.unwrap()
|
||||
.split(' ')
|
||||
.last()
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
let mut helper = || {
|
||||
reader
|
||||
.next()
|
||||
.unwrap()
|
||||
.split(' ')
|
||||
.last()
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
let b = reader
|
||||
.next()
|
||||
.unwrap()
|
||||
.split(' ')
|
||||
.last()
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
let a = helper();
|
||||
let b = helper();
|
||||
|
||||
(a, b)
|
||||
}
|
||||
@@ -46,7 +42,7 @@ fn find_repetition(mut pos: i32, mut die: i32) -> i32 {
|
||||
pub fn part1(input: &mut dyn Read) -> String {
|
||||
const TARGET_SCORE: i32 = 1000;
|
||||
|
||||
let (a, b) = read_input(input);
|
||||
let (mut a, mut b) = read_input(input);
|
||||
|
||||
let a10 = find_repetition(a, 1);
|
||||
let b10 = find_repetition(b, 4);
|
||||
@@ -54,22 +50,21 @@ pub fn part1(input: &mut dyn Read) -> String {
|
||||
let a_win = TARGET_SCORE / a10;
|
||||
let b_win = TARGET_SCORE / b10;
|
||||
|
||||
let mut rolls = 3 * 20 * a_win.min(b_win);
|
||||
let mut a_score = rolls / 3 / 20 * a10;
|
||||
let mut b_score = rolls / 3 / 20 * b10;
|
||||
let mut a_pos = a;
|
||||
let mut b_pos = b;
|
||||
let win = a_win.min(b_win);
|
||||
let mut a_score = win * a10;
|
||||
let mut b_score = win * b10;
|
||||
let mut die = 1;
|
||||
let mut rolls = 3 * 20 * win;
|
||||
|
||||
let (loser_score, rolls) = if a_win < b_win {
|
||||
while a_score < TARGET_SCORE {
|
||||
a_pos = simulate(die, a_pos);
|
||||
a_score += a_pos;
|
||||
a = simulate(die, a);
|
||||
a_score += a;
|
||||
rolls += 3;
|
||||
|
||||
if a_score < TARGET_SCORE {
|
||||
b_pos = simulate(die + 3, b_pos);
|
||||
b_score += b_pos;
|
||||
b = simulate(die + 3, b);
|
||||
b_score += b;
|
||||
rolls += 3;
|
||||
}
|
||||
|
||||
@@ -79,11 +74,11 @@ pub fn part1(input: &mut dyn Read) -> String {
|
||||
(b_score, rolls)
|
||||
} else {
|
||||
while b_score < TARGET_SCORE {
|
||||
a_pos = simulate(die, a_pos);
|
||||
a_score += a_pos;
|
||||
a = simulate(die, a);
|
||||
a_score += a;
|
||||
|
||||
b_pos = simulate(die + 3, b_pos);
|
||||
b_score += b_pos;
|
||||
b = simulate(die + 3, b);
|
||||
b_score += b;
|
||||
|
||||
rolls += 6;
|
||||
|
||||
@@ -96,39 +91,50 @@ pub fn part1(input: &mut dyn Read) -> String {
|
||||
(loser_score * rolls).to_string()
|
||||
}
|
||||
|
||||
const MAX_TURNS: usize = 21; // Trivial upper bound, could be lower
|
||||
const MAX_TURNS: usize = 13;
|
||||
const ROLLS: [i32; 7] = [3, 4, 5, 6, 7, 8, 9];
|
||||
const FREQS: [u64; 7] = [1, 3, 6, 7, 6, 3, 1];
|
||||
|
||||
fn multiverse_recursive(
|
||||
remaining: &mut [u64],
|
||||
wins: &mut [u64],
|
||||
turn: usize,
|
||||
alive: u64,
|
||||
pos: i32,
|
||||
score: i32,
|
||||
) {
|
||||
for (roll, freq) in ROLLS.into_iter().zip(FREQS) {
|
||||
let new_pos = (pos + roll - 1) % 10 + 1;
|
||||
let new_score = score + new_pos;
|
||||
let new_alive = alive * freq;
|
||||
|
||||
if new_score >= 21 {
|
||||
wins[turn] += new_alive;
|
||||
} else {
|
||||
remaining[turn] += new_alive;
|
||||
multiverse_recursive(remaining, wins, turn + 1, new_alive, new_pos, new_score);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn multiverse(pos: i32) -> ([u64; MAX_TURNS], [u64; MAX_TURNS]) {
|
||||
type State = [[u64; 10]; 21];
|
||||
|
||||
// Let's work with pos - 1 as pos for now, indexes nicer;
|
||||
let pos = pos as usize - 1;
|
||||
|
||||
let mut alive = [0; MAX_TURNS];
|
||||
let mut wins = [0; MAX_TURNS];
|
||||
|
||||
alive[0] = 1;
|
||||
|
||||
multiverse_recursive(&mut alive, &mut wins, 1, 1, pos, 0);
|
||||
let mut current = [[0u64; 10]; 21];
|
||||
current[0][pos] = 1;
|
||||
let mut next = [[0u64; 10]; 21];
|
||||
|
||||
let mut helper = |turn, current: &State, next: &mut State| {
|
||||
next.iter_mut().flatten().for_each(|v| *v = 0);
|
||||
|
||||
for (score, score_row) in current.iter().enumerate() {
|
||||
for (pos, &pop) in score_row.iter().enumerate() {
|
||||
for (roll, freq) in ROLLS.into_iter().zip(FREQS) {
|
||||
let new_pos = (pos + (roll as usize)) % 10;
|
||||
let new_score = score + new_pos + 1; // +1 because above
|
||||
let new_pop = freq * pop;
|
||||
|
||||
if new_score >= next.len() {
|
||||
wins[turn] += new_pop;
|
||||
} else {
|
||||
alive[turn] += new_pop;
|
||||
next[new_score][new_pos] += new_pop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for turn in (1..MAX_TURNS).step_by(2) {
|
||||
helper(turn, ¤t, &mut next);
|
||||
helper(turn + 1, &next, &mut current);
|
||||
}
|
||||
|
||||
(wins, alive)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use std::io::Read;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag;
|
||||
@@ -13,78 +12,147 @@ use nom::IResult;
|
||||
|
||||
use crate::common::read_input;
|
||||
|
||||
type CRange = RangeInclusive<i32>;
|
||||
type Point3 = [i32; 3];
|
||||
|
||||
fn parse_range(input: &[u8]) -> IResult<&[u8], CRange> {
|
||||
use nom::character::complete::i32;
|
||||
|
||||
map(separated_pair(i32, tag(".."), i32), |(first, last)| {
|
||||
first..=last
|
||||
})(input)
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
struct Cuboid {
|
||||
lower: Point3,
|
||||
upper: Point3,
|
||||
}
|
||||
|
||||
fn parse_input(input: &[u8]) -> IResult<&[u8], Vec<(bool, [CRange; 3])>> {
|
||||
let parse_state = alt((map(tag("on x="), |_| true), map(tag("off x="), |_| false)));
|
||||
let parse_line = map(
|
||||
impl Cuboid {
|
||||
pub fn new(lower: Point3, upper: Point3) -> Self {
|
||||
// The input uses an inclusive range for representation, but an exclusive one simplifies a
|
||||
// lot of computations, so we convert here.
|
||||
Self::new_internal(lower, upper.map(|c| c + 1))
|
||||
}
|
||||
|
||||
fn new_internal(lower: Point3, upper: Point3) -> Self {
|
||||
debug_assert!(lower.iter().zip(&upper).all(|(a, b)| a < b));
|
||||
Self { lower, upper }
|
||||
}
|
||||
|
||||
pub fn is_small(&self) -> bool {
|
||||
self.lower
|
||||
.iter()
|
||||
.chain(&self.upper.map(|c| c - 1)) // begrudgingly convert back
|
||||
.all(|c| c.abs() <= 50)
|
||||
}
|
||||
|
||||
pub fn volume(&self) -> i64 {
|
||||
self.lower
|
||||
.iter()
|
||||
.zip(&self.upper)
|
||||
.map(|(&l, &h)| (h - l) as i64)
|
||||
.product()
|
||||
}
|
||||
|
||||
fn overlaps(&self, other: &Self) -> bool {
|
||||
self.lower
|
||||
.iter()
|
||||
.zip(&self.upper)
|
||||
.zip(other.lower.iter().zip(&other.upper))
|
||||
.all(|((&al, &ah), (&bl, &bh))| al < bh && bl < ah)
|
||||
}
|
||||
|
||||
pub fn retain_nonoverlapping(&self, other: &Self, new_cubes: &mut Vec<Self>) -> bool {
|
||||
if !self.overlaps(other) {
|
||||
// Cube can be kept as-is.
|
||||
return true;
|
||||
}
|
||||
|
||||
let mut lower = self.lower;
|
||||
let mut upper = self.upper;
|
||||
|
||||
for axis in 0..3 {
|
||||
if other.lower[axis] > self.lower[axis] {
|
||||
let mut new_upper = upper;
|
||||
new_upper[axis] = other.lower[axis];
|
||||
|
||||
new_cubes.push(Cuboid {
|
||||
lower,
|
||||
upper: new_upper,
|
||||
});
|
||||
|
||||
lower[axis] = other.lower[axis];
|
||||
}
|
||||
|
||||
if other.upper[axis] < self.upper[axis] {
|
||||
let mut new_lower = lower;
|
||||
new_lower[axis] = other.upper[axis];
|
||||
|
||||
new_cubes.push(Cuboid {
|
||||
lower: new_lower,
|
||||
upper,
|
||||
});
|
||||
|
||||
upper[axis] = other.upper[axis];
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_tuple(input: &[u8]) -> IResult<&[u8], (i32, i32)> {
|
||||
use nom::character::complete::i32;
|
||||
separated_pair(i32, tag(".."), i32)(input)
|
||||
}
|
||||
|
||||
fn parse_cuboid(input: &[u8]) -> IResult<&[u8], Cuboid> {
|
||||
map(
|
||||
tuple((
|
||||
parse_state,
|
||||
parse_range,
|
||||
preceded(tag(",y="), parse_range),
|
||||
preceded(tag(",z="), parse_range),
|
||||
parse_tuple,
|
||||
preceded(tag(",y="), parse_tuple),
|
||||
preceded(tag(",z="), parse_tuple),
|
||||
)),
|
||||
|(b, x, y, z)| (b, [x, y, z]),
|
||||
);
|
||||
|((xl, xh), (yl, yh), (zl, zh))| Cuboid::new([xl, yl, zl], [xh, yh, zh]),
|
||||
)(input)
|
||||
}
|
||||
|
||||
fn parse_input(input: &[u8]) -> IResult<&[u8], Vec<(bool, Cuboid)>> {
|
||||
let parse_state = alt((map(tag("on x="), |_| true), map(tag("off x="), |_| false)));
|
||||
let parse_line = tuple((parse_state, parse_cuboid));
|
||||
|
||||
separated_list1(newline, parse_line)(input)
|
||||
}
|
||||
|
||||
pub fn part1(input: &mut dyn Read) -> String {
|
||||
const MAX_ABS_VAL: i32 = 50;
|
||||
const SIDE_LEN: usize = 2 * (MAX_ABS_VAL as usize) + 1;
|
||||
let commands = read_input(input, parse_input);
|
||||
let mut cubes = Vec::new();
|
||||
let mut new_cubes = Vec::new();
|
||||
|
||||
let mut state = [[0u128; SIDE_LEN]; SIDE_LEN];
|
||||
for (state, cube) in commands.into_iter().filter(|(_, c)| c.is_small()) {
|
||||
cubes.retain(|existing: &Cuboid| existing.retain_nonoverlapping(&cube, &mut new_cubes));
|
||||
|
||||
let valid_range = -MAX_ABS_VAL..=MAX_ABS_VAL;
|
||||
// Add new cubes to the end
|
||||
cubes.append(&mut new_cubes);
|
||||
|
||||
let ranges = read_input(input, parse_input);
|
||||
|
||||
for (toggle, [xr, yr, zr]) in ranges {
|
||||
for z in zr {
|
||||
if !valid_range.contains(&z) {
|
||||
continue;
|
||||
}
|
||||
for y in yr.clone() {
|
||||
if !valid_range.contains(&y) {
|
||||
continue;
|
||||
}
|
||||
let row = &mut state[(z + MAX_ABS_VAL) as usize][(y + MAX_ABS_VAL) as usize];
|
||||
|
||||
for x in xr.clone() {
|
||||
if !valid_range.contains(&x) {
|
||||
continue;
|
||||
}
|
||||
let bit = 1 << (x + MAX_ABS_VAL);
|
||||
|
||||
if toggle {
|
||||
*row |= bit;
|
||||
} else {
|
||||
*row &= !bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
if state {
|
||||
cubes.push(cube);
|
||||
}
|
||||
}
|
||||
|
||||
state
|
||||
.iter()
|
||||
.flatten()
|
||||
.map(|val| val.count_ones())
|
||||
.sum::<u32>()
|
||||
.to_string()
|
||||
cubes.iter().map(Cuboid::volume).sum::<i64>().to_string()
|
||||
}
|
||||
|
||||
pub fn part2(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
pub fn part2(input: &mut dyn Read) -> String {
|
||||
let commands = read_input(input, parse_input);
|
||||
let mut cubes = Vec::new();
|
||||
let mut new_cubes = Vec::new();
|
||||
|
||||
for (state, cube) in commands {
|
||||
cubes.retain(|existing: &Cuboid| existing.retain_nonoverlapping(&cube, &mut new_cubes));
|
||||
|
||||
// Add new cubes to the end
|
||||
cubes.append(&mut new_cubes);
|
||||
|
||||
if state {
|
||||
cubes.push(cube);
|
||||
}
|
||||
}
|
||||
|
||||
cubes.iter().map(Cuboid::volume).sum::<i64>().to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -93,15 +161,39 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
const SAMPLE: &[u8] = include_bytes!("samples/22.txt");
|
||||
const SAMPLE1: &[u8] = include_bytes!("samples/22.1.txt");
|
||||
const SAMPLE2: &[u8] = include_bytes!("samples/22.2.txt");
|
||||
|
||||
#[test]
|
||||
fn test_overlap() {
|
||||
let cube_a = Cuboid {
|
||||
lower: [1, 1, 1],
|
||||
upper: [4, 4, 4],
|
||||
};
|
||||
|
||||
let cube_b = Cuboid {
|
||||
lower: [2, 2, 2],
|
||||
upper: [3, 3, 3],
|
||||
};
|
||||
|
||||
let mut new_cubes = Vec::new();
|
||||
|
||||
// B is fully inside A so it should overlap and the result should be empty
|
||||
assert!(!cube_b.retain_nonoverlapping(&cube_a, &mut new_cubes));
|
||||
assert_eq!(new_cubes, Vec::new());
|
||||
|
||||
// In the reverse case, we should have lots of new cubes
|
||||
assert!(!cube_a.retain_nonoverlapping(&cube_b, &mut new_cubes));
|
||||
assert_eq!(new_cubes.len(), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
test_implementation(part1, SAMPLE, 590784);
|
||||
test_implementation(part1, SAMPLE1, 590784);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_part2() {
|
||||
// test_implementation(part2, SAMPLE, 230)
|
||||
test_implementation(part2, SAMPLE2, 2758514936282235u64);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,341 @@
|
||||
use std::cmp::Reverse;
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::collections::BinaryHeap;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Display;
|
||||
use std::io::Read;
|
||||
use std::mem::swap;
|
||||
|
||||
pub fn part1(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
use crate::common::LineIter;
|
||||
|
||||
type Item = (u32, u32, State);
|
||||
type Todo = BinaryHeap<Reverse<Item>>;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Hash)]
|
||||
enum Pod {
|
||||
A,
|
||||
B,
|
||||
C,
|
||||
D,
|
||||
}
|
||||
|
||||
impl Pod {
|
||||
pub fn cost(self) -> u32 {
|
||||
match self {
|
||||
Pod::A => 1,
|
||||
Pod::B => 10,
|
||||
Pod::C => 100,
|
||||
Pod::D => 1000,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<char> for Pod {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(c: char) -> Result<Self, Self::Error> {
|
||||
match c {
|
||||
'A' => Ok(Pod::A),
|
||||
'B' => Ok(Pod::B),
|
||||
'C' => Ok(Pod::C),
|
||||
'D' => Ok(Pod::D),
|
||||
_ => Err("Invalid pod"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
|
||||
struct State {
|
||||
hallway: [Option<Pod>; 7],
|
||||
rooms: [[Option<Pod>; 2]; 4],
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn is_done(&self) -> bool {
|
||||
self == &State {
|
||||
hallway: [None; 7],
|
||||
rooms: [
|
||||
[Some(Pod::A); 2],
|
||||
[Some(Pod::B); 2],
|
||||
[Some(Pod::C); 2],
|
||||
[Some(Pod::D); 2],
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
fn add_to_queue(self, cost: u32, todo: &mut Todo, visited: &mut HashMap<Self, u32>) {
|
||||
let entry = visited.entry(self.clone());
|
||||
|
||||
if matches!(&entry, Entry::Occupied(entry) if *entry.get() <= cost) {
|
||||
// Already got a better one
|
||||
return;
|
||||
}
|
||||
|
||||
// print!("Next: \n{}", self);
|
||||
|
||||
// nightly only :'(
|
||||
// entry.insert(cost);
|
||||
*entry.or_default() = cost;
|
||||
|
||||
todo.push(Reverse((cost + self.estimate(), cost, self)))
|
||||
}
|
||||
|
||||
fn estimate(&self) -> u32 {
|
||||
// A* estimate. For every entry that is not already "at rest", the cost is the cost
|
||||
// required to get it to the top of its intended room.
|
||||
let mut estimate = 0;
|
||||
|
||||
for (x, &pod) in self.hallway.iter().enumerate() {
|
||||
if let Some(pod) = pod {
|
||||
let cost = if x == 0 {
|
||||
4 + pod as u32 * 2
|
||||
} else if x == 6 {
|
||||
4 + (3 - pod as u32) * 2
|
||||
} else if x <= (pod as usize) + 1 {
|
||||
2 + 2 * (pod as u32 + (x as u32 - 1))
|
||||
} else {
|
||||
2 + 2 * (x as u32 - pod as u32 - 2)
|
||||
};
|
||||
estimate += cost * pod.cost();
|
||||
}
|
||||
}
|
||||
|
||||
for (index, room) in self.rooms.iter().enumerate() {
|
||||
if let Some(last) = room
|
||||
.iter()
|
||||
.rposition(|&pod| !matches!(pod, Some(pod) if pod as usize == index))
|
||||
{
|
||||
for pos in 0..=last {
|
||||
if let Some(pod) = room[pos] {
|
||||
if pod as usize != index {
|
||||
let abs_diff = index.max(pod as usize) - index.min(pod as usize);
|
||||
estimate += (pos + 2 + 2 * abs_diff) as u32 * pod.cost();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
estimate
|
||||
}
|
||||
|
||||
pub fn generate_next(&self, cost: u32, todo: &mut Todo, visited: &mut HashMap<Self, u32>) {
|
||||
self.generate_hallway(cost, todo, visited);
|
||||
self.generate_rooms(cost, todo, visited);
|
||||
}
|
||||
|
||||
fn generate_rooms(&self, cost: u32, todo: &mut Todo, visited: &mut HashMap<Self, u32>) {
|
||||
for (index, room) in self.rooms.iter().enumerate() {
|
||||
// Check what part of the room should still move
|
||||
if let Some(last) = room
|
||||
.iter()
|
||||
.rposition(|&pod| !matches!(pod, Some(pod) if pod as usize == index))
|
||||
{
|
||||
for pos in 0..=last {
|
||||
let pod = match room[pos] {
|
||||
Some(pod) => pod,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
// Check if we can move up
|
||||
if pos > 0 && room[pos - 1].is_none() {
|
||||
let mut new_state = self.clone();
|
||||
new_state.rooms[index].swap(pos, pos - 1);
|
||||
let new_cost = cost + pod.cost();
|
||||
new_state.add_to_queue(new_cost, todo, visited);
|
||||
}
|
||||
|
||||
// Check if we can move down
|
||||
if pos + 1 < room.len() && room[pos + 1].is_none() {
|
||||
let mut new_state = self.clone();
|
||||
new_state.rooms[index].swap(pos, pos + 1);
|
||||
let new_cost = cost + pod.cost();
|
||||
new_state.add_to_queue(new_cost, todo, visited);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we can pop out of the room
|
||||
if let Some(pod) = room[0] {
|
||||
for pos in [index + 1, index + 2] {
|
||||
if self.hallway[pos].is_none() {
|
||||
let mut new_state = self.clone();
|
||||
swap(&mut new_state.rooms[index][0], &mut new_state.hallway[pos]);
|
||||
let new_cost = cost + pod.cost();
|
||||
new_state.add_to_queue(new_cost, todo, visited);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_hallway(&self, cost: u32, todo: &mut Todo, visited: &mut HashMap<Self, u32>) {
|
||||
for index in 0..self.hallway.len() {
|
||||
let pod = if let Some(pod) = self.hallway[index] {
|
||||
pod
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// Check if we can move right
|
||||
if index + 1 < self.hallway.len() && self.hallway[index + 1].is_none() {
|
||||
let mut new_state = self.clone();
|
||||
new_state.hallway.swap(index, index + 1);
|
||||
let added_cost = if index == 0 || index == 5 {
|
||||
pod.cost()
|
||||
} else {
|
||||
2 * pod.cost()
|
||||
};
|
||||
|
||||
let new_cost = cost + added_cost;
|
||||
new_state.add_to_queue(new_cost, todo, visited);
|
||||
}
|
||||
|
||||
// Check if we can move left
|
||||
if index > 1 && self.hallway[index - 1].is_none() {
|
||||
let mut new_state = self.clone();
|
||||
new_state.hallway.swap(index, index - 1);
|
||||
let added_cost = if index == 1 || index == 6 {
|
||||
pod.cost()
|
||||
} else {
|
||||
2 * pod.cost()
|
||||
};
|
||||
|
||||
let new_cost = cost + added_cost;
|
||||
new_state.add_to_queue(new_cost, todo, visited);
|
||||
}
|
||||
|
||||
// Check if we can pop into a room to the right
|
||||
if (1..=4).contains(&index) && self.rooms[index - 1][0].is_none() {
|
||||
let mut new_state = self.clone();
|
||||
swap(
|
||||
&mut new_state.hallway[index],
|
||||
&mut new_state.rooms[index - 1][0],
|
||||
);
|
||||
|
||||
let new_cost = cost + 2 * pod.cost();
|
||||
new_state.add_to_queue(new_cost, todo, visited);
|
||||
}
|
||||
|
||||
if (2..=5).contains(&index) && self.rooms[index - 2][0].is_none() {
|
||||
let mut new_state = self.clone();
|
||||
swap(
|
||||
&mut new_state.hallway[index],
|
||||
&mut new_state.rooms[index - 2][0],
|
||||
);
|
||||
|
||||
let new_cost = cost + 2 * pod.cost();
|
||||
new_state.add_to_queue(new_cost, todo, visited);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for State {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let helper = |opt_pod| match opt_pod {
|
||||
Some(Pod::A) => 'A',
|
||||
Some(Pod::B) => 'B',
|
||||
Some(Pod::C) => 'C',
|
||||
Some(Pod::D) => 'D',
|
||||
None => '.',
|
||||
};
|
||||
writeln!(f, "#############")?;
|
||||
write!(f, "#{}{}", helper(self.hallway[0]), helper(self.hallway[1]))?;
|
||||
for i in 2..=5 {
|
||||
write!(f, ".{}", helper(self.hallway[i]))?;
|
||||
}
|
||||
writeln!(f, "{}#", helper(self.hallway[6]))?;
|
||||
|
||||
for i in 0..(self.rooms[0].len()) {
|
||||
writeln!(
|
||||
f,
|
||||
" #{}#{}#{}#{}#",
|
||||
helper(self.rooms[0][i]),
|
||||
helper(self.rooms[1][i]),
|
||||
helper(self.rooms[2][i]),
|
||||
helper(self.rooms[3][i])
|
||||
)?;
|
||||
}
|
||||
|
||||
write!(f, " #########")
|
||||
}
|
||||
}
|
||||
|
||||
fn read_input(input: &mut dyn Read) -> State {
|
||||
let mut reader = LineIter::new(input);
|
||||
let mut state = State::default();
|
||||
|
||||
let _ = reader.next();
|
||||
let _ = reader.next();
|
||||
|
||||
let mut helper = |idx: usize| {
|
||||
reader
|
||||
.next()
|
||||
.unwrap()
|
||||
.chars()
|
||||
.filter_map(|c| Pod::try_from(c).ok())
|
||||
.zip(&mut state.rooms)
|
||||
.for_each(|(pod, room)| room[idx] = Some(pod))
|
||||
};
|
||||
|
||||
helper(0);
|
||||
helper(1);
|
||||
|
||||
state
|
||||
}
|
||||
|
||||
pub fn part1(input: &mut dyn Read) -> String {
|
||||
let state = read_input(input);
|
||||
let mut todo = Todo::new();
|
||||
|
||||
let mut visited = HashMap::new();
|
||||
visited.insert(state.clone(), 0);
|
||||
|
||||
todo.push(Reverse((state.estimate(), 0, state)));
|
||||
|
||||
while let Some(Reverse((_, cost, state))) = todo.pop() {
|
||||
if state.is_done() {
|
||||
return cost.to_string();
|
||||
}
|
||||
|
||||
// println!("\nExpanding:\n{}", state);
|
||||
|
||||
state.generate_next(cost, &mut todo, &mut visited);
|
||||
}
|
||||
|
||||
panic!("No route found!")
|
||||
}
|
||||
|
||||
pub fn part2(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use crate::test_implementation;
|
||||
|
||||
const SAMPLE: &[u8] = include_bytes!("samples/23.txt");
|
||||
|
||||
#[test]
|
||||
fn test_is_done() {
|
||||
let state = State {
|
||||
hallway: Default::default(),
|
||||
rooms: [
|
||||
[Some(Pod::A); 2],
|
||||
[Some(Pod::B), Some(Pod::B)],
|
||||
[Some(Pod::C), Some(Pod::C)],
|
||||
[Some(Pod::D), Some(Pod::D)],
|
||||
],
|
||||
};
|
||||
|
||||
assert!(state.is_done());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
test_implementation(part1, SAMPLE, 12521);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,85 @@
|
||||
use std::io::Read;
|
||||
|
||||
pub fn part1(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
fn read_input(input: &mut dyn Read) -> (usize, Vec<u8>) {
|
||||
let mut result = Vec::new();
|
||||
input.read_to_end(&mut result).unwrap();
|
||||
|
||||
let width = result.iter().position(|&c| c == b'\n').unwrap();
|
||||
result.retain(|c| !c.is_ascii_whitespace());
|
||||
|
||||
(width, result)
|
||||
}
|
||||
|
||||
fn advance(width: usize, state: &mut [u8]) -> bool {
|
||||
debug_assert_eq!(state.len() % width, 0);
|
||||
let mut changes = false;
|
||||
|
||||
// Move the eastbound herd
|
||||
for src in state.chunks_exact_mut(width) {
|
||||
let swap_last = src[0] == b'.' && src[width - 1] == b'>';
|
||||
let mut x = 0;
|
||||
|
||||
while x < src.len() - 1 {
|
||||
if src[x] == b'>' && src[x + 1] == b'.' {
|
||||
src.swap(x, x + 1);
|
||||
changes = true;
|
||||
x += 2;
|
||||
} else {
|
||||
x += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if swap_last {
|
||||
src.swap(0, width - 1);
|
||||
changes = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Then move the southbound herd. Need to do by column because of the first entry special case
|
||||
for x in 0..width {
|
||||
let last_index = state.len() - width + x;
|
||||
let swap_last = state[x] == b'.' && state[last_index] == b'v';
|
||||
|
||||
let mut offset = x;
|
||||
|
||||
while offset < state.len() - width {
|
||||
if state[offset] == b'v' && state[offset + width] == b'.' {
|
||||
state.swap(offset, offset + width);
|
||||
changes = true;
|
||||
offset += 2 * width;
|
||||
} else {
|
||||
offset += width;
|
||||
}
|
||||
}
|
||||
|
||||
if swap_last {
|
||||
state.swap(x, last_index);
|
||||
changes = true;
|
||||
}
|
||||
}
|
||||
|
||||
changes
|
||||
}
|
||||
|
||||
pub fn part1(input: &mut dyn Read) -> String {
|
||||
let (width, mut state) = read_input(input);
|
||||
|
||||
(1..)
|
||||
.find(|_| !advance(width, &mut state))
|
||||
.unwrap()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use crate::test_implementation;
|
||||
|
||||
const SAMPLE: &[u8] = include_bytes!("samples/25.txt");
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
test_implementation(part1, SAMPLE, 58);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ pub fn get_implementation(day: usize, part2: bool) -> Solution {
|
||||
|
||||
#[cfg(test)]
|
||||
fn test_implementation(solution: Solution, data: &[u8], answer: impl ToString) {
|
||||
let result = solution(&mut data.as_ref());
|
||||
let result = solution(&mut &data[..]);
|
||||
|
||||
assert_eq!(answer.to_string(), result);
|
||||
}
|
||||
|
||||
60
2021/src/samples/22.2.txt
Normal file
60
2021/src/samples/22.2.txt
Normal file
@@ -0,0 +1,60 @@
|
||||
on x=-5..47,y=-31..22,z=-19..33
|
||||
on x=-44..5,y=-27..21,z=-14..35
|
||||
on x=-49..-1,y=-11..42,z=-10..38
|
||||
on x=-20..34,y=-40..6,z=-44..1
|
||||
off x=26..39,y=40..50,z=-2..11
|
||||
on x=-41..5,y=-41..6,z=-36..8
|
||||
off x=-43..-33,y=-45..-28,z=7..25
|
||||
on x=-33..15,y=-32..19,z=-34..11
|
||||
off x=35..47,y=-46..-34,z=-11..5
|
||||
on x=-14..36,y=-6..44,z=-16..29
|
||||
on x=-57795..-6158,y=29564..72030,z=20435..90618
|
||||
on x=36731..105352,y=-21140..28532,z=16094..90401
|
||||
on x=30999..107136,y=-53464..15513,z=8553..71215
|
||||
on x=13528..83982,y=-99403..-27377,z=-24141..23996
|
||||
on x=-72682..-12347,y=18159..111354,z=7391..80950
|
||||
on x=-1060..80757,y=-65301..-20884,z=-103788..-16709
|
||||
on x=-83015..-9461,y=-72160..-8347,z=-81239..-26856
|
||||
on x=-52752..22273,y=-49450..9096,z=54442..119054
|
||||
on x=-29982..40483,y=-108474..-28371,z=-24328..38471
|
||||
on x=-4958..62750,y=40422..118853,z=-7672..65583
|
||||
on x=55694..108686,y=-43367..46958,z=-26781..48729
|
||||
on x=-98497..-18186,y=-63569..3412,z=1232..88485
|
||||
on x=-726..56291,y=-62629..13224,z=18033..85226
|
||||
on x=-110886..-34664,y=-81338..-8658,z=8914..63723
|
||||
on x=-55829..24974,y=-16897..54165,z=-121762..-28058
|
||||
on x=-65152..-11147,y=22489..91432,z=-58782..1780
|
||||
on x=-120100..-32970,y=-46592..27473,z=-11695..61039
|
||||
on x=-18631..37533,y=-124565..-50804,z=-35667..28308
|
||||
on x=-57817..18248,y=49321..117703,z=5745..55881
|
||||
on x=14781..98692,y=-1341..70827,z=15753..70151
|
||||
on x=-34419..55919,y=-19626..40991,z=39015..114138
|
||||
on x=-60785..11593,y=-56135..2999,z=-95368..-26915
|
||||
on x=-32178..58085,y=17647..101866,z=-91405..-8878
|
||||
on x=-53655..12091,y=50097..105568,z=-75335..-4862
|
||||
on x=-111166..-40997,y=-71714..2688,z=5609..50954
|
||||
on x=-16602..70118,y=-98693..-44401,z=5197..76897
|
||||
on x=16383..101554,y=4615..83635,z=-44907..18747
|
||||
off x=-95822..-15171,y=-19987..48940,z=10804..104439
|
||||
on x=-89813..-14614,y=16069..88491,z=-3297..45228
|
||||
on x=41075..99376,y=-20427..49978,z=-52012..13762
|
||||
on x=-21330..50085,y=-17944..62733,z=-112280..-30197
|
||||
on x=-16478..35915,y=36008..118594,z=-7885..47086
|
||||
off x=-98156..-27851,y=-49952..43171,z=-99005..-8456
|
||||
off x=2032..69770,y=-71013..4824,z=7471..94418
|
||||
on x=43670..120875,y=-42068..12382,z=-24787..38892
|
||||
off x=37514..111226,y=-45862..25743,z=-16714..54663
|
||||
off x=25699..97951,y=-30668..59918,z=-15349..69697
|
||||
off x=-44271..17935,y=-9516..60759,z=49131..112598
|
||||
on x=-61695..-5813,y=40978..94975,z=8655..80240
|
||||
off x=-101086..-9439,y=-7088..67543,z=33935..83858
|
||||
off x=18020..114017,y=-48931..32606,z=21474..89843
|
||||
off x=-77139..10506,y=-89994..-18797,z=-80..59318
|
||||
off x=8476..79288,y=-75520..11602,z=-96624..-24783
|
||||
on x=-47488..-1262,y=24338..100707,z=16292..72967
|
||||
off x=-84341..13987,y=2429..92914,z=-90671..-1318
|
||||
off x=-37810..49457,y=-71013..-7894,z=-105357..-13188
|
||||
off x=-27365..46395,y=31009..98017,z=15428..76570
|
||||
off x=-70369..-16548,y=22648..78696,z=-1892..86821
|
||||
on x=-53470..21291,y=-120233..-33476,z=-44150..38147
|
||||
off x=-93533..-4276,y=-16170..68771,z=-104985..-24507
|
||||
5
2021/src/samples/23.txt
Normal file
5
2021/src/samples/23.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
#############
|
||||
#...........#
|
||||
###B#C#B#D###
|
||||
#A#D#C#A#
|
||||
#########
|
||||
9
2021/src/samples/25.txt
Normal file
9
2021/src/samples/25.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
v...>>.vv>
|
||||
.vv>>.vv..
|
||||
>>.>v>...v
|
||||
>>v>>.>.v.
|
||||
v>v.vv.v..
|
||||
>.>>..v...
|
||||
.vv..>.>v.
|
||||
v.v..>>v.v
|
||||
....v..v.>
|
||||
Reference in New Issue
Block a user