3 changed files with 4361 additions and 0 deletions
@ -0,0 +1,129 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"bufio" |
|||
"fmt" |
|||
"io" |
|||
"os" |
|||
"strconv" |
|||
"strings" |
|||
) |
|||
|
|||
var _ = strings.NewReader |
|||
|
|||
func openf(path string) io.Reader { |
|||
f, err := os.Open(path) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
return f |
|||
} |
|||
|
|||
func main() { |
|||
i := openf("inputs/0101.txt") |
|||
_ = i |
|||
t := strings.NewReader(`L68 |
|||
L30 |
|||
R48 |
|||
L5 |
|||
R60 |
|||
L55 |
|||
L1 |
|||
L99 |
|||
R14 |
|||
L82 |
|||
`) |
|||
|
|||
//fmt.Println(puzzle_0101(i))
|
|||
fmt.Println(puzzle_0102(t)) |
|||
fmt.Println(puzzle_0102(i)) |
|||
} |
|||
|
|||
func puzzle_0101(r io.Reader) string { |
|||
|
|||
turn := func(position, distance int) int { |
|||
n := position + distance |
|||
for n > 99 { |
|||
n -= 100 |
|||
} |
|||
|
|||
for n < 0 { |
|||
n += 100 |
|||
} |
|||
return n |
|||
} |
|||
|
|||
p := 50 |
|||
var total int |
|||
s := bufio.NewScanner(r) |
|||
for s.Scan() { |
|||
if p == 0 { |
|||
total++ |
|||
} |
|||
|
|||
l := s.Text() |
|||
dir, dig := l[0], l[1:] |
|||
n, _ := strconv.Atoi(dig) |
|||
switch dir { |
|||
case 'R': |
|||
case 'L': |
|||
n = -n |
|||
} |
|||
|
|||
p = turn(p, n) |
|||
|
|||
} |
|||
return strconv.Itoa(total) |
|||
} |
|||
|
|||
func puzzle_0102(r io.Reader) string { |
|||
|
|||
turnR := func(position, distance int) (pos, clicks int) { |
|||
clicks = distance / 100 |
|||
r := distance % 100 |
|||
pos = position + r |
|||
if pos > 99 { |
|||
pos = pos-100 |
|||
if pos != 0 { |
|||
clicks++ |
|||
} |
|||
} |
|||
return pos, clicks |
|||
} |
|||
|
|||
turnL := func(position, distance int) (pos, clicks int) { |
|||
clicks = distance / 100 |
|||
r := distance % 100 |
|||
pos = position - r |
|||
if pos < 0 { |
|||
pos = 100+pos |
|||
if position != 0 { |
|||
clicks++ |
|||
} |
|||
} |
|||
|
|||
return pos, clicks |
|||
} |
|||
|
|||
p := 50 |
|||
var total, clicks int |
|||
s := bufio.NewScanner(r) |
|||
for s.Scan() { |
|||
l := s.Text() |
|||
dir, dig := l[0], l[1:] |
|||
n, _ := strconv.Atoi(dig) |
|||
switch dir { |
|||
case 'R': |
|||
p, clicks = turnR(p, n) |
|||
case 'L': |
|||
p, clicks = turnL(p, n) |
|||
} |
|||
|
|||
total += clicks |
|||
if p == 0 { total++ } |
|||
|
|||
//fmt.Printf("INST %s, POS %d, CLICKS %d, TOTAL %d\n", l, p, clicks, total)
|
|||
|
|||
} |
|||
return strconv.Itoa(total) |
|||
} |
|||
File diff suppressed because it is too large
Loading…
Reference in new issue