1
0

day 6 part 1

This commit is contained in:
2024-12-06 06:20:41 +01:00
parent 8cf59c3725
commit f632977666

View File

@@ -1,4 +1,7 @@
use std::fs::read_to_string; use std::{
collections::{HashMap, HashSet},
fs::read_to_string,
};
pub fn day_main() { pub fn day_main() {
let input = read_to_string("input/day06.txt").unwrap(); let input = read_to_string("input/day06.txt").unwrap();
@@ -7,10 +10,54 @@ pub fn day_main() {
println!(" part2: {}", part2(input)); println!(" part2: {}", part2(input));
} }
type RiddleResult = i64; type RiddleResult = usize;
fn part1(input: &str) -> RiddleResult { fn part1(input: &str) -> RiddleResult {
0 let mut m = HashMap::new();
let mut pos = None;
input.lines().enumerate().for_each(|(y, line)| {
line.char_indices().for_each(|(x, c)| {
let x = x as i32;
let y = y as i32;
if c == '^' {
pos = Some((x, y));
m.insert((x, y), '.');
} else {
m.insert((x, y), c);
}
});
});
let mut result = 0;
let mut pos = pos.unwrap();
let mut dir = '^';
let mut visited = HashSet::new();
while m.contains_key(&pos) {
let (x, y) = pos;
// println!("at {x},{y} {dir}");
visited.insert(pos);
result += 1;
let next = match dir {
'^' => (x, y - 1),
'v' => (x, y + 1),
'<' => (x - 1, y),
'>' => (x + 1, y),
_ => unreachable!(),
};
if let Some('#') = m.get(&next) {
// println!("bump at {x},{y}");
dir = match dir {
'^' => '>',
'v' => '<',
'<' => '^',
'>' => 'v',
_ => unreachable!("asdf"),
};
} else {
pos = next;
}
}
visited.len()
} }
fn part2(_input: &str) -> RiddleResult { fn part2(_input: &str) -> RiddleResult {
@@ -21,11 +68,21 @@ fn part2(_input: &str) -> RiddleResult {
mod test { mod test {
use super::{part1, part2}; use super::{part1, part2};
const TEST_INPUT: &str = r""; const TEST_INPUT: &str = r"....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
";
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(part1(TEST_INPUT), 0); assert_eq!(part1(TEST_INPUT), 41);
} }
#[test] #[test]