Compare commits

...

2 Commits

Author SHA1 Message Date
setop 4f3a52c210 more stats 2022-12-30 23:05:12 +01:00
setop 6ff32e169a day 22, in Rust, not mine 2022-12-29 12:36:44 +01:00
3 changed files with 495 additions and 41 deletions

146
d22/part1.rs Normal file
View File

@ -0,0 +1,146 @@
use std::iter::repeat;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct Instruction {
dist: Option<isize>,
rotation: Option<isize>,
}
pub fn main() {
let s = include_bytes!("./input");
let mut pos = (s.iter().position(|b| *b == b'.').unwrap() as isize, 0);
let mut facing: (isize, isize) = (1, 0);
let w = s
.split(|b| *b == b'\n')
.take_while(|l| !l.is_empty())
.map(|l| l.len())
.max()
.unwrap();
let map = s
.split(|b| *b == b'\n')
.take_while(|l| !l.is_empty())
.map(|l| {
l.iter()
.take(l.len())
.chain(repeat(&b' ').take(w - l.len()))
.copied()
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let h = map.len();
let mut path: Vec<Instruction> = Vec::new();
let mut acc = 0;
s.split(|b| *b == b'\n')
.rev()
.nth(1)
.unwrap()
.iter()
.for_each(|b| match b {
b'0'..=b'9' => {
acc = acc * 10 + (b - b'0') as isize;
}
b'R' => {
path.push(Instruction {
dist: Some(acc),
rotation: None,
});
path.push(Instruction {
dist: None,
rotation: Some(1),
});
acc = 0;
}
b'L' => {
path.push(Instruction {
dist: Some(acc),
rotation: None,
});
path.push(Instruction {
dist: None,
rotation: Some(-1),
});
acc = 0;
}
_ => unreachable!(),
});
path.push(Instruction {
dist: Some(acc),
rotation: None,
});
#[cfg(debug_assertions)]
println!("{} {}", map.len(), map[0].len());
#[cfg(debug_assertions)]
println!(
"pos: ({}, {}), facing ({} {})",
pos.0, pos.1, facing.0, facing.1
);
for ins in path.iter() {
#[cfg(debug_assertions)]
println!("{:?}", ins);
match (ins.dist, ins.rotation) {
(Some(dist), None) => {
for step in 0..dist {
let mut next_pos = (pos.0 + facing.0, pos.1 + facing.1);
#[cfg(debug_assertions)]
println!("next_pos: {} {}", next_pos.0, next_pos.1);
let next_char = if next_pos.0 >= 0
&& next_pos.0 < w as isize
&& next_pos.1 >= 0
&& next_pos.1 < h as isize
{
Some(map[next_pos.1 as usize][next_pos.0 as usize])
} else {
None
};
if next_char == Some(b'.') {
pos = next_pos;
} else if next_char.is_none() || next_char == Some(b' ') {
next_pos = (pos.0 - facing.0, pos.1 - facing.1);
while next_pos.0 >= 0
&& next_pos.0 < w as isize
&& next_pos.1 >= 0
&& next_pos.1 < h as isize
&& map[next_pos.1 as usize][next_pos.0 as usize] != b' '
{
next_pos = (next_pos.0 - facing.0, next_pos.1 - facing.1);
}
next_pos = (next_pos.0 + facing.0, next_pos.1 + facing.1);
if map[next_pos.1 as usize][next_pos.0 as usize] != b'#' {
pos = next_pos;
} else {
#[cfg(debug_assertions)]
println!("pos: {} {}", pos.0, pos.1);
break;
}
} else {
#[cfg(debug_assertions)]
println!("pos: {} {}", pos.0, pos.1);
break;
}
}
}
(None, Some(rot)) => match rot {
1 => (facing.0, facing.1) = (-facing.1, facing.0),
-1 => (facing.0, facing.1) = (facing.1, -facing.0),
_ => unreachable!(),
},
_ => unreachable!(),
}
#[cfg(debug_assertions)]
println!(
"pos: ({}, {}), facing ({} {})",
pos.0, pos.1, facing.0, facing.1
);
}
let facing_dir = facing.0.abs() * (1 - facing.0) + facing.1.abs() * (1 - facing.1);
#[cfg(debug_assertions)]
println!(
"pos: ({}, {}), facing ({} {}) = {}",
pos.0 + 1,
pos.1 + 1,
facing.0,
facing.1,
facing_dir
);
print!("{} ", 1000 * (pos.1 + 1) + 4 * (pos.0 + 1) + facing_dir);
}

197
d22/part2.rs Normal file
View File

@ -0,0 +1,197 @@
use std::iter::repeat;
type Pos = (isize, isize);
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
struct Instruction {
dist: Option<isize>,
rotation: Option<isize>,
}
fn rotate(facing: Pos, by: isize) -> Pos {
match by.rem_euclid(4) {
0 => facing,
1 => (-facing.1, facing.0),
2 => (-facing.0, -facing.1),
3 => (facing.1, -facing.0),
_ => unreachable!(),
}
}
fn facing_angle_to_axes(facing: isize) -> Pos {
match facing.rem_euclid(4) {
0 => (1, 0),
1 => (0, 1),
2 => (-1, 0),
3 => (0, -1),
_ => unreachable!(),
}
}
fn next_pos_facing(
pos: Pos,
facing: Pos,
map: &Vec<Vec<u8>>,
edges: &Vec<(Vec<Pos>, Vec<Pos>, isize, isize)>,
) -> (Pos, Pos) {
let mut next_pos = (pos.0 + facing.0, pos.1 + facing.1);
let mut next_facing = facing;
for edge in edges.iter() {
if let Some(i) = edge.0.iter().position(|p| *p == pos) {
if facing_angle_to_axes(edge.2) == facing {
(next_pos, next_facing) = (edge.1[i], rotate(facing, (edge.3 + 2) - edge.2));
#[cfg(debug_assertions)]
println!(
"cross edge: {} {} ({} {}) -> {} {} ({} {})",
pos.0,
pos.1,
facing.0,
facing.1,
next_pos.0,
next_pos.1,
next_facing.0,
next_facing.1
);
}
} else if let Some(i) = edge.1.iter().position(|p| *p == pos) {
if facing_angle_to_axes(edge.3) == facing {
(next_pos, next_facing) = (edge.0[i], rotate(facing, (edge.2 + 2) - edge.3));
#[cfg(debug_assertions)]
println!(
"cross edge: {} {} ({} {}) -> {} {} ({} {})",
pos.0,
pos.1,
facing.0,
facing.1,
next_pos.0,
next_pos.1,
next_facing.0,
next_facing.1
);
}
}
}
(next_pos, next_facing)
}
pub fn main() {
let s = include_bytes!("./input");
let mut pos = (s.iter().position(|b| *b == b'.').unwrap() as isize, 0);
let mut facing: (isize, isize) = (1, 0);
let w = s
.split(|b| *b == b'\n')
.take_while(|l| !l.is_empty())
.map(|l| l.len())
.max()
.unwrap();
let map = s
.split(|b| *b == b'\n')
.take_while(|l| !l.is_empty())
.map(|l| {
l.iter()
.take(l.len())
.chain(repeat(&b' ').take(w - l.len()))
.copied()
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let l = 50isize;
#[rustfmt::skip]
let edges: Vec<(Vec<Pos>, Vec<Pos>, isize, isize)> = vec![
((0..l).zip(repeat(l*2).take(l as usize)).collect(), repeat(l).take(l as usize).zip(l..l*2).collect(), 3, 2),
(repeat(0).take(l as usize).zip(l*2..l*3).collect(), repeat(l).take(l as usize).zip((0..l).rev()).collect(), 2, 2),
(repeat(0).take(l as usize).zip(l*3..l*4).collect(), (l..l*2).zip(repeat(0).take(l as usize)).collect(), 2, 3),
((0..l).zip(repeat(l*4-1).take(l as usize)).collect(), (l*2..l*3).zip(repeat(0).take(l as usize)).collect(), 1, 3),
(repeat(l-1).take(l as usize).zip(l*3..l*4).collect(), (l..l*2).zip(repeat(l*3-1).take(l as usize)).collect(), 0, 1),
(repeat(l*2-1).take(l as usize).zip(l*2..l*3).collect(), repeat(l*3-1).take(l as usize).zip((0..l).rev()).collect(), 0, 0),
(repeat(l*2-1).take(l as usize).zip(l..l*2).collect(), (l*2..l*3).zip(repeat(l-1).take(l as usize)).collect(), 0, 1),
];
let mut path: Vec<Instruction> = Vec::new();
let mut acc = 0;
s.split(|b| *b == b'\n')
.rev()
.nth(1)
.unwrap()
.iter()
.for_each(|b| match b {
b'0'..=b'9' => {
acc = acc * 10 + (b - b'0') as isize;
}
b'R' => {
path.push(Instruction {
dist: Some(acc),
rotation: None,
});
path.push(Instruction {
dist: None,
rotation: Some(1),
});
acc = 0;
}
b'L' => {
path.push(Instruction {
dist: Some(acc),
rotation: None,
});
path.push(Instruction {
dist: None,
rotation: Some(-1),
});
acc = 0;
}
_ => unreachable!(),
});
path.push(Instruction {
dist: Some(acc),
rotation: None,
});
#[cfg(debug_assertions)]
println!("{} {}", map.len(), map[0].len());
#[cfg(debug_assertions)]
println!(
"pos: ({}, {}), facing ({} {})",
pos.0, pos.1, facing.0, facing.1
);
for ins in path.iter() {
#[cfg(debug_assertions)]
println!("{:?}", ins);
match (ins.dist, ins.rotation) {
(Some(dist), None) => {
for _step in 0..dist {
let (next_pos, next_facing) = next_pos_facing(pos, facing, &map, &edges);
match map[next_pos.1 as usize][next_pos.0 as usize] {
b'#' => {
break;
}
b'.' => {
(pos, facing) = (next_pos, next_facing);
}
_ => {
panic!("unexpected wall as next character");
}
}
}
}
(None, Some(rot)) => {
(facing.0, facing.1) = rotate((facing.0, facing.1), rot);
}
_ => unreachable!(),
}
#[cfg(debug_assertions)]
println!(
"pos: ({}, {}), facing ({} {})",
pos.0, pos.1, facing.0, facing.1
);
}
let facing_dir = facing.0.abs() * (1 - facing.0) + facing.1.abs() * (2 - facing.1);
#[cfg(debug_assertions)]
println!(
"pos: ({}, {}), facing ({} {}) = {}",
pos.0 + 1,
pos.1 + 1,
facing.0,
facing.1,
facing_dir
);
print!("{} ", 1000 * (pos.1 + 1) + 4 * (pos.0 + 1) + facing_dir);
}

193
stats.slk
View File

@ -2,117 +2,228 @@ ID;PCALCOOO32
C;X1;Y1;K217872
C;X2;Y1;K28530
C;X3;Y1;K246402;EA1+B1
C;X5;Y1;K204702
C;X6;Y1;K8364
C;X7;Y1;K213066;EE1+F1
C;X4;Y1;K0.11578639783768;EB1/C1
C;X6;Y1;K242990
C;X7;Y1;K10457
C;X8;Y1;K253447;EF1+G1
C;X9;Y1;K0.0412591192635936;EG1/H1
C;X10;Y1;K"stock calories"
C;X1;Y2;K194482
C;X2;Y2;K8024
C;X3;Y2;K202506;EA2+B2
C;X5;Y2;K169839
C;X6;Y2;K7521
C;X7;Y2;K177360;EE2+F2
C;X4;Y2;K0.0396235173278816;EB2/C2
C;X6;Y2;K204642
C;X7;Y2;K9507
C;X8;Y2;K214149;EF2+G2
C;X9;Y2;K0.044394323578443;EG2/H2
C;X10;Y2;K"pierre feuille ciseaux"
C;X1;Y3;K137678
C;X2;Y3;K38713
C;X3;Y3;K176391;EA3+B3
C;X5;Y3;K143978
C;X6;Y3;K7051
C;X7;Y3;K151029;EE3+F3
C;X4;Y3;K0.21947264883129;EB3/C3
C;X6;Y3;K177465
C;X7;Y3;K8919
C;X8;Y3;K186384;EF3+G3
C;X9;Y3;K0.047852819984548;EG3/H3
C;X10;Y3;K"elves packages"
C;X1;Y4;K106585
C;X2;Y4;K6568
C;X3;Y4;K113153;EA4+B4
C;X5;Y4;K131242
C;X6;Y4;K2170
C;X7;Y4;K133412;EE4+F4
C;X4;Y4;K0.0580453014944367;EB4/C4
C;X6;Y4;K164350
C;X7;Y4;K2919
C;X8;Y4;K167269;EF4+G4
C;X9;Y4;K0.0174509323305574;EG4/H4
C;X10;Y4;K"pair overlap"
C;X1;Y5;K93739
C;X2;Y5;K4820
C;X3;Y5;K98559;EA5+B5
C;X5;Y5;K109082
C;X6;Y5;K2026
C;X7;Y5;K111108;EE5+F5
C;X4;Y5;K0.0489047169715602;EB5/C5
C;X6;Y5;K141388
C;X7;Y5;K2457
C;X8;Y5;K143845;EF5+G5
C;X9;Y5;K0.0170808856755535;EG5/H5
C;X10;Y5;K"cargo cranes"
C;X1;Y6;K92062
C;X2;Y6;K7157
C;X3;Y6;K99219;EA6+B6
C;X5;Y6;K105272
C;X6;Y6;K966
C;X7;Y6;K106238;EE6+F6
C;X4;Y6;K0.0721333615537347;EB6/C6
C;X6;Y6;K140026
C;X7;Y6;K1287
C;X8;Y6;K141313;EF6+G6
C;X9;Y6;K0.00910744234429953;EG6/H6
C;X10;Y6;K"start-of-packet marker 4,14"
C;X1;Y7;K90753
C;X2;Y7;K2480
C;X3;Y7;K93233;EA7+B7
C;X5;Y7;K65714
C;X6;Y7;K1988
C;X7;Y7;K67702;EE7+F7
C;X4;Y7;K0.0266000235967951;EB7/C7
C;X6;Y7;K101987
C;X7;Y7;K2151
C;X8;Y7;K104138;EF7+G7
C;X9;Y7;K0.0206552843342488;EG7/H7
C;X10;Y7;K"unix dirs"
C;X1;Y8;K70599
C;X2;Y8;K12569
C;X3;Y8;K83168;EA8+B8
C;X5;Y8;K24458
C;X6;Y8;K6391
C;X7;Y8;K30849;EE8+F8
C;X4;Y8;K0.151127837629858;EB8/C8
C;X6;Y8;K96958
C;X7;Y8;K6555
C;X8;Y8;K103513;EF8+G8
C;X9;Y8;K0.0633253794209423;EG8/H8
C;X10;Y8;K"plantation"
C;X1;Y9;K65999
C;X2;Y9;K10759
C;X3;Y9;K76758;EA9+B9
C;X7;Y9;K0;EE9+F9
C;X4;Y9;K0.140167800099012;EB9/C9
C;X6;Y9;K74595
C;X7;Y9;K10205
C;X8;Y9;K84800;EF9+G9
C;X9;Y9;K0.120341981132075;EG9/H9
C;X10;Y9;K"cordes"
C;X1;Y10;K67262
C;X2;Y10;K1897
C;X3;Y10;K69159;EA10+B10
C;X7;Y10;K0;EE10+F10
C;X4;Y10;K0.0274295464075536;EB10/C10
C;X6;Y10;K76812
C;X7;Y10;K4735
C;X8;Y10;K81547;EF10+G10
C;X9;Y10;K0.0580646743595718;EG10/H10
C;X10;Y10;K"écran"
C;X1;Y11;K60349
C;X2;Y11;K338
C;X3;Y11;K60687;EA11+B11
C;X7;Y11;K0;EE11+F11
C;X4;Y11;K0.00556956185014913;EB11/C11
C;X6;Y11;K62101
C;X7;Y11;K8303
C;X8;Y11;K70404;EF11+G11
C;X9;Y11;K0.117933640134083;EG11/H11
C;X10;Y11;K"monkey backpack"
C;X1;Y12;K50948
C;X2;Y12;K2102
C;X3;Y12;K53050;EA12+B12
C;X7;Y12;K0;EE12+F12
C;X4;Y12;K0.0396229971724788;EB12/C12
C;X6;Y12;K52925
C;X7;Y12;K903
C;X8;Y12;K53828;EF12+G12
C;X9;Y12;K0.0167756557925243;EG12/H12
C;X10;Y12;K"montain"
C;X1;Y13;K53038
C;X2;Y13;K929
C;X3;Y13;K53967;EA13+B13
C;X7;Y13;K0;EE13+F13
C;X4;Y13;K0.017214223506958;EB13/C13
C;X6;Y13;K46027
C;X7;Y13;K1048
C;X8;Y13;K47075;EF13+G13
C;X9;Y13;K0.0222623473181094;EG13/H13
C;X10;Y13;K"packet ordering"
C;X1;Y14;K47059
C;X2;Y14;K7170
C;X3;Y14;K54229;EA14+B14
C;X7;Y14;K0;EE14+F14
C;X4;Y14;K0.132217079422449;EB14/C14
C;X6;Y14;K43760
C;X7;Y14;K866
C;X8;Y14;K44626;EF14+G14
C;X9;Y14;K0.0194057276027428;EG14/H14
C;X10;Y14;K"sable"
C;X1;Y15;K39432
C;X2;Y15;K3564
C;X3;Y15;K42996;EA15+B15
C;X7;Y15;K0;EE15+F15
C;X4;Y15;K0.0828914317610941;EB15/C15
C;X6;Y15;K35093
C;X7;Y15;K5328
C;X8;Y15;K40421;EF15+G15
C;X9;Y15;K0.131812671631083;EG15/H15
C;X10;Y15;K"losanges"
C;X1;Y16;K33597
C;X2;Y16;K1709
C;X3;Y16;K35306;EA16+B16
C;X7;Y16;K0;EE16+F16
C;X4;Y16;K0.0484053701920354;EB16/C16
C;X6;Y16;K19647
C;X7;Y16;K5217
C;X8;Y16;K24864;EF16+G16
C;X9;Y16;K0.209821428571429;EG16/H16
C;X10;Y16;K"vanne volcan"
C;X1;Y17;K33586
C;X2;Y17;K1511
C;X3;Y17;K35097;EA17+B17
C;X7;Y17;K0;EE17+F17
C;X4;Y17;K0.0430521127161866;EB17/C17
C;X6;Y17;K18320
C;X7;Y17;K5789
C;X8;Y17;K24109;EF17+G17
C;X9;Y17;K0.240117798332573;EG17/H17
C;X10;Y17;K"tetris"
C;X1;Y18;K25687
C;X2;Y18;K183
C;X3;Y18;K25870;EA18+B18
C;X7;Y18;K0;EE18+F18
C;X4;Y18;K0.00707383069192114;EB18/C18
C;X6;Y18;K22011
C;X7;Y18;K4558
C;X8;Y18;K26569;EF18+G18
C;X9;Y18;K0.171553314012571;EG18/H18
C;X10;Y18;K"lava obsidian"
C;X1;Y19;K17189
C;X2;Y19;K258
C;X3;Y19;K17447;EA19+B19
C;X7;Y19;K0;EE19+F19
C;X4;Y19;K0.0147876425746547;EB19/C19
C;X6;Y19;K14198
C;X7;Y19;K902
C;X8;Y19;K15100;EF19+G19
C;X9;Y19;K0.0597350993377483;EG19/H19
C;X10;Y19;K"geode factory"
C;X1;Y20;K23207
C;X2;Y20;K377
C;X3;Y20;K23584;EA20+B20
C;X7;Y20;K0;EE20+F20
C;X4;Y20;K0.0159854138398915;EB20/C20
C;X6;Y20;K18813
C;X7;Y20;K925
C;X8;Y20;K19738;EF20+G20
C;X9;Y20;K0.0468639173168508;EG20/H20
C;X10;Y20;K"grove positioning system"
C;X1;Y21;K20211
C;X2;Y21;K6492
C;X3;Y21;K26703;EA21+B21
C;X7;Y21;K0;EE21+F21
C;X4;Y21;K0.243118750702168;EB21/C21
C;X6;Y21;K19701
C;X7;Y21;K2823
C;X8;Y21;K22524;EF21+G21
C;X9;Y21;K0.125332978156633;EG21/H21
C;X10;Y21;K"math expr"
C;X1;Y22;K16039
C;X2;Y22;K6058
C;X3;Y22;K22097;EA22+B22
C;X7;Y22;K0;EE22+F22
C;X4;Y22;K0.274154862651039;EB22/C22
C;X6;Y22;K12355
C;X7;Y22;K4803
C;X8;Y22;K17158;EF22+G22
C;X9;Y22;K0.279927730504721;EG22/H22
C;X10;Y22;K"cube"
C;X1;Y23;K13576
C;X2;Y23;K2189
C;X3;Y23;K15765;EA23+B23
C;X7;Y23;K0;EE23+F23
C;X4;Y23;K0.138851887091659;EB23/C23
C;X6;Y23;K15250
C;X7;Y23;K231
C;X8;Y23;K15481;EF23+G23
C;X9;Y23;K0.0149215166978877;EG23/H23
C;X10;Y23;K"éparpillement delfs"
C;X1;Y24;K13103
C;X2;Y24;K132
C;X3;Y24;K13235;EA24+B24
C;X7;Y24;K0;EE24+F24
C;X4;Y24;K0.00997355496788818;EB24/C24
C;X6;Y24;K12944
C;X7;Y24;K190
C;X8;Y24;K13134;EF24+G24
C;X9;Y24;K0.0144662707476778;EG24/H24
C;X10;Y24;K"blizzard"
C;X1;Y25;K11320
C;X2;Y25;K5992
C;X3;Y25;K17312;EA25+B25
C;X7;Y25;K0;EE25+F25
C;X4;Y25;K0.346118299445471;EB25/C25
C;X6;Y25;K9855
C;X7;Y25;K6023
C;X8;Y25;K15878;EF25+G25
C;X9;Y25;K0.37932989041441;EG25/H25
C;X10;Y25;K"base 5 -2"
C;X1;Y27;K2021
C;X6;Y27;K2022
E