Skip to content

Commit

Permalink
fix 32-bit
Browse files Browse the repository at this point in the history
  • Loading branch information
rene-d committed Feb 6, 2024
1 parent 94d2f33 commit 8a080a5
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 82 deletions.
16 changes: 8 additions & 8 deletions 2016/day9/day9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ fn main() {
}

/// Do part 1 of the puzzle
fn part1(data: &str) -> usize {
data.split('\n').fold(0, |acc, line| acc + expand_v1(line).len())
fn part1(data: &str) -> u64 {
data.split('\n').fold(0, |acc, line| acc + expand_v1(line).len() as u64)
}

/// Do part 2 of the puzzle
fn part2(data: &str) -> usize {
fn part2(data: &str) -> u64 {
data.split('\n').fold(0, |acc, line| acc + expand_v2(line))
}

Expand Down Expand Up @@ -53,12 +53,12 @@ fn expand_v1(s: &str) -> String {
}

/// ``expand_v2`` returns the length of expanded string according to the format v2.
fn expand_v2(s: &str) -> usize {
fn expand_v2(s: &str) -> u64 {
expand(s, 2)
}

/// ``expand`` returns the length of the expanded string (v1 or v2).
fn expand(s: &str, version: u8) -> usize {
fn expand(s: &str, version: u8) -> u64 {
let mut new_len = 0;
let mut chars = s.chars();

Expand All @@ -76,13 +76,13 @@ fn expand(s: &str, version: u8) -> usize {
.by_ref()
.take_while(|c| *c != ')')
.collect::<String>()
.parse::<usize>()
.parse::<u64>()
.unwrap();

let taken = chars.by_ref().take(take);

let count = if version == 1 {
taken.count()
taken.count() as u64
} else {
expand(taken.collect::<String>().as_str(), version)
};
Expand All @@ -101,7 +101,7 @@ fn expand(s: &str, version: u8) -> usize {
fn test_expand_v1() {
fn test_v1(s: &str, expected: &str) {
assert_eq!(expand_v1(s), expected);
assert_eq!(expand(s, 1), expected.len());
assert_eq!(expand(s, 1), expected.len() as u64);
}
test_v1("ADVENT", "ADVENT");
test_v1("A(1x5)BC", "ABBBBBC");
Expand Down
10 changes: 5 additions & 5 deletions 2020/day23/day23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Puzzle {
.collect();
}

fn solve(&self, nb_cups: usize, nb_moves: usize) -> usize {
fn solve(&self, nb_cups: usize, nb_moves: usize) -> u64 {
// cups are labeled from 1 to 9 (and from 10 to ncups), cup no. 0 is *unused*

let cups = |i: usize| {
Expand Down Expand Up @@ -68,26 +68,26 @@ impl Puzzle {

if nb_cups > self.cups.len() {
// answer for part 2
(pos[pos[1]]) * (pos[1])
(pos[pos[1]] as u64) * (pos[1] as u64)
} else {
// answer for part 1
let mut result = 0;
let mut cup = pos[1];
while cup != 1 {
result = result * 10 + cup;
result = result * 10 + cup as u64;
cup = pos[cup];
}
result
}
}

/// Solve part one.
fn part1(&self) -> usize {
fn part1(&self) -> u64 {
self.solve(self.cups.len(), 100)
}

/// Solve part two.
fn part2(&self) -> usize {
fn part2(&self) -> u64 {
self.solve(1_000_000, 10_000_000)
}
}
Expand Down
2 changes: 1 addition & 1 deletion 2022/day11/day11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void solve(const std::vector<Monkey> &monkeys_orig, int rounds)
{
return a.inspections > b.inspections;
});
uint64_t monkey_business_level = monkeys[0].inspections * monkeys[1].inspections;
uint64_t monkey_business_level = (uint64_t)monkeys[0].inspections * (uint64_t)monkeys[1].inspections;

std::cout << monkey_business_level << std::endl;
}
Expand Down
8 changes: 4 additions & 4 deletions 2022/day11/day11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ impl Puzzle {
}

/// Solves part one
fn part1(&self) -> usize {
fn part1(&self) -> u64 {
self.solve(20)
}

/// Solve part two
fn part2(&self) -> usize {
fn part2(&self) -> u64 {
self.solve(10000)
}

/// Solve the puzzle
fn solve(&self, rounds: usize) -> usize {
fn solve(&self, rounds: usize) -> u64 {
// we need to clone the monkeys array since we modify the starting item lists
let mut monkeys = self.monkeys.clone();

Expand Down Expand Up @@ -161,7 +161,7 @@ impl Puzzle {
.map(|x| x.inspections)
.collect::<Vec<usize>>();
monkey_business.sort_by(|a, b| b.cmp(a));
monkey_business[0] * monkey_business[1]
(monkey_business[0] as u64) * (monkey_business[1] as u64)
}
}

Expand Down
6 changes: 3 additions & 3 deletions 2022/day15/day15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Puzzle {
}

// Solves part one
fn part1(&self) -> usize {
fn part1(&self) -> u32 {
let bx_min = self.beacons.iter().map(|x| x.0).min().unwrap() - self.max_d;
let bx_max = self.beacons.iter().map(|x| x.0).max().unwrap() + self.max_d;

Expand Down Expand Up @@ -90,7 +90,7 @@ impl Puzzle {
}

// Solve part two
fn part2(&self) -> usize {
fn part2(&self) -> i64 {
for y in 0..=self.field_size {
// each sensor defines a zone where there is only one beacon
// this zone is all points at a distance less than or equal to the Manhattan distance to its beacon
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Puzzle {

if column.len() > 1 {
let x = column.first().unwrap().1;
return (x * 4_000_000 + y) as usize;
return x * 4_000_000 + y;
}
}

Expand Down
24 changes: 12 additions & 12 deletions 2022/day20/day20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,37 @@ impl Puzzle {
fn decrypt(&self, key: i64, rounds: usize) -> i64 {
let mut q = VecDeque::new();

q.extend(self.numbers.iter().map(|x| (*x) * key).enumerate());
q.extend(self.numbers.iter().map(|x| (*x) * key).zip(0..));

let nb = self.numbers.len();
let nb = self.numbers.len() as i64;

for _ in 0..rounds {
for i in 0..nb {
let mut shift = (0usize, 0i64);
let mut shift = (0, 0);

while let Some(e) = q.pop_front() {
if e.0 == i {
if e.1 == i {
shift = e;
break;
}
q.push_back(e);
}

match shift.1 {
o if o > 0 => q.rotate_left((o as usize) % (nb - 1)),
o if o < 0 => q.rotate_right((-o as usize) % (nb - 1)),
match shift.0 {
o if o > 0 => q.rotate_left(((o) % (nb - 1)) as usize),
o if o < 0 => q.rotate_right(((-o) % (nb - 1)) as usize),
_ => (),
}

q.push_back(shift);
}
}

for (i, v) in q.iter().enumerate() {
if v.1 == 0 {
return q.get((i + 1000) % nb).unwrap().1
+ q.get((i + 2000) % nb).unwrap().1
+ q.get((i + 3000) % nb).unwrap().1;
for (v, i) in q.iter().zip(0..) {
if v.0 == 0 {
return q.get(((i + 1000) % nb) as usize).unwrap().0
+ q.get(((i + 2000) % nb) as usize).unwrap().0
+ q.get(((i + 3000) % nb) as usize).unwrap().0;
}
}
0
Expand Down
22 changes: 11 additions & 11 deletions 2023/day11/day11.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! [Day 11: Cosmic Expansion](https://adventofcode.com/2023/day/11)
struct Puzzle {
galaxies: Vec<(usize, usize)>,
empty_rows: Vec<usize>,
empty_cols: Vec<usize>,
galaxies: Vec<(u64, u64)>,
empty_rows: Vec<u64>,
empty_cols: Vec<u64>,
}

impl Puzzle {
Expand All @@ -21,9 +21,9 @@ impl Puzzle {

let mut grid = vec![];

for (y, line) in data.lines().enumerate() {
for (line, y) in data.lines().zip(0..) {
let row: Vec<_> = line.chars().collect();
for (x, &c) in row.iter().enumerate() {
for (&c, x) in row.iter().zip(0..) {
if c == '#' {
self.galaxies.push((x, y));
}
Expand All @@ -38,12 +38,12 @@ impl Puzzle {

for x in 0..grid[0].len() {
if (0..grid.len()).all(|y| grid[y][x] == '.') {
self.empty_cols.push(x);
self.empty_cols.push(x as u64);
}
}
}

fn solve(&self, expansion_factor: usize) -> usize {
fn solve(&self, expansion_factor: u64) -> u64 {
let expansion_factor = expansion_factor - 1;
let mut result = 0;
for (i, &(x1, y1)) in self.galaxies.iter().enumerate() {
Expand All @@ -55,15 +55,15 @@ impl Puzzle {
.empty_cols
.iter()
.filter(|&&col| x1.min(x2) <= col && col <= x1.max(x2))
.count()
.count() as u64
* expansion_factor;

// expand empty spaces vertically
distance += self
.empty_rows
.iter()
.filter(|&&row| y1.min(y2) <= row && row <= y1.max(y2))
.count()
.count() as u64
* expansion_factor;

result += distance;
Expand All @@ -74,12 +74,12 @@ impl Puzzle {
}

/// Solve part one.
fn part1(&self) -> usize {
fn part1(&self) -> u64 {
self.solve(2)
}

/// Solve part two.
fn part2(&self) -> usize {
fn part2(&self) -> u64 {
self.solve(1_000_000)
}
}
Expand Down
12 changes: 6 additions & 6 deletions 2023/day21/day21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Puzzle {
self.garden[usize::try_from(i).unwrap()]
}

fn count(&self, n: i32) -> usize {
fn count(&self, n: i32) -> u64 {
// nota: still not really optimized, could probably memoize something
let mut p = HashSet::new();
p.insert((self.start_x, self.start_y));
Expand Down Expand Up @@ -116,10 +116,10 @@ impl Puzzle {
p = np;
}

p.len()
p.len() as u64
}

fn big_count(&self, n: i32) -> usize {
fn big_count(&self, n: i32) -> u64 {
// the step count curve is parabolic
let (t, x0) = n.div_rem(&self.n);

Expand All @@ -138,18 +138,18 @@ impl Puzzle {
let a = y2 - 2 * y1 + y0;
let b = y1 - y0;

let t = usize::try_from(t).unwrap();
let t = u64::try_from(t).unwrap();

a * t * (t - 1) / 2 + b * t + y0
}

/// Solve part one.
fn part1(&self) -> usize {
fn part1(&self) -> u64 {
self.count(64)
}

/// Solve part two.
fn part2(&self) -> usize {
fn part2(&self) -> u64 {
self.big_count(26_501_365)
}
}
Expand Down
12 changes: 6 additions & 6 deletions 2023/day8/day8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use num::Integer;
use std::collections::HashMap;

fn lcm(values: &Vec<usize>) -> usize {
fn lcm(values: &Vec<u64>) -> u64 {
let mut m = 1;
for x in values {
m = m.lcm(x);
Expand Down Expand Up @@ -44,7 +44,7 @@ impl Puzzle {
}
}

fn solve(&self, part1: bool) -> usize {
fn solve(&self, part1: bool) -> u64 {
let start = u32::from_str_radix(if part1 { "AAA" } else { "A" }, 36).unwrap();
let stop = u32::from_str_radix(if part1 { "ZZZ" } else { "Z" }, 36).unwrap();
let mask = if part1 { 36 * 36 * 36 } else { 36 };
Expand Down Expand Up @@ -81,10 +81,10 @@ impl Puzzle {
*node = new_node;

if new_node % mask == stop {
z.insert(i, n);
z.insert(i, n as u64);

if z.len() == size {
let z = z.values().copied().collect::<Vec<usize>>();
let z: Vec<_> = z.values().copied().collect();
return lcm(&z);
}
}
Expand All @@ -93,12 +93,12 @@ impl Puzzle {
}

/// Solve part one.
fn part1(&self) -> usize {
fn part1(&self) -> u64 {
self.solve(true)
}

/// Solve part two.
fn part2(&self) -> usize {
fn part2(&self) -> u64 {
self.solve(false)
}
}
Expand Down
Loading

0 comments on commit 8a080a5

Please sign in to comment.