summary refs log tree commit diff stats
path: root/day7.py
blob: 1a2d20671e77e0197d42edd32d4708d927700321 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python

import numpy as np

def triangle(num):
    return (num * (num + 1)) / 2

with open("day7.txt") as data:
    distances = np.array([num for num in map(int, next(data).strip().split(','))]).astype(int)
    # part 1
    median = np.median(distances)
    print(np.sum(np.abs(distances - median).astype(int)))

    # part 2
    minim, maxim = np.amin(distances), np.amax(distances)
    least_dist = None
    for i in range(minim, maxim + 1):
        dist = np.sum(triangle(np.abs(distances - i)).astype(int))
        if least_dist is None or least_dist > dist:
            least_dist = dist
    print(least_dist)
class='alt'>
df1d1ef ^



bce5265 ^

b961a70 ^
bce5265 ^
df1d1ef ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65



             
             











                                                             
                                                                  

                      
 


                                       
                                                       






                                                           
                                                                       



                           


                                                                           
 
                                                                            
                                                                                    





                              
                                                                 
                       
                                                                        

         
                                                                             
                                                                        



                      

                                                         
                                               
         
 
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/getwtxt/registry"
)

// Requests to apiEndpointPOSTHandler are passed off to this
// function. apiPostUser then fetches the twtxt data, then if
// it's an individual user's file, adds it. If it's registry
// output, it scrapes the users/urls/statuses from the remote
// registry before adding each user to the local cache.
func apiPostUser(w http.ResponseWriter, r *http.Request) {
	if err := r.ParseForm(); err != nil {
		log400(w, r, "Error Parsing Values: "+err.Error())
		return
	}

	nick := r.FormValue("nickname")
	urls := r.FormValue("url")
	if nick == "" || urls == "" {
		log400(w, r, "Nickname or URL missing")
		return
	}

	uip := getIPFromCtx(r.Context())

	out, remoteRegistry, err := registry.GetTwtxt(urls)
	if err != nil {
		log400(w, r, "Error Fetching twtxt Data: "+err.Error())
		return
	}

	if remoteRegistry {
		remoteRegistries.Mu.Lock()
		remoteRegistries.List = append(remoteRegistries.List, urls)
		remoteRegistries.Mu.Unlock()

		if err := twtxtCache.CrawlRemoteRegistry(urls); err != nil {
			log400(w, r, "Error Crawling Remote Registry: "+err.Error())
			return
		}
		log200(r)
		return
	}

	statuses, err := registry.ParseUserTwtxt(out, nick, urls)
	if err != nil {
		log.Printf("Error Parsing User Data: %v\n", err.Error())
	}

	if err := twtxtCache.AddUser(nick, urls, uip, statuses); err != nil {
		log400(w, r, "Error Adding User to Cache: "+err.Error())
		return
	}

	log200(r)
	_, err = w.Write([]byte(fmt.Sprintf("200 OK\n")))
	if err != nil {
		log.Printf("%v\n", err.Error())
	}
}