add 2023/13/1

This commit is contained in:
kageru 2023-12-13 09:30:03 +01:00
parent ff7cfe5768
commit f23a95e2e2
15 changed files with 1456 additions and 44 deletions

1339
2023/inputs/day13 Normal file

File diff suppressed because it is too large Load Diff

View File

@ -40,21 +40,21 @@ fn part2(parsed: &Parsed) -> usize {
.sum()
}
#[cfg(test)]
const TEST_INPUT_2: &str = "two1nine
boilerplate! {
TEST_INPUT == "\
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet",
TEST_INPUT_2 == "\
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen";
boilerplate! {
TEST_INPUT == "1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet",
tests: {
7pqrstsixteen"
for tests: {
part1: { TEST_INPUT => 142 },
part2: { TEST_INPUT_2 => 281 },
},

View File

@ -49,8 +49,8 @@ Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green",
tests: {
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green"
for tests: {
part1: { TEST_INPUT => 8 },
part2: { TEST_INPUT => 2286 },
},

View File

@ -92,8 +92,8 @@ boilerplate! {
..592.....
......755.
...$.*....
.664.598..",
tests: {
.664.598.."
for tests: {
part1: { TEST_INPUT => 4361 },
part2: { TEST_INPUT => 467835 },
},

View File

@ -46,8 +46,8 @@ Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11",
tests: {
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"
for tests: {
part1: { TEST_INPUT => 13 },
part2: { TEST_INPUT => 30 },
},

View File

@ -116,8 +116,8 @@ temperature-to-humidity map:
humidity-to-location map:
60 56 37
56 93 4",
tests: {
56 93 4"
for tests: {
part1: { TEST_INPUT => 35 },
part2: { TEST_INPUT => 46 },
},

View File

@ -29,8 +29,8 @@ fn part2((_, (time, distance)): &Parsed) -> usize {
boilerplate! {
TEST_INPUT == "\
Time: 7 15 30
Distance: 9 40 200",
tests: {
Distance: 9 40 200"
for tests: {
part1: { TEST_INPUT => 288 },
part2: { TEST_INPUT => 71503 },
},

View File

@ -93,8 +93,8 @@ boilerplate! {
T55J5 684
KK677 28
KTJJT 220
QQQJA 483",
tests: {
QQQJA 483"
for tests: {
part1: { TEST_INPUT => 6440 },
part2: { TEST_INPUT => 5905 },
},

View File

@ -75,8 +75,15 @@ fn part2(parsed: &Parsed) -> usize {
parsed.1.iter().filter(|&&n| ends_with(n, b'A')).fold(1, |acc, n| lcm(acc, steps_until(parsed, *n)))
}
#[cfg(test)]
const TEST_INPUT_P2: &str = "LR
boilerplate! {
TEST_INPUT == "\
LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)",
TEST_INPUT_P2 == "\
LR
11A = (11B, XXX)
11B = (XXX, 11Z)
@ -85,15 +92,8 @@ const TEST_INPUT_P2: &str = "LR
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)";
boilerplate! {
TEST_INPUT == "LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)",
tests: {
XXX = (XXX, XXX)"
for tests: {
part1: { TEST_INPUT => 6 },
part2: { TEST_INPUT_P2 => 6 },
},

View File

@ -56,8 +56,8 @@ boilerplate! {
TEST_INPUT == "\
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45",
tests: {
10 13 16 21 30 45"
for tests: {
part1: { TEST_INPUT => 114 },
part2: { TEST_INPUT => 2 },
},

View File

@ -128,8 +128,8 @@ boilerplate! {
7S-7|
L|7||
-L-J|
L|-JF",
tests: {
L|-JF"
for tests: {
part1: { TEST_INPUT => 4 },
part2: {
INPUT_P2 => 4,

View File

@ -61,8 +61,8 @@ boilerplate! {
.........#
..........
.......#..
#...#.....",
tests: {
#...#....."
for tests: {
part1: { TEST_INPUT => 374 },
},
bench1 == 9623138,

View File

@ -122,8 +122,8 @@ boilerplate! {
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1",
tests: {
?###???????? 3,2,1"
for tests: {
part1: { TEST_INPUT => 21 },
part2: { TEST_INPUT => 525152 },
},

71
2023/src/bin/day13.rs Normal file
View File

@ -0,0 +1,71 @@
#![feature(test, try_blocks)]
extern crate test;
use aoc2023::{boilerplate, common::*};
const DAY: usize = 13;
type Parsed<'a> = Vec<Vec<Vec<u8>>>;
fn parse_input(raw: &str) -> Parsed {
raw.split("\n\n").map(|block| block.lines().map(|l| l.as_bytes().to_vec()).collect()).collect()
}
fn find_reflection(block: &[Vec<u8>]) -> Option<usize> {
'outer: for i in 0..block.len() - 1 {
let mut offset = 0;
loop {
match try { (block.get(i.checked_sub(offset)?)?, block.get(i + 1 + offset)?) } {
None => return Some(i + 1),
Some((a, b)) if a == b => offset += 1,
Some(_) => continue 'outer,
}
}
}
None
}
fn part1(blocks: &Parsed) -> usize {
blocks.iter().map(|block| find_reflection(block).map(|n| n * 100).or_else(|| find_reflection(&transpose(&block))).unwrap()).sum()
}
fn transpose(orig: &[Vec<u8>]) -> Vec<Vec<u8>> {
let mut out = vec![vec![0; orig.len()]; orig[0].len()];
for i in 0..orig.len() {
for j in 0..orig[0].len() {
out[j][i] = orig[i][j];
}
}
out
}
fn part2(parsed: &Parsed) -> usize {
unimplemented!()
}
boilerplate! {
TEST_INPUT_HORIZONTAL == "\
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#",
TEST_INPUT_VERTICAL == "\
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#."
for tests: {
part1: {
TEST_INPUT_HORIZONTAL => 400,
TEST_INPUT_VERTICAL => 5,
},
part2: { TEST_INPUT_VERTICAL => 0 },
},
bench1 == 37561,
bench2 == 0,
bench_parse: Vec::len => 100,
}

View File

@ -1,8 +1,8 @@
#[macro_export]
macro_rules! boilerplate {
(
TEST_INPUT == $ti: literal,
tests: {
$($tin: ident == $ti: literal),+
for tests: {
$($part: ident: { $($tpi: expr $(,$ati: expr)* => $to: expr),+$(,)? }),*$(,)?
},
$(unittests: {
@ -24,7 +24,9 @@ macro_rules! boilerplate {
use super::*;
use aoc2023::*;
const TEST_INPUT: &str = $ti;
$(
const $tin: &str = $ti;
)+
$($($(paste::paste! {
#[test]