Skip to content

Commit 5d31475

Browse files
committed
Add day9 part1
1 parent 97624de commit 5d31475

File tree

1 file changed

+64
-11
lines changed

1 file changed

+64
-11
lines changed

src/day9.rs

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use aoc_runner_derive::{aoc, aoc_generator};
22

3-
#[derive(Debug)]
3+
#[derive(Debug, Clone, Copy)]
44
pub enum Disk {
55
File { id: usize, blocks: usize },
66
Free { blocks: usize },
@@ -28,16 +28,69 @@ pub fn input_generator(input: &str) -> Vec<Disk> {
2828

2929
#[aoc(day9, part1)]
3030
pub fn part1(input: &[Disk]) -> usize {
31-
dbg!(input);
32-
0
33-
}
31+
for i in input {
32+
match i {
33+
Disk::File { id, blocks } => {
34+
print!("{}", id.to_string().repeat(*blocks));
35+
}
36+
Disk::Free { blocks } => {
37+
print!("{}", ".".repeat(*blocks));
38+
}
39+
}
40+
}
41+
println!();
42+
43+
let mut i = 0;
44+
let mut input = input.to_vec();
45+
let mut results: Vec<usize> = Vec::new();
46+
47+
loop {
48+
if i > input.len() {
49+
break;
50+
}
51+
52+
match input[i] {
53+
Disk::File { id, blocks } => results.extend(std::iter::repeat(id).take(blocks)),
54+
Disk::Free {
55+
blocks: free_blocks,
56+
} => loop {
57+
if let Some(last) = input.pop() {
58+
match last {
59+
Disk::Free { blocks } => continue,
60+
Disk::File { id: _, blocks } => {
61+
if blocks == *free_blocks {
62+
results.push(last);
63+
}
64+
}
65+
}
66+
} else {
67+
break;
68+
}
69+
},
70+
}
71+
}
72+
73+
for i in results {
74+
match i {
75+
Disk::File { id, blocks } => {
76+
print!("{}", id.to_string().repeat(blocks));
77+
}
78+
Disk::Free { blocks } => {
79+
print!("{}", ".".repeat(blocks));
80+
}
81+
}
82+
}
83+
println!();
3484

35-
#[aoc(day9, part2)]
36-
pub fn part2(input: &[Disk]) -> usize {
37-
dbg!(input);
3885
0
3986
}
4087

88+
// #[aoc(day9, part2)]
89+
// pub fn part2(input: &[Disk]) -> usize {
90+
// // dbg!(input);
91+
// 0
92+
// }
93+
4194
#[cfg(test)]
4295
mod tests {
4396
use super::*;
@@ -49,8 +102,8 @@ mod tests {
49102
assert_eq!(1, part1(&input_generator(INPUT)))
50103
}
51104

52-
#[test]
53-
fn test_part2() {
54-
assert_eq!(1, part2(&input_generator(INPUT)))
55-
}
105+
// #[test]
106+
// fn test_part2() {
107+
// assert_eq!(1, part2(&input_generator(INPUT)))
108+
// }
56109
}

0 commit comments

Comments
 (0)