diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2025-12-06 20:34:18 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2025-12-06 20:40:12 +0100 |
| commit | d65dda9e5b818bc1bae6aaf6453145e08f00e5b7 (patch) | |
| tree | 04223988fe7524191be311e8eaf852fea072038e | |
Day01 - Day06
| -rw-r--r-- | day01/.gitignore | 3 | ||||
| -rw-r--r-- | day01/Cargo.lock | 7 | ||||
| -rw-r--r-- | day01/Cargo.toml | 4 | ||||
| -rw-r--r-- | day01/src/main.rs | 80 | ||||
| -rw-r--r-- | day02/.gitignore | 3 | ||||
| -rw-r--r-- | day02/Cargo.lock | 7 | ||||
| -rw-r--r-- | day02/Cargo.toml | 6 | ||||
| -rw-r--r-- | day02/src/main.rs | 70 | ||||
| -rw-r--r-- | day03/.gitignore | 3 | ||||
| -rw-r--r-- | day03/Cargo.lock | 7 | ||||
| -rw-r--r-- | day03/Cargo.toml | 6 | ||||
| -rw-r--r-- | day03/src/main.rs | 38 | ||||
| -rw-r--r-- | day04/.gitignore | 3 | ||||
| -rw-r--r-- | day04/Cargo.lock | 7 | ||||
| -rw-r--r-- | day04/Cargo.toml | 6 | ||||
| -rw-r--r-- | day04/src/main.rs | 96 | ||||
| -rw-r--r-- | day05/.gitignore | 3 | ||||
| -rw-r--r-- | day05/Cargo.lock | 7 | ||||
| -rw-r--r-- | day05/Cargo.toml | 6 | ||||
| -rw-r--r-- | day05/src/main.rs | 123 | ||||
| -rw-r--r-- | day06/.gitignore | 3 | ||||
| -rw-r--r-- | day06/Cargo.lock | 7 | ||||
| -rw-r--r-- | day06/Cargo.toml | 6 | ||||
| -rw-r--r-- | day06/src/main.rs | 103 |
24 files changed, 604 insertions, 0 deletions
diff --git a/day01/.gitignore b/day01/.gitignore new file mode 100644 index 0000000..9cd70c0 --- /dev/null +++ b/day01/.gitignore @@ -0,0 +1,3 @@ +/target +/input.txt +/example*.txt
\ No newline at end of file diff --git a/day01/Cargo.lock b/day01/Cargo.lock new file mode 100644 index 0000000..2a24086 --- /dev/null +++ b/day01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day01" +version = "0.1.0" diff --git a/day01/Cargo.toml b/day01/Cargo.toml new file mode 100644 index 0000000..43c943c --- /dev/null +++ b/day01/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "day01" +version = "0.1.0" +edition = "2024" diff --git a/day01/src/main.rs b/day01/src/main.rs new file mode 100644 index 0000000..f391c2e --- /dev/null +++ b/day01/src/main.rs @@ -0,0 +1,80 @@ +use std::io; + +fn rotate(dial: i32, change: i32, all_zeros: &mut i32) -> i32 { + /* + let mut ret = dial + change; + if ret == 0 { + *all_zeros += 1; + } + while ret < 0 { + ret += 100; + if dial > 0 { + *all_zeros += 1; + } + } + while ret > 99 { + ret -= 100; + if dial > 0 { + *all_zeros += 1; + } + } + return ret; + */ + let mut ret = dial; + let mut change = change; + loop { + if change < 0 { + ret -= 1; + change += 1; + if ret == -1 { + ret = 99; + } + } else { + ret += 1; + change -= 1; + if ret == 100 { + ret = 0; + } + } + if ret == 0 { + *all_zeros += 1; + } + if change == 0 { + break; + } + } + ret +} + +fn main() { + let mut dial = 50; + let mut zeros = 0; + let mut all_zeros = 0; + + loop { + let mut buffer = String::new(); + let bytes = io::stdin().read_line(&mut buffer).unwrap(); + if bytes == 0 { + break; + } + let (dir, value_str) = buffer.trim_end().split_at(1); + let left = match dir { + "L" => true, + "R" => false, + _ => panic!(), + }; + let mut value = value_str.parse::<i32>().unwrap(); + if left { + value = -value; + } + if value != 0 { + dial = rotate(dial, value, &mut all_zeros); + } + if dial == 0 { + zeros += 1; + } + } + + println!("Zeros {}", zeros); + println!("All zeros {}", all_zeros); +} diff --git a/day02/.gitignore b/day02/.gitignore new file mode 100644 index 0000000..9cd70c0 --- /dev/null +++ b/day02/.gitignore @@ -0,0 +1,3 @@ +/target +/input.txt +/example*.txt
\ No newline at end of file diff --git a/day02/Cargo.lock b/day02/Cargo.lock new file mode 100644 index 0000000..2762917 --- /dev/null +++ b/day02/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day02" +version = "0.1.0" diff --git a/day02/Cargo.toml b/day02/Cargo.toml new file mode 100644 index 0000000..3304419 --- /dev/null +++ b/day02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day02" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day02/src/main.rs b/day02/src/main.rs new file mode 100644 index 0000000..ce21752 --- /dev/null +++ b/day02/src/main.rs @@ -0,0 +1,70 @@ +use std::io; + +struct Range { + start: i64, + end: i64, +} + +impl Range { + fn new<'a>(mut iter: impl DoubleEndedIterator<Item = &'a str>) -> Self { + let start = iter.next().unwrap().parse::<i64>().unwrap(); + let end = iter.next().unwrap().parse::<i64>().unwrap(); + Self { start, end } + } +} + +fn is_invalid(value: i64) -> bool { + let tmp = value.to_string(); + /* + if tmp.len() % 2 != 0 { + return false + } + let (a, b) = tmp.split_at(tmp.len() / 2); + return a == b; + */ + for x in 1..=tmp.len() / 2 { + if tmp.len() % x != 0 { + continue; + } + let (first, rest) = tmp.split_at(x); + let mut rest2 = rest; + let mut all_match = true; + for _ in 0..tmp.len() / x - 1 { + match rest2.strip_prefix(first) { + Some(value) => rest2 = value, + None => { + all_match = false; + break; + } + }; + } + if all_match { + return true; + } + } + false +} + +fn invalid(range: &Range) -> i64 { + let mut total = 0; + for i in range.start..=range.end { + if is_invalid(i) { + total += i; + } + } + total +} + +fn main() { + let mut buffer = String::new(); + io::stdin().read_line(&mut buffer).unwrap(); + let mut total = 0; + for range in buffer + .trim_end() + .split(',') + .map(|x| Range::new(x.split('-'))) + { + total += invalid(&range); + } + println!("Invalid: {}", total); +} diff --git a/day03/.gitignore b/day03/.gitignore new file mode 100644 index 0000000..9cd70c0 --- /dev/null +++ b/day03/.gitignore @@ -0,0 +1,3 @@ +/target +/input.txt +/example*.txt
\ No newline at end of file diff --git a/day03/Cargo.lock b/day03/Cargo.lock new file mode 100644 index 0000000..3ea7b86 --- /dev/null +++ b/day03/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day03" +version = "0.1.0" diff --git a/day03/Cargo.toml b/day03/Cargo.toml new file mode 100644 index 0000000..b98f31f --- /dev/null +++ b/day03/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day03" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day03/src/main.rs b/day03/src/main.rs new file mode 100644 index 0000000..6d86727 --- /dev/null +++ b/day03/src/main.rs @@ -0,0 +1,38 @@ +use std::io; + +fn highest_jolt(batteries: &str) -> i64 { + let batteries: Vec<char> = batteries.chars().collect(); + let mut indexes = Vec::new(); + let mut last = 0; + for x in 0..12 { + let mut index = last; + for i in last + 1..batteries.len() - 11 + x { + if batteries[i] > batteries[index] { + index = i; + } + } + indexes.push(index); + last = index + 1; + } + let mut tmp = String::new(); + for i in indexes { + tmp.push(batteries[i]); + } + tmp.parse().unwrap() +} + +fn main() { + let mut total = 0; + + loop { + let mut buffer = String::new(); + let bytes = io::stdin().read_line(&mut buffer).unwrap(); + if bytes == 0 { + break; + } + let jolt = highest_jolt(buffer.trim_end()); + total += jolt; + } + + println!("Total: {}", total); +} diff --git a/day04/.gitignore b/day04/.gitignore new file mode 100644 index 0000000..9cd70c0 --- /dev/null +++ b/day04/.gitignore @@ -0,0 +1,3 @@ +/target +/input.txt +/example*.txt
\ No newline at end of file diff --git a/day04/Cargo.lock b/day04/Cargo.lock new file mode 100644 index 0000000..de1e82f --- /dev/null +++ b/day04/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day04" +version = "0.1.0" diff --git a/day04/Cargo.toml b/day04/Cargo.toml new file mode 100644 index 0000000..a3cc735 --- /dev/null +++ b/day04/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day04" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day04/src/main.rs b/day04/src/main.rs new file mode 100644 index 0000000..0205021 --- /dev/null +++ b/day04/src/main.rs @@ -0,0 +1,96 @@ +use std::io; + +fn valid(map: &[Vec<char>], x: i32, y: i32) -> bool { + if x < 0 { + return false; + } + if y < 0 { + return false; + } + let uy: usize = y.try_into().unwrap(); + if uy >= map.len() { + return false; + } + let ux: usize = x.try_into().unwrap(); + if ux >= map[uy].len() { + return false; + } + true +} + +fn is_paper(map: &[Vec<char>], x: i32, y: i32) -> bool { + if valid(map, x, y) { + let uy: usize = y.try_into().unwrap(); + let ux: usize = x.try_into().unwrap(); + return map[uy][ux] == '@'; + } + false +} + +fn adjacent(map: &[Vec<char>], x: i32, y: i32) -> usize { + let mut count = 0; + for dx in -1..=1 { + if is_paper(map, x + dx, y - 1) { + count += 1; + } + if is_paper(map, x + dx, y + 1) { + count += 1; + } + } + if is_paper(map, x - 1, y) { + count += 1; + } + if is_paper(map, x + 1, y) { + count += 1; + } + count +} + +fn main() { + let mut map: Vec<Vec<char>> = Vec::new(); + + loop { + let mut buffer = String::new(); + let bytes = io::stdin().read_line(&mut buffer).unwrap(); + if bytes == 0 { + break; + } + map.push(buffer.trim_end().chars().collect()); + } + + let mut total = 0; + for y in 0..map.len() { + for x in 0..map[y].len() { + if is_paper(&map, x.try_into().unwrap(), y.try_into().unwrap()) + && adjacent(&map, x.try_into().unwrap(), y.try_into().unwrap()) < 4 + { + total += 1; + } + } + } + + println!("{}", total); + + let mut total2 = 0; + loop { + total = 0; + let mut next = map.clone(); + for y in 0..map.len() { + for x in 0..map[y].len() { + if is_paper(&map, x.try_into().unwrap(), y.try_into().unwrap()) + && adjacent(&map, x.try_into().unwrap(), y.try_into().unwrap()) < 4 + { + total += 1; + next[y][x] = 'x'; + } + } + } + if total == 0 { + break; + } + map = next; + total2 += total; + } + + println!("{}", total2); +} diff --git a/day05/.gitignore b/day05/.gitignore new file mode 100644 index 0000000..9cd70c0 --- /dev/null +++ b/day05/.gitignore @@ -0,0 +1,3 @@ +/target +/input.txt +/example*.txt
\ No newline at end of file diff --git a/day05/Cargo.lock b/day05/Cargo.lock new file mode 100644 index 0000000..83aa796 --- /dev/null +++ b/day05/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day05" +version = "0.1.0" diff --git a/day05/Cargo.toml b/day05/Cargo.toml new file mode 100644 index 0000000..4dee21c --- /dev/null +++ b/day05/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day05" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day05/src/main.rs b/day05/src/main.rs new file mode 100644 index 0000000..ce90869 --- /dev/null +++ b/day05/src/main.rs @@ -0,0 +1,123 @@ +use std::io; + +#[derive(Clone)] +struct Range { + first: i64, + last: i64, +} + +impl Range { + fn new(first: i64, last: i64) -> Self { + assert!(first <= last); + Self { first, last } + } + + fn contains(&self, value: i64) -> bool { + value >= self.first && value <= self.last + } + + fn len(&self) -> i64 { + 1 + self.last - self.first + } +} + +fn in_ranges(ranges: &Vec<Range>, value: i64) -> bool { + for range in ranges { + if range.contains(value) { + return true; + } + } + false +} + +fn merge_or_add(ranges: &mut Vec<Range>, range: &Range) { + if range.len() == 0 { + return; + } + let i = ranges.partition_point(|x| x.last < range.first); + if i == ranges.len() { + ranges.push(range.clone()); + return; + } + if ranges[i].first > range.last { + ranges.insert(i, range.clone()); + return; + } + if ranges[i].contains(range.first) { + if ranges[i].contains(range.last) { + // Whole range is swallowed + return; + } + merge_or_add(ranges, &Range::new(ranges[i].last + 1, range.last)); + return; + } + if ranges[i].contains(range.last) { + ranges.insert(i, Range::new(range.first, ranges[i].first - 1)); + return; + } + + let before = Range::new(range.first, ranges[i].first - 1); + let after = Range::new(ranges[i].last + 1, range.last); + + ranges.insert(i, before); + merge_or_add(ranges, &after); +} + +fn remove_overlap(ranges: &Vec<Range>) -> Vec<Range> { + let mut ret = Vec::new(); + + for range in ranges { + merge_or_add(&mut ret, range); + } + + ret +} + +fn main() { + let mut ranges = Vec::new(); + + loop { + let mut buffer = String::new(); + let bytes = io::stdin().read_line(&mut buffer).unwrap(); + if bytes == 0 || buffer == "\n" { + break; + } + let mut iter = buffer.trim_end().split('-'); + let first = iter.next().unwrap().parse::<i64>().unwrap(); + let last = iter.next().unwrap().parse::<i64>().unwrap(); + ranges.push(Range::new(first, last)); + } + + ranges.sort_by(|a, b| a.first.cmp(&b.first)); + + let mut available = Vec::new(); + + loop { + let mut buffer = String::new(); + let bytes = io::stdin().read_line(&mut buffer).unwrap(); + if bytes == 0 { + break; + } + available.push(buffer.trim_end().parse::<i64>().unwrap()); + } + + let mut fresh = 0; + + for ingredient in available { + if in_ranges(&ranges, ingredient) { + fresh += 1; + } + } + + println!("Fresh ingrediences {}", fresh); + + let ranges2 = remove_overlap(&ranges); + + let mut fresh2 = 0; + + for range in ranges2 { + fresh2 += range.len(); + } + + println!("Fresh ids {}", fresh2); +} diff --git a/day06/.gitignore b/day06/.gitignore new file mode 100644 index 0000000..9cd70c0 --- /dev/null +++ b/day06/.gitignore @@ -0,0 +1,3 @@ +/target +/input.txt +/example*.txt
\ No newline at end of file diff --git a/day06/Cargo.lock b/day06/Cargo.lock new file mode 100644 index 0000000..5148a23 --- /dev/null +++ b/day06/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day06" +version = "0.1.0" diff --git a/day06/Cargo.toml b/day06/Cargo.toml new file mode 100644 index 0000000..6613f87 --- /dev/null +++ b/day06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day06" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/day06/src/main.rs b/day06/src/main.rs new file mode 100644 index 0000000..e5fd0fa --- /dev/null +++ b/day06/src/main.rs @@ -0,0 +1,103 @@ +use std::io; + +fn main() { + let mut rows = Vec::new(); + let mut ops = Vec::new(); + + /* Part 1 + loop { + let mut buffer = String::new(); + let bytes = io::stdin().read_line(&mut buffer).unwrap(); + if bytes == 0 { + break; + } + let mut iter = buffer.trim_end().split_ascii_whitespace(); + let first = iter.next().unwrap(); + match first.parse::<i64>() { + Ok(number) => { + let mut numbers = Vec::new(); + numbers.push(number); + numbers.extend(iter.map(|x| x.parse::<i64>().unwrap())); + rows.push(numbers); + }, + Err(_) => { + ops.push(first.to_string()); + ops.extend(iter.map(|x| x.to_string())); + } + } + } + */ + + let mut lines = Vec::new(); + + loop { + let mut buffer = String::new(); + let bytes = io::stdin().read_line(&mut buffer).unwrap(); + if bytes == 0 { + break; + } + buffer.pop(); + lines.push(buffer.to_string()); + } + + ops.extend( + lines + .pop() + .unwrap() + .split_ascii_whitespace() + .map(|x| x.to_string()), + ); + + let lines2: Vec<Vec<char>> = lines.iter().map(|x| x.chars().collect()).collect(); + + let mut row = Vec::<i64>::new(); + + for col in (0..lines2[0].len()).rev() { + let mut numbers = Vec::new(); + for line in &lines2 { + if line[col] != ' ' { + numbers.push(line[col].to_digit(10).unwrap()); + } + } + match numbers.pop() { + Some(number) => { + let mut tmp = number; + let mut tmp2 = 10; + while let Some(number) = numbers.pop() { + tmp += number * tmp2; + tmp2 *= 10; + } + row.push(tmp.into()); + } + None => { + rows.insert(0, row); + row = Vec::new(); + } + } + } + + rows.insert(0, row); + + let mut column = Vec::new(); + + for col in 0..ops.len() { + let mut result = 0; + match ops[col].as_str() { + "+" => { + for row in &rows[col] { + result += row; + } + } + "*" => { + result = 1; + for row in &rows[col] { + result *= row; + } + } + &_ => todo!(), + } + column.push(result); + } + + println!("{}", column.iter().sum::<i64>()) +} |
