Day 3

Previous: Day 02 | Next: Day 04
###AUTO-ACQUIRED DATA FOLLOWS…
day-03/day03.go
day-03/day03.go
package main 
    
    import ( 
        "bufio"
        "math"
        "fmt"
        "log"
        "os"
        "strconv"
    )
    var masks [12]int64;
    var oxg_c = 0;
    var co2_c = 1;
    
    var BIT1 int64 = 0x10;
    var BIT2 int64 = 0x08;
    var BIT3 int64 = 0x04
    var BIT4 int64 = 0x02;
    var BIT5 int64 = 0x01;
    
    //ShockTohp
    
    func main() {
        for i, _ := range masks {
            masks[i] = int64(math.Pow(2.0, float64(11 - i)));
        }
        part1("day3");
        part2("day3");
    }
    
    func part1 (filename string)  {
        var one_count = [12]int64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
        var zer_count = [12]int64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        var file, err = os.Open(fmt.Sprintf("./inputs/%s", filename));
        if err != nil {
            log.Fatal(err);
        }
        defer file.Close();
    
        var scanner = bufio.NewScanner(file);
        for scanner.Scan() {
            num, _  := strconv.ParseInt(scanner.Text(), 2, 64);
            for i, _ := range masks {
                var shift = 11 - i;
                one_count[i] = one_count[i] + ((masks[i] & num) >> shift);
                zer_count[i] = zer_count[i] + (((masks[i] & num) >> shift) ^ 0x01);
            }
        }
        var g = 0x00;
        var f = 0x00;
        for i, _ := range one_count {
            var shift = 11 - i;
            if (one_count[i] > zer_count[i]) {
                g = g | (1 << shift);
            } else {
                f = f | (1 << shift);
            }
    
        }
        var p = g\*f;
        fmt.Printf("power: %d\n", p);
    }
    
    
    func part2 (filename string)  {
        var readings []int64;
        var file, err = os.Open(fmt.Sprintf("./inputs/%s", filename));
        if err != nil {
            log.Fatal(err);
        }
        defer file.Close();
    
        var scanner = bufio.NewScanner(file);
        for scanner.Scan() {
            num, _  := strconv.ParseInt(scanner.Text(), 2, 64);
            readings = append(readings, num);
        }
        oxg, co2 := filterReadings(readings);
    
        var i = 0;
        for len(oxg) > 1 ||  len(co2) > 1{
            oxg = filterOx(oxg, i);
            co2 = filterCo2(co2, i);
            i++;
    
        }
        var pow = oxg[0] \* co2[0];
        fmt.Printf("Life: %d\n", pow);
    }
    
    func filterReadings(readings []int64) (oxg []int64, co2[]int64) {
        for _, val := range readings {
            var t = (val & masks[0]);
            if t > 0 {
                oxg = append(oxg, val);
            } else {
                co2 = append(co2, val);
            }
        }
        if len (co2) > len(oxg) {
            tmp := oxg;
            oxg = co2;
            co2 = tmp;
        }
    
        return;
    }
    
    func filterOx(oxg []int64, index int) ([]int64) {
        if len(oxg) == 1 {
            return oxg;
        }
        var a   []int64;
        var b   []int64;
        mask := masks[index];
        for _, val := range oxg {
            if (val & mask) > 0 {
                a = append(a, val);
            } else {
                b = append(b, val);
            }
            
        }
        if len(b) > len(a) {
            return b;
        }
    
        return a;
    }
    
    
    func filterCo2(co2 []int64, index int) ([]int64) {
        if len(co2) == 1 {
            return co2;
        }
        var a   []int64;
        var b   []int64;
        mask := masks[index];
        for _, val := range co2 {
            if (val & mask) > 0 {
                a = append(a, val);
            } else {
                b = append(b, val);
            }
            
        }
        if len(b) > len(a) && len(a) >= 1 {
            return a;
        }
    
        return b;
    
    
    }
    
day-03/smolheck_day03.py
day-03/smolheck_day03.py
#!/usr/bin/python
    
    bins = [ [ d for d in l ] for l in open('input').read().splitlines() ]
    
    gr, er = '', '' # gamma rate, epsilon rate
    for b in [ list(b) for b in zip(\*bins) ]:
        x, y = ('1','0') if b.count('1') > len(b)/2 else ('0','1')
        gr += x
        er += y
    
    print("Part 1:", int(gr, 2)\*int(er, 2))
    
    o2, co2 = bins[:], bins[:]  # oxygen rating, co2 rating
    for i in range(len(bins[0])):
        if len(o2) > 1:
            o2_t = [ list(b) for b in zip(\*o2) ][i]
            if o2_t.count('1') >= len(o2_t)/2:
                o2[:] = (x for x in o2 if x[i] == '1')
            else:
                o2[:] = (x for x in o2 if x[i] == '0')
        if len(co2) > 1:
            co2_t = [ list(b) for b in zip(\*co2) ][i]
            if co2_t.count('1') >= len(co2_t)/2:
                co2[:] = (x for x in co2 if x[i] == '0')
            else:
                co2[:] = (x for x in co2 if x[i] == '1')
    
    o2, co2 = ''.join(o2[0]), ''.join(co2[0])
    print("Part 2:", int(o2, 2)\*int(co2, 2))
    

Tags: