Compare commits
2 commits
4dcc750416
...
6fdf2e641d
Author | SHA1 | Date | |
---|---|---|---|
6fdf2e641d | |||
315b7c2d5e |
3 changed files with 124 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
*.in
|
||||
*.ex*
|
||||
day??
|
||||
|
|
42
day04.rs
Normal file
42
day04.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use std::collections::HashSet;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
fn main() {
|
||||
// Part 1
|
||||
let mut part1 = 0;
|
||||
let cards: Vec<usize> = io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let line = line.unwrap();
|
||||
let (_, card) = line.split_once(": ").unwrap();
|
||||
let (winning, owned) = card.split_once(" | ").unwrap();
|
||||
|
||||
let winning: HashSet<i32> = winning
|
||||
.split_whitespace()
|
||||
.flat_map(str::parse)
|
||||
.collect();
|
||||
|
||||
let owned: HashSet<i32> = owned
|
||||
.split_whitespace()
|
||||
.flat_map(str::parse)
|
||||
.collect();
|
||||
|
||||
let count = winning.intersection(&owned).count();
|
||||
|
||||
part1 += 1 << (count - 1);
|
||||
count
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Part 2
|
||||
let mut dp = vec![1; cards.len()];
|
||||
let part2: i32 = (0..cards.len())
|
||||
.map(|i| {
|
||||
dp[i] += dp[i - cards[cards.len() - i - 1]..i].iter().sum::<i32>();
|
||||
dp[i]
|
||||
})
|
||||
.sum();
|
||||
|
||||
println!("{} {}", part1, part2);
|
||||
}
|
81
day05.py
Normal file
81
day05.py
Normal file
|
@ -0,0 +1,81 @@
|
|||
import fileinput
|
||||
import sys
|
||||
|
||||
input = fileinput.input()
|
||||
|
||||
seeds = [int(seed) for seed in next(input).split()[1:]]
|
||||
next(input)
|
||||
next(input)
|
||||
|
||||
|
||||
def parse_map(input):
|
||||
try:
|
||||
while line := next(input).strip():
|
||||
yield tuple(int(n) for n in line.strip().split())
|
||||
next(input)
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
yield (0, 0, sys.maxsize)
|
||||
|
||||
|
||||
seed_to_soil = list(parse_map(input))
|
||||
soil_to_fert = list(parse_map(input))
|
||||
fert_to_water = list(parse_map(input))
|
||||
water_to_light = list(parse_map(input))
|
||||
light_to_temp = list(parse_map(input))
|
||||
temp_to_humid = list(parse_map(input))
|
||||
humid_to_loc = list(parse_map(input))
|
||||
|
||||
|
||||
def transform_ranges(map, ranges):
|
||||
for start, length in ranges:
|
||||
# find the start of the range
|
||||
(dst_start, src_start, len) = next((dst, src, len) for (dst, src, len) in map if src <= start < (src + len))
|
||||
|
||||
end = start + length
|
||||
src_end = src_start + len
|
||||
|
||||
# yield the starting range
|
||||
yield (dst_start + start - src_start, min(src_end, end) - start)
|
||||
|
||||
# if range is not complete, recurse into the rest
|
||||
if end > src_end:
|
||||
yield from transform_ranges(map, [(src_end, end - src_end)])
|
||||
|
||||
|
||||
def part1():
|
||||
result = sys.maxsize
|
||||
|
||||
for seed in seeds:
|
||||
soil = next(dst + seed - src for (dst, src, len) in seed_to_soil if src <= seed < (src + len))
|
||||
fert = next(dst + soil - src for (dst, src, len) in soil_to_fert if src <= soil < (src + len))
|
||||
water = next(dst + fert - src for (dst, src, len) in fert_to_water if src <= fert < (src + len))
|
||||
light = next(dst + water - src for (dst, src, len) in water_to_light if src <= water < (src + len))
|
||||
temp = next(dst + light - src for (dst, src, len) in light_to_temp if src <= light < (src + len))
|
||||
humid = next(dst + temp - src for (dst, src, len) in temp_to_humid if src <= temp < (src + len))
|
||||
loc = next(dst + humid - src for (dst, src, len) in humid_to_loc if src <= humid < (src + len))
|
||||
result = min(result, loc)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def part2():
|
||||
result = sys.maxsize
|
||||
|
||||
for start, len in zip(*(iter(seeds),) * 2):
|
||||
soils = transform_ranges(seed_to_soil, [(start, len)])
|
||||
ferts = transform_ranges(soil_to_fert, soils)
|
||||
waters = transform_ranges(fert_to_water, ferts)
|
||||
lights = transform_ranges(water_to_light, waters)
|
||||
temps = transform_ranges(light_to_temp, lights)
|
||||
humids = transform_ranges(temp_to_humid, temps)
|
||||
locs = transform_ranges(humid_to_loc, humids)
|
||||
|
||||
(loc, _) = min(locs)
|
||||
result = min(result, loc)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
print(part1(), part2())
|
Loading…
Reference in a new issue