~charles/misc

0bf4507b7a0be42299cf5fce3c48b5edd72786b0 — Charles Daniels 9 months ago 77b03ec
advent of code day 1
A advent2019/README.md => advent2019/README.md +12 -0
@@ 0,0 1,12 @@
# Advent of Code 2019 Solutions

https://adventofcode.com/

My solutions to Advent of Code. Spoiler alert!

I've decided to do this year's solutions in Go, since I've wanted to learn Go
for a while.

Run code with `go run somefile.go`.

Build executable with `go build somefile.go`.

A advent2019/day1.md => advent2019/day1.md +27 -0
@@ 0,0 1,27 @@
# Advent of Code 2019 Day 1

https://adventofcode.com/2019/day/1

--- Day 1: The Tyranny of the Rocket Equation ---

Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

The Elves quickly load you into a spacecraft and prepare to launch.

At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.

Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.

For example:

    For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
    For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
    For a mass of 1969, the fuel required is 654.
    For a mass of 100756, the fuel required is 33583.

The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.

What is the sum of the fuel requirements for all of the modules on your spacecraft?


A advent2019/day1_input.txt => advent2019/day1_input.txt +100 -0
@@ 0,0 1,100 @@
135568
93567
74616
116524
97342
78397
146320
147564
62929
119077
67410
120049
70541
110713
112447
145348
102068
114736
53901
86524
120050
127555
146277
53860
80276
137697
79305
134372
96073
69122
125296
117850
143600
88494
56534
109901
124036
82066
135308
91000
93619
147786
125127
76653
149032
65864
50784
108189
109455
98761
56177
96484
71289
73103
101584
105978
63607
131868
81217
96817
95946
137326
72221
95949
142230
102055
88307
89670
130285
127194
65451
80781
123180
110380
59598
132438
62048
128950
63286
91364
148439
111630
121158
97784
143589
140514
135369
114701
51451
107496
138090
50617
88343
120007
77216
77028
64462
70883
115812
113776

A advent2019/day1p1.go => advent2019/day1p1.go +67 -0
@@ 0,0 1,67 @@
/* Copyright 2019 Charles Daniels */

/* Solution for Advent of Code 2019, Day 1, part 1 */

/*
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 *
 * For more information, please refer to <http://unlicense.org/>
 */


package main

import "fmt"
import "bufio"
import "os"
import "strconv"
import "math"

func main() {
	var total int64;

	/* iterate over stdin */
	scanner := bufio.NewScanner(os.Stdin);
	for scanner.Scan() {
		line := scanner.Text();

		/* extract the number, skipping the line if there is a float
		 * parsing error */
		val, err := strconv.ParseFloat(line, 64);
		if (err != nil) { continue; }

		/* compute the result */
		res := math.Floor(val / 3) -2;

		/* display, casting to int */
		//fmt.Println(int(res));

		/* accumulate */
		total += int64(res);
	}

	fmt.Printf("TOTAL=%d\n", total);
}


A advent2019/day1p2.go => advent2019/day1p2.go +98 -0
@@ 0,0 1,98 @@
/* Copyright 2019 Charles Daniels */

/* Solution for Advent of Code 2019, Day 1, part 2 */

/*
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 *
 * For more information, please refer to <http://unlicense.org/>
 */

package main

import "fmt"
import "bufio"
import "os"
import "strconv"
import "math"

/* compute the fuel cost for a given mass, not accounting for the additional
 * fuel required to lift the fuel */
func basefuelcost(mass float64) float64 {
	/* mass over three, rounded down, less 2 */
	return math.Floor(mass / 3) - 2;
}

/* compute the total fuel cost for a given mass, including the additional fuel
 * required to lift the fuel */
func totalfuelcost(mass float64) float64 {

	basecost := basefuelcost(mass);		/* cost to lift just the mass */
	marginal := 0.0;			/* cost to lift the fuel */

	if (mass <= 0) {
		/* base case */
		return 0.0;
	} else {
		/* recurs */
		marginal = totalfuelcost(basecost);
	}

	//fmt.Printf("mass=%f basecost=%f marginal=%f\n", mass, basecost, marginal);

	if (marginal < 0) {
		/* ignore negative marginal costs */
		return basecost;
	} else {
		return basecost + marginal;
	}
}

func main() {
	var total int64;

	/* iterate over stdin */
	scanner := bufio.NewScanner(os.Stdin);
	for scanner.Scan() {
		line := scanner.Text();

		/* extract the number, skipping the line if there is a float
		 * parsing error */
		val, err := strconv.ParseFloat(line, 64);
		if (err != nil) { continue; }

		/* compute the result */
		res := totalfuelcost(val);

		/* display, casting to int */
		//fmt.Println(int(res));

		/* accumulate */
		total += int64(res);
	}

	fmt.Printf("TOTAL=%d\n", total);
}


A advent2019/gohello.go => advent2019/gohello.go +10 -0
@@ 0,0 1,10 @@
package main

/* https://gobyexample.com/hello-world */

import "fmt"

func main() {
    fmt.Println("hello world")
}


A advent2019/temp.txt => advent2019/temp.txt +1 -0
@@ 0,0 1,1 @@
1969