2022 day 25 rust 🫡

This commit is contained in:
Maciej Jur 2022-12-25 11:29:15 +01:00
parent a2c822598a
commit 26e2d0ce19
5 changed files with 223 additions and 2 deletions

132
2022/rust/inputs/day25.txt Normal file
View file

@ -0,0 +1,132 @@
1--20
1-00==1-1==
12011==21020=-2021-
101--=11
22-=02=
112=12-222
10=-0-=10
202-1-20=01-10-2
11==
122==1=100-
1====2==121--=1
1=-02-=011-2=2-00=00
1000=2
12-
201=2-1-1=02--=1=1
1=01-1=-1=012-2-=
1=1=
11102
10=
2-=--2=2=-==11-11
2=02
10-1-00=0-20
1-0=11=
2=1=0=1022=-
1==1--2-=02=
1=12-2
200-
20=02-211=11=
1==12=-0-
1=1-0101=-1
2=21000-22
1--1=11-2-=1--211-
2-=101=-
1-20210-0-1=101=1
1===1===-
211=2-0==
21===1==0
10
2=0-1=110221-2-2
1001220120=-=
10=21=0-=--=21-=
1=0=1-2-0
21-0===21=2100-02
1=-=0-0-12=-0--202=
101200-111--10-=-
2201==0--=-
12-02201-====-=
2-1220-
10-
2=00-2-2
22-
1=01--==
202-11-=02-22-02
2-0=1=1=-1-0-2=-0=
1=01-1=2=11=10
1-011
1-0-1
11000=--0122-
1=-==2=101
1=10-0=110--1
200=-21-
222==20=22121-==2
11=
1-2=22-0212=1
2=--2=02
1=0-2=1-0==0=-=1-1
101
2=1=1=1202222010=1
1-=0
1-02
1=21=2==2=2
11-10-=20==1
1002=---012=1
222
1==-1=
1-212=-=2
212=--0
1-0=2-121=-11
11211011=--
2-
12=
22-202
2=20=-1220=10=---
11=--0122==20-121
200=
2=220=
1=00=001121=1-00-
100210222
2-2==1
220
2-==-1=10
1=212=212=0=-2200
201--0=12-101
1==0000=1=0
2===
121-021220=022
1=====--10201111-0
22=2122
1=2110=2-21022
120=1=1-2002-12211
221200
110--
100120=1
22
2-==----2020=
10=-
11==-0=2-==0==2=
1===122=0=
21==-1
1-11-21--1
1011=1-0---=11=0
2=01=21-0
12=0=1-1==00010-2-
20-20--011
10=21212===-011=2=1
1-112110021-12220
1==022=112-2
1-=0-
1=
1=1
21212=211110--2
11-2-212=--02=0120
1-=1--=010=
2-11221101=-2=2=2=
2=
1=021
1===0020-1==
1022212
1-=221=
122-2-1-2--200=
1-00-10
102-20=-02--=201=

View file

@ -26,5 +26,6 @@ fn main() {
// solutions::day21::run();
// solutions::day22::run();
// solutions::day23::run();
solutions::day24::run();
// solutions::day24::run();
solutions::day25::run();
}

View file

@ -0,0 +1,87 @@
#![allow(dead_code)]
use crate::utils;
pub fn run() -> () {
let data = utils::read_lines(utils::Source::Day(25));
println!("Day 25");
println!("Part 1: {}", solve1(&data));
}
fn snafu2dec<T: AsRef<str>>(str: T) -> i64 {
str.as_ref().chars()
.fold(0, |acc, char| {
let num = match char {
'0' => 0,
'1' => 1,
'2' => 2,
'-' => -1,
'=' => -2,
_ => unreachable!(),
};
acc * 5 + num
})
}
fn dec2snafu(mut num: i64) -> String {
let mut snafu = String::new();
while num > 0 {
let rem = num % 5;
num = num / 5 + if rem > 2 { 1 } else { 0 };
snafu.push(match rem {
0 => '0',
1 => '1',
2 => '2',
3 => '=',
4 => '-',
_ => unreachable!(),
});
}
if snafu.is_empty() { snafu.push('0'); }
snafu.chars().rev().collect()
}
fn solve1<T: AsRef<str>>(data: &[T])-> String {
dec2snafu(data.iter().map(snafu2dec).sum())
}
#[cfg(test)]
mod tests {
use super::*;
static DATA: &[&str] = &[
"1=-0-2",
"12111",
"2=0=",
"21",
"2=01",
"111",
"20012",
"112",
"1=-1=",
"1-12",
"12",
"1=",
"122",
];
#[test]
fn part1() {
assert_eq!(1, snafu2dec("1"));
assert_eq!(5, snafu2dec("10"));
assert_eq!(2022, snafu2dec("1=11-2"));
assert_eq!(12345, snafu2dec("1-0---0"));
assert_eq!(314159265, snafu2dec("1121-1110-1=0"));
assert_eq!("1", dec2snafu(1));
assert_eq!("10", dec2snafu(5));
assert_eq!("1=11-2", dec2snafu(2022));
assert_eq!("1-0---0", dec2snafu(12345));
assert_eq!("1121-1110-1=0", dec2snafu(314159265));
// test input
assert_eq!("2=-1=0", solve1(DATA));
}
}

View file

@ -22,3 +22,4 @@ pub mod day21;
pub mod day22;
pub mod day23;
pub mod day24;
pub mod day25;

View file

@ -13,7 +13,7 @@ pub fn read_lines(source: Source) -> Vec<String> {
let path = match source {
Source::Scratch => "inputs/scratch.txt".to_string(),
Source::Day(day) => {
assert!(1 <= day && day < 25);
assert!(1 <= day && day <= 25);
format!("inputs/day{:0>2}.txt", day)
}
};