Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2017/day02-corruption_checksum/Cargo.toml
Normal file
8
2017/day02-corruption_checksum/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day02-corruption_checksum"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
57
2017/day02-corruption_checksum/challenge.txt
Normal file
57
2017/day02-corruption_checksum/challenge.txt
Normal file
|
@ -0,0 +1,57 @@
|
|||
\--- Day 2: Corruption Checksum ---
|
||||
----------
|
||||
|
||||
As you walk through the door, a glowing humanoid shape yells in your direction. "You there! Your state appears to be idle. Come help us repair the corruption in this spreadsheet - if we take another millisecond, we'll have to display an hourglass cursor!"
|
||||
|
||||
The spreadsheet consists of rows of apparently-random numbers. To make sure the recovery process is on the right track, they need you to calculate the spreadsheet's *checksum*. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.
|
||||
|
||||
For example, given the following spreadsheet:
|
||||
|
||||
```
|
||||
5 1 9 5
|
||||
7 5 3
|
||||
2 4 6 8
|
||||
```
|
||||
|
||||
* The first row's largest and smallest values are `9` and `1`, and their difference is `8`.
|
||||
* The second row's largest and smallest values are `7` and `3`, and their difference is `4`.
|
||||
* The third row's difference is `6`.
|
||||
|
||||
In this example, the spreadsheet's checksum would be `8 + 4 + 6 = 18`.
|
||||
|
||||
*What is the checksum* for the spreadsheet in your puzzle input?
|
||||
|
||||
Your puzzle answer was `45972`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
"Great work; looks like we're on the right track after all. Here's a *star* for your effort." However, the program seems a little worried. Can programs *be* worried?
|
||||
|
||||
"Based on what we're seeing, it looks like all the User wanted is some information about the *evenly divisible values* in the spreadsheet. Unfortunately, none of us are equipped for that kind of calculation - most of us specialize in bitwise operations."
|
||||
|
||||
It sounds like the goal is to find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line's result.
|
||||
|
||||
For example, given the following spreadsheet:
|
||||
|
||||
```
|
||||
5 9 2 8
|
||||
9 4 7 3
|
||||
3 8 6 5
|
||||
```
|
||||
|
||||
* In the first row, the only two numbers that evenly divide are `8` and `2`; the result of this division is `4`.
|
||||
* In the second row, the two numbers are `9` and `3`; the result is `3`.
|
||||
* In the third row, the result is `2`.
|
||||
|
||||
In this example, the sum of the results would be `4 + 3 + 2 = 9`.
|
||||
|
||||
What is the *sum of each row's result* in your puzzle input?
|
||||
|
||||
Your puzzle answer was `326`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, all that is left is for you to [admire your Advent calendar](/2017).
|
||||
|
||||
If you still want to see it, you can [get your puzzle input](2/input).
|
44
2017/day02-corruption_checksum/src/lib.rs
Normal file
44
2017/day02-corruption_checksum/src/lib.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
pub fn run(input: &str) -> (usize, usize) {
|
||||
let table: Vec<Vec<usize>> = input.lines().map(|line| line.split(' ').map(|i| i.parse::<usize>().unwrap()).collect()).collect();
|
||||
(sum_of_line_spans(&table), sum_of_divisible(&table))
|
||||
}
|
||||
|
||||
fn sum_of_divisible(table: &[Vec<usize>]) -> usize {
|
||||
table.iter()
|
||||
.map(|row| (0..row.len())
|
||||
.map(|l| (0..row.len())
|
||||
.filter(|&r| l != r && row[l] % row[r] == 0)
|
||||
.map(|r| row[l] / row[r])
|
||||
.max().unwrap_or(0)
|
||||
).max().unwrap()
|
||||
)
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn sum_of_line_spans(table: &[Vec<usize>]) -> usize {
|
||||
table.iter()
|
||||
.map(|row| row.iter().max().unwrap() - row.iter().min().unwrap())
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
fn read_file(name: &str) -> String {
|
||||
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(run(&sample_input), (18, 9));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input), (45972, 326));
|
||||
}
|
||||
}
|
16
2017/day02-corruption_checksum/tests/challenge_input
Normal file
16
2017/day02-corruption_checksum/tests/challenge_input
Normal file
|
@ -0,0 +1,16 @@
|
|||
515 912 619 2043 96 93 2242 1385 2110 860 2255 621 1480 118 1230 99
|
||||
161 6142 142 1742 237 6969 211 4314 5410 4413 3216 6330 261 3929 5552 109
|
||||
1956 4470 3577 619 105 3996 128 1666 720 4052 108 132 2652 306 1892 1869
|
||||
2163 99 2257 895 112 1771 1366 1631 2064 2146 103 865 123 1907 2362 876
|
||||
1955 3260 1539 764 185 5493 5365 5483 4973 175 207 1538 4824 205 1784 2503
|
||||
181 3328 2274 3798 1289 2772 4037 851 1722 3792 175 603 725 158 2937 174
|
||||
405 247 2083 956 725 258 2044 206 2054 561 2223 2003 2500 355 306 2248
|
||||
837 937 225 1115 446 451 160 1219 56 61 62 922 58 1228 1217 1302
|
||||
1371 1062 2267 111 135 2113 1503 2130 1995 2191 129 2494 2220 739 138 1907
|
||||
3892 148 2944 371 135 1525 3201 3506 3930 3207 115 3700 2791 597 3314 132
|
||||
259 162 186 281 210 180 184 93 135 208 88 178 96 25 103 161
|
||||
1080 247 1036 936 108 971 908 1035 123 974 103 1064 129 1189 1089 938
|
||||
148 1874 122 702 922 2271 123 111 454 1872 2142 2378 126 813 1865 1506
|
||||
842 267 230 1665 2274 236 262 1714 3281 4804 4404 3833 661 4248 3893 1105
|
||||
1112 1260 809 72 1104 156 104 1253 793 462 608 84 99 1174 449 929
|
||||
707 668 1778 1687 2073 1892 62 1139 908 78 1885 800 945 712 57 65
|
3
2017/day02-corruption_checksum/tests/sample_input
Normal file
3
2017/day02-corruption_checksum/tests/sample_input
Normal file
|
@ -0,0 +1,3 @@
|
|||
5 9 2 8
|
||||
9 4 7 3
|
||||
3 8 6 5
|
Loading…
Add table
Add a link
Reference in a new issue