advent-of-code

Solutions for Advent of Code.
git clone git://git.amin.space/advent-of-code.git
Log | Files | Refs | LICENSE

day01.zig (2653B)


      1 const std = @import("std");
      2 const dbg = std.debug;
      3 
      4 pub fn main() void {
      5     var fuel_for_module_mass: i32 = 0;
      6     var fuel_for_total_mass: i32 = 0;
      7     for (input) |mass| {
      8         var module_mass_fuel = get_fuel(mass);
      9         var total_fuel = get_total_module_fuel(mass);
     10         fuel_for_module_mass += module_mass_fuel;
     11         fuel_for_total_mass += total_fuel;
     12     }
     13     dbg.warn("01-1 Total fuel for the modules: {}\n", fuel_for_module_mass);
     14     dbg.warn("01-2 Total fuel for the modules and fuel: {}\n", fuel_for_total_mass);
     15 }
     16 
     17 fn get_fuel(mass: i32) i32 {
     18     return @divFloor(mass, 3) - 2;
     19 }
     20 
     21 test "get fuel" {
     22     dbg.assert(get_fuel(12) == 2);
     23     dbg.assert(get_fuel(14) == 2);
     24     dbg.assert(get_fuel(1969) == 654);
     25     dbg.assert(get_fuel(100756) == 33583);
     26 }
     27 
     28 fn get_total_module_fuel(module_mass: i32) i32 {
     29     const module_fuel_mass = get_fuel(module_mass);
     30     var unfueled_fuel = module_fuel_mass;
     31     var fuel_fuel_mass: i32 = 0;
     32     while (unfueled_fuel > 0) {
     33         // "fuel" is a really strange word
     34         var fuel_for_fuel = get_fuel(unfueled_fuel);
     35         if (fuel_for_fuel <= 0) {
     36             break;
     37         }
     38         fuel_fuel_mass += fuel_for_fuel;
     39         unfueled_fuel = fuel_for_fuel;
     40     }
     41     return module_fuel_mass + fuel_fuel_mass;
     42 }
     43 
     44 test "get total module fuel" {
     45     dbg.assert(get_total_module_fuel(14) == 2);
     46     dbg.assert(get_total_module_fuel(1969) == 966);
     47     dbg.assert(get_total_module_fuel(100756) == 50346);
     48 }
     49 
     50 const input = [_]i32{
     51     91617,
     52     134652,
     53     101448,
     54     83076,
     55     53032,
     56     80487,
     57     106061,
     58     103085,
     59     71513,
     60     143874,
     61     102830,
     62     121433,
     63     139937,
     64     104468,
     65     53098,
     66     75999,
     67     113915,
     68     73992,
     69     90028,
     70     64164,
     71     101248,
     72     111333,
     73     89201,
     74     89076,
     75     129360,
     76     81573,
     77     54381,
     78     64105,
     79     104272,
     80     144188,
     81     81022,
     82     125558,
     83     87910,
     84     135654,
     85     110929,
     86     131610,
     87     147160,
     88     139648,
     89     118129,
     90     93967,
     91     123117,
     92     77927,
     93     112034,
     94     84847,
     95     145527,
     96     72652,
     97     123043,
     98     136324,
     99     71228,
    100     118583,
    101     56992,
    102     141812,
    103     60119,
    104     105185,
    105     97653,
    106     134563,
    107     54195,
    108     64473,
    109     75606,
    110     148515,
    111     88765,
    112     112562,
    113     52156,
    114     119805,
    115     117149,
    116     149791,
    117     128964,
    118     108955,
    119     55806,
    120     86025,
    121     148350,
    122     74382,
    123     73632,
    124     141124,
    125     101688,
    126     106829,
    127     132594,
    128     113645,
    129     90320,
    130     104874,
    131     95210,
    132     118499,
    133     56445,
    134     86371,
    135     113833,
    136     122860,
    137     112507,
    138     55964,
    139     105993,
    140     92005,
    141     83760,
    142     90258,
    143     56238,
    144     127426,
    145     147641,
    146     129484,
    147     107162,
    148     99535,
    149     107975,
    150     136238,
    151 };