From 3d2b624cba351e8d10d407e9322e87d31a20f3be Mon Sep 17 00:00:00 2001 From: b-idea Date: Wed, 25 Oct 2023 21:06:14 +0200 Subject: added day 09 --- 2018/aoc2018-d01-02.py | 32 ++++++++++++++++++++++++++++++++ 2018/aoc2018-d09.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 2018/aoc2018-d01-02.py create mode 100644 2018/aoc2018-d09.py diff --git a/2018/aoc2018-d01-02.py b/2018/aoc2018-d01-02.py new file mode 100644 index 0000000..ee8bab2 --- /dev/null +++ b/2018/aoc2018-d01-02.py @@ -0,0 +1,32 @@ +print(); +f = open("input"); + +part1 = 0; +results = []; +not_found = True; +loopcount = 0; +myinput = []; + +for i in f: + myinput.append(int(i)); + +while not_found: + for i in myinput: + #print(i, "endofline"); + part1 += i; + results.append(part1); + #for x in results: + if (results.count(part1) > 1): + part2 = part1; + print(part2); + not_found = False; + break; + loopcount += 1; + #print("loop", loopcount); + +#results.sort(); +#for i in results: +# print(i); + +print(part1); +print(part2); diff --git a/2018/aoc2018-d09.py b/2018/aoc2018-d09.py new file mode 100644 index 0000000..3a300b9 --- /dev/null +++ b/2018/aoc2018-d09.py @@ -0,0 +1,50 @@ +#advent of code 2018 +#day 09 +#part 1 and part 2 + +from collections import deque +import time + +#parsing input is hardcoded for 1st and 7th word in text +f = open("input.txt", 'r'); +inputline = f.readline().split(); +playercount = int(inputline[0]); #input1 +lastmarble = int(inputline[6]); #input2 + +testinput = False; +if testinput: + playercount = 30; # 09 / 10 / 13 / 17 / 21 / 30 + lastmarble = 5807; # 25 / 1618 / 7999 / 1104 / 6111 / 5807 + # 32 / 8317 / 146373 / 2764 / 54718 / 37305 + +def MarbleGame(playercount, lastmarble): + playerlist = []; + player = 1; + for p in range(playercount+1): + playerlist.append(0); + + marblelist = deque([0]); + + for m in range(1, lastmarble+1): + if(m%23==0): + playerlist[player] += m; + marblelist.rotate(7); + playerlist[player] += marblelist.pop(); + marblelist.rotate(-1); + else: + marblelist.rotate(-1); + marblelist.append(m); + player +=1; + if (player > playercount): + player = 1; + + p1 = max(playerlist); + return p1; + +t1 = time.time(); +part1 = MarbleGame(playercount, lastmarble); +part2 = MarbleGame(playercount, lastmarble*100); +t2 = time.time(); +print("part 1 = ", part1); +print("part 2 = ", part2); +print("runtime ", round(t2-t1,3), " sec"); -- cgit v1.2.3