mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement 2022 day 25
This commit is contained in:
126
2022/inputs/25.txt
Normal file
126
2022/inputs/25.txt
Normal file
@@ -0,0 +1,126 @@
|
||||
2=1
|
||||
2=-02211=
|
||||
2=-1111=2-0-20=
|
||||
2=2-=12
|
||||
1--2-=2211-221
|
||||
1=20011
|
||||
1221=021-0=2011-1
|
||||
1=00=202011=
|
||||
200=121-=2-11=21
|
||||
2-21002021202
|
||||
1-11=12011112
|
||||
1==2-=0-=2-0-0
|
||||
20=-12--12
|
||||
101122=222021221-
|
||||
12=2200-1==1=12=1=1
|
||||
2==-21=-=2
|
||||
1==-1222-1==2-
|
||||
111-2-2=0
|
||||
1=2
|
||||
1=20=-00=22
|
||||
1=2=0-2101=2
|
||||
10=
|
||||
10-=12101-102=1=
|
||||
12=11=-
|
||||
1202=0--0-2==0
|
||||
1-0--
|
||||
10210210-10021=2-2
|
||||
22=
|
||||
12-110101
|
||||
2=-===0=0=11--
|
||||
2=21022-00-
|
||||
2-111102102--201=0
|
||||
2=-2=
|
||||
12=22=11-1000=121
|
||||
11=10-02111211
|
||||
20210121--0=1=-=
|
||||
1=-
|
||||
10=-==02-20-0=01
|
||||
21=0=-=
|
||||
2=10002
|
||||
1-0102-020201121
|
||||
11---01101=--
|
||||
1=-1=-=-011
|
||||
211-00-2==21==
|
||||
21
|
||||
1201-1202=2-0
|
||||
21000=2=1-=10--2
|
||||
1=0=
|
||||
1-1
|
||||
1-2---=1====-0210=-0
|
||||
2-220-=
|
||||
122-
|
||||
110200-0
|
||||
2=--2
|
||||
20112=0-2022-
|
||||
1=-0--
|
||||
1=-01-0-10=2=0===-
|
||||
1=22=2=--11=-0-2111
|
||||
1==00-1=00=0-
|
||||
1=2212-1-=201
|
||||
2-200=2001
|
||||
2222012-10-0=-0=
|
||||
1-====
|
||||
100=2200
|
||||
1-11==0
|
||||
1--2111-1
|
||||
1---2121-202210=2
|
||||
11222=
|
||||
1=2102-2221-111-
|
||||
111
|
||||
1=
|
||||
1--0=-1
|
||||
10=00=10=---1=-
|
||||
1=0==-2
|
||||
220002
|
||||
1==211-0=222102-
|
||||
1-==-121021
|
||||
1202-1=01--202
|
||||
12=-0===11=0
|
||||
20-001=
|
||||
1000-2-==2=-
|
||||
1=01--
|
||||
10=--01221
|
||||
1-=2-=0=-0==10-22-
|
||||
2021212-1020=002
|
||||
11===1==-2-01-21
|
||||
2-00-10-
|
||||
12-0221212000
|
||||
201==
|
||||
20011
|
||||
2---0=1222--02
|
||||
20-
|
||||
10-=02010
|
||||
101--0202=-2=1==01
|
||||
2=0121-02112-0=
|
||||
2-=-=1011
|
||||
11-=02
|
||||
110-2001
|
||||
1==011-12000101-11
|
||||
2=
|
||||
1-11-=--=
|
||||
110
|
||||
101=21
|
||||
1=11
|
||||
212-10020-212111-2
|
||||
102=22
|
||||
1-
|
||||
1-02=22-=1==2
|
||||
11=10-00-=00-00=-
|
||||
220200-=2
|
||||
1-0=----
|
||||
1022=0-2--==--1
|
||||
1-=201-1021
|
||||
2=-1-10=1=-2020=00
|
||||
12201=0102-0110=-
|
||||
20=2
|
||||
2020=-===1==-212
|
||||
1=0-222-2=22=-=
|
||||
1=--12==0100=-21
|
||||
1=1-2
|
||||
1=-220001=1
|
||||
20
|
||||
1=1
|
||||
1=0=-=1=0-=01
|
||||
202102-200
|
||||
2-=2-110012
|
||||
@@ -1,5 +1,71 @@
|
||||
use anyhow::Result;
|
||||
|
||||
pub fn part1(_input: &[u8]) -> Result<String> {
|
||||
anyhow::bail!("not implemented")
|
||||
fn parse_num(num: &[u8]) -> Result<i64> {
|
||||
let mut total = 0;
|
||||
let mut factor = 1;
|
||||
|
||||
for &b in num.iter().rev() {
|
||||
match b {
|
||||
b'0' => (),
|
||||
b'1' => total += factor,
|
||||
b'2' => total += 2 * factor,
|
||||
b'-' => total -= factor,
|
||||
b'=' => total -= 2 * factor,
|
||||
other => anyhow::bail!("Invalid digit {other}"),
|
||||
}
|
||||
|
||||
factor *= 5;
|
||||
}
|
||||
|
||||
Ok(total)
|
||||
}
|
||||
|
||||
fn encode(mut num: i64) -> String {
|
||||
let mut buffer = Vec::new();
|
||||
|
||||
while num > 0 {
|
||||
match num % 5 {
|
||||
0 => buffer.push(b'0'),
|
||||
1 => buffer.push(b'1'),
|
||||
2 => buffer.push(b'2'),
|
||||
3 => {
|
||||
buffer.push(b'=');
|
||||
num += 2
|
||||
}
|
||||
4 => {
|
||||
buffer.push(b'-');
|
||||
num += 1;
|
||||
}
|
||||
_ => unreachable!("math"),
|
||||
}
|
||||
|
||||
num /= 5;
|
||||
}
|
||||
|
||||
// We've built the string right to left, to print we must reverse
|
||||
buffer.reverse();
|
||||
|
||||
// Safe unwrap as we've only pushed valid ascii characters
|
||||
String::from_utf8(buffer).unwrap()
|
||||
}
|
||||
|
||||
pub fn part1(input: &[u8]) -> Result<String> {
|
||||
let total = input
|
||||
.split(|&b| b == b'\n')
|
||||
.map(parse_num)
|
||||
.try_fold(0, |acc, val| val.map(|val| val + acc))?;
|
||||
|
||||
Ok(encode(total))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const SAMPLE: &[u8] = include_bytes!("./samples/25.txt");
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
assert_eq!(part1(SAMPLE).unwrap(), "2=-1=0");
|
||||
}
|
||||
}
|
||||
|
||||
13
2022/src/samples/25.txt
Normal file
13
2022/src/samples/25.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
1=-0-2
|
||||
12111
|
||||
2=0=
|
||||
21
|
||||
2=01
|
||||
111
|
||||
20012
|
||||
112
|
||||
1=-1=
|
||||
1-12
|
||||
12
|
||||
1=
|
||||
122
|
||||
Reference in New Issue
Block a user