mid-kid
1 year ago
4 changed files with 378 additions and 1 deletions
@ -0,0 +1,109 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"os" |
|||
"fmt" |
|||
"strings" |
|||
"strconv" |
|||
) |
|||
|
|||
type input []string |
|||
|
|||
func parse(f string) (seq input) { |
|||
seq = make(input, 0) |
|||
|
|||
for _, step := range strings.Split(f, ",") { |
|||
step = strings.TrimSpace(step) |
|||
seq = append(seq, step) |
|||
} |
|||
|
|||
return |
|||
} |
|||
|
|||
func hash(in string) (hash uint8) { |
|||
hash = 0 |
|||
for _, byt := range []byte(in) { |
|||
hash += byt |
|||
hash *= 17 |
|||
} |
|||
return |
|||
} |
|||
|
|||
func part1(seq input) (sum uint) { |
|||
for _, step := range seq { sum += uint(hash(step)) } |
|||
return |
|||
} |
|||
|
|||
type lens struct { |
|||
label string |
|||
focal uint64 |
|||
} |
|||
|
|||
func lens_find_label(arr []lens, label string) (pos int) { |
|||
for pos, val := range arr{ |
|||
if val.label == label { return pos } |
|||
} |
|||
return -1 |
|||
} |
|||
|
|||
func part2(seq input) (sum uint64) { |
|||
boxes := [256][]lens{} |
|||
for i := 0; i < 256; i++ { boxes[i] = make([]lens, 0) } |
|||
|
|||
for _, step := range seq { |
|||
var label string |
|||
var op byte |
|||
|
|||
var off int |
|||
if strings.Index(step, "=") >= 0 { |
|||
off = strings.Index(step, "=") |
|||
label = step[:off] |
|||
op = step[off] |
|||
} else if strings.Index(step, "-") >= 0 { |
|||
off = strings.Index(step, "-") |
|||
label = step[:off] |
|||
op = step[off] |
|||
} |
|||
|
|||
box := &boxes[hash(label)] |
|||
pos := lens_find_label(*box, label) |
|||
|
|||
switch op { |
|||
case '=': |
|||
focal, _ := strconv.ParseUint(step[off + 1:], 0, 64) |
|||
newlens := lens{ |
|||
label: label, |
|||
focal: focal, |
|||
} |
|||
if pos >= 0 { |
|||
(*box)[pos] = newlens |
|||
} else { |
|||
*box = append(*box, newlens) |
|||
} |
|||
|
|||
case '-': |
|||
if pos >= 0 { |
|||
*box = append((*box)[:pos], (*box)[pos + 1:]...) |
|||
} |
|||
} |
|||
} |
|||
|
|||
sum = 0 |
|||
for box_i, box := range boxes { |
|||
for lens_i, lens := range box { |
|||
fmt.Println(box_i + 1, ":", lens_i + 1, lens.label, lens.focal) |
|||
sum += uint64(1 + box_i) * uint64(1 + lens_i) * lens.focal |
|||
} |
|||
} |
|||
|
|||
return |
|||
} |
|||
|
|||
func main() { |
|||
file, _ := os.ReadFile(os.Args[1]) |
|||
f := string(file) |
|||
|
|||
in := parse(f) |
|||
fmt.Println(part1(in)) |
|||
fmt.Println(part2(in)) |
|||
} |
File diff suppressed because one or more lines are too long
@ -0,0 +1,267 @@ |
|||
517315 |
|||
1 : 1 rn 9 |
|||
2 : 1 qp 7 |
|||
2 : 2 jspd 5 |
|||
3 : 1 dck 9 |
|||
3 : 2 xxr 1 |
|||
4 : 1 bfk 4 |
|||
4 : 2 qbld 6 |
|||
5 : 1 btn 8 |
|||
6 : 1 jsh 3 |
|||
9 : 1 gksc 2 |
|||
9 : 2 kbxs 6 |
|||
11 : 1 bnz 4 |
|||
11 : 2 xqq 2 |
|||
12 : 1 cll 9 |
|||
13 : 1 fnll 8 |
|||
14 : 1 gf 7 |
|||
15 : 1 sfrs 9 |
|||
15 : 2 gqf 1 |
|||
15 : 3 tz 3 |
|||
16 : 1 tk 4 |
|||
17 : 1 npr 4 |
|||
18 : 1 dsz 9 |
|||
19 : 1 bp 6 |
|||
20 : 1 jgb 8 |
|||
21 : 1 scn 2 |
|||
21 : 2 sksc 4 |
|||
23 : 1 qtq 7 |
|||
24 : 1 bvlgl 1 |
|||
27 : 1 jp 4 |
|||
29 : 1 ht 8 |
|||
29 : 2 vtr 2 |
|||
31 : 1 vh 4 |
|||
31 : 2 ndl 5 |
|||
32 : 1 lvm 5 |
|||
34 : 1 dm 4 |
|||
34 : 2 cczqms 7 |
|||
35 : 1 lgdk 7 |
|||
37 : 1 bb 5 |
|||
38 : 1 ssrql 1 |
|||
39 : 1 gkd 4 |
|||
41 : 1 pvb 3 |
|||
42 : 1 tkln 3 |
|||
44 : 1 jq 5 |
|||
45 : 1 jb 1 |
|||
47 : 1 rfjtx 4 |
|||
47 : 2 vzck 5 |
|||
49 : 1 vrh 3 |
|||
50 : 1 tm 4 |
|||
50 : 2 gslk 7 |
|||
50 : 3 mdp 1 |
|||
51 : 1 tpxv 3 |
|||
52 : 1 cp 5 |
|||
52 : 2 dtk 6 |
|||
54 : 1 qhl 1 |
|||
54 : 2 qd 4 |
|||
54 : 3 ftqvd 5 |
|||
55 : 1 pf 9 |
|||
56 : 1 dqb 2 |
|||
57 : 1 bss 6 |
|||
57 : 2 pzpjt 8 |
|||
57 : 3 vqdgf 2 |
|||
58 : 1 tnvq 7 |
|||
58 : 2 bct 7 |
|||
58 : 3 zqn 2 |
|||
60 : 1 pjjg 7 |
|||
60 : 2 kp 6 |
|||
60 : 3 xsp 6 |
|||
61 : 1 zb 7 |
|||
61 : 2 dxfkdk 5 |
|||
63 : 1 xf 3 |
|||
64 : 1 sjr 8 |
|||
65 : 1 fz 3 |
|||
66 : 1 fsh 7 |
|||
67 : 1 tn 8 |
|||
69 : 1 rr 5 |
|||
70 : 1 bdqlrp 1 |
|||
71 : 1 xxv 6 |
|||
74 : 1 kqm 4 |
|||
77 : 1 kq 7 |
|||
78 : 1 vgp 3 |
|||
78 : 2 js 2 |
|||
78 : 3 kb 5 |
|||
79 : 1 xv 8 |
|||
81 : 1 ckr 8 |
|||
82 : 1 gj 3 |
|||
82 : 2 tgv 2 |
|||
84 : 1 nbs 1 |
|||
85 : 1 kcpf 3 |
|||
85 : 2 sq 7 |
|||
86 : 1 bpbrtk 3 |
|||
87 : 1 vhbv 4 |
|||
87 : 2 nnz 4 |
|||
91 : 1 nzr 6 |
|||
91 : 2 flh 9 |
|||
93 : 1 ptx 2 |
|||
95 : 1 jt 9 |
|||
95 : 2 zd 9 |
|||
96 : 1 djmd 6 |
|||
96 : 2 vvc 1 |
|||
96 : 3 xqv 7 |
|||
97 : 1 qnlmx 9 |
|||
97 : 2 fft 9 |
|||
98 : 1 pfnm 1 |
|||
99 : 1 vl 8 |
|||
99 : 2 gch 2 |
|||
99 : 3 dhv 5 |
|||
99 : 4 gk 5 |
|||
100 : 1 fcz 9 |
|||
100 : 2 fm 9 |
|||
101 : 1 tp 7 |
|||
101 : 2 fck 2 |
|||
101 : 3 rjh 4 |
|||
101 : 4 xdxdl 9 |
|||
102 : 1 dq 3 |
|||
102 : 2 sr 9 |
|||
103 : 1 rhl 7 |
|||
103 : 2 zhd 8 |
|||
105 : 1 qg 5 |
|||
105 : 2 nlhbd 4 |
|||
106 : 1 ctb 7 |
|||
107 : 1 hxj 1 |
|||
108 : 1 fkz 8 |
|||
111 : 1 zt 9 |
|||
112 : 1 kd 3 |
|||
114 : 1 hgfl 5 |
|||
114 : 2 sdjp 3 |
|||
114 : 3 mhl 4 |
|||
115 : 1 gsh 3 |
|||
117 : 1 fn 1 |
|||
120 : 1 ct 7 |
|||
121 : 1 jpgg 8 |
|||
122 : 1 nnm 1 |
|||
123 : 1 sct 8 |
|||
125 : 1 nn 2 |
|||
127 : 1 nhtt 1 |
|||
129 : 1 dlp 1 |
|||
129 : 2 lksf 7 |
|||
129 : 3 ffpd 9 |
|||
131 : 1 mscqn 7 |
|||
131 : 2 dkc 6 |
|||
131 : 3 hz 3 |
|||
132 : 1 hk 4 |
|||
132 : 2 kkm 1 |
|||
135 : 1 tr 2 |
|||
136 : 1 ds 5 |
|||
139 : 1 pz 2 |
|||
142 : 1 pgv 9 |
|||
143 : 1 tfjsg 3 |
|||
144 : 1 mb 4 |
|||
144 : 2 bcj 1 |
|||
145 : 1 rpn 6 |
|||
145 : 2 zv 3 |
|||
147 : 1 jh 1 |
|||
147 : 2 dks 6 |
|||
148 : 1 dvsf 4 |
|||
149 : 1 jjnbp 4 |
|||
150 : 1 cjx 5 |
|||
151 : 1 jzr 5 |
|||
152 : 1 lmlbp 9 |
|||
153 : 1 mdg 9 |
|||
155 : 1 bx 8 |
|||
156 : 1 rfs 1 |
|||
158 : 1 gkk 5 |
|||
159 : 1 qfg 5 |
|||
159 : 2 fll 9 |
|||
159 : 3 np 7 |
|||
163 : 1 fbhr 3 |
|||
163 : 2 mjk 9 |
|||
163 : 3 kg 7 |
|||
164 : 1 vgf 4 |
|||
165 : 1 xl 6 |
|||
165 : 2 pzvd 7 |
|||
166 : 1 kzbvkm 5 |
|||
167 : 1 mgb 7 |
|||
169 : 1 hrxjl 7 |
|||
169 : 2 shm 7 |
|||
169 : 3 brcmt 1 |
|||
170 : 1 nqj 9 |
|||
171 : 1 sg 3 |
|||
173 : 1 bj 9 |
|||
175 : 1 rdx 4 |
|||
176 : 1 dpk 4 |
|||
179 : 1 lf 6 |
|||
180 : 1 kh 8 |
|||
180 : 2 snb 8 |
|||
181 : 1 jj 1 |
|||
181 : 2 znl 2 |
|||
181 : 3 vfx 5 |
|||
182 : 1 lfkx 1 |
|||
184 : 1 vq 3 |
|||
184 : 2 bklck 2 |
|||
185 : 1 zcgrr 8 |
|||
186 : 1 fc 4 |
|||
187 : 1 dv 2 |
|||
188 : 1 xkx 3 |
|||
188 : 2 sh 3 |
|||
189 : 1 clsz 5 |
|||
189 : 2 jhj 6 |
|||
191 : 1 ksld 4 |
|||
191 : 2 qnzqgm 6 |
|||
192 : 1 qlgk 1 |
|||
193 : 1 nqtm 3 |
|||
193 : 2 nszdq 5 |
|||
193 : 3 nr 1 |
|||
193 : 4 jdlf 5 |
|||
194 : 1 xvc 6 |
|||
194 : 2 mt 3 |
|||
195 : 1 rrtj 5 |
|||
195 : 2 lv 3 |
|||
196 : 1 mncnct 3 |
|||
197 : 1 hdpzn 4 |
|||
197 : 2 lll 7 |
|||
197 : 3 zj 8 |
|||
202 : 1 vc 1 |
|||
203 : 1 snrg 8 |
|||
204 : 1 lqpn 3 |
|||
205 : 1 hmg 5 |
|||
205 : 2 dh 3 |
|||
207 : 1 jvn 9 |
|||
207 : 2 bl 8 |
|||
207 : 3 zfn 5 |
|||
207 : 4 pkc 8 |
|||
209 : 1 lvbl 9 |
|||
209 : 2 csj 2 |
|||
210 : 1 ns 8 |
|||
212 : 1 zqh 8 |
|||
213 : 1 zz 5 |
|||
213 : 2 vmcrl 6 |
|||
214 : 1 fxg 3 |
|||
218 : 1 xxjmjx 7 |
|||
218 : 2 gfjb 4 |
|||
218 : 3 vs 5 |
|||
220 : 1 frs 2 |
|||
222 : 1 jxhfcj 3 |
|||
224 : 1 qn 7 |
|||
226 : 1 mstm 9 |
|||
226 : 2 fmn 6 |
|||
227 : 1 fvsc 2 |
|||
228 : 1 bsn 5 |
|||
228 : 2 qjvtkc 2 |
|||
228 : 3 zljxk 9 |
|||
230 : 1 kz 4 |
|||
230 : 2 klfh 9 |
|||
231 : 1 smv 2 |
|||
231 : 2 kk 4 |
|||
232 : 1 jm 1 |
|||
232 : 2 mxr 4 |
|||
234 : 1 rmj 8 |
|||
235 : 1 gs 1 |
|||
236 : 1 mfh 8 |
|||
236 : 2 thmb 4 |
|||
239 : 1 sk 3 |
|||
240 : 1 cl 2 |
|||
240 : 2 prpm 7 |
|||
241 : 1 tkqp 9 |
|||
243 : 1 srm 2 |
|||
247 : 1 zzjx 2 |
|||
247 : 2 ptr 7 |
|||
251 : 1 xb 5 |
|||
252 : 1 ckm 4 |
|||
252 : 2 grr 5 |
|||
253 : 1 vdb 3 |
|||
254 : 1 mtl 2 |
|||
254 : 2 lgj 3 |
|||
255 : 1 frf 3 |
|||
247763 |
Loading…
Reference in new issue